2015-10-15 171 views
3

我试图在R的plm包中使用固定效果和model = 'within'运行回归,同时发生了群集标准错误。从plm使用Cigar数据集,我运行:使用plm(带有固定效应)的R中的聚集标准错误

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得到不同(写过雪茄文件作为.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有相同的结果。我错过了什么吗?

回答

5

Stata使用a finite sample correction减少downwards bias由于有限数量的簇造成的错误。它是方差 - 协方差矩阵的乘性因子,其中G是组数,N是观察次数,K是参数的数量。我认为coeftest只使用$ c'= \ frac {N-1} {NK} $,因为如果我用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 

与的0.2137726塔塔的错误和-5.70 T-STAT一致。

此代码可能并不理想,因为数据中的状态数可能与回归中的状态数不同,但我懒得弄清楚如何获得正确数量的面板。

+0

看起来是工作(如果我使用类型HC1)。谢谢! – user5450835

+0

@ user5450835好的!让我编辑帖子。 –

+0

@ user5450835请选择此项作为解决问题的答案。 –

3

Stata使用已在plm 1.5中实施的特定小样本校正。

试试这个:

require(plm) 
require(lmtest) 
data(Cigar) 
model <- plm(price ~ sales + factor(state), model = 'within', data = Cigar) 
coeftest(model, function(x) vcovHC(x, type = 'sss')) 

这将产生:

t test of coefficients: 

     Estimate Std. Error t value Pr(>|t|)  
sales -1.2196  0.2137 -5.707 1.415e-08 *** 
--- 
Signif. codes: 0 ‘***’ 0.001 ‘**’ 0.01 ‘*’ 0.05 ‘.’ 0.1 ‘ ’ 1 

这给出了同样的SE估计高达3个位数:

x <- coeftest(model, function(x) vcovHC(x, type = 'sss')) 
x[ , "Std. Error"] 
## [1] 0.2136951 
+0

相关:http://stackoverflow.com/questions/27367974/different-robust-standard-errors-of-logit-regression-in-stata-and-r/27368468#27368468 – Helix123

+1

@ Helix123只与部分相关。虽然这个问题确实进入了Stata小样本纠正,但它围绕'sandwich :: vcovHC'而不是'plm :: vcovHC'(这个问题)。 – landroni