I have a mids object created from mice . I would like to recode some imputed variables and save the mids object. I know that I could convert the mids object to "long" with complete() , but I want to save the mids object, since it has some additional features.
Here is an example of using the nhanes . Running mice() creates 5 imputed data sets for variables in nhanes . I am focused on hyp .
library(mice) names(nhanes) nhanes$hyp
How could I recode the imp hyp values ββinside the mids imp object (e.g. 1 became 5).
So far, my only ideas have been to convert imp to long, extract the variables of interest into a new data frame, transcode, convert the new data frame through as.mids , and then return to imp via cbind.mids() .
imp_long <- complete(imp, "long", include=T) hyp <- imp_long[, "hyp"] hyp2 <- hyp hyp2[hyp2==1] <- 5 hyp4mids <- data.frame(.imp = rep(0:5, each = nrow(nhanes)), .id = rep(1:nrow(nhanes), times = 6), hyp2, TMP = NA) hyp4mids <- as.mids(hyp4mids, .imp = 1, .id = 2) hyp4mids$chainMean <- hyp4mids$chainVar <- array(NA, dim = c(2, 25, 5), dimnames = list( c("hyp2", "TMP"), 1:25, paste0("Chain ", 1:5))) imp2 <- cbind.mids(imp, hyp4mids) imp2$imp$hyp2
It works, but I think I should be able to modify the hyp in the mids imp object directly.