Reputation: 70
I'm using MICE
in R
to perform multiple imputation on multilevel data.
Where only continuous data requires imputation, I have used 2l.lmer with success. However things go wrong when I try to impute categorical data i.e. level 2 data.
(Level 1: repeated measurements (within subjects) or subjects (within classes) Level 2: time-constant/baseline covariates, between subjects effects, variables on the group level)
I am trying to use 2l.pan
in combination with 2lonly.norm
or 2lonly.mean
however I get an error message "missing values in pred not allowed".
I'm sure it's something simple.
Please see below for a reproducible example.
Many thanks.
library("mice")
library("pan")
#Not multilevel to illustrate need
set.seed(100)
patid <- rep(1:3, each = 5)
day <- rep(1:5, times = 3)
crp <- c(68, 78, 93, NA, 143, 5,7,9,13,NA, 97, NA, 56, 52, 34)
sex <- rep(c("M", "F", "M"), each = 5)
sex[3] <- NA
alb <- c(23, NA, 22, 21, 20, 33, 32, 32, NA, 30, 19, 20, NA, 22, 24)
raw_data.df <- data.frame(patid, sex, day, crp, alb)
data_mice.df <- mice(raw_data.df, m = 5, maxit = 5)
complete(data_mice.df)
#pt4's crp not well predicted, and pt1 allocated wrong sex, so try multilevel
##multilevel
pred <- data_mice.df$predictorMatrix
pred[,"patid"] <- -2 #identify class variable
pred[,"day"] <- 0 #don't use time
pred
multilevel_mice <- mice(raw_data.df, method = c("","2lonly.norm","","2l.pan","2l.pan"), predictorMatrix = pred, maxit = 5)```
Error message:
iter imp variable
1 1 sex crpError in pan::pan(y1, subj, pred, xcol, zcol, prior, seed = s1, iter = paniter) :
missing values in pred not allowed
Upvotes: 1
Views: 1574
Reputation: 408
There are two issues at hand:
2lonly.norm()
method is meant for numeric variables and doesn't really handle factor variables that well. The problem here is that sex
is imputed NA
, which causes a problem further down the stream when imputing crp
, which expects a complete set of predictors.sex
does not change within patid
a more appropriate method is 2lonly.mean
. However, until now 2lonly.mean
also wasn't suitable for second-level factors.mice 3.6.4
at https://github.com/stefvanbuuren/mice extends mice.impute.2lonly.mean()
to factors. After installing this version, then you should be able to run
multilevel_mice <- mice(raw_data.df, method = c("","2lonly.mean","","2l.pan","2l.pan"), predictorMatrix = pred, maxit = 5)
Hope this helps.
Upvotes: 1