Plot an R function curve in rpy2
<p>I am trying to get the plot a simple curve in rpy2. </p>

<p><code>curve((x))</code> in R behaves as expected, but I cannot implement this in rpy2. </p>

<p>When I issue the following commands in sequence:</p>

<pre><code>import rpy2.robjects as ro
R = ro.r
R.curve(R.x) 
</code></pre>

<p>I get the error that <code>AttributeError: 'R' object has no attribute 'x'</code>...</p>

<p>How do I access <code>x</code> as the vectorizing function within python? (I can issue <code>ro.r('curve((x))')</code> and it works as expected, but I need to be able to pass arguments from python to the curve function).</p>

<p>More generally, how do I plot a function curve in rpy2 ala this post: <a href="http://stackoverflow.com/questions/26091323/how-to-plot-a-function-curve-in-r">plotting function curve in R</a></p>

<p><strong><em>EDIT 1</em></strong></p>

<p>Some context:</p>

<p>I am trying to plot a curve of the inverse logit:</p>

<pre><code>invlogit = function(x){ + exp(x)/(1 + exp(x)) }
</code></pre>

<p>of the linear function:</p>

<pre><code>invlogit(coef(mod1)[1] + coef(mod1)[2]*x
</code></pre>

<p>Where coef(mod1) are the coefficients of a GLM I ran.</p>

<p>In R, I can do the following:</p>

<pre><code>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)
</code></pre>

<p>And I get the expected sigmoidal curve.</p>

<p>I python/rpy2, I get my model and coefficients:</p>

<pre><code>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))
</code></pre>

<p>Set up the plot</p>

<pre><code>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))
</code></pre>

<p>So far so good...</p>

<p>Then, I get my coefficients from the model:</p>

<pre><code>a = R.coef(mod1)[0] 
b = R.coef(mod1)[1] 
</code></pre>

<p>And then try to run the curve function by passing in these arguments, all to no avail, trying such constructs as</p>

<pre><code>R.curve(invlogit(a + b*R.x)) 
</code></pre>

<p>I've tried many others too besides this, all of which are embarrassingly weird.</p>

<p>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. </p>

<p>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?</p>

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

<p>Second: Should I not be able to construct my function, <code>invlogit(a + b*x)</code> in python and pass it for evaluation in R's curve function? </p>

<p>I am grabbing <code>invlogit</code> from an R file by reading it in using the STAP library: <code>from rpy2.robjects.packages import STAP</code>. </p>

<p>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.</p>

<p>Simply pass in an actual function, call, or expression like <code>sin</code> as <code>x</code> is not assigned in Python. Below uses the example from the R documentation for <a href="http://stat.ethz.ch/R-manual/R-devel/library/graphics/html/curve.html" rel="nofollow">curve</a>: <code>curve(sin, -2*pi, 2*pi)</code>. Also, because you output a graph use <code>grDevices</code> (built-in R package) to save image to file:</p>

<pre><code>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()
</code></pre>

<p><a href="http://i.stack.imgur.com/LabOp.png" rel="nofollow"><img src="http://i.stack.imgur.com/LabOp.png" alt="RPy2 curve plot image 1"></a></p>

<p>Alternatively, you can define <code>(x)</code> just as your link shows:</p>

<pre><code>grdevices.png(file="Rpy2Curve.png", width=512, height=512)
ro.r('''eq &lt;- 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 
</code></pre>

<p><a href="http://i.stack.imgur.com/csF80.png" rel="nofollow"><img src="http://i.stack.imgur.com/csF80.png" alt="RPy2 curve plot image 2"></a></p>

<hr>

<p>UPDATE</p>

<p>Specifically to the OP's issue, to plot the inverse logit curve with the Python variables, <em>a</em> and <em>b</em>, derived from model coefficients, consider concatenating them to the <code>robjects.r()</code> string parameter:</p>

<pre><code>import rpy2.robjects as ro
ro.r('invlogit &lt;- function(x){ + exp(x)/(1 + exp(x)) }')

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

python curve rpy2