# Makes some groups in mtcars
mtcars$clust <- letters[1:4]
# Matrices for regression
x <- model.matrix(~ cyl + disp, mtcars)
y <- matrix(mtcars$mpg)
# Regression coefficients
b <- solve(crossprod(x), crossprod(x, y))
# Residuals
r <- y - x %*% b
# Robust variance matrix
vcov <- rs_var(r, x, ids = mtcars$clust)
if (FALSE) {
# Same as plm
library(plm)
mdl <- plm(mpg ~ cyl + disp, mtcars, model = "pooling", index = "clust")
vcov2 <- vcovHC(mdl, type = "sss", cluster = "group")
vcov - vcov2
}
Run the code above in your browser using DataLab