[python 데이터 분석] Python 에서 R언어 패키지 호출 : Calling R From Python With rpy2

Calling R From Python With rpy2

by Isabella Velásquez

This post on R Views is about… Python! Surprising, I know. Python has several well-written packages for statistics and data science, but CRAN, R’s central repository, contains thousands of packages implementing sophisticated statistical algorithms that have been field-tested over many years. Thanks to the rpy2 package, Pythonistas can take advantage of the great work already done by the R community. rpy2 provides an interface that allows you to run R in Python processes. Users can move between languages and use the best of both programming languages.

Below, I walk you through how to call three powerful R packages from Python: stats, lme4, and ggplot2. Each section contains detailed steps, and you can find the complete script in the appendix.

Getting started with rpy2

Installing rpy2

First up, install the necessary packages. You must have Python >=3.7 and R >= 4.0 installed to use rpy2 3.5.2. Once R is installed, install the rpy2 package by running pip install rpy2. If you’d like to see where you installed rpy2 on your machine, you can run python -m rpy2.situation.

If you are working in a Jupyter notebook, you may want to see your ggplot2 plots within your notebook. Run conda install r-ggplot2 in your Jupyter environment so that they show up.


Installation

pip install rpy2
conda install r-ggplot2

Importing rpy2 packages and subpackages

Then, you’ll import the packages and subpackages. Import the top-level rpy2 package by running import rpy2.

Import the top-level subpackage robjects with import rpy2.robjects as robjects. Running robjects also initializes R in the current Python process.

There are a couple of other steps to make working in a notebook a bit easier:

  • rpy2 customizes the display of R objects, such as data frames in a notebook. Run rpy2.ipython.html.init_printing() to enable this customization.
  • You may want to see ggplot2 objects in an output cell of a notebook. Enable this with from rpy2.ipython.ggplot import image_png.

Import rpy2 packages and subpackages

import rpy2
import rpy2.robjects as robjects

## To aid in printing HTML in notebooks
import rpy2.ipython.html
rpy2.ipython.html.init_printing()

## To see plots in an output cell
from rpy2.ipython.ggplot import image_png

Installing and loading R packages with rpy2

Installing and loading R packages are often the first steps in R scripts. The rpy2 package provides a function rpy2.robjects.packages.importr() that mimics these steps. Run the below, where you’ll also import the function data() for later.

from rpy2.robjects.packages import importr, data

Use importr() to load the utils and base packages, which normally come preinstalled with R.

utils = importr('utils')
base = importr('base')

You can also download and install packages from a package repository (such as CRAN) with rpy2.

First, select your desired mirror with utils.chooseCRANmirror() and install your packages with utils.install_packages(). Below, you will install stats and lme4.

utils.install_packages('stats')
utils.install_packages('lme4')

Once installed, load the packages with importr().

stats = importr('stats')
lme4 = importr('lme4')

Installing and loading ggplot2 with rpy2

The rpy2 documentation recommends a different method for installing ggplot2:

import rpy2.robjects.lib.ggplot2 as ggplot2

Installation of R packages

from rpy2.robjects.packages import importr, data

utils = importr('utils')
base = importr('base')

utils.chooseCRANmirror(ind=1)
utils.install_packages('lme4')
utils.install_packages('stats')

stats = importr('stats')
lme4 = importr('lme4')

import rpy2.robjects.lib.ggplot2 as ggplot2

Using the Python interface for R

With everything set up, you can begin using the Python interface! However, this is not as simple as writing R code in your Jupyter notebook. You have to ‘translate’ R functions to call them from Python.

Below, I analyze the sleepstudy data from lme4. The data represents the average reaction time per day (in milliseconds) for subjects in a sleep deprivation study. On days 0-1, the subjects had their regular amount of sleep. Day 2 was baseline, after which sleep deprivation started.

Working with data in rpy2

You can ‘fetch’ data from R packages with rpy2. The code below is the equivalent to lme4::sleepstudy in R. Notice you use the data() function imported earlier:

sleepstudy = data(lme4).fetch('sleepstudy')['sleepstudy']
sleepstudy

Visualizing data with ggplot2 from Python

The ggplot2 package provides flexible and robust functionality for creating graphics. We can plot our data in Python with a few tweaks to the usual ggplot2 syntax.

Visualize the relationship between days and reaction time from the sleep study:

gp = ggplot2.ggplot(sleepstudy)

p1 = (gp +
      ggplot2.aes_string(x = 'Days', y = 'Reaction') +
      ggplot2.geom_point(color = '#648FFF', alpha = 0.7) +
      ggplot2.geom_smooth(method = 'lm', color = 'black') +
      ggplot2.theme_minimal())

display(image_png(p1))
R[write to console]: `geom_smooth()` using formula 'y ~ x'

p1.png

경축! 아무것도 안하여 에스천사게임즈가 새로운 모습으로 재오픈 하였습니다.
어린이용이며, 설치가 필요없는 브라우저 게임입니다.
https://s1004games.com

 

 

Running models and viewing results with rpy2

You can leverage R’s statistical capabilities in Python. Call the lm() function from the stats package to write a linear model with rpy2:

lm1 = stats.lm('Reaction ~ Days', data = sleepstudy)
# print(base.summary(lm1))

Printing the fit object with print(base.summary(lm1)) displays both the code used to perform the fit and the results. For brevity, limit the output to the coefficients:

print(stats.coef(lm1))
(Intercept)        Days 
  251.40510    10.46729 

Print the 95% confidence intervals as well:

print(stats.confint(lm1))
                 2.5 %    97.5 %
(Intercept) 238.360753 264.44946
Days          8.023855  12.91072

The results show that the more days of sleep deprivation, the slower the reaction time. However, you may realize that the linear model disguises the variation across the study’s subjects. To illustrate these differences, you can plot Reaction vs. Days for each subject separately with ggplot2’s facet_wrap():

p2 = (p1 +
      ggplot2.facet_wrap(robjects.Formula('. ~ Subject'), **{'ncol':6}))

display(image_png(p2))
R[write to console]: `geom_smooth()` using formula 'y ~ x'

p2.png

 

 

The plots show that the average reaction time increases approximately linearly with the number of sleep-deprived days. However, the slope and intercept vary by subject.

You can use the lme4 package to create a model that calculates the fixed effects of days of sleep deprivation on reaction time while considering starting reaction time and differences for each individual.

fm1 = lme4.lmer('Reaction ~ Days + (Days | Subject)', data = sleepstudy)
print(base.summary(fm1))

Here is the output from summary, abbreviated to the results:

REML criterion at convergence: 1743.6

Scaled residuals: 
    Min      1Q  Median      3Q     Max 
-3.9536 -0.4634  0.0231  0.4634  5.1793 

Random effects:
 Groups   Name        Variance Std.Dev. Corr
 Subject  (Intercept) 612.10   24.741       
          Days         35.07    5.922   0.07
 Residual             654.94   25.592       
Number of obs: 180, groups:  Subject, 18

Fixed effects:
            Estimate Std. Error t value
(Intercept)  251.405      6.825  36.838
Days          10.467      1.546   6.771

Correlation of Fixed Effects:
     (Intr)
Days -0.138

The estimates of the standard deviations of the random effects for the intercept and the slope are 24.74 ms and 5.92 ms/day. The fixed-effects coefficients are 251.4 ms and 10.47 ms/day for the intercept and slope. These results match the ones output by R.1

Learning how to use rpy2

Working with rpy2 took investigative work. There were R functions that I assumed would be straightforward to translate to Python but they turned out to be more complicated.

For example, I wanted to confirm that each subject had different linear model results. The subset() function is part of base R, so I thought I could run:

lm1 = stats.lm('Reaction ~ Days', data =  base.subset(sleepstudy, Subject == 308))

Unfortunately, this is incorrect. I found the answer reading the Extracting items section of the documentation. After learning how rpy2 works with R data frames, I was able to find the actual way to subset the data:

lm308 = stats.lm('Reaction ~ Days', data = sleepstudy.rx(sleepstudy.rx('Subject').ro == 308, True))
print(stats.confint(lm308))
                 2.5 %    97.5 %
(Intercept) 179.433863 308.95148
Days          9.634267  33.89514

You will need knowledge of Python for more flexibility with the rpy2 interface. For instance, the ability to define the number of columns with **{'ncol':6} in facet_wrap() above was not in the rpy2 documentation. Unpacking values with keyword arguments, denoted by **{}, is part of the general Python syntax.

Learn more

There are many ways that Python users can leverage R packages and functions in their scripts. The rpy2 is a helpful tool for those who want to use the R community’s impactful work. As stated in the rpy2 documentation, “Reuse. Get things done. Don’t reimplement.”

Appendix

# Installation
pip install rpy2
conda install r-ggplot2

# Import rpy2 packages and subpackages
import rpy2
import rpy2.robjects as robjects

## To aid in printing HTML in notebooks
import rpy2.ipython.html
rpy2.ipython.html.init_printing()

## To see plots in an output cell
from rpy2.ipython.ggplot import image_png

# Installation of R packages
from rpy2.robjects.packages import importr, data

utils = importr('utils')
base = importr('base')

utils.chooseCRANmirror(ind=1)
utils.install_packages('lme4')
utils.install_packages('stats')

stats = importr('stats')
lme4 = importr('lme4')

import rpy2.robjects.lib.ggplot2 as ggplot2

# Working with data in rpy2
sleepstudy = data(lme4).fetch('sleepstudy')['sleepstudy']
sleepstudy

# Visualizing with ggplot2 in Python
gp = ggplot2.ggplot(sleepstudy)

p1 = (gp +
      ggplot2.aes_string(x = 'Days', y = 'Reaction') +
      ggplot2.geom_point(color = '#648FFF', alpha = 0.7) +
      ggplot2.geom_smooth(method = 'lm', color = 'black') +
      ggplot2.theme_minimal())

display(image_png(p1))

p2 = (p1 +
      ggplot2.facet_wrap(robjects.Formula('. ~ Subject'), **{'ncol':6}))

display(image_png(p2))

# Run models and view results
lm1 = stats.lm('Reaction ~ Days', data = sleepstudy)
print(stats.coef(lm1))
print(stats.confint(lm1))

fm1 = lme4.lmer('Reaction ~ Days + (Days | Subject)', data = sleepstudy)
print(base.summary(fm1))

  1. Douglas Bates, Martin Maechler, Ben Bolker, Steve Walker (2015). Fitting Linear Mixed-Effects Models Using lme4. Journal of Statistical Software, 67(1), 1-48. doi:10.18637/jss.v067.i01. 

[출처] https://rviews.rstudio.com/2022/05/25/calling-r-from-python-with-rpy2/

 

 

 

본 웹사이트는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 웹사이트 서버의 유지 및 관리, 그리고 기술 콘텐츠 향상을 위해 쓰여집니다.
번호 제목 글쓴이 날짜 조회 수
15 [python 데이터분석] [jupyter] 주피터 노트북에 이미지 삽입 file 졸리운_곰 2025.09.06 524
14 [python 데이터분석] [Python] Streamlit 사용법 (python 데이터분석 웹 만들기) file 졸리운_곰 2024.12.22 462
13 [python 데이터분석] Anaconda : Error while loading conda entry point: conda-libmamba-solver (libarchive.so.19: cannot open shared object file: No such file or directory) 졸리운_곰 2024.12.14 520
12 [python 데이터분석] Anaconda | Conda update 반영 안됨(update 후에도 버전 변경 없음) file 졸리운_곰 2024.11.18 431
11 [python 데이터분석] Keeping Anaconda Up To Date 졸리운_곰 2024.05.30 552
10 [python 데이터 분석] 국내 경제 100대 통계지표 졸리운_곰 2024.02.18 672
» [python 데이터 분석] Python 에서 R언어 패키지 호출 : Calling R From Python With rpy2 file 졸리운_곰 2024.01.28 676
8 [python 데이터 분석] 파이썬을 활용한 코스피, 달러 환율정보 수집부터 차트 시각화까지 file 졸리운_곰 2023.12.11 735
7 [Python 데이터분석][pandas] [Python pandas] DataFrame의 문자열 칼럼을 숫자형으로 바꾸기 : pd.to_numeric(), DataFrame.astype() file 졸리운_곰 2023.12.09 357
6 [Python 데이터분석] [Python 환경설정] VS code 설치 및 Anaconda와 연동하기 file 졸리운_곰 2023.03.17 527
5 [Python 데이터분석][python 데이터분석 프로덕션] [Python] Docker를 사용한 Dash 웹앱 생성 file 졸리운_곰 2021.12.10 403
4 [Python 데이터분석] [pandas] 공공데이터(csv) 활용시 한글 깨짐 현상 해결 file 졸리운_곰 2021.09.30 592
3 [Python 데이터분석] 공공데이터포털::공휴일 데이터 조회 (REST API) file 졸리운_곰 2021.09.30 358
2 [Python 데이터 분석] pandas의 to_csv()를 사용해서 csv 파일로 저장하기(save 하기) 졸리운_곰 2021.09.29 603
1 [Python 데이터 분석] 데이터 과학을 단순하게 만드는 3가지 Python 패키지 file 졸리운_곰 2021.09.24 513
대표 김성준 주소 : 경기 용인 분당수지 U타워 등록번호 : 142-07-27414
통신판매업 신고 : 제2012-용인수지-0185호 출판업 신고 : 수지구청 제 123호 개인정보보호최고책임자 : 김성준 sjkim70@stechstar.com
대표전화 : 010-4589-2193 [fax] 02-6280-1294 COPYRIGHT(C) stechstar.com ALL RIGHTS RESERVED