2016-09-26 65 views
0

使用情節和曲線在R,我可以運行圖和曲線運行一個命令得到一個預測概率和預測變量之間的關係:在rpy2

plot(outcome~survrate, data = d, ylab = "P(outcome = 1 | 
survrate)", xlab = "SURVRATE: Probability of Survival after 5 
Years", xaxp = c(0, 95, 19)) 

curve(transform(coef(mod1)[1] + coef(mod1)[2]*x), add = TRUE) 

如果變換是一個自定義的一個R函數。

我試圖做同樣的事情在rpy2,到目前爲止有以下幾點:

rplot = ro.r('plot') 
formula = Formula('outcome~survrate') 
formula.getenvironment()['outcome'] = r_analytical_set.rx2('outcome') 
formula.getenvironment()['survrate'] = r_analytical_set.rx2('survrate') 
ro.r.plot(formula, data=r_analytical_set, ylab = 'P(outcome = 1 | pass)', xlab = 'SURVRATE: Probability of Survival after 5 
Years', xaxp = ro.r.c(0, 95, 19)) 

# read in R function from file 
with open('/Users/gregsilverman//development/python/rest_api/rest_api/utils.r', 'r') as f: 
    string = f.read() 

from rpy2.robjects.packages import STAP 
invlogit = STAP(string, "invlogit") 

ro.r.curve(transform(ro.r.coef(fit)[0] + ro.r.coef(fit)[1]*ro.r.x), add = True) 

在這種狀態下,ro.r.curve給出了一個錯誤TypeError: unsupported operand type(s) for *: 'float' and 'FloatVector'

所以,按照這個multiplying all elements of a vector in R,我跑

ro.r.curve(transform(ro.r.coef(fit)[0] + ro.r.prod(ro.r.coef(fit)[1],ro.r.x)), add = True) 

不過,現在我得到一個錯誤TypeError: unsupported operand type(s) for +: 'float' and 'FloatVector'

在我浪費更多時間瞭解如何爲矢量添加標量之前,我想知道是否有更高效的方法來實現我的最終目標。

回答