简体   繁体   English

在rpy2中绘制R函数曲线

[英]Plot an R function curve in rpy2

I am trying to get the plot a simple curve in rpy2. 我正在尝试使绘图在rpy2中成为一条简单曲线。

curve((x)) in R behaves as expected, but I cannot implement this in rpy2. R中的curve((x))表现正常,但是我无法在rpy2中实现。

When I issue the following commands in sequence: 当我依次发出以下命令时:

import rpy2.robjects as ro
R = ro.r
R.curve(R.x) 

I get the error that AttributeError: 'R' object has no attribute 'x' ... 我收到AttributeError: 'R' object has no attribute 'x' ...

How do I access x as the vectorizing function within python? 如何在python中将x作为向量化函数访问? (I can issue ro.r('curve((x))') and it works as expected, but I need to be able to pass arguments from python to the curve function). (我可以发出ro.r('curve((x))') ,它可以按预期工作,但我需要能够将python中的参数传递给curve函数。

More generally, how do I plot a function curve in rpy2 ala this post: plotting function curve in R 更笼统地说,我如何在rpy2中绘制函数曲线?这篇文章: 在R中绘制函数曲线

EDIT 1 编辑1

Some context: 一些背景:

I am trying to plot a curve of the inverse logit: 我正在尝试绘制逆对数的曲线:

invlogit = function(x){ + exp(x)/(1 + exp(x)) }

of the linear function: 线性函数:

invlogit(coef(mod1)[1] + coef(mod1)[2]*x

Where coef(mod1) are the coefficients of a GLM I ran. 其中coef(mod1)是我运行的GLM的系数。

In R, I can do the following: 在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(invlogit(coef(mod1)[1] + coef(mod1)[2]*x), add = TRUE)

And I get the expected sigmoidal curve. 我得到了预期的S形曲线。

I python/rpy2, I get my model and coefficients: 我是python / rpy2,我得到了我的模型和系数:

formula = 'outcome~survrate'
mod1 = R.glm(formula=R(formula), data=r_analytical_set,   family=R('binomial(link="logit")'))
s = R.summary(mod1)
print(mod1)
print(R.summary(mod1))

Set up the plot 设置情节

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

So far so good... 到现在为止还挺好...

Then, I get my coefficients from the model: 然后,我从模型中得到系数:

a = R.coef(mod1)[0] 
b = R.coef(mod1)[1] 

And then try to run the curve function by passing in these arguments, all to no avail, trying such constructs as 然后尝试通过传递这些参数来运行curve函数,但无济于事,尝试使用以下结构:

R.curve(invlogit(a + b*R.x)) 

I've tried many others too besides this, all of which are embarrassingly weird. 除此以外,我还尝试了许多其他方法,所有这些方法都令人尴尬地怪异。

First, the naive question: If term (x) in curve() is a special R designation for last environment expression, I assume I should be able to access this somehow through python/rpy2. 首先,是一个幼稚的问题:如果curve()中的(x)项是最后一个环境表达式的特殊R指定,那么我认为我应该能够通过python / rpy2以某种方式访问​​它。

I understand that its representation in the curve function is a ListVector of 101 elements. 我知道它在曲线函数中的表示形式是101个元素的ListVector。 I do not follow what it means though that it "is a special R designation for last environment expression." 我不理解它的含义,尽管它“是最后一个环境表达的特殊R名称”。 Could someone please elaborate? 有人可以详细说明吗? If this is an object in R, should I not be able to access it through the at least the low-level interface? 如果这是R中的对象,我是否至少不能通过低级接口访问它?

Or, do I actually have to create x as a python function to represent my x, y tuples as two lists and then convert them to a ListVector for use in the function to plot its curve. 或者,实际上我是否必须将x创建为python函数,以将x,y元组表示为两个列表,然后将它们转换为ListVector以便在函数中绘制其曲线。

Second: Should I not be able to construct my function, invlogit(a + b*x) in python and pass it for evaluation in R's curve function? 第二:我是否应该无法在python中构造函数invlogit(a + b*x)并将其传递给R的曲线函数进行评估?

I am grabbing invlogit from an R file by reading it in using the STAP library: from rpy2.robjects.packages import STAP . 我抓住invlogit通过使用STAP图书馆阅读它从R文件: from rpy2.robjects.packages import STAP

Third: Am I over complicating things? 第三:我是否使事情复杂化了? My goal is to recreate an analysis I had previously done in R using python/rpy2 to work through all the idiosyncrasies, before I try doing a new one in python/rpy2. 我的目标是在尝试在python / rpy2中进行新的分析之前,重新创建以前使用R / python / rpy2在R中进行的分析以遍历所有特性。

Simply pass in an actual function, call, or expression like sin as x is not assigned in Python. 只需传递一个实际的函数,调用或表达式,例如sin因为x在Python中未分配。 Below uses the example from the R documentation for curve : curve(sin, -2*pi, 2*pi) . 下面使用R文档中的curve示例: curve(sin, -2*pi, 2*pi) Also, because you output a graph use grDevices (built-in R package) to save image to file: 另外,由于输出了图,因此请使用grDevices (内置R包)将图像保存到文件中:

import rpy2.robjects as ro
from rpy2.robjects.packages import importr

grdevices = importr('grDevices')

grdevices.png(file="Rpy2Curve.png", width=512, height=512)
p = ro.r('curve(sin, -2*pi, 2*pi)')    
grdevices.dev_off()

RPy2曲线图图像1

Alternatively, you can define (x) just as your link shows: 另外,您可以定义(x) ,就像您的链接显示的那样:

grdevices.png(file="Rpy2Curve.png", width=512, height=512)
ro.r('''eq <- function(x) {x*x}''')
p = ro.r('curve(eq,1,1000)')            # OUTPUTS TO FILE
grdevices.dev_off()

p = ro.r('curve(eq,1,1000)')            # OUTPUTS TO SCREEN 

RPy2曲线图图像2


UPDATE 更新

Specifically to the OP's issue, to plot the inverse logit curve with the Python variables, a and b , derived from model coefficients, consider concatenating them to the robjects.r() string parameter: 专门针对OP的问题,要使用从模型系数派生的Python变量ab绘制反logit曲线,请考虑将其连接到robjects.r()字符串参数:

import rpy2.robjects as ro
ro.r('invlogit <- function(x){ + exp(x)/(1 + exp(x)) }')

p = ro.r('curve(invlogit({0} + {1}*x), add = TRUE)'.format(a,b))

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

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