Search code examples
rlinear-regressioncovariance

How to cluster standard errors with small sample corrections in R


I have the following code:

library(lmtest)
library(sandwich)
library(plm)
library(multiwayvcov)

reg <- lm(Y ~ x1 + x1_sq + x2 + x2_sq + x1x2 + d1 + d2 + d3 + d4, df)
coeftest(reg, vcov = vcovHC(reg, type="HC1")
coeftest(reg, vcov = vcovHC(reg, type="sss", cluster="study"))

I want to compare the regression when I use typical heteroskedasticity-robust standard errors and when I cluster the standard errors at the study level with a small sample correction. The regression and first -coeftest- work, but the second spits out a clear error message:

Error in match.arg(type) : 'arg' should be one of “HC3”, “const”, “HC”, “HC0”, “HC1”, “HC2”, “HC4”, “HC4m”, “HC5”

I found the code online where they use -type="sss"- as a small sample correction but it doesn't seem to work here. Is there something I am doing wrong or is one of the above listed in the error message the heteroskedastic-adjusted covariance matrix and the code was maybe updated? Clearly I cannot use -type="sss"-, but I don't know how to incorporate the small sample correction otherwise.


Solution

  • Using -...vcovHC(df, type="sss", cluster="study")- is a dated way to incorporate small sample corrections. Upon understanding differences between the sandwich estimators HC0-HC4, using the code previous to it:

    coeftest(reg, vcov = vcovHC(reg, type="HC1")
    

    is appropriate with the corresponding sandwich estimator in the type argument. The issue was with the dated syntax that followed and this is the correct format.