1

我需要将变量添加到使用构建的估算数据集中mice(),然后使用as.mids()将它们重新组合成一个mids对象以供以后分析。但是,当我complete()在重建的mids对象上使用时,我发现添加到数据集中的新变量中的许多值都变成了 NA。

library(mice)
d1 = as.data.frame(matrix(rnorm(100), nrow = 10))
missingness = matrix(as.logical(rbinom(100,1,.2)), ncol = 10)
d1[which(missingness, arr.ind = T)] = NA     #replace some values with NA
d.mids = mice(d1, printFlag = F)             #make the imputations
d.long = complete(d.mids, "long", T)         #extract the original dataset and the imputed ones
added = data.frame(rowSums(d.long[,3:12]))   #make a new column
d.long.aug = cbind(d.long,added)             #add it to the data.frame
d.remids = as.mids(d.long.aug)               #turn it back into a mids object
d.relong = complete(d.remids,"long",T)       #extract it from the mids object
sum(is.na(d.long.aug[11:30,13]))             #0, unless a variable failed to impute due to collinearity
sum(is.na(d.relong[11:30,13]))               #should be the same as  previous value, but almost never is

在上面的示例中,我创建了一个新的 long data.frame 并将其应用于as.mdids()它,但是如果我使用cbind将新变量添加到d.long,或者将新变量分配给 ,我会得到相同的结果d.long$added

重新组装对象后,如何确保新变量中的值保留在那里mids

4

0 回答 0