We begin with some basics, establishing that the environment is working.
In [1]:
print "hello from python"
In [2]:
import sys
sys.version
Out[2]:
Python is working just fine. Let's load up access to R:
In [3]:
%load_ext rmagic
In [4]:
%R print("hello from R")
In [5]:
%R R.version
Out[5]:
In [6]:
import random
x = [random.normalvariate(0, 1) for i in range(10000)]
sum(x) / len(x)
Out[6]:
In [7]:
%matplotlib inline
import matplotlib.pyplot as plt
plt.hist(x, bins=20)
plt.show()
Note: at this point I spent two hours configuring the ipython environment to work. I couldn't fix a problem that left the figures not drawing, so I gave up and installed the anaconda environment: https://store.continuum.io/cshop/anaconda/ and this worked fine, after separately installing rpy2 to provide R integration.
In [8]:
%R x <- rnorm(10000, 0, 1)
%R hist(x)
Out[8]:
In [9]:
%R xl <- rlnorm(10000)
Out[9]:
In [10]:
plt.hist(xl, bins=20)
plt.show()
That doesn't work, but there's another way:
In [11]:
from rpy2.robjects import r
xl = r('rlnorm(10000)')
plt.hist(xl, bins=20)
plt.show()
That worked!
Let's try again with a log y-axis to better show the range of data:
In [12]:
plt.hist(xl, bins=20, log=True)
plt.show()