R: Multiple Imputation with lme4, mice, and miceadds packages

Viewed 423

I have correlated data, and I am having trouble imputing the data using a random intercept model (from lme4). The miceadds package seems to have this functionality. But I cannot get the imputations to work properly. I've prepared the example below, and any guidance would be much appreciated!

The dataset is one line per school per student. There are 10 schools and 1600 students. There are two variables I would like to impute: reading score, and math score. As the students within a school are correlated, I would like to impute with a regression model including a random intercept for school, e.g. lme4::lmer(reading ~ math + (1|school_id), data = dat).

I've included the code I have attempted below. The help file for the lme4 imputation from the miceadds package is here: https://alexanderrobitzsch.github.io/miceadds/reference/mice.impute.ml.lmer.html

Thank you!

library(miceadds)
library(dplyr)

data(data.ma07, package = "miceadds")
dat <-
  data.ma07 %>%
  select(
    school_id = id2,
    student_id = id1,
    reading = y1,
    math = y2
  ) %>%
  as_tibble()


#----- specify levels of variables
variables_levels <- miceadds:::mice_imputation_create_type_vector(colnames(dat), value = "")
variables_levels[c("reading", "math")] <- "school_id"

#----- specify predictor matrix
predmat <- mice::make.predictorMatrix(data = dat)
predmat[, c("school_id", "student_id")] <- 0


#----- specify imputation methods
method <- mice::make.method(data = dat)
method[c("reading")] <- "ml.lmer"
method[c("math")] <- "ml.lmer"


#----- specify hierarchical structure of imputation models
levels_id <- list()
#** hierarchical structure for variables reading and math
levels_id[["reading"]] <- c("school_id")
levels_id[["math"]] <- c("school_id")

#----- assume only random intercepts
random_slopes <- NULL

#----- imputation in mice (this does not run!)
imp1 <- mice::mice(dat,
  maxit = 10, m = 5, method = method,
  predictorMatrix = predmat, levels_id = levels_id, random_slopes = random_slopes,
  variables_levels = variables_levels
)
0 Answers
Related