I am analyzing a dataset in which data is clustered in several groups (towns in regions). The dataset looks like:
R> df <- data.frame(x = rnorm(10),
y = 3*rnorm(x),
groups = factor(sample(c('0','1'), 10, TRUE)))
R> head(df)
x y groups
1 -0.8959 1.54 1
2 -0.1008 -2.73 1
3 0.4406 0.44 0
4 0.0683 1.62 1
5 -0.0037 -0.20 1
6 -0.8966 -2.34 0
I want my lm() estimates to account for intraclass correlation in groups and for that purpose I am using a function cl()
that takes an lm()
and returns the robust clustered covariance matrix (original here):
cl <- function(fm, cluster) {
library(sandwich)
M <- length(unique(cluster))
N <- length(cluster)
K <- fm$rank
dfc <- (M/(M-1))*((N-1)/(N-K-1))
uj <- apply(estfun(fm), 2, function(x) tapply(x, cluster, sum));
vcovCL <- dfc * sandwich(fm, meat = crossprod(uj)/N)
return(vcovCL)
}
Now,
output <- lm(y ~ x, data = df)
clcov <- cl(output, df$groups)
coeftest(output, clcov, nrow(df) - 1)
gives me the estimates I need. The problem now is that I want to use the model for prediction, and I need the standard error of the prediction to be calculated with the new covariance matrix clcov
. That is, I need
predict(output, se.fit = TRUE)
but using clcov
instead of vcov(output)
. Something like a vcov() <-
would be perfect.
Of course, I could write my own function to do predictions, but I am just wondering whether there is a more practical method that allows me to use methods for signature lm
(like arm::sim).