I want to estimate a fixed effect model and use a robust variance-covariance matrix with the HC3 small-sample adjustment.
For the model itself I use following lines of code:
require(plm)
require(sandwich)
require(lmtest)
require(car)
QSFE <- plm(log(SPREAD)~PERIOD, data = na.omit(QSREG), index = c("STOCKS", "TIME"), model = "within")
This works very fine, now to calculate the HC3 robust standard error, I use used the function coeftest
with vcovHC
in it.
coeftest(x = QSFE, vcov = vcovHC(QSFE, type = "HC3", method = "arellano"))
And this does not work. The returned error goes as follows:
Error in 1 - diaghat : non-numeric argument to binary operator
The issue is in vcovHC: when one sets the type to "HC3"
. It uses the function hatvalues()
to calculate "diaghat"
, which does not support plm
objects and returns the error:
Error in UseMethod("hatvalues") :
no applicable method for 'hatvalues' applied to an object of class "c('plm', 'panelmodel')"
Does anyone know, how to use the HC3 (HC2) estimator for plm. I think it should depend on the function hatvalues used in vcov, since HC0/HC1 works fine, because this do not need it.
In the method supplied by plm
for plm objects, there is no function hatvalues
in package plm
, the word "hatvalues" is not even in plm's source code. Be sure to have package plm
loaded when you execute coeftest
. Also, be sure to have the latest version of plm installed from CRAN (currently, version 2.2-3).
If you have package plm
loaded, the code should work. It does with a toy example on my machine. To be sure, you may want to force the use of vcovHC
as supplied by plm
:
Fist, try vcovHC(QSFE, type = "HC3", method = "arellano")
. If that gives the same error, try plm::vcovHC(QSFE, type = "HC3", method = "arellano")
.
Next, please try:
coeftest(QSFE, vcov.=function(x) vcovHC(QSFE, method="arellano", type="HC3"))
Edit:
Using the data set supplied, it is clear that dispatching to vcovHC.plm
works correctly. Package sandwich
is not involved here. The root cause is the memory demand of the function vcovHC.plm
with the argument type
set to "HC3"
(and others). This also explains your comment about the function working for a subset of the data.
Edit2:
Memory demand of vcovHC.plm
's small sample adjustments is significantly lower from plm version 2.4-0 onwards (internal function dhat
optimized) and the error does not happen anymore.
vcovHC(QSFE, type = "HC3", method = "arellano")
Error in 1 - diaghat : non-numeric argument to binary operator
Called from: omega(uhat, diaghat, df, G)
Browse[1]> diaghat
[1] "Error : cannot allocate vector of size 59.7 Gb\n"
attr(,"class")
[1] "try-error"
attr(,"condition")
<simpleError: cannot allocate vector of size 59.7 Gb>