漏洞? Stargazer 无法正确显示具有 "omit" 选项的固定 effects/factor 标签?
Bug? Stargazer fails to properly display fixed effects/factor labels with "omit" options?
当我在 stargazer
中生成 table 并省略固定效果,然后指定 omit.labels
选项时,stargazer(版本 5.2.2)显示每个 "No"柱子。这是一个例子:
library(stargazer)
# generate example data
set.seed(1)
list_of_states <- rep(c(1, 2, 3, 4, 5), 5)
df <- data.frame("state" = list_of_states, "y" = runif(25),
"x1" = runif(25), "x2" = runif(25))
# OLS without fixed effects
ols.1 <- glm(y ~ x1, data = df)
ols.2 <- glm(y ~ x1 + x2, data = df)
# OLS with fixed effects
fe.1 <- glm(y ~ x1 + factor(state), data = df)
fe.2 <- glm(y ~ x1 + x2 + factor(state), data = df)
stargazer(ols.1, ols.2, fe.1, fe.2,
type = "text",
align = TRUE,
omit = c("state"),
omit.labels = c("State FE"))
这输出
==================================================
Dependent variable:
--------------------------------
y
(1) (2) (3) (4)
--------------------------------------------------
x1 0.088 0.098 0.151 0.157
(0.241) (0.264) (0.263) (0.283)
x2 0.028 0.022
(0.270) (0.287)
Constant 0.485*** 0.467* 0.466** 0.452
(0.142) (0.227) (0.215) (0.280)
--------------------------------------------------
State FE No No No No
--------------------------------------------------
Observations 25 25 25 25
Log Likelihood -5.321 -5.315 -3.528 -3.524
Akaike Inf. Crit. 14.642 16.630 19.056 21.048
==================================================
Note: *p<0.1; **p<0.05; ***p<0.01
"State FE" 行中的标签在最后两列中应显示 "Yes"。这是一个错误吗?
简短的回答是这是一个错误。看来代码的目的是创建一个看起来像
的矩阵
cbind(names(coef(ols.1)), names(coef(fe.1)))
# [,1] [,2]
# [1,] "(Intercept)" "(Intercept)"
# [2,] "x1" "x1"
# [3,] "(Intercept)" "factor(state)2"
# [4,] "x1" "factor(state)3"
# [5,] "(Intercept)" "factor(state)4"
# [6,] "x1" "factor(state)5"
然后检查每一列的 omit
正则表达式。然而,实际发生的事情是
cbind(cbind(NULL, names(coef(ols.1))), names(coef(fe.1)))
# [,1] [,2]
# [1,] "(Intercept)" "(Intercept)"
# [2,] "x1" "x1"
这导致找不到 omit
字词。发生这种情况是因为,从 ?cbind
When the arguments consist of a mix of matrices and vectors the number of columns (rows) of the result is determined by the number of columns (rows) of the matrix arguments. Any vectors have their values recycled or subsetted to achieve this length.
当我在 stargazer
中生成 table 并省略固定效果,然后指定 omit.labels
选项时,stargazer(版本 5.2.2)显示每个 "No"柱子。这是一个例子:
library(stargazer)
# generate example data
set.seed(1)
list_of_states <- rep(c(1, 2, 3, 4, 5), 5)
df <- data.frame("state" = list_of_states, "y" = runif(25),
"x1" = runif(25), "x2" = runif(25))
# OLS without fixed effects
ols.1 <- glm(y ~ x1, data = df)
ols.2 <- glm(y ~ x1 + x2, data = df)
# OLS with fixed effects
fe.1 <- glm(y ~ x1 + factor(state), data = df)
fe.2 <- glm(y ~ x1 + x2 + factor(state), data = df)
stargazer(ols.1, ols.2, fe.1, fe.2,
type = "text",
align = TRUE,
omit = c("state"),
omit.labels = c("State FE"))
这输出
==================================================
Dependent variable:
--------------------------------
y
(1) (2) (3) (4)
--------------------------------------------------
x1 0.088 0.098 0.151 0.157
(0.241) (0.264) (0.263) (0.283)
x2 0.028 0.022
(0.270) (0.287)
Constant 0.485*** 0.467* 0.466** 0.452
(0.142) (0.227) (0.215) (0.280)
--------------------------------------------------
State FE No No No No
--------------------------------------------------
Observations 25 25 25 25
Log Likelihood -5.321 -5.315 -3.528 -3.524
Akaike Inf. Crit. 14.642 16.630 19.056 21.048
==================================================
Note: *p<0.1; **p<0.05; ***p<0.01
"State FE" 行中的标签在最后两列中应显示 "Yes"。这是一个错误吗?
简短的回答是这是一个错误。看来代码的目的是创建一个看起来像
的矩阵cbind(names(coef(ols.1)), names(coef(fe.1)))
# [,1] [,2]
# [1,] "(Intercept)" "(Intercept)"
# [2,] "x1" "x1"
# [3,] "(Intercept)" "factor(state)2"
# [4,] "x1" "factor(state)3"
# [5,] "(Intercept)" "factor(state)4"
# [6,] "x1" "factor(state)5"
然后检查每一列的 omit
正则表达式。然而,实际发生的事情是
cbind(cbind(NULL, names(coef(ols.1))), names(coef(fe.1)))
# [,1] [,2]
# [1,] "(Intercept)" "(Intercept)"
# [2,] "x1" "x1"
这导致找不到 omit
字词。发生这种情况是因为,从 ?cbind
When the arguments consist of a mix of matrices and vectors the number of columns (rows) of the result is determined by the number of columns (rows) of the matrix arguments. Any vectors have their values recycled or subsetted to achieve this length.