Rpy2 and R debugging

After some problems, I successfully installed rpy2.

My goal is to build models (gam; library mgcv of Simon Wood) and use the prediction function, passing the pandas framework from python via rpy2 to the gam model and get a forecast.

The R script is checked by loading the txt file and processing it using the same R functions that the python / rpy2 script calls, and it works fine. In a python script, I start with the pickled version of the text file (as if I were in my final code, starting with the pandas data frame).

I can also trigger other errors in the R script that make sense (sending an empty data frame or a data frame with a missing column for successful prediction and triggering an error, as it would be in R.) I really get into the game function with intact input .

I am close to the finish line, but I keep getting this error:

Error in ExtractData (object, data, NULL): The attribute 'names' [1] must have the same length as the vector [0]

I don't know how to get more reviews from R in my python script. How can I debug? Or can someone point me to what could be the problem in R? Or is it part of the .convert_to_r_dataframe () function, which I don't fully understand.

R code:

f_clean_data <- function(df) { t = df ... some preprocessing t } tc <- f_clean_data(t) f_py_gam_predict <- function(gam, df) { dfc = f_clean_data(df) result <- predict(gam, dfc) result } bc_gam = gam(BC ~ +s() .... some gam model , data=tc, method="REML" ) summary(bc_gam) testfile = 'a_test_file.txt' ttest <- read.table(file=testfile ,sep='\t',header=TRUE); result = f_py_gam_predict(bc_gam, ttest) 

F_py_gam_predict is available in python script.

Thanks Luke

+4
source share
2 answers

Check the type of data you are submitting with s() . I also got Error in ExtractData(object, data, NULL) : 'names' attribute [1] must be the same length as the vector [0] when I used the datetime explanatory variable. I worked on this, going over a few days from launch.

 > library(lubridate) > library(mgcv) > df <- data.frame(x=today() + 1:20, y=1:20) > gam(y~s(x), data=df) Error in ExtractData(object, data, knots) : 'names' attribute [1] must be the same length as the vector [0] > df$xnum <- (df$x - df$x[1])/ddays(1) > str(df) 'data.frame': 20 obs. of 3 variables: $ x : Date, format: "2013-04-09" "2013-04-10" "2013-04-11" "2013-04-12" ... $ y : int 1 2 3 4 5 6 7 8 9 10 ... $ xnum: num 0 1 2 3 4 5 6 7 8 9 ... > gam(y~s(xnum), data=df) 

The last call is working fine.

Regarding debugging, I often call save.image() from rpy2 and then upload the .RData file to a regular R session for further study.

+2
source

Conventional R debugging tools can be used from RPy, for example

 ro.r("debug(glm)") 

or ro.r("options(error=recovery)")

+1
source

All Articles