Search code examples
rpython-3.xstatisticstime-seriesrpy2

How to access class attribute of an R object in python?


I am using the vars package in jupyter notebook as so

For simplicity's sake the first couple of lines on your Jupyter notebook should be as

import pandas as pd, numpy as np

# Call function from R
import os
os.environ['R_USER'] = 'D:\Anaconda3\Lib\site-packages\rpy2'
import rpy2.robjects as robjects
from rpy2.robjects import pandas2ri
pandas2ri.activate()

from rpy2.robjects.packages import importr
utils = importr('utils')
ggplot2 = importr("ggplot2", lib_loc = "C:/.../R/win-library/3.3")

# Import vars
Rvars = importr("vars", lib_loc = "C:/.../R/win-library/3.3")

and I have a dataframe say

df = pd.DataFrame(np.random.random((108, 2)), columns=['Number1','Number2'])
df.head()

I coded

Model2=Rvars.VAR(df,p=3, type='const')
type(Model2)

and recieved the following

rpy2.robjects.vectors.ListVector

When coding with tuple

tuple(Model2.slots)

I received

('names', 'class')

Thus printing the names as

print(Model2.names)

Generates

[1] "varresult"    "datamat"      "y"            "type"         "p"           

 [6] "K"            "obs"          "totobs"       "restrictions" "call"

My Question

All the above values are only elements of a list with class attribute 'varest' in R as described in

https://cran.r-project.org/web/packages/vars/vars.pdf

on page 45

How may I access varresult from python?


Solution

  • I think I have figured it out. I received good help from

    http://rpy.sourceforge.net/rpy2/doc-2.1/html/introduction.html

    For illustration purpose, consider the trivial case of getting the variable y which represents the data matrix of the endogenous variables.

    .rx and .rx2 are new features in rpy2 as discussed in

    http://rpy.sourceforge.net/rpy2/doc-2.2/html/changes.html

    Although different, a similar question was answered in

    extract coefficients from R lme model in rpy2

    Based on my raised question

    let us extract the variable y which represents the data matrix of the endogenous variables.

    This can be achieved by the python attribute rx2 as

    A=Model2.rx2('y')
    y=pd.DataFrame(np.array(A),columns=A.names[1])
    y.head()
    

    You will find that the y.head() is the same as the original dataframe df.head()

    However, to extract the element varresult is a bit more complicated

    I followed the similar argument as in

    A=Model2.rx2('varresult')
    print(A.names)
    
    Output: [1] "Number1" "Number2"
    

    Then

    B=A.rx2('Number1')
    print(B.names)
    
    Output: 
    [1] "coefficients"  "residuals"     "effects"       "rank"         
    
     [5] "fitted.values" "assign"        "qr"            "df.residual"  
    
     [9] "xlevels"       "call"          "terms"         "model"
    

    For example, to get the residuals of the regression from the first equation one may use the following

    resid1=np.array(B.rx2('residuals'))