0

I'm learning how to use rpy2, and I would like to create formatted regression output using the stargazer package. 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.

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. So it could just be a problem with the windows build, but it seems odd that everything except stargazer would work.

bhackinen
  • 123
  • 1
  • 5

1 Answers1

1

I am not familiar with the R package stargazer but from a quick look at the documentation this seems to be the correct usage.

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. If not, this is likely an issue with the R code and/or stargazer. If you get it to work the issue is on the rpy2/conversion side.

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:

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"])
lgautier
  • 11,363
  • 29
  • 42
  • Regarding the first question, I only see the error when the second command is run. But I'm not sure how to interpret that. If stargazer prints an error to the console, and that is stored in p as a string, wouldn't that generate the same result? I have also run stargazer in r without any problems using the equivalent syntax. – bhackinen Nov 27 '16 at 20:16
  • What seems to be happening is that the call to `stargazer()` completes without an error... but the result vector of strings is saying that there is an error. A bit unusual, but I am not familiar with the package "stargazer". In any case this means that there is something in out object `fit` that it does not like but it won't tell us what it is. There were typos in my second snippet of code; it should be running now that I fixed them. I think that stargazer is not handling fit objects using anonymous data frames. That's something to see with the maintainer of the package. – lgautier Nov 28 '16 at 14:00