簡體   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