【发布时间】: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
但是,我想坚持模型的原始排序。 知道可能是什么问题以及如何在不改变模型顺序的情况下解决它?谢谢。
【问题讨论】: