Search code examples
pythonpython-3.ximportjupyter-notebookrpy2

Import and use R 'heavy' package using rpy2


I am trying to use the R heavy package to run a GLM in Python. I think I managed to import that package, but I get an error message when I try to use it. Here's my code :

from rpy2.robjects.packages import importr 
from rpy2.robjects import pandas2ri

pandas2ri.activate()

data_r_df = pandas2ri.py2ri(data)

d = {'package.dependencies': 'package_dot_dependencies',
 'package_dependencies': 'package_uscore_dependencies'}
custom_analytics = importr('heavy', 
               robject_translations = d, lib_loc = "/usr/local/lib/R/3.5/site-library")
stage1_processed_data = heavy.heavyLm('Y ~ .', data = data_r_df, family = Student(df = 4))

The error message that I get is :

NameError Traceback (most recent call last) in 17 type(data_r_df) 18 ---> 19 stage1_processed_data = heavy.heavyLm('Y ~ .', data = data_r_df, family = Student(df = 4))

NameError: name 'heavy' is not defined


Solution

  • May be this is because there is no object called heavy defined in your Python code as you have chosen to map the R package to an object called custom_analytics ?

    If the case, then the R function you are looking for can be called with custom_analytics.heavyLm().