As.mids Replaces Added NA Values

I need to add variables to the imputed data sets built with mice() , and then use as.mids() to collect them into a mids object for later analysis. However, when I use complete() on the restored mids object, I find that many of the values ​​of the new variable added to the dataset become 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 

In the above example, I created a new long data.frame and applied as.mdids() to it, but I get the same results if I use cbind to add a new variable to d.long , or if I assign a new variable << 29 >.

How can I make sure that the values ​​in the new variable remain there after the mids object is mids ?

+1
source share

Source: https://habr.com/ru/post/1205916/


All Articles