I have a dataset where I am trying to use multiple imputation with the packages mice
, miceadds
and micemd
for a categorical/factor variable in a multilevel setting. I am able to use the method 2l.2stage.pois
for a continuous variable, which works quite well. The imputation for the categorical variable also works with polyreg
, but this does not make use of the multilevel data.
Here is replicable code that illustrates my problem.
dat <- data.frame(x=rep(c("A", "B"), 50),
y=rep(c(10:14), 20),
r=rep(c(1:50), 2),
z=rep(c(1:4), 25))
dat[40:44, c("x")] <- NA
dat[60:64, c("y")] <- NA
dat[80:84, c("r")] <- NA
predm <- mice::make.predictorMatrix(data=dat)
predm[, c("z")] <- -2
predm[, c("r")] <- 3
predm[row(predm) == col(predm)] <- 0
meth <- c("x"="polyreg", "y"="2l.2stage.pois", "r"="", "z"="")
imputed = mice(dat, method=meth, predictorMatrix=predm, m=1, maxit=1)
imp <- complete(imputed)
table(imp$x, dat$x, useNA = "always")
table(imp$y, dat$y, useNA = "always")
In essence, I am trying to substitute polyreg
with something that should probably start with 2l.
. I have tried 2l.2stage.bin
, but that is for binary variables. Thanks!