gpt4 book ai didi

r - 在 Stargazer 的省略参数中包含两个变量的正确方法?

转载 作者:行者123 更新时间:2023-12-04 14:47:22 27 4
gpt4 key购买 nike

我正在尝试使用包含 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

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

最佳答案

我笨拙的解决方案是手动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 添加线条的示例。 .

关于r - 在 Stargazer 的省略参数中包含两个变量的正确方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69790765/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com