## ----include = FALSE---------------------------------------------------------- knitr::opts_chunk$set(collapse = TRUE, comment = "#>", fig.width = 7, fig.height = 4.5) run_mice <- requireNamespace("mice", quietly = TRUE) ## ----message = FALSE---------------------------------------------------------- library(VIM) set.seed(2026) data(sleep, package = "VIM") truth <- na.omit(sleep[, c("BodyWgt", "BrainWgt", "NonD", "Sleep", "Span", "Gest")]) truth <- as.data.frame(scale(truth)) # common scale keeps the example compact nrow(truth) ## ----------------------------------------------------------------------------- amp <- makeMissing(truth, prop = 0.25, mechanism = "MAR", vars = c("Sleep", "Span"), seed = 1) colSums(is.na(amp)) ## ----------------------------------------------------------------------------- mi <- vimpute(amp, spec = list(.default = vs_ranger(num.trees = 100)), m = 5, sequential = TRUE, nseq = 3, seed = 7, verbose = FALSE) mi ## ----fig.height = 5.5--------------------------------------------------------- plot(mi) # chains: mean/sd of the imputed values per iteration ## ----------------------------------------------------------------------------- plot(mi, "density") # observed (blue, bold) vs per-imputation imputed (red) ## ----eval = run_mice---------------------------------------------------------- fits <- with(mi, lm(Sleep ~ BodyWgt + Span)) pooled <- mice::pool(fits) summary(pooled) ## ----eval = run_mice---------------------------------------------------------- mids <- vim_as_mids(mi) class(mids) ## ----------------------------------------------------------------------------- mi_tuned <- vimpute(amp, spec = list(Sleep = vs_ranger(num.trees = 100, tune = TRUE), .default = vs_ranger(num.trees = 100)), tune_control = vimpute_tune_control(budget = 4, folds = 3), m = 2, sequential = FALSE, seed = 7, verbose = FALSE) tl <- mi_tuned$tuning_log tail(tl, 1)[[1]][c("variable", "tuned", "tuned_better", "n_evals", "folds")] ## ----------------------------------------------------------------------------- ov <- overimpute(amp, "Sleep", spec = list(.default = vs_ranger(num.trees = 100)), draws = 5, folds = 3, sequential = FALSE, seed = 3) ov plot(ov) ## ----------------------------------------------------------------------------- completed <- vim_complete(mi, 1) evaluation(truth, completed, where = attr(amp, "where"))