![](/img/trans.png)
[英]R: Regressions with group fixed effects and clustered standard errors with imputed dataset
[英]Clustered standard errors in R using plm (with fixed effects)
我試圖在R的plm
包中運行帶有固定效果和model = 'within'
的回歸,同時具有聚類標准錯誤。 使用plm
的Cigar
數據集,我正在運行:
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使用有限樣本校正來減少由於有限數量的簇而導致的誤差的向下偏差 。 它是方差 - 協方差矩陣的乘法因子,$ 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和t-stat為-5.70一致。
這段代碼可能並不理想,因為數據中的狀態數可能與回歸中的狀態數不同,但我太懶了,無法弄清楚如何獲得正確數量的面板。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.