如何在 modelsummary 包中同时拥有具有不同 vcov 标准错误的 lm 和 fixst 模型?

How to have both lm and fixest model with different vcov standard errors in modelsummary package?

我想创建一个具有 HC3 校正标准误差的 LM 和一个具有集群鲁棒标准误差的固定模型 table。

在下面查看我的 MRE:

df <- mtcars

models <- list()
models[[1]] <- lm(cyl~disp, data = df)
models[[2]] <- feols(cyl~disp|as.factor(gear), data = df)
library(modelsummary)

# this works
modelsummary::modelsummary(models)

# but these do not
modelsummary::modelsummary(models, vcov = c("HC3", "cluster"))
modelsummary::modelsummary(models, vcov = c(HC3, cluster))
modelsummary::modelsummary(models, vcov = list(HC3, cluster))
modelsummary::modelsummary(models, vcov = list(vcovHC, cluster))
modelsummary::modelsummary(models, vcov = list(vcovHC, vcovHC))
modelsummary::modelsummary(models, vcov = c(vcovHC, vcovHC))

好的——我想出了一个 hack,但仍然悬而未决,以防有人找到更巧妙的解决方案。

df <- mtcars
models <- list()
fit <- lm(cyl~disp, data = df)
models[[1]] <- coeftest(fit, vcovHC(fit, type = "HC3"))
models[[2]] <- summary(feols(cyl~disp|as.factor(gear), data = df), "cluster")
library(modelsummary)

# this works
modelsummary::modelsummary(models)

默认情况下,当您包含固定效应时,fixest 会自动计算 cluster-robust 个标准误差。如果您设置 vcov=NULLmodelsummary 将 return 默认标准错误,然后将是 cluster-robust.

或者,您可以设置 vcov=~gear 以使用引擎盖下的 sandwich::vcovCL 函数计算标准误差。

使用 modelsummary 的 0.10.0 版和 fixest 的 0.10.4 版,我们可以:

library(modelsummary)
library(fixest)

models <- list(
    lm(cyl~disp, data = mtcars),
    feols(cyl ~ disp | gear, data = mtcars),
    feols(cyl ~ disp | gear, data = mtcars))

modelsummary(models,
             fmt = 6,
             vcov = list("HC3", NULL, ~gear))
Model 1 Model 2 Model 3
(Intercept) 3.188568
(0.289130)
disp 0.012998 0.012061 0.012061
(0.001310) (0.002803) (0.002803)
Num.Obs. 32 32 32
R2 0.814 0.819 0.819
R2 Adj. 0.807 0.800 0.800
R2 Within 0.614 0.614
R2 Pseudo
AIC 79.1 80.2 80.2
BIC 83.5 86.1 86.1
Log.Lik. -36.573 -36.107 -36.107
F 98.503
Std.Errors HC3 by: gear by: gear
FE: gear X X

注意第 2 列和第 3 列的结果相同,也等于普通 fixest 摘要:

summary(models[[2]])
#> OLS estimation, Dep. Var.: cyl
#> Observations: 32 
#> Fixed-effects: gear: 3
#> Standard-errors: Clustered (gear) 
#>      Estimate Std. Error t value Pr(>|t|)    
#> disp 0.012061   0.002803 4.30299 0.049993 *  
#> ---
#> Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
#> RMSE: 0.747808     Adj. R2: 0.799623
#>                  Within R2: 0.614334

modelsummary 的某些过去版本中,table 底部的标准错误 标签 存在问题。我将很快开发一个更强大的系统,以确保标签与标准错误相匹配。在大多数情况下,modelsummary 会自行计算稳健标准误差,因此它可以完全控制标记和计算。像 fixestestimatr 这样的包让事情变得有点棘手,因为它们有时包含多个 SE,而且因为默认值并不总是“经典的”。