I have a problem with performing statistical analysis of longitudinal data after imputing missing values using mice. After imputing the passes into the wide data-format, I convert the extracted data to longformat. Due to the longitudinal, data participants have duplicate rows (3 time points), and this causes problems when converting a long-format data set to an object of type mids. Does anyone know how to create a mids object or something else appropriate after imputation? I want to use lmer, lme for combined fixed effects. I tried a lot of different things, but still can't figure it out.
Thanks in advance and see the code below:
set.seed(2)
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)
)
library(mice)
m.out <- mice(Data, maxit = 5, m = 2, seed = 9, pred=quickpred(Data, mincor = 0.0, exclude = c("ID","GROUP")))
is.mids(m.out)
imp_data <- complete(m.out, action = "long", include = TRUE)[, -2]
library(reshape)
imp_long <- melt(imp_data, id=c(".imp","ID","GROUP"))
imp_long <- imp_long[order(imp_long$.imp, imp_long$ID, imp_long$GROUP),]
row.names(imp_long)<-NULL
as.mids(imp_long,.imp=1, .id=2)
as.mids(imp_long)
Best
Julian