R中的鲁棒聚类标准误差和回归权重



如何在 R 中运行同时使用样本权重和稳健聚类标准误差的 OLS 回归?我知道lm会接受weights的论点,但plm——我能找到的集群标准错误包——似乎不接受权重。

现在有一个简单的解决方案,使用 estimatr 包中的lm_robust,您可以从 CRAN install.packages(estimatr) 安装。

> library(estimatr)
> lmro <- lm_robust(mpg ~ hp, data = mtcars, clusters = cyl, weights = wt, se_type = "stata")
> summary(lmro)
Call:
lm_robust(formula = mpg ~ hp, data = mtcars, weights = wt, clusters = cyl, 
    se_type = "stata")
Weighted, Standard error type:  stata 
Coefficients:
            Estimate Std. Error Pr(>|t|) CI Lower CI Upper DF
(Intercept) 28.54865    4.01353  0.01920  11.2798 45.81749  2
hp          -0.06249    0.01908  0.08191  -0.1446  0.01959  2
Multiple R-squared:  0.5851 ,   Adjusted R-squared:  0.5713 
F-statistic: 42.31 on 1 and 30 DF,  p-value: 3.437e-07

您可以在此处查看有关它使用的确切估计器的更多信息。

以下函数计算聚类标准误差,并且因为它依赖于lm也可以合并权重(我检查了一下,它产生的结果与 Stata 相同)。

cl   <- function(dat,fm, cluster){
           require(sandwich, quietly = TRUE)
           require(lmtest, quietly = TRUE)
           M <- length(unique(cluster))
           N <- length(cluster)
           K <- fm$rank
           dfc <- (M/(M-1))*((N-1)/(N-K))
           uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
           vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)
           coeftest(fm, vcovCL) }

最新更新