Skip to content

Instantly share code, notes, and snippets.

@alexwoolford
Last active August 29, 2015 14:07
Show Gist options
  • Save alexwoolford/c77f1fc87e37d8a572e4 to your computer and use it in GitHub Desktop.
Save alexwoolford/c77f1fc87e37d8a572e4 to your computer and use it in GitHub Desktop.
$ ipython
In [1]: import pandas as pd
In [2]: mpg = pd.read_csv('mpg.csv')
# move mpg dataframe to R. The "-i mpg" is an input. Dataframe moves from pandas to R.
In [3]: %load_ext rpy2.ipython
In [4]: %%R -i mpg
...: mpg.lm <- lm(hwy ~ ., data = mpg)
...: summary(mpg.lm)
...:
Call:
lm(formula = hwy ~ ., data = mpg)
Residuals:
Min 1Q Median 3Q Max
-2.56544 -0.54876 0.00631 0.46380 2.54085
Coefficients: (20 not defined because of singularities)
Estimate Std. Error t value Pr(>|t|)
(Intercept) -147.89919 45.12149 -3.278 0.001259 **
manufacturerchevrolet -0.07523 0.79154 -0.095 0.924389
manufacturerdodge -5.13203 1.41378 -3.630 0.000371 ***
manufacturerford -1.55781 1.38801 -1.122 0.263239
manufacturerhonda -0.59813 1.41389 -0.423 0.672778
manufacturerhyundai -1.28086 1.39510 -0.918 0.359810
manufacturerjeep -5.35642 1.40137 -3.822 0.000183 ***
manufacturerland rover -3.27903 1.40198 -2.339 0.020459 *
manufacturerlincoln -3.17958 1.48043 -2.148 0.033095 *
manufacturermercury -4.60493 1.42801 -3.225 0.001502 **
manufacturernissan -4.87560 1.41981 -3.434 0.000741 ***
manufacturerpontiac 1.00415 0.80246 1.251 0.212464
manufacturersubaru -2.19715 1.24436 -1.766 0.079174 .
manufacturertoyota -5.50331 1.38981 -3.960 0.000109 ***
manufacturervolkswagen 0.91416 0.67962 1.345 0.180313
modela4 2.09185 1.18317 1.768 0.078783 .
modela4 quattro 0.93240 1.11315 0.838 0.403370
modela6 quattro NA NA NA NA
modelaltima 4.65544 1.19531 3.895 0.000139 ***
modelc1500 suburban 2wd -3.83985 1.38658 -2.769 0.006217 **
modelcamry 5.07921 1.30992 3.877 0.000149 ***
modelcamry solara 5.13686 0.99411 5.167 6.36e-07 ***
modelcaravan 2wd 2.99227 0.51484 5.812 2.82e-08 ***
modelcivic NA NA NA NA
modelcorolla 6.35934 1.11122 5.723 4.40e-08 ***
modelcorvette 1.29816 1.46072 0.889 0.375365
modeldakota pickup 4wd 0.26640 0.44834 0.594 0.553146
modeldurango 4wd 0.13012 0.47705 0.273 0.785350
modelexpedition 2wd -1.48593 0.68925 -2.156 0.032444 *
modelexplorer 4wd -3.38590 0.53383 -6.343 1.83e-09 ***
modelf150 pickup 4wd -3.89203 0.50532 -7.702 9.14e-13 ***
modelforester awd -0.64526 0.63354 -1.019 0.309828
modelgrand cherokee 4wd NA NA NA NA
modelgrand prix NA NA NA NA
modelgti -1.43019 1.05924 -1.350 0.178675
modelimpreza awd NA NA NA NA
modeljetta -1.20836 1.02930 -1.174 0.241986
modelk1500 tahoe 4wd -5.42887 1.42240 -3.817 0.000187 ***
modelland cruiser wagon 4wd 1.11909 0.82087 1.363 0.174520
modelmalibu NA NA NA NA
modelmaxima 3.54620 1.39625 2.540 0.011952 *
modelmountaineer 4wd NA NA NA NA
modelmustang NA NA NA NA
modelnavigator 2wd NA NA NA NA
modelnew beetle -1.17533 1.31465 -0.894 0.372522
modelpassat NA NA NA NA
modelpathfinder 4wd NA NA NA NA
modelram 1500 pickup 4wd NA NA NA NA
modelrange rover NA NA NA NA
modelsonata 1.06729 1.30742 0.816 0.415409
modeltiburon NA NA NA NA
modeltoyota tacoma 4wd 0.10859 0.53127 0.204 0.838273
displ 0.27043 0.24309 1.112 0.267452
year 0.08069 0.02273 3.550 0.000494 ***
cyl -0.34384 0.14603 -2.355 0.019644 *
transauto(l3) -0.62059 0.96716 -0.642 0.521920
transauto(l4) 0.92040 0.61091 1.507 0.133697
transauto(l5) 1.39939 0.58240 2.403 0.017305 *
transauto(l6) 1.29868 0.72348 1.795 0.074351 .
transauto(s4) 0.63402 0.91104 0.696 0.487389
transauto(s5) 2.14954 0.85193 2.523 0.012511 *
transauto(s6) 0.68904 0.62041 1.111 0.268239
transmanual(m5) 1.11020 0.59822 1.856 0.065140 .
transmanual(m6) 0.90339 0.56318 1.604 0.110476
drvf NA NA NA NA
drvr NA NA NA NA
cty 0.91478 0.05681 16.102 < 2e-16 ***
fld -1.07125 1.23220 -0.869 0.385819
fle -4.92816 1.09789 -4.489 1.29e-05 ***
flp -4.13173 1.04039 -3.971 0.000104 ***
flr -3.32290 1.02569 -3.240 0.001429 **
classcompact -0.44789 0.78249 -0.572 0.567779
classmidsize 0.06069 1.18808 0.051 0.959318
classminivan NA NA NA NA
classpickup NA NA NA NA
classsubcompact NA NA NA NA
classsuv NA NA NA NA
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
Residual standard error: 0.9467 on 177 degrees of freedom
Multiple R-squared: 0.9808, Adjusted R-squared: 0.9747
F-statistic: 161.5 on 56 and 177 DF, p-value: < 2.2e-16
# The "-o meanHwyMpg" moves the R dataframe, that's calculated within the R cell, back to Python.
In [6]: %%R -o meanHwyMpg
...: library(plyr)
...: meanHwyMpg <- ddply(mpg, .(manufacturer), summarize, meanHwyMpg = mean(hwy))
...:
# And now, here we are back in Python, with the result from the previous R cell.
In [7]: meanHwyMpg
Out[7]:
manufacturer meanHwyMpg
0 audi 26.444444
1 chevrolet 21.894737
2 dodge 17.945946
3 ford 19.360000
4 honda 32.555556
5 hyundai 26.857143
6 jeep 17.625000
7 land rover 16.500000
8 lincoln 17.000000
9 mercury 18.000000
10 nissan 24.615385
11 pontiac 26.400000
12 subaru 25.571429
13 toyota 24.911765
14 volkswagen 29.222222
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment