Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple Imputation of longitudinal data in MICE and statistical analyses of object type mids

Tags:

r

r-mice

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

like image 568
Julian Schulze Avatar asked Jul 21 '14 19:07

Julian Schulze


Video Answer


1 Answers

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.

like image 131
SimonG Avatar answered Sep 23 '22 10:09

SimonG