In [2]:
# pip install rpy2
In [26]:
%load_ext rpy2.ipython
In [27]:
import numpy as np
X = np.array([4.5,6.3,7.9, 10.3])
%Rpush X
%R mean(X)
Out[27]:
In [7]:
%%R
Y = c(2,4,3,9)
summary(lm(Y~X))
In [8]:
%R plot(X, Y)
In [9]:
%R dat = data.frame(X, Y)
Out[9]:
In [10]:
%Rpull dat
In [11]:
dat
Out[11]:
In [24]:
dat['X']
Out[24]:
In [12]:
import rpy2.interactive as r
import rpy2.interactive.packages # this can take few seconds
r.packages.importr('ggplot2')
Out[12]:
In [13]:
%%R
p = ggplot(data = dat, mapping = aes(x = X, y =Y))
p + geom_point()
In [14]:
%%R
library(lattice)
attach(mtcars)
# scatterplot matrix
splom(mtcars[c(1,3,4,5,6)], main="MTCARS Data")
In [15]:
%%R
data(diamonds)
set.seed(42)
small = diamonds[sample(nrow(diamonds), 1000), ]
head(small)
p = ggplot(data = small, mapping = aes(x = carat, y = price))
p + geom_point()
In [16]:
%%R
p = ggplot(data=small, mapping=aes(x=carat, y=price, shape=cut, colour=color))
p+geom_point()
In [17]:
import rpy2.robjects as ro
from rpy2.robjects.packages import importr
base = importr('base')
fit_full = ro.r("lm('mpg ~ wt + cyl', data=mtcars)")
print(base.summary(fit_full))
In [18]:
diamonds = ro.r("data(diamonds)")
In [19]:
%R head(diamonds)
Out[19]:
In [21]:
fit_dia = ro.r("lm('price ~ carat + cut + color + clarity + depth', data=diamonds)")
In [22]:
print(base.summary(fit_dia))
In [ ]: