This site uses different types of cookies, including analytics and functional cookies (its own and from other sites). To change your cookie settings or find out more, click here. If you continue browsing our website, you accept these cookies.
About Matthew: Matthew is a Data Scientist at C2FO in Kansas City. He previously studied Physics for his BS at the University of Notre Dame followed by the University of Kansas for his MS. When he is not programming, Matthew enjoys playing board games, especially Race for the Galaxy.
During my time as a Data Scientist, I have been primarily a Python user. However, I wanted access to some of the power offered by R, specifically the auto.arima function in the R forecast package. This post will go over how to get you started on incorporating R functionality into your python workflow.
Want to follow along?
Download Rodeo and head to the tutorials section to find the code!
The two most popular options for data analysis and modeling are R and Python. Each has their own unique strengths and weaknesses, many which drive a user or a team to choose one over the other. Here are a few key strengths of both that I find particularly valuable:
Python is a 'real' programming language, allowing for more flexibility in your ability to solve specific problems
It offers many other libraries in addition to those needed for a Data Scientist's models
Python is making strides in the data analysis space with pandas, statsmodels, and scikitlearn
R libraries have been battle tested far longer than Python, giving a Data Scientist a verified set of tools at their disposal.
There are also many implementations of various functions, allowing you to find the library that is right for you.
Due to the long history of R packages, there is a strong community around data analysis.
How Does RPy2 come into play?
RPy2 creates a framework that can translate Python objects into R objects, pass them into R functions, and convert R output back into Python objects. There are many ways that a user can integrate this into their workflow. You may decide to call R library functions as you would native Python functions, or you may decide to write a single R script to run your data on. Below I'll go over the few basics of how to get used to the flow of RPy2, using some sample data from R. We will load in some data, model it in R, and plot the results back in Python.
Note: While I will be working with time series data, I will not be passing them back and forth between Python and R. This can get really tricky and can cause many headaches, so I find it easier to handle all the time series indexing on the Python side.
Importing R objects and libraries to Python
We can import both R functions and libraries as Python objects. We can load a default R function like ts() through the robjects.r() function, and assign it to a Python variable. Similarly, we can use importr to load an R library into a namespace.
import rpy2.robjects as robjects
from rpy2.robjects.packages import importr
Now that we have these objects in loaded, we can call them similar to standard Python practices. Let's load up some data to model, and create a forecast off of it. You can download the dataset here.
import pandas as pd
from rpy2.robjects import pandas2ri
Note: Take care of the import of pandas2ri and the activate() function. These a key to transforming certain datatypes from Python to R.
We've taken our data, transformed it into an robject, and called R functions on our objects. However, we are left with one really messy issue, and that is the output of our function. Our R forecast object isn't nicely translated into a neat Python object for you to parse. You can find all the information from your forecast that you need as shown below:
import matplotlib.pyplot as plt
import seaborn as sns
We can also draw our newly-created plot and save it as a png file on our machine:
Blocking R code into a Function
Instead of bringing everything into Python, we can instead manipulate our objects purely in R, and return only the desired output back to Python. Similar to how we used the robjects.r() to create a python object mapping to the ts function, we can define our own function and assign it to a Python object. We still have to create an R data object to pass into the function, but the rest is done on the R side.
We now have our resulting forecast in an R Dataframe! You (hopefully) have seen the pandas2ri import above, as this adds a nice easy finish to our data transformation. With its ri2py() function, we can convert our R Dataframe to a Pandas DataFrame object.
RPy2 is a fantastic tool that brings the leverage of R functions into a Python workflow. Unfortunately, it is not optimized to return all R objects into a nicely wrapped Python object, though you can write some tools to do so. However, using the method of a self defined R function, you can easily pass in some data you want to analyze, run it through the R functions, and return what you need to Python where its ready to be consumed for future use.