简体   繁体   English

如何使用Stargazer在rpy2中打印适合

[英]How to use Stargazer to print fit in rpy2

I'm learning how to use rpy2, and I would like to create formatted regression output using the stargazer package. 我正在学习如何使用rpy2,我想使用stargazer软件包创建格式化的回归输出。 My best guess of how to do this is the following code: 我对如何执行此操作的最佳猜测是以下代码:

import pandas as pd
import rpy2.robjects as robjects

from rpy2.robjects.packages import importr
stargazer = importr('stargazer')

from rpy2.robjects import pandas2ri
pandas2ri.activate()

r = robjects.r

df = pd.DataFrame({'x': [1,2,3,4,5],
                   'y': [2,1,3,5,4]})

fit = r.lm('y~x', data=df)

print fit

print r.stargazer(fit)

However, when I run it, the I get the following output: 但是,当我运行它时,我得到以下输出:

Coefficients:

(Intercept)            x  

        0.6          0.8  



[1] "\n"                                  

[2] "% Error: Unrecognized object type.\n"

So the fit is being generated, and prints fine. 因此正在生成拟合,并且可以正常打印。 But stargazer doesn't seem to recognize the fit object as something it can parse. 但是观星者似乎并没有将fit对象识别为可以解析的对象。

Any suggestions? 有什么建议么? Am I calling stargazer incorrectly in this context? 在这种情况下,我会打错电话吗?

I should mention that I am running this in Python 2.7.5 on a windows 10 machine, with R 3.3.2, and rpy2 version 2.7.8 from the unofficial windows binary. 我应该提到的是,我是在Windows 10机器上以Python 2.7.5在R 3.3.2和来自非官方Windows二进制文件的rpy2版本2.7.8上运行的。 So it could just be a problem with the windows build, but it seems odd that everything except stargazer would work. 因此,Windows构建可能只是一个问题,但是除观星人之外的所有东西都可以正常工作似乎很奇怪。

I am not familiar with the R package stargazer but from a quick look at the documentation this seems to be the correct usage. 我对R软件包stargazer并不熟悉,但是从快速浏览文档来看,这似乎是正确的用法。

Before anything, you may want to check whether the issue is with execution or with printing. 在开始之前,您可能需要检查问题是执行还是打印。 At which one of the two lines is this failing ? 这两条线中哪一条失败?

p = r.stargazer(fit)
print(p)

If the failure is with the execution, you may want to move more code to R and see if you reach a point where you get it to work. 如果失败与执行有关,则您可能需要将更多代码移至R,并查看是否达到使它工作的程度。 If not, this is likely an issue with the R code and/or stargazer. 如果不是,这可能是R代码和/或注视者的问题。 If you get it to work the issue is on the rpy2/conversion side. 如果您可以正常使用,则问题出在rpy2 / conversion端。

rcode = """
df <- data.frame(x = c(1,2,3,4,5),
                 y = c(2,1,3,5,4))

fit <- lm('y~x', data=df)

p <- stargazer(fit)
"""

# parse and evaluate the R code
r(rcode)

# intermediate objects can be retrieved from the `globalenv` to
# investigate where they differ from the ones obtained earlier.
# For example:
print(robjects.globalenv["p"])

Now that we showed that it is likely an issue on the stargazer side, we can make the use of arbitrary data frames a matter of binding it to a symbol in R's globalenv: 既然我们已经证明了在stargazer方面这可能是一个问题,我们可以使使用任意数据帧成为将其绑定到R的globalenv中的符号的问题:

robjects.globalenv["df"] = df
rcode = """    
fit <- lm('y~x', data=df)

p <- stargazer(fit)
"""

# parse and evaluate the R code
r(rcode)

print(robjects.globalenv["p"])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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