2
$\begingroup$

I have a longitudinal data set with missing values. I want to multiply impute (let us say $m$ = 20 times) the missing values in the wide format using the R-package mice. Thereafter, I would like to fit a multilevel model based on the imputed data with the function lmer from the R-package lme4. The function lmer does, however, only seem to be able to fit a model based on the long format.

I therefore extracted the 20 imputed datasets with the function complete from mice and converted each data frame into the long format using the function pivot_longer from the tidyr package. Subsequently, I fitted a multilevel model on each long-format dataset using lmer, resulting in 20 regression outputs. This works.

However, I would eventually want to obtain standard errors of the estimated regression parameters that are based on the within-imputation and between-imputation variance (the whole reason one does want to use multiple imputation). This is usually easily done with the mice function pool. However, pool can only be used on an object of the class mira, which is only obtained if the regression models are directly fitted on a object of the mids class, which again is the object class that is returned by the function mice. Since I converted the multiply imputed data sets, this does not seem to be possible. I found some similar questions on stack overflow, such as these here:

Question 1

Question 2

However, all the questions/answers either do not tell how to obtained pooled standard errors or they deal with imputing the data with a multilevel model (such as mice.impute.2l), which does not work in my case (imputation fails). I simply want to use a single-level model, as outlined here: https://stefvanbuuren.name/fimd/sec-fdd.html

Can anybody give adivce?

$\endgroup$
2
  • $\begingroup$ I'm very happy to have found this question because it was exactly one I was going to ask. Even happier that there is this solution. I just wanted to check though. At what stage do you convert the list to long format? Regarding Erik's code, my DV is imputed in wide format e.g df <- mice(df1, method = meth, predictorMatrix = predM, m=20, maxit = 20) Where df1 contains ID DV_Group1 DV_Group2 DV_Group3 DV_Group4 DV_Group6 IV1 IV2 IV3 IV4 At what point do I convert the df to long format? $\endgroup$ – sunshinecheesesauce Mar 25 at 10:22
  • $\begingroup$ Hi! I extracted the multiply imputed datasets, which I had imputed in the wide format, with the function "complete". Directly after that, I converted the data frame into the long format using the function "pivot_longer". Subsequently, I stacked the 20 long-format data sets (and the original data with missing values) into one big data frame including the column "imp", which indicates the number of the imputed data set (number 0 for the original data frame with missing values). Then I used the "as.mitml.list" function as written by Erik Ruzek. I hope I was able to answer your question $\endgroup$ – Benkyozamurai Apr 3 at 19:24
1
$\begingroup$

If the imputed datasets are in long form (dataset 2 stacked onto dataset 1) then you can use mitml the to do the pooling of the estimates from your model to give you the correct standard errors. See the code below:

library(mitml) 

### Define a list that mitml will link to the multiply imputed data.

implist <-  as.mitml.list(split(df, df$imp)) #imp is the variable that identifies the imputed dataset an observation belongs to
    
### Analyze the imputed datasets and pool the results. 

m_imp <- "DV ~ IV1 + IV2 + IV3 + (1 || Group)"
analysis <- with(implist, lmer(m_imp, REML = F))
estimates <- testEstimates(analysis, var.comp = T, df.com = NULL)
estimates
$\endgroup$
1
  • 1
    $\begingroup$ Thank you very much, it worked! $\endgroup$ – Benkyozamurai Dec 29 '20 at 0:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.