rpy2中plot和curve的使用
Use of plot and curve in rpy2
在 R 中,我可以 运行 绘图和绘制曲线来获得预测概率和预测变量之间的关系,只需 运行ning:
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)
其中 t运行sform 是自定义 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'
在我浪费更多时间弄清楚如何将标量添加到向量之前,我想知道是否有更有效的方法来实现我的最终目标。
使用访问器 "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]
在 R 中,我可以 运行 绘图和绘制曲线来获得预测概率和预测变量之间的关系,只需 运行ning:
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)
其中 t运行sform 是自定义 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'
在我浪费更多时间弄清楚如何将标量添加到向量之前,我想知道是否有更有效的方法来实现我的最终目标。
使用访问器 "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]