Random Effects in Longitudinal Multilevel Imputation Models Using MICE

This answer is probably a bit late for you, but it may be able to help some people who read this in the future:

How to work with 2l.pan

Below are some details about specifying multilevel imputation models with mice. Because the application is longitudinal, I use the term "persons" to refer to units at Level 2. These are the most relevant arguments for 2l.pan as mentioned in the mice documentation:

type

Vector of length ncol(x) identifying random and class variables. Random effects are identified by a 2. The group variable (only one is allowed) is coded as -2. Random effects also include the fixed effect. If for a covariates X1 group means shall be calculated and included as further fixed effects choose 3. In addition to the effects in 3, specification 4 also includes random effects of X1.

There are 5 different codes you can use in the predictor matrix for variables imputed with 2l.pan. The person identifier is coded as -2 (this is different from 2l.norm). To include predictor variables with fixed or random effects, these variables are coded with 1 or 2, respectively. If coded as 2, the corresponding fixed effect is automatically included.

In addition, 2l.pan offers the codes 3 and 4, which have similar meanings as 1 and 2 but will include an additional fixed effect for the person mean of that variable. This is useful if you're trying to model within- and between-person effects of time-varying predictor variables.

intercept

Logical determining whether the intercept is automatically added.

By default, 2l.pan includes the intercept as both a fixed and a random effect. For this reason, it is not required to include a constant term in the predictor matrix. If one sets intercept=FALSE, this behavior is changed, and the intercept is dropped from the imputation model.

groupcenter.slope

If TRUE, in case of group means (type is 3 or 4) group mean centering for these predictors are conducted before doing imputations. Default is FALSE.

Using this option, it is possible to center predictor variables around the person mean instead of including the predictor variable "as is" (i.e., without centering). This only applies to variables coded as 3 or 4. For predictors coded as 3, this is not very important because the models with and without centering are identical.

However, when predictor variables are coded as 4 (i.e., with a random slope), then centering alters the meaning of the random effect so that the random slope no longer applies to the variable "as is" but to the within-person deviation of that variable.


In your example, you can include a simple random slope for time as follows:

library(mice)
ini <- mice(df, maxit=0)

# predictor matrix (following 'type')
pred <- ini$predictorMatrix
pred["score",] <- c(-2, 1, 2, 0)

# imputation method
meth <- c("", "", "", "2l.pan")

imp <- mice(df, method=meth, pred=pred, maxit=10, m=10)

In this example, coding time as 3 or 4 wouldn't make a lot of sense because the person means of time are identical for all persons. However, if you have time-varying covariates that you want to include as predictor variables in the imputation model, 3 and 4 can be useful.

The additional arguments like intercept and groupcenter.slope can be specified directly in the call to mice(), for example:

imp <- mice(df, ..., groupcenter.slope=TRUE)

Regarding your Questions

So, to answer your questions as stated in the post:

  1. Yes, 2l.pan provides a multilevel (or rather two-level) imputation model. The intercept is included as both a fixed and a random effect by default (can be changed with intercept=FALSE) and need not be specified in the predictor matrix (this is in contrast to 2l.norm).

  2. Yes, you can specify random slopes with 2l.pan. To do that, predictors with random slopes are coded as 2 or 4 in the predictor matrix. If coded as 2, the random slope is included. If coded as 4, the random slope is included as well as an additional fixed effect for the person means of that variable. If coded as 4, the meaning of the random slope may be altered by making use of groupcenter.slope=TRUE (see above).

This article also includes some worked examples for how to work with 2l.pan and other functions for mutlivel imputation: [Link]


The pan library doesn't require an intercept term.

You can dig into the function using

library(pan)
?pan

That said mice uses a wrapper around pan called mice.impute.2l.pan with the mice library loaded you can look at the help for that function. It states: it has a parameters called intercept which is [a] Logical [and] determin[es] whether the intercept is automatically added. It is TRUE by default. This is defined as a random intercept by default. Found this out after browsing the R code for the mice wrapper:

if (intercept) { x <- cbind(1, as.matrix(x)) type <- c(2, type) }

Where the pan function parameter type is a Vector of length ncol(x) identifying random and class variables. The intercept is added by default and defined as a random effect.

They do provide and example like you stated with a 1 for "x" in the prediction matrix for fixed effects.

It also states for 2l.norm, The random intercept is automatically added in mice.impute.2l.norm().

It has a few examples with descriptions. The CRAN documentation for pan might help you.