[This article was first published on Yet Another Blog in Statistical Computing » S+/R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Different from RPy2, PypeR provides another simple way to access R from Python through pipes (http://www.jstatsoft.org/v35/c02/paper). This handy feature enables data analysts to do the data munging with python and the statistical analysis with R by passing objects interactively between two computing systems.

Below is a simple demonstration on how to call R within Python through RypeR, estimate a Beta regression, and then return the model prediction from R back to Python.

```In : # LOAD PYTHON PACKAGES

In : import pandas as pd

In : import pyper as pr

In : # READ DATA

In : data = pd.read_table("/home/liuwensui/Documents/data/csdata.txt", header = 0)

In : # CREATE A R INSTANCE WITH PYPER

In : r = pr.R(use_pandas = True)

In : # PASS DATA FROM PYTHON TO R

In : r.assign("rdata", data)

In : # SHOW DATA SUMMARY

In : print r("summary(rdata)")
try({summary(rdata)})
LEV_LT3           TAX_NDEB           COLLAT1           SIZE1
Min.   :0.00000   Min.   :  0.0000   Min.   :0.0000   Min.   : 7.738
1st Qu.:0.00000   1st Qu.:  0.3494   1st Qu.:0.1241   1st Qu.:12.317
Median :0.00000   Median :  0.5666   Median :0.2876   Median :13.540
Mean   :0.09083   Mean   :  0.8245   Mean   :0.3174   Mean   :13.511
3rd Qu.:0.01169   3rd Qu.:  0.7891   3rd Qu.:0.4724   3rd Qu.:14.751
Max.   :0.99837   Max.   :102.1495   Max.   :0.9953   Max.   :18.587
PROF2              GROWTH2             AGE              LIQ
Min.   :0.0000158   Min.   :-81.248   Min.   :  6.00   Min.   :0.00000
1st Qu.:0.0721233   1st Qu.: -3.563   1st Qu.: 11.00   1st Qu.:0.03483
Median :0.1203435   Median :  6.164   Median : 17.00   Median :0.10854
Mean   :0.1445929   Mean   : 13.620   Mean   : 20.37   Mean   :0.20281
3rd Qu.:0.1875148   3rd Qu.: 21.952   3rd Qu.: 25.00   3rd Qu.:0.29137
Max.   :1.5902009   Max.   :681.354   Max.   :210.00   Max.   :1.00018
IND2A            IND3A            IND4A             IND5A
Min.   :0.0000   Min.   :0.0000   Min.   :0.00000   Min.   :0.00000
1st Qu.:0.0000   1st Qu.:0.0000   1st Qu.:0.00000   1st Qu.:0.00000
Median :1.0000   Median :0.0000   Median :0.00000   Median :0.00000
Mean   :0.6116   Mean   :0.1902   Mean   :0.02692   Mean   :0.09907
3rd Qu.:1.0000   3rd Qu.:0.0000   3rd Qu.:0.00000   3rd Qu.:0.00000
Max.   :1.0000   Max.   :1.0000   Max.   :1.00000   Max.   :1.00000

In : # LOAD R PACKAGE

In : r("library(betareg)")

In : # ESTIMATE A BETA REGRESSION

In : r("m <- betareg(LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata, subset = LEV_LT3 > 0)")
Out: 'try({m <- betareg(LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata, subset = LEV_LT3 > 0)})\n'

In : # OUTPUT MODEL SUMMARY

In : print r("summary(m)")
try({summary(m)})

Call:
betareg(formula = LEV_LT3 ~ SIZE1 + PROF2 + GROWTH2 + AGE + IND3A, data = rdata,
subset = LEV_LT3 > 0)

Standardized weighted residuals 2:
Min      1Q  Median      3Q     Max
-7.2802 -0.5194  0.0777  0.6037  5.8777

Coefficients (mean model with logit link):
Estimate Std. Error z value Pr(>|z|)
(Intercept)  1.229773   0.312990   3.929 8.53e-05 ***
SIZE1       -0.105009   0.021211  -4.951 7.39e-07 ***
PROF2       -2.414794   0.377271  -6.401 1.55e-10 ***
GROWTH2      0.003306   0.001043   3.169  0.00153 **
AGE         -0.004999   0.001795  -2.786  0.00534 **
IND3A        0.688314   0.074069   9.293  < 2e-16 ***

Phi coefficients (precision model with identity link):
Estimate Std. Error z value Pr(>|z|)
(phi)   3.9362     0.1528   25.77   <2e-16 ***
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

Type of estimator: ML (maximum likelihood)
Log-likelihood: 266.7 on 7 Df
Pseudo R-squared: 0.1468
Number of iterations: 25 (BFGS) + 2 (Fisher scoring)

In : # CALCULATE MODEL PREDICTION

In : r("beta_fit <- predict(m, link = 'response')")
Out: "try({beta_fit <- predict(m, link = 'response')})\n"

In : # SHOW PREDICTION SUMMARY IN R

In : print r("summary(beta_fit)")
try({summary(beta_fit)})
Min. 1st Qu.  Median    Mean 3rd Qu.    Max.
0.1634  0.3069  0.3465  0.3657  0.4007  0.6695

In : # PASS DATA FROM R TO PYTHON

In : pydata = pd.DataFrame(r.get("beta_fit"), columns = ["y_hat"])

In : # SHOW PREDICTION SUMMARY IN PYTHON

In : pydata.y_hat.describe()
Out:
count    1116.000000
mean        0.365675
std         0.089804
min         0.163388
25%         0.306897
50%         0.346483
75%         0.400656
max         0.669489
```  