在使用小鼠估算缺失值后,我在对纵向数据进行统计分析时遇到了问题。在对宽数据格式的缺失进行插补后,我将提取的数据转换为长格式。由于纵向数据参与者有重复的行(3 个时间点),这在将长格式数据集转换为类型 mids 对象时会导致问题。有谁知道如何在插补后创建一个 mids 对象或其他合适的东西?之后我想使用 lmer,lme 来合并固定效果。我尝试了很多不同的东西,但仍然无法弄清楚。
在此先感谢并查看以下代码:
# 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
最好的,
朱利安