8

I have a problem with performing statistical analyses of longitudinal data after the imputation of missing values using mice. After the imputation of missings in the wide data-format I convert the extracted data to the longformat. Because of the longitudinal data participants have duplicate rows (3 timepoints) and this causes problems when converting the long-formatted data set into a type mids object. Does anyone know how to create a mids object or something else appropriate after the imputation? I want to use lmer,lme for pooled fixed effects afterwards. I tried a lot of different things, but still cant figure it out.

Thanks in advance and see the code below:

# minimal reproducible example

## Make up some data
set.seed(2)

# ID Variable, Group, 3 Timepoints outcome measure (X1-X3)
Data <- data.frame(
    ID = sort(sample(1:100)),
    GROUP = sample(c(0, 1), 100, replace = TRUE),
    matrix(sample(c(1:5,NA), 300, replace=T), ncol=3)
)

# install.packages("mice")
library(mice)

# Impute the data in wide format
m.out <- mice(Data, maxit = 5, m = 2, seed = 9, pred=quickpred(Data, mincor = 0.0, exclude = c("ID","GROUP"))) # ignore group here for easiness

# mids object?
is.mids(m.out) # TRUE

# Extract imputed data
imp_data <- complete(m.out, action = "long", include = TRUE)[, -2]

# Converting data into long format
# install.packages("reshape")
library(reshape)
imp_long <- melt(imp_data, id=c(".imp","ID","GROUP"))
# sort data
imp_long <- imp_long[order(imp_long$.imp, imp_long$ID, imp_long$GROUP),]
row.names(imp_long)<-NULL

# save as.mids
as.mids(imp_long,.imp=1, .id=2) # doesnt work
as.mids(imp_long) # doesnt work

Best,

Julian

slamballais
  • 3,161
  • 3
  • 18
  • 29
  • I'm not really sure what you're trying to achieve and why you want to re-convert the imputed data into a `mids` object. Usually analysis of imputed data works fine as soon as you get past `complete`. – SimonG Aug 15 '14 at 13:25

3 Answers3

4

I hope I can answer your question with this small example. I don't really see why conversion back to the mids class is necessary. Usually when I use mice I convert the imputed data to a list of completed datasets, then analyse that list using apply.

library(mice)
library(reshape)
library(lme4)

Data <- data.frame(
    ID = sort(sample(1:100)),
    GROUP = sample(c(0, 1), 100, replace = TRUE),
    matrix(sample(c(1:5,NA), 300, replace=T), ncol=3)
)

# impute
m.out <- mice(Data, pred=quickpred(Data, mincor=0, exclude=c("ID","GROUP")))

# complete
imp.data <- as.list(1:5)
for(i in 1:5){
  imp.data[[i]] <- complete(m.out, action=i)
}

# reshape
imp.data <- lapply(imp.data, melt, id=c("ID","GROUP"))

# analyse
imp.fit <- lapply(imp.data, FUN=function(x){
  lmer(value ~ as.numeric(variable)+(1|ID), data=x) 
})
imp.res <- sapply(imp.fit, fixef)

Keep in mind, however, that single-level imputation is not a good idea when you're interested in relationships of variables that vary at different levels. For these tasks you should use procedures that maintain the two-level variation and do not suppress it as mice does in this configuration.

There are workarounds for mice, but for example Mplus and the pan package in R are specifically designed for two-level MI.

SimonG
  • 4,701
  • 3
  • 20
  • 31
  • Dear Simon, thanks a lot for answering my question and for your single-imputation sidenote. I will have a look at the pan package and will figure out if it makes a difference in the effects. You were of great help. – Julian Schulze Aug 17 '14 at 10:07
  • @JulianSchulze If you use `pan`, you can also have a look at the `mitml` package, which automizes much of `lmer`-based analyses with MI. – SimonG May 30 '17 at 12:00
2

No sure how relevant my answer is since you have asked a question long time ago, but in any case... In this slide deck toward the end, on the slide titled "Method POST" the author uses function long2mids():

imp1 <- mice(boys) 
long <- complete(imp1, "long", inc = TRUE)
long$whr <- with(long, wgt / (hgt / 100))
imp2 <- long2mids(long)

However, long2mids() has been deprecated in favor of as.mids() since version 2.22.

slamballais
  • 3,161
  • 3
  • 18
  • 29
Volha
  • 21
  • 1
-1

as.mids() from the miceadds package will work here