Clustered standard errors in R using plm (with fixed effects)
Asked Answered
R

2

8

I'm trying to run a regression in R's plm package with fixed effects and model = 'within', while having clustered standard errors. Using the Cigar dataset from plm, I'm running:

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

This is (slightly) different than what I'd get by using Stata (having written the Cigar file as a .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

Namely, the standard error and T statistic are different. I've tried rerunning the R code with different "types", but none give the same result as Stata. Am I missing something?

Ralphralston answered 15/10, 2015 at 18:13 Comment(1)
If you are looking for clustered standard errors in three way fixed effects, check my relevant answer.Sennit
M
9

Stata uses a finite sample correction to reduce downwards bias in the errors due to the finite number of clusters. It is a multiplicative factor on the variance-covariance matrix, $c=\frac{G}{G-1} \cdot \frac{N-1}{N-K}$, where G is the number of groups, N is the number of observations, and K is the number of parameters. I think coeftest only uses $c'=\frac{N-1}{N-K}$ since if I scale R's standard error by the square of the first term in c, I get something pretty close to Stata's standard error:

display 0.21136*(46/(46-1))^(.5)
.21369554

Here's how I would replicate what Stata is doing in 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"))

This yields:

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 

which agrees with Stata's error of 0.2137726 and t-stat of -5.70.

This code is probably not ideal, since the number of states in the data may be different than the number of states in the regression, but I am too lazy to figure out how to get the right number of panels.

Monto answered 15/10, 2015 at 23:41 Comment(2)
@Ralphralston Please select this as the answer now that it is fixed.Monto
related: #27368474Brigadier
G
9

Stata uses a specific small-sample correction that has been implemented in plm 1.5.

Try this:

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

Which will yield:

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

This gives the same SE estimate up to 3 digits:

x <- coeftest(model, function(x) vcovHC(x, type = 'sss'))
x[ , "Std. Error"]
## [1] 0.2136951
Gebler answered 5/3, 2016 at 9:19 Comment(2)
related: #27368474Brigadier
@Brigadier Only partly related. While that question goes indeed into the Stata small-sample correction, it's centered around sandwich::vcovHC rather than plm::vcovHC (this question).Gebler

© 2022 - 2024 — McMap. All rights reserved.