0

I am using rpy2 to communicate with mgcv pkg, to get a gam prediction.

I am able to get a gam fit by using the mgcv pkg, however when I try to use the predict method, it errors out with the error:

NameError: name 'predict' is not defined

Following is my code.

import pandas as pd 
import numpy as np
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
import pandas.rpy.common as com
from rpy2.robjects import pandas2ri

pandas2ri.activate()
r_mgcv = importr('mgcv')
base = importr('base')

MainDt = pd.read_csv(FileLocation, header=0)
R_MainDF = com.convert_to_r_dataframe(MainDt)

modparams = "PGOOD ~ "

for c in R_MainDF.colnames:
    if 'RAW' in str(c):
        modparams += " s (`" + c + "`) + " 

modparams = str(modparams)[:-2]
gamFit = r_mgcv.gam(ro.Formula(modparams), data=R_MainDF)

The lines below error out:

eolPred= r_mgcv.predict(gamFit,newdata=R_MainDF, type="terms")
r_mgcv.matrix(ro.NA_Character,base.nrow(R_MainDF), base.ncol(R_MainDF)-2)

What am I doing wrong ?

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
P Ved
  • 109
  • 3
  • 13

1 Answers1

2

As the error says:

NameError: name 'predict' is not defined

predict is not found in the namespace r_mgcv.

Check how to find where (in which package / namespace) an R symbol is defined here:

https://rpy2.github.io/doc/v3.1.x/html/robjects_rpackages.html#finding-where-an-r-symbol-is-coming-from

lgautier
  • 11,363
  • 29
  • 42