Diego - 11 months ago 58

R Question

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

`sandwich`

`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

`vcovHAC`

Answer Source

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)
}
```