mice
mice copied to clipboard
Post-processing: function ifdo() not yet implemented
Dear Stef,
Another problem I encountered today showed itself when I was trying to reproduce some of your code from your book "Flexible Imputation of Missing Data" (fantastic book!).
At page 135-136 you present two code-solutions to the same post-processing setting:
library(mice)
ini <- mice(airquality[, 1:2], maxit = 0)
post <- ini$post
post["Ozone"] <- "imp[[j]][,i] <- squeeze(imp[[j]][,i], c(1,200))"
imp <- mice(airquality[, 1:2], method = "norm.nob", m = 1, maxit = 1, seed = 1, post = post)
Alternative to line 4:
post["Ozone"] <- "ifdo(c(Ozone < 1, Ozone > 200), c(1, 200))"
The former works fine but the latter gives the OzoneFunction ifdo() not yet implemented.
Has the "ifdo" function been withdrawn from the package?
Best regards, Mikkel
Just read through the R-script for the chapter on multiple-imputation.com and noticed that you actually write it isn't yet implemented.
Yes, this is still one on the todo list
Note: I found this code on my machine. I didn't test, and it probably does not work, but can be used for inspiration.
##' Conditional imputation helper
##'
##' Sorry, the \code{ifdo()} function is not yet implemented.
##' @aliases ifdo
##' @param condition a vector of conditions
##' @param action a vector of actions to do when condition is TRUE
##' @return Currently returns an error message.
##' @author Stef van Buuren, 2017
##' @examples
##' \dontrun {
##' # old form
##' ini <- mice(airquality[, 1:2], maxit = 0)
##' post <- ini$post
##' post["Ozone"] <- "imp[[j]][, i] <- squeeze(imp[[j]][, i],c(1, 200))"
##'
##' # ifdo form
##' post["Ozone"] <- "ifdo(c(Ozone < 1, Ozone > 200), c(1, 200))"
##' imp <- mice(airquality[, 1:2], method = "norm.nob", m = 1, maxit = 1, seed = 1, post = post)
##' }
##' @keywords internal
# ifdo <- function(condition, action, data = p$data) {
# if (length(condition) != length(action))
# stop("Different length of `condition` and `action`")
# imputes <- data[where[, j], , drop = FALSE]
# for (k in 1:length(condition)) {
# eval(parse(paste("idx <- with(imputes,", condition[k], ")")))
# if (substring(action[k], 1, 1) == "~") {
# eval(parse(paste("imp[[j]][idx, i] <- model.frame(as.formula(", action[k], "), data = imputes[idx, , drop = FALSE])")))
# } else {
# eval(parse(paste("imp[[j]][idx, i] <- with(imputes[idx, , drop = FALSE], ", action[k])))
# }
# # cat("Function ifdo() not yet implemented.\n")
# }
# }