我需要将变量添加到使用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
对象后,如何确保新变量中的值保持不变?