简体   繁体   中英

how to suppress python printing code using rpy2 for summary of models

I am using rpy2 for running ivreg. However, whenever I try to print the summary of the model it prints the entire function and dataframe instead of just the results like R. Is there a way to have R like print. Here is the code I am using:

import pandas as pd
import json 
# R2pi package
from rpy2.robjects import numpy2ri, pandas2ri
from rpy2.robjects.packages import importr
import rpy2.robjects as ro
# R imports (Assuming AER and ivpack are installed in R)
base = importr("base")
aer = importr("AER")
# Automatic conversion btw R Vectors and Python objects
numpy2ri.activate()
pandas2ri.activate()


def ivreg(*argv, **kwargs):
    model = aer.ivreg(*argv, **kwargs)
    return model

n = 10000
df = pd.DataFrame(data=np.random.multivariate_normal(
                            mean=[0] * 5,
                            cov=np.ones((5,5)) + np.eye(5),
                            size=n),
                  columns =["Y", "X1", "X2", "Z1", "Z2"])
clusterid = np.tile([1,2,3,4,5], (n//5, ))
df['X3'] = np.random.uniform(1,4,n).round(0).astype(int)

# Example regression
model = ivreg(formula="Y ~ . ", data=df)
print(base.summary(model))

This because your object is passed "inline" to the function call, that is it has no associated symbol name in R. In the snippet below df is just a data structure.

model = ivreg(formula="Y ~ . ", data=df)
print(base.summary(model))

You could assign columns in df to symbols in R's globalenv, as shown in the introduction: https://rpy2.github.io/doc/v3.3.x/html/introduction.html#linear-models

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM