I would like to compute cluster robust standard errors using a mids class object. This arise from multiple imputation of missing values in a column of my original data. A minimal example below.
library(mice)
y <- c(1,0,0,1,1,1,1,0)
x <- c(26, 34, 55, 15, 31 ,47, 97, 12)
z <- c(2, NA, 0, NA, 3 ,7,7, 5)
mydata <- as.data.frame(cbind(y,x,z))
tempData <- mice(mydata,m=5,maxit=5,meth='pmm',seed=500)
class(tempData)
# [1] "mids"
modelFit <- with(tempData,lm(y ~ x + z))
summary(modelFit)
At this point I would like to get the cluster robust standard errors. Unfortunately miceadds::lm.cluster does not allow "mids" class objects.
The function lm.cluster
in miceadds
is intended for regular data frames. An example for an application to multiply imputed data is given in the documentation.
Given below is a version adapted to your question. I used the first variables as a cluster indicator because your example didn't have one.
library(mice)
library(miceadds)
id <- c(1,0,0,1,1,1,1,0)
y <- c(26,34,55,15,31,47,97,12)
x <- c(2,NA,0,NA,3,7,7,5)
dat <- data.frame(id,y,x)
imp <- mice(dat, m=5, maxit=5, method='pmm', seed=500)
implist <- lapply(1:5, function(i) complete(imp,i))
mod <- lapply( implist, function(i){
lm.cluster( i, formula=y~x, cluster=i$id )
})
# extract parameters and covariance matrices
betas <- lapply(mod, coef)
vars <- lapply(mod, vcov)
# pool
summary(pool_mi( qhat=betas, u=vars ))