post-processing in mice, replace one variable with another

Viewed 202

I'm trying to perform multiple imputation on a dataset in R where I have two variables, one of which needs to be the same or greater than the other one. I have set up the method and the predictive matrix, but I am having trouble understanding how to configure the post-processing. The manual (or main paper - van Buuren and Groothuis-Oudshoorn, 2011) states (section 3.5): "The mice() function has an argument post that takes a vector of strings of R commands. These commands are parsed and evaluated just after the univariate imputation function returns, and thus provide a way to post-process the imputed values." There are a couple of examples, of which the second one seems most useful:

R> post["gen"] <- "imp[[j]][p$data$age[!r[,j]]<5,i] <- levels(boys$gen)[1]"

this suggests to me that I could do:

R> ini <- mice(cbind(boys), max = 0, print = FALSE)
R> post["A"] <- "imp[[j]][p$data$B[!r[,j]]>p$data$A[!r[,j]],i] <- levels(boys$A)[boys$B]"

However, this doesn't work (when I plot A v B, I get random scatter rather than the points being confined to one half of the graph where A >= B).

I have also tried using the ifdo() function, as suggested in another sx post:

post["A"] <- "ifdo(A < B), B"

However, it seems the ifdo() function is not yet implemented. I tried running the code suggested for inspiration but afraid my R programming skills are not that brilliant.

So, in summary, has anyone any advice about how to implement post-processing in mice such that value A >= value B in the final imputed datasets?

1 Answers

Ok, so I've found an answer to my own question - but maybe this isn't the best way to do it.

In FIMD, there is a suggestion to do this kind of thing outside the imputation process, which thus gives:

R> long <- mice::complete(imp, "long", include = TRUE)
R> long$A <- with(long, ifelse(B < A, B, A))

This seems to work, so I'm happy.

Related