繁体   English   中英

rpy2和R调试

[英]rpy2 and R debugging

遇到麻烦后我成功安装了rpy2。

我的目标是构建模型(gam; Simon Woodcv),并通过将pthon数据帧从python传递到rpy2到gam模型并检索预测来使用预测函数。

R脚本通过加载txt文件进行测试,并通过与python / rpy2脚本调用的相同R函数进行处理,并且工作正常。 在python脚本中,我从文本文件的pickle版本开始(好像我在我的最终代码中,从pandas数据帧开始)。

我也能够触发R脚本中有意义的其他错误(传递空数据帧,或者缺少列的数据帧成功执行预测都会像在R中那样触发错误。)我确实进入了输入数据完整的gam函数。

我接近完成,但我一直收到这个错误:

ExtractData中的错误(对象,数据,NULL):'names'属性[1]的长度必须与vector [0]的长度相同

我不知道如何在我的python脚本中从R获得更多反馈。 我怎么调试? 或者任何人都可以指出R中的问题可能是什么? 或者这是“.convert_to_r_dataframe()”函数的一部分我完全没有掌握?

R-代码:

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在python脚本中可用。

谢谢,吕克

检查您提供给s()的数据类型。 Error in ExtractData(object, data, NULL) : 'names' attribute [1] must be the same length as the vector [0]也有Error in ExtractData(object, data, NULL) : 'names' attribute [1] must be the same length as the vector [0]当我使用日期时间解释变量时Error in ExtractData(object, data, NULL) : 'names' attribute [1] must be the same length as the vector [0] 我通过转换为自开始以来的天数来解决这个问题。

> 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)

最后一个电话没问题。

至于调试,我经常从rpy2调用save.image() ,然后将.RData文件加载到普通的R会话中进行进一步的审查。

通常的R调试工具可以在RPy中使用,例如

ro.r("debug(glm)")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM