##// END OF EJS Templates
rerunning notebook serially
rerunning notebook serially

File last commit:

r7216:af4d975a
r7216:af4d975a
Show More
rmagic_extension.ipynb
333 lines | 163.1 KiB | text/plain | TextLexer

Rmagic Functions Extension

Line magics

IPython has an rmagic extension that contains a some magic functions for working with R via rpy2. This extension can be loaded using the %load_ext magic as follows:

In [1]:
%load_ext rmagic
    

A typical use case one imagines is having some numpy arrays, wanting to compute some statistics of interest on these arrays and return the result back to python. Let's suppose we just want to fit a simple linear model to a scatterplot.

In [2]:
import numpy as np
import pylab
X = np.array([0,1,2,3,4])
Y = np.array([3,5,4,6,7])
pylab.scatter(X, Y)
Out[2]:
<matplotlib.collections.PathCollection at 0x10b9f2590>
No description has been provided for this image

We can accomplish this by first pushing variables to R, fitting a model and returning the results.

In [3]:
%Rpush X Y
%Rinline lm(Y~X)$coef
hee
Out[3]:
array([ 3.2,  0.9])

We can check that this is correct fairly easily:

In [4]:
Xr = X - X.mean(); Yr = Y - Y.mean()
slope = (Xr*Yr).sum() / (Xr**2).sum()
intercept = Y.mean() - X.mean() * slope
(intercept, slope)
Out[4]:
(3.2000000000000002, 0.90000000000000002)

There is one more line magic, %Rpull which assumes that some R code has been executed and there are variables in the rpy2 namespace that one would like to export to the python namespace. See below for an example.

Cell level magic

For the cell level magic, inputs can be passed via the -i or --inputs argument in the line. These variables are copied from the shell namespace to R's namespace. It would be nice not to have to copy these -- rnumpy ( http://bitbucket.org/njs/rnumpy/wiki/API ) has done some work to limit or at least make transparent the number of copies of an array. This seems like a natural thing to try to build on. Arrays can be output from R via the -o or --outputs argument in the line. All other arguments are sent to R's png function, which is the graphics device used to create the plots.

We can redo the above calculations in one ipython cell. We might also want to add some output such as a summary from R or perhaps the standard plotting diagnostics of the lm.

In [5]:
%%R -i X,Y -o XYcoef
XYlm = lm(Y~X)
XYcoef = coef(XYlm)
print(summary(XYlm))
par(mfrow=c(2,2))
plot(XYlm)
Call:
lm(formula = Y ~ X)

Residuals:
   1    2    3    4    5 
-0.2  0.9 -1.0  0.1  0.2 

Coefficients:
            Estimate Std. Error t value Pr(>|t|)  
(Intercept)   3.2000     0.6164   5.191   0.0139 *
X             0.9000     0.2517   3.576   0.0374 *
---
Signif. codes:  0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

Residual standard error: 0.7958 on 3 degrees of freedom
Multiple R-squared:  0.81,	Adjusted R-squared: 0.7467 
F-statistic: 12.79 on 1 and 3 DF,  p-value: 0.03739 

No description has been provided for this image

Often, we will want to do more than a simple linear regression model. There may be several lines of R code that we want to use before returning to python. This is the cell-level magic.

In this example, we will generate some random data, fit a LASSO model using the lars package and plot the results, returning the array of coefficients. This example assumes that the lars package is installed in R.

In [6]:
X = np.random.standard_normal((100,10))
Y = np.random.standard_normal((100,1))
In [7]:
%%R -i X,Y -o larscoef
library(lars)
larsobj = lars(X, Y)
plot(larsobj)
larscoef = coef(larsobj)
larspred = predict(larsobj, s=seq(0,1,length=101), mode='fraction')
lcoef = larspred$coefficients
lfrac = larspred$fraction
Loaded lars 1.1

Warning message:
In predict.lars(larsobj, s = seq(0, 1, length = 101), mode = "fraction") :
  Type=fit with no newx argument; type switched to coefficients
No description has been provided for this image
In [8]:
larscoef.shape
Out[8]:
(11, 10)

Using Rpull

In [9]:
%Rpull lcoef lfrac
f = [pylab.plot(lfrac, c) for c in lcoef.T]
No description has been provided for this image

Passing data back and forth

Currently, data is passed through RMagics.pyconverter when going from python to R and RMagics.Rconverter when going from R to python. These currently default to numpy.ndarray. Future work will involve writing better converters, most likely involving integration with http://pandas.sourceforge.net.

In [9]:
 
In [ ]: