horcle_buzz horcle_buzz - 25 days ago 4
Python Question

Plot an R function curve in rpy2

I am trying to get the plot a simple curve in rpy2.

in R behaves as expected, but I cannot implement this in rpy2.

When I issue the following commands in sequence:

import rpy2.robjects as ro
R = ro.r

I get the error that
AttributeError: 'R' object has no attribute 'x'

How do I access
as the vectorizing function within python? (I can issue
and it works as expected, but I need to be able to pass arguments from python to the curve function).

More generally, how do I plot a function curve in rpy2 ala this post: plotting function curve in R


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.

In R, I can do the following:

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.

I python/rpy2, I get my model and coefficients:

formula = 'outcome~survrate'
mod1 = R.glm(formula=R(formula), data=r_analytical_set, family=R('binomial(link="logit")'))
s = 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

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.

I understand that its representation in the curve function is a ListVector of 101 elements. I do not follow what it means though that it "is a special R designation for last environment expression." 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?

Or, do I actually have to create
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.

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?

I am grabbing
from an R file by reading it in using the STAP library:
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.


Simply pass in an actual function, call, or expression like sin as x is not assigned in Python. Below uses the example from the R documentation for curve: curve(sin, -2*pi, 2*pi). Also, because you output a graph use grDevices (built-in R package) to save image to file:

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)')    

RPy2 curve plot image 1

Alternatively, you can define (x) just as your link shows:

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

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

RPy2 curve plot image 2