我试图在具有固定效果和plm
的R的model = 'within'
程序包中运行回归,同时使标准错误聚类。使用Cigar
中的plm
数据集,我正在运行:
require(plm)
require(lmtest)
data(Cigar)
model <- plm(price ~ sales + factor(state), model = 'within', data = Cigar)
coeftest(model, vcovHC(model, type = 'HC0', cluster = 'group'))
Estimate Std. Error t value Pr(>|t|)
sales -1.21956 0.21136 -5.7701 9.84e-09
这与我使用Stata(将Cigar文件写为.dta)所获得的(略)不同:
use cigar
xtset state year
xtreg price sales, fe vce(cluster state)
price Coef. Std. Err. t P>t [95% Conf. Interval]
sales -1.219563 .2137726 -5.70 0.000 -1.650124 -.7890033
即,标准误差和T统计量不同。我尝试使用不同的“类型”重新运行R代码,但没有一个能提供与Stata相同的结果。我想念什么吗?
最佳答案
由于群集数量有限,Stata使用a finite sample correction来减少downwards bias的错误。它是方差-协方差矩阵$ c = \ frac {G} {G-1} \ cdot \ frac {N-1} {NK} $的乘法因子,其中G是组数,N是观察数,K是参数数。我认为coeftest
仅使用$ c'= \ frac {N-1} {N-K} $,因为如果我按C中第一项的平方来缩放R的标准误差,我会得到与Stata的标准误差非常接近的信息:
display 0.21136*(46/(46-1))^(.5)
.21369554
这是我将复制Stata在R中所做的操作的方法:
require(plm)
require(lmtest)
data(Cigar)
model <- plm(price ~ sales, model = 'within', data = Cigar)
G <- length(unique(Cigar$state))
c <- G/(G - 1)
coeftest(model,c * vcovHC(model, type = "HC1", cluster = "group"))
这样产生:
t test of coefficients:
Estimate Std. Error t value Pr(>|t|)
sales -1.219563 0.213773 -5.70496 1.4319e-08 ***
---
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1
这与Stata的误差0.2137726和t-stat的-5.70一致。
这段代码可能并不理想,因为数据中的状态数可能不同于回归中的状态数,但是我太懒了,无法弄清楚如何获得正确的面板数。
关于r - 使用plm在R中聚集标准错误(具有固定效果),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33155638/