| ifdo | R Documentation |
ifdo() replaces imputations that meet a condition. It is meant to
be used inside the post argument of mice(), where it
acts on the imputations of the current variable in the current iteration.
ifdo(cond, action)
cond |
A condition, or several conditions combined with |
action |
The replacement values, one for each condition, combined
with |
Each condition is evaluated against the data as they are after the
imputation of the current variable, so the current variable holds its new
imputations. Expressions may refer to any variable in the data. For the
rows where condition k is TRUE, the imputations are replaced
by action k. An action can be a constant, or an expression that is
evaluated for those rows, such as Ozone / 2. All conditions are
evaluated before any replacement is made, so a later action does not
change which rows meet a later condition. If a row meets more than one
condition, the last action wins. Rows where a condition is NA are
left unchanged.
ifdo() works wherever mice() applies post, that is,
for variables imputed by a univariate method. It does nothing useful
outside post.
Called for its side effect: it updates the imputations of the
current variable. Returns NULL invisibly.
Stef van Buuren, 2012, 2026
Van Buuren, S. (2018). Flexible Imputation of Missing Data. Second Edition. Chapman & Hall/CRC. Section 6.4.6.
squeeze, make.post
post <- make.post(airquality[, 1:2])
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, print = FALSE
)
range(imp$imp$Ozone)
# same result as
post["Ozone"] <- "imp[[j]][, i] <- squeeze(imp[[j]][, i], c(1, 200))"
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.