如何在R中运行OLS回归,该回归使用样本权重和强大的聚类标准错误?我知道lm
会接受weights
参数,但plm
- 我能找到的聚簇标准错误包 - 似乎不接受权重。
答案 0 :(得分:0)
以下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) }
答案 1 :(得分:0)
There is now a simple solution using lm_robust
from the estimatr
package, which you can install from 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
You can see more about what exact estimator it is using here.