2

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.

slamballais
  • 3,161
  • 3
  • 18
  • 29
Caserio
  • 472
  • 1
  • 3
  • 14
  • Possible duplicate of [R: Clustered robust standard errors using miceadds lm.cluster - error with subset and weights](https://stackoverflow.com/questions/43942417/r-clustered-robust-standard-errors-using-miceadds-lm-cluster-error-with-subse) – altabq Jul 05 '19 at 14:44

1 Answers1

2

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 ))
SimonG
  • 4,701
  • 3
  • 20
  • 31