Diego Diego - 3 months ago 15
R Question

Configure function with HAC matrix in R

Here is an example of how to configure a function that estimates robust standard errors with

sandwich
package in R

library("sandwich")
se.robust <- function(model.object) {
model.fit <- vcovHC(model.object, type = "HC")
out <- sqrt(diag(model.fit))
return(out)
}


In order to deal with heteroscedasticity and AR(1) process it would be necessary to use the HAC matrix,
vcovHAC
.

How can I configure this function to work with the HAC matrix? Specifically, what should I put in the out line to make the
vcovHAC
configuration work properly? Since now I'm dealing with a full matrix and not only the diagonal.

Answer

You still want diagonal elements of the variance covariance matrix, because they are variance. So as long as you have something like vcovHAC(model.object), it is fine:

library("sandwich")
se.robust <- function(model.object) {
  model.fit <- vcovHAC(model.object)
  out <- sqrt(diag(model.fit))
  return(out)
  }
Comments