horcle_buzz - 6 months ago 47

Python Question

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

`curve((x))`

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

How do I access

`x`

`ro.r('curve((x))')`

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)

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

`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

`x`

Second: Should I not be able to construct my function,

`invlogit(a + b*x)`

I am grabbing

`invlogit`

`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.

Answer

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)')
grdevices.dev_off()
```

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
grdevices.dev_off()
p = ro.r('curve(eq,1,1000)') # OUTPUTS TO SCREEN
```