Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R中的稳健聚类标准误差和回归权重_R_Regression - Fatal编程技术网

R中的稳健聚类标准误差和回归权重

R中的稳健聚类标准误差和回归权重,r,regression,R,Regression,如何在R中运行OLS回归,同时使用样本权重和稳健的聚集标准误差?我知道lm将接受weights参数,但是plm——我可以找到的聚集标准错误包——似乎不接受权重。以下计算聚集标准错误,因为它依赖于lm,所以也可以包含权重(我进行了检查,结果与Stata相同) cl现在有一个简单的解决方案,使用的lm\u robust,您可以从CRANinstall.packages(estimatr)安装 >库(estimatr) >lmro摘要(lmro) 电话: lm_稳健(公式=mpg~hp,数据=mtca

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

以下计算聚集标准错误,因为它依赖于
lm
,所以也可以包含权重(我进行了检查,结果与Stata相同)


cl现在有一个简单的解决方案,使用的
lm\u robust
,您可以从CRAN
install.packages(estimatr)
安装

>库(estimatr)
>lmro摘要(lmro)
电话:
lm_稳健(公式=mpg~hp,数据=mtcars,重量=wt,聚类=cyl,
se_type=“stata”)
加权标准误差类型:stata
系数:
估计标准误差Pr(>t)CI下CI上DF
(截距)28.54865 4.01353 0.01920 11.2798 45.81749 2
hp-0.06249 0.01908 0.08191-0.1446 0.01959 2
倍数R平方:0.5851,调整后的R平方:0.5713
F-统计量:1和30 DF上的42.31,p-值:3.437e-07
你可以看到更多关于它所使用的精确估计器的信息

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) }
> 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