**UPDATE (2014-09-29)**: in newer versions of rpy2, the IPython extension with the R magic is `rpy2.ipython`

and not `rmagic`

as stated in the book.

- Download and install R for your operating system. (http://cran.r-project.org/mirrors.html)
- Download and install rpy2. Windows users can try to download an
*experimental*installer on Chris Gohlke's webpage. (http://www.lfd.uci.edu/~gohlke/pythonlibs/#rpy2) - Then, to be able to execute R code in an IPython notebook, execute
`%load_ext rpy2.ipython`

first.

rpy2 does not appear to work well on Windows. We recommend using Linux or Mac OS X.

To install R and rpy2 on Ubuntu, run the following commands:

```
sudo apt-get install r-base-dev
sudo apt-get install python-rpy2
```

- Let's load the
*longley*dataset with the statsmodels package. This dataset contains a few economic indicators in the US from 1947 to 1962. We also load the IPython R extension.

In [ ]:

```
import statsmodels.datasets as sd
```

In [ ]:

```
data = sd.longley.load_pandas()
```

In [ ]:

```
%load_ext rpy2.ipython
```

- We define
`x`

and`y`

as the exogeneous (independent) and endogenous (dependent) variables, respectively. The endogenous variable quantifies the total employment in the country.

In [ ]:

```
data.endog_name, data.exog_name
```

In [ ]:

```
y, x = data.endog, data.exog
```

- For convenience, we add the endogenous variable to the
`x`

DataFrame.

In [ ]:

```
x['TOTEMP'] = y
```

In [ ]:

```
x
```

- We will make a simple plot in R. First, we need to pass Python variables to R. We can use the
`%R -i var1,var2`

magic. Then, we can call R's`plot`

command.

In [ ]:

```
gnp = x['GNP']
totemp = x['TOTEMP']
```

In [ ]:

```
%R
```

In [ ]:

```
%R -i totemp,gnp plot(gnp, totemp)
```

- Now that the data has been passed to R, we can fit a linear model to the data. The
`lm`

function lets us perform a linear regression. Here, we want to express`totemp`

(total employement) as a function of the country's GNP.

In [ ]:

```
%%R
fit <- lm(totemp ~ gnp); # Least-squares regression
print(fit$coefficients) # Display the coefficients of the fit.
plot(gnp, totemp) # Plot the data points.
abline(fit) # And plot the linear regression.
```

You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).

IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).