简体   繁体   English

在rpy2中使用图和曲线

[英]Use of plot and curve in rpy2

In R, I can run plot and curve to get the relationship between a predicted probability and the predictor variable by just running: 在R中,我可以通过运行以下图表来运行图和曲线以获取预测概率与预测变量之间的关系:

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)

Where transform is a custom R function. 其中transform是自定义R函数。

I am trying to do the same thing in rpy2, and so far have the following: 我正在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)

In this state, ro.r.curve gives an error that TypeError: unsupported operand type(s) for *: 'float' and 'FloatVector' 在这种状态下, ro.r.curve会给出一个错误: TypeError: unsupported operand type(s) for *: 'float' and 'FloatVector'

So, as per this multiplying all elements of a vector in R , I ran 因此,按照将R中向量的所有元素相乘的方式

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

But, now I am getting an error TypeError: unsupported operand type(s) for +: 'float' and 'FloatVector' 但是,现在我收到一个错误TypeError: unsupported operand type(s) for +: 'float' and 'FloatVector'

Before I waste any more time figuring out how to add a scalar to a vector, I was wondering if there was a more efficient way of achieving my end goal. 在我花更多的时间弄清楚如何向向量添加标量之前,我想知道是否有更有效的方法来实现最终目标。

Use the accessor "R-operator" ( .ro - see http://rpy2.readthedocs.io/en/version_2.8.x/vector.html#operators ): 使用访问器“ R-operator”(. .ro参见http://rpy2.readthedocs.io/en/version_2.8.x/vector.html#operators ):

In [1]: from rpy2.robjects.vectors import FloatVector

In [2]: FloatVector((1,2,3)).ro + 2
Out[2]: 
R object with classes: ('numeric',) mapped to:
<FloatVector - Python:0x7fde744c0308 / R:0x44db740>
[3.000000, 4.000000, 5.000000]

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

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