How to output several variables in the same row using stargazer in R

一笑奈何 提交于 2019-12-11 03:35:20

问题


I would like to output the interaction terms from several regressions in the same row and call it "Interaction". So far what I have is that the interaction terms show up in two different rows called "Interaction" (see code below).

This question has already been asked here, but my score isn't high enough yet to upvote it or comment on it: https://stackoverflow.com/questions/28859569/several-coefficients-in-one-line.

library("stargazer")
stargazer(attitude)
stargazer(attitude, summary=FALSE)
# 2 OLS models with Interactions
linear.1 <- lm(rating ~ complaints + privileges + complaints*privileges
           , data=attitude)
linear.2 <- lm(rating ~ complaints + learning + complaints*learning, data=attitude)
stargazer(linear.1, linear.2, title="Regression Results", type="text", 
      covariate.labels=c("Complaints", "Privileges", "Interaction", "Learning", "Interaction"))

Thank you for your help.


回答1:


I think this is not natively supported because it is not a good idea. You're asking to obfuscate the meaning of the numbers in your table, which won't help your reader.

That caveat now stated, you can do this by modifying the contents of the lm objects:

# copy objects just for demonstration
m1 <- linear.1
m2 <- linear.2

# see names of coefficients
names(m1$coefficients)
# [1] "(Intercept)"           "complaints"            "privileges"            "complaints:privileges"
names(m2$coefficients)
# [1] "(Intercept)"         "complaints"          "learning"            "complaints:learning"

# replace names
names(m1$coefficients)[names(m1$coefficients) == "complaints:privileges"] <- "interaction"
names(m2$coefficients)[names(m2$coefficients) == "complaints:learning"] <- "interaction"

The result:

> stargazer(m1, m2, title="Regression Results", type="text")

Regression Results
==========================================================
                                  Dependent variable:     
                              ----------------------------
                                         rating           
                                   (1)            (2)     
----------------------------------------------------------
complaints                       1.114**         0.307    
                                 (0.401)        (0.503)   

privileges                        0.434                   
                                 (0.570)                  

learning                                        -0.171    
                                                (0.570)   

interaction                       -0.007         0.006    
                                 (0.008)        (0.009)   

Constant                          -7.737        31.203    
                                 (27.409)      (31.734)   

----------------------------------------------------------
Observations                        30            30      
R2                                0.692          0.713    
Adjusted R2                       0.657          0.680    
Residual Std. Error (df = 26)     7.134          6.884    
F Statistic (df = 3; 26)        19.478***      21.559***  
==========================================================
Note:                          *p<0.1; **p<0.05; ***p<0.01



回答2:


In case anyone is wondering, I needed this for a different purpose for the felm package. The following code is required for that:

reg ~ felm(....)
rownames(reg$coefficients)[rownames(reg$coefficients)=='oldname']<-'newname'
rownames(reg$beta)[rownames(reg$beta)=='oldname']<-'newname'


来源:https://stackoverflow.com/questions/29463182/how-to-output-several-variables-in-the-same-row-using-stargazer-in-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!