【发布时间】:2015-01-31 03:43:52
【问题描述】:
如何在 R 中运行同时使用样本权重和稳健聚类标准误的 OLS 回归?我知道lm 将接受weights 参数,但plm——我能找到的集群标准错误包——似乎不接受权重。
【问题讨论】:
标签: r regression
如何在 R 中运行同时使用样本权重和稳健聚类标准误的 OLS 回归?我知道lm 将接受weights 参数,但plm——我能找到的集群标准错误包——似乎不接受权重。
【问题讨论】:
标签: r regression
现在有一个使用estimatr package 中的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
您可以查看更多关于它使用的确切估算器here。
【讨论】:
以下function 计算聚类标准误差,因为它依赖于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) }
【讨论】: