【问题标题】:Clustered standard errors in R using plm (with fixed effects)使用 plm 的 R 中的聚类标准错误(具有固定效应)
【发布时间】:2016-01-14 07:56:02
【问题描述】:

我正在尝试在 R 的 plm 包中运行具有固定效果和 model = 'within' 的回归,同时具有聚集的标准错误。使用来自plmCigar 数据集,我正在运行:

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 相同的结果。我错过了什么吗?

【问题讨论】:

    标签: r stata plm standard-error


    【解决方案1】:

    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}{NK}$ 因为如果我将 R 的标准误差按 c 中第一项的平方缩放,我得到的结果非常接近 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 和 -5.70 的 t-stat 一致。

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

    【讨论】:

    【解决方案2】:

    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
    

    这给出了最多 3 位数的相同 SE 估计值:

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

    【讨论】:

    • @Helix123 仅部分相关。虽然这个问题确实属于 Stata 小样本校正,但它的中心是 sandwich::vcovHC 而不是 plm::vcovHC(这个问题)。
    猜你喜欢
    • 2021-12-25
    • 2017-08-18
    • 1970-01-01
    • 1970-01-01
    • 2015-04-06
    • 2020-08-11
    • 2017-09-24
    • 2019-03-10
    • 2017-01-26
    相关资源
    最近更新 更多