如何使用R中的stargazer在同一行中输出多个变量

时间:2015-04-05 23:01:26

标签: r latex regression interaction stargazer

我想从同一行中的几个回归中输出交互项,并将其称为“#34;交互”#34;。到目前为止,我所拥有的是交互术语出现在两个不同的行中,称为" Interaction" (见下面的代码)。

此问题已在此处提出,但我的分数还不够高,尚未对其进行评论或评论: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"))

感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

我认为这不是本机支持的,因为它不是一个好主意。您要求对表格中数字的含义进行模糊处理,这无法帮助您的读者。

现在说明了这一点,你可以通过修改lm个对象的内容来实现这个目的:

# 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"

结果:

> 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

答案 1 :(得分:1)

以下回应:

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

似乎适用于大多数情况。

尽管我有时会遇到问题。当felm与IV一起使用时,这是必需的。区分带IV和不带IV的变量虽然很好,但与其他模型相比,这些表会很麻烦!这样的语法将很有帮助。

答案 2 :(得分:0)

万一有人想知道,我出于felm软件包的其他目的而需要它。为此,需要以下代码:

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