【问题标题】:Correct way to include two variables in omit parameter of Stargazer?在 Stargazer 的省略参数中包含两个变量的正确方法?
【发布时间】:2021-12-15 19:41:49
【问题描述】:

我正在尝试使用包含 4 个不同 lm 模型的 R 包 Stargazer 构建回归表。我的数据包含 x,y 和两个分类变量。根据模型,我要么不包括任何类别变量,要么包括一个或两个类别变量。但是,我不希望分类变量的值显示在回归中,而是将它们作为参数传递给 stargazer 命令中的 omit 参数。但是,对于模型 4,我没有得到预期的是/否输出,说明分类变量是否包含在模型中。

这是一个最低限度的工作示例:

library(stargazer)

set.seed(42)
x <- rnorm(100, mean = 100, sd = 5)
e <- rnorm(100, mean = 0, sd = 10)
y <- x*1.5+e
countries <- sample(c("CAN", "GRC", "PRT", "THA", "NZL"), size=100, replace=T)
birth_cohorts <- sample(c("1980", "1990", "2000", "2010"), size=100, replace=T)

model1 <- lm(y ~ x)
sum1 <- summary(model1)
sum1
model2 <- lm(y ~ x + countries - 1)
sum2 <- summary(model2)
sum2
model3 <- lm(y ~ x + birth_cohorts - 1)
sum3 <- summary(model3)
sum3
model4 <- lm(y ~ x + countries + birth_cohorts - 1)
sum4 <- summary(model4)
sum4

stargazer(model1, model2, model3, model4,
          type = "text",
          omit = c("countries", "birth_cohorts"),
          omit.labels = c("Country-fixed effects", "Cohort-fixed effects"),
          omit.yes.no = c("Yes", "No"))

预期输出:

==========================================================================================================================
                                                              Dependent variable:                                         
                      ----------------------------------------------------------------------------------------------------
                                                                       y                                                  
                               (1)                      (2)                       (3)                       (4)           
--------------------------------------------------------------------------------------------------------------------------
x                            1.554***                1.534***                  1.541***                  1.517***         
                             (0.175)                  (0.173)                   (0.174)                   (0.171)         
                                                                                                                          
Constant                      -6.316                                                                                      
                             (17.585)                                                                                     
                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------
Cohort-fixed effects            No                      No                        Yes                       Yes            
Country-fixed effects           No                      Yes                       No                        Yes           
--------------------------------------------------------------------------------------------------------------------------
Observations                   100                      100                       100                       100           
R2                            0.445                    0.997                     0.997                     0.997          
Adjusted R2                   0.439                    0.996                     0.996                     0.997          
Residual Std. Error      9.083 (df = 98)          8.916 (df = 94)           8.984 (df = 95)           8.764 (df = 91)     
F Statistic           78.590*** (df = 1; 98) 4,692.164*** (df = 6; 94) 5,546.079*** (df = 5; 95) 3,238.705*** (df = 9; 91)
==========================================================================================================================
Note:                                                                                          *p<0.1; **p<0.05; ***p<0.01

我得到的输出:

==========================================================================================================================
                                                              Dependent variable:                                         
                      ----------------------------------------------------------------------------------------------------
                                                                       y                                                  
                               (1)                      (2)                       (3)                       (4)           
--------------------------------------------------------------------------------------------------------------------------
x                            1.554***                1.534***                  1.541***                  1.517***         
                             (0.175)                  (0.173)                   (0.174)                   (0.171)         
                                                                                                                          
Constant                      -6.316                                                                                      
                             (17.585)                                                                                     
                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------
Cohort-fixed effects            No                      No                        Yes                       No           
Country-fixed effects           No                      Yes                       No                        Yes           
--------------------------------------------------------------------------------------------------------------------------
Observations                   100                      100                       100                       100           
R2                            0.445                    0.997                     0.997                     0.997          
Adjusted R2                   0.439                    0.996                     0.996                     0.997          
Residual Std. Error      9.083 (df = 98)          8.916 (df = 94)           8.984 (df = 95)           8.764 (df = 91)     
F Statistic           78.590*** (df = 1; 98) 4,692.164*** (df = 6; 94) 5,546.079*** (df = 5; 95) 3,238.705*** (df = 9; 91)
==========================================================================================================================
Note:                                                                                          *p<0.1; **p<0.05; ***p<0.01

在较早的帖子 (Dummy variables in several regressions using Stargazer in R) 中,有人建议像这样翻转模型

stargazer(model4, model3, model2, model1,
          type = "text",
          omit = c("countries", "birth_cohorts"),
          omit.labels = c("Country-fixed effects", "Cohort-fixed effects"),
          omit.yes.no = c("Yes", "No"))

这确实有效,我得到了正确的是/否值:

==========================================================================================================================
                                                              Dependent variable:                                         
                      ----------------------------------------------------------------------------------------------------
                                                                       y                                                  
                                 (1)                       (2)                       (3)                     (4)          
--------------------------------------------------------------------------------------------------------------------------
x                             1.517***                  1.541***                  1.534***                 1.554***       
                               (0.171)                   (0.174)                   (0.173)                 (0.175)        
                                                                                                                          
Constant                                                                                                    -6.316        
                                                                                                           (17.585)       
                                                                                                                          
--------------------------------------------------------------------------------------------------------------------------
Cohort-fixed effects             Yes                       Yes                       No                       No          
Country-fixed effects            Yes                       No                        Yes                      No          
--------------------------------------------------------------------------------------------------------------------------
Observations                     100                       100                       100                     100          
R2                              0.997                     0.997                     0.997                   0.445         
Adjusted R2                     0.997                     0.996                     0.996                   0.439         
Residual Std. Error        8.764 (df = 91)           8.984 (df = 95)           8.916 (df = 94)         9.083 (df = 98)    
F Statistic           3,238.705*** (df = 9; 91) 5,546.079*** (df = 5; 95) 4,692.164*** (df = 6; 94) 78.590*** (df = 1; 98)
==========================================================================================================================
Note:                                                                                          *p<0.1; **p<0.05; ***p<0.01

但是,我想坚持模型的原始排序。 知道可能是什么问题以及如何在不改变模型顺序的情况下解决它?谢谢。

【问题讨论】:

    标签: r latex stargazer


    【解决方案1】:

    我笨拙的解决方案是手动add.lines。即

    stargazer(model1, model2, model3, model4,
              type = "text",
              omit = c("countries", "birth_cohorts"),
              add.lines = list(c('Country FE','No','No','Yes','Yes'),
                               c('Birth cohort FE', 'No', 'Yes', 'No', 'Yes'))
    )
    
    
    ------------------------------------------------------------------------------------------------------------------------
    Country FE                    No                      No                        Yes                       Yes
    Birth cohort FE               No                      Yes                       No                        Yes
    Observations                 100                      100                       100                       100
    R2                          0.445                    0.997                     0.997                     0.997
    Adjusted R2                 0.439                    0.996                     0.996                     0.997
    Residual Std. Error    9.083 (df = 98)          8.916 (df = 94)           8.984 (df = 95)           8.764 (df = 91)
    F Statistic         78.590*** (df = 1; 98) 4,692.164*** (df = 6; 94) 5,546.079*** (df = 5; 95) 3,238.705*** (df = 9; 91)
    ========================================================================================================================
    Note:                                                                                        *p<0.1; **p<0.05; ***p<0.01
    

    基于this discussion,Stargazer omit.labels 中可能存在错误,而且很遗憾,该软件包已经有一段时间没有更新了。

    如果您正在使用 LaTeX,您还可以查看 starpolishr。在this question的答案中可以找到如何使用starpolishr添加线条的示例。

    【讨论】:

    • 感谢您的建议。那确实有效,是的。我不想这样做,因为我有很多回归表并且不想在任何地方指定它。我现在所做的是将模型按照上面提到的链接 (stackoverflow.com/questions/36022621/…) 中建议的顺序放置,然后我编写了一个简短的 python 脚本来同时将许多表的列反转为正常。如果它对任何人有帮助,我将其发布在我的 github 上(与 stackoverflow 上的名称相同)
    猜你喜欢
    • 1970-01-01
    • 2021-11-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-10
    • 2014-01-23
    • 2017-06-19
    相关资源
    最近更新 更多