Skip to contents

Introduction

Multiple imputation with mice() creates m imputed data sets by running m chains of the Gibbs sampler. Chain i only uses its own imputations from the previous iteration, so the chains are independent and can run at the same time on different cores or machines.

mice() can do this itself with the argument future = TRUE. This builds the parallel run into mice(), instead of the wrapper functions parlmice() (deprecated) and futuremice() (Volker and Vink 2022). The chains run as futures of the future framework (Bengtsson 2021), so the same code runs on a laptop, a workstation or a cluster. You choose where the chains run by setting a future plan; mice() itself never changes it.

This vignette shows how to use future = TRUE, what stays the same, and what to watch out for. The companion vignette Parallel imputation: benchmarks shows when running in parallel pays off, and how mice(future = TRUE) compares to futuremice().

Parallel imputation needs the future and future.apply packages:

install.packages(c("future", "future.apply"))

Basic use

Set a plan with workers, and add future = TRUE to the call:

plan(multisession, workers = 2)
imp <- mice(nhanes, m = 5, seed = 123, future = TRUE)
#> 
#>  Imputed 5 chains with future (multisession) on 2 workers, 5 iterations (0.31 secs)
imp
#> Class: mids
#> Number of multiple imputations:  5 
#> Imputation methods:
#>   age   bmi   hyp   chl 
#>    "" "pmm" "pmm" "pmm" 
#> PredictorMatrix:
#>     age bmi hyp chl
#> age   0   1   1   1
#> bmi   1   0   1   1
#> hyp   1   1   0   1
#> chl   1   1   1   0

mice() first sets up the imputation model and the starting imputations, exactly as for future = FALSE. It then sends chain i, with all its maxit iterations, to a worker, and combines the results into one mids object. With print = TRUE (the default), mice() reports the plan, the number of workers that ran the chains, and the total time, rather than the iteration history. The same information is stored in the result:

imp$future[c("plan", "workers")]
#> $plan
#> [1] "multisession"
#> 
#> $workers
#> [1] 2

mice() never uses more workers than there are chains, so m = 5 keeps at most five workers busy. The workers come on top of your own R session, the main process. The main process sets up the imputation model and the starting imputations, sends the chains to the workers, and collects and combines the results. While the chains run, it mostly waits. A common choice is therefore to leave one core free for it, e.g. plan(multisession, workers = parallelly::availableCores(omit = 1)).

The plan is yours

mice() uses the current plan as is. It does not start workers itself, and it does not change or close the plan. This has three consequences.

  • Workers are reused. Starting a worker takes time, mainly because each worker has to load mice and its dependencies. On a recent laptop this costs about a second, which is more than the whole imputation of a small data set. The first call with future = TRUE loads mice on the workers, and later calls reuse them:

    imp1 <- mice(nhanes, m = 5, seed = 1, future = TRUE, print = FALSE)
    imp2 <- mice(nhanes2, m = 5, seed = 1, future = TRUE, print = FALSE)
  • sequential is respected. Under the default plan, sequential, future = TRUE runs the chains one after the other in your own session. The result is the same as on parallel workers, which is useful for debugging. The printed summary then reports on 1 worker, a reminder that no plan with workers was set.

  • You choose the backend. Any future backend works, also those that mice does not know about. Some examples:

    # forked processes, Linux and macOS outside RStudio only
    plan(multicore, workers = 8)
    
    # background R sessions based on mirai, with the future.mirai package
    plan(future.mirai::mirai_multisession, workers = 8)
    
    # R sessions on other machines
    plan(cluster, workers = c("node1", "node2", "node3"))
    
    # jobs on an HPC scheduler, with the future.batchtools package
    plan(future.batchtools::batchtools_slurm)

See vignette("future-1-overview", package = "future") for all options. With future = FALSE (the default), mice() ignores the plan and uses the classic sequential sampler, which performs iteration 1 for all chains, then iteration 2 for all chains, and so on. With print = TRUE (the default), it prints the iteration history while it runs.

Reproducibility

Each chain gets its own random number stream, derived from seed (future.seed = TRUE in future.apply). As a result, the imputations are reproducible with future = TRUE, and they do not depend on the plan or the number of workers:

imp2 <- mice(nhanes, m = 5, seed = 123, future = TRUE, print = FALSE)
plan(sequential)
imp1 <- mice(nhanes, m = 5, seed = 123, future = TRUE, print = FALSE)
plan(multisession, workers = 2)
identical(complete(imp1, "long"), complete(imp2, "long"))
#> [1] TRUE

The imputations do differ from those of future = FALSE with the same seed. The classic sampler takes the random numbers for all chains from one and the same stream, in the order in which it visits them: iteration 1 of chain 1, iteration 1 of chain 2, and so on. With future = TRUE, each chain draws from its own stream:

imps <- mice(nhanes, m = 5, seed = 123, print = FALSE)
identical(complete(imps, "long"), complete(imp2, "long"))
#> [1] FALSE

Both are valid multiple imputations. If you need to reproduce an earlier analysis, use the same setting of future as before.

The result is an ordinary mids object

The object returned by mice(future = TRUE) has the same structure as that of the sequential sampler. All functions that work on a mids object work as before. Convergence plots use chainMean and chainVar, which hold one trace per chain. We remove bmi from the boys data, because it is a deterministic function of hgt and wgt, which makes the chains mix slowly:

boys2 <- boys[, names(boys) != "bmi"]
imp <- mice(boys2, m = 5, maxit = 10, seed = 1, future = TRUE,
            print = FALSE)
plot(imp, c("hgt", "wgt", "hc"))

The events that the chains log, such as removed predictors, are collected in loggedEvents in the order of the sequential sampler. The usual analysis and pooling steps are unchanged:

fit <- with(imp, lm(wgt ~ age + hgt))
summary(pool(fit))
#>          term   estimate  std.error statistic       df      p.value
#> 1 (Intercept) -9.2307298 2.05918173 -4.482717 732.8193 8.550317e-06
#> 2         age  2.3410638 0.19104065 12.254271 718.4590 1.738862e-31
#> 3         hgt  0.1901132 0.02827777  6.723060 728.7687 3.593962e-11

Adding iterations

mice.mids() adds iterations to an existing mids object. An object created with future = TRUE continues as futures, and the result is the same as that of a single call with all iterations:

imp5 <- mice(nhanes, m = 5, maxit = 5, seed = 1, future = TRUE,
             print = FALSE)
imp10 <- mice.mids(imp5, maxit = 5, print = FALSE)
imp10$iteration
#> [1] 10

full <- mice(nhanes, m = 5, maxit = 10, seed = 1, future = TRUE,
             print = FALSE)
identical(complete(imp10, "long"), complete(full, "long"))
#> [1] TRUE

This works because the mids object stores how it was created (imp$future), and the state of the random number stream of each chain after the last iteration (imp$chainSeed).

You can also switch. future = TRUE continues a sequential object as futures, for example to add many iterations to a model that you first checked with a short sequential run. future = FALSE continues a future object with the classic sampler, for example to see the iteration history or to debug an imputation method:

imp_seq <- mice.mids(imp5, maxit = 1, future = FALSE)
#> 
#>  iter imp variable
#>   6   1  bmi  hyp  chl
#>   6   2  bmi  hyp  chl
#>   6   3  bmi  hyp  chl
#>   6   4  bmi  hyp  chl
#>   6   5  bmi  hyp  chl

After a switch, the results are reproducible, but they differ from those of a single call, because the random numbers come from other streams.

Following the progress

For long runs, mice() can report its progress through the progressr package. It signals one step after each iteration of each chain, so m * maxit steps in total. With future = TRUE, the workers relay these steps while the chains run. Nothing is shown unless you enable a progress handler:

library(progressr)
handlers("cli")

with_progress(
  imp <- mice(boys2, m = 20, maxit = 20, future = TRUE, print = FALSE)
)

To show the progress of every call in the session, use handlers(global = TRUE) instead of with_progress(). The progress bar disappears when the imputation has finished, so for a fast run you may see nothing at all. Set options(progressr.clear = FALSE) to keep it. Progress reporting does not change the imputations.

User-written imputation methods

An imputation method is a function called mice.impute.<name>(). You can write your own and use it in method. With future = TRUE, mice() sends such functions to the workers, together with the objects from your session that they use, such as helper functions:

shrink <- function(x, f) mean(x) + f * (x - mean(x))

mice.impute.pmmshrink <- function(y, ry, x, ...) {
  shrink(mice.impute.pmm(y, ry, x, ...), f = 0.5)
}

meth <- make.method(nhanes)
meth["bmi"] <- "pmmshrink"
imp <- mice(nhanes, method = meth, seed = 1, future = TRUE,
            print = FALSE)

Imputation methods from other packages, such as those in miceadds, are loaded on the workers automatically, but the package must be installed on every worker.

Post-processing per imputation

The argument post contains R code that is run after each imputation of a variable. The code can use the variable j, the iteration k, the column i of the imputations in imp[[j]], and the imputation number chain. A common use is to keep imputations within a range:

post <- make.post(nhanes)
post["chl"] <- "imp[[j]][, i] <- squeeze(imp[[j]][, i], c(120, 300))"

Use i only to index imp[[j]]. With future = TRUE, each worker holds a single chain, so i is always 1 there. For anything that should differ between the imputations, use chain. For example, in a sensitivity analysis that shifts the imputed values by an amount that differs per imputation:

delta <- c(0, 5, 10, 15, 20)
post["chl"] <- "imp[[j]][, i] <- imp[[j]][, i] + delta[chain]"
imp <- mice(nhanes, post = post, seed = 1, future = TRUE,
            print = FALSE)

With future = FALSE, i and chain are equal, so code that uses chain works in both cases.

When an imputation fails

If an imputation method or a post statement fails, mice() stops with an error that tells where the sampler was. This works in the same way with future = TRUE, where the failing chain stops all chains:

post <- make.post(nhanes)
post["bmi"] <- "if (k == 3 && chain == 4) stop('implausible values')"
imp <- mice(nhanes, post = post, seed = 1, future = TRUE,
            print = FALSE)
#> Caught mice_sampler_error. Canceling all iterations ...
#> Error:
#> ! mice() stopped at iteration 3, imputation 4, variable 'bmi' (post-processing):
#>   implausible values

The error is a condition of class mice_sampler_error. Its fields contain the details, and the field parent holds the original error:

e <- tryCatch(
  mice(nhanes, post = post, seed = 1, future = TRUE,
       print = FALSE),
  error = identity
)
#> Caught mice_sampler_error. Canceling all iterations ...
e[c("iteration", "imputation", "variable", "method", "post")]
#> $iteration
#> [1] 3
#> 
#> $imputation
#> [1] 4
#> 
#> $variable
#> [1] "bmi"
#> 
#> $method
#> [1] "pmm"
#> 
#> $post
#> [1] TRUE
e$parent
#> <simpleError in eval(parse(text = cmd)): implausible values>

Things to watch out for

  • The workers are separate R sessions. They do not see the objects, packages and options of your session, except for what mice() sends them. Pass settings for an imputation method as arguments, for example through ... or blots, rather than through options().

  • Workers load the installed mice. If you develop mice and load it with devtools::load_all(), the workers still use the version in your library. Install the package to test changes in parallel.

  • Workers run your startup files. Each new worker reads ~/.Rprofile. Code that is only meant for interactive use, such as loading development tools, slows down every worker and may produce warnings. Wrap such code in if (interactive()) { ... }.

  • Avoid parallelism within parallelism. Some imputation methods use several threads themselves. Running such methods on many workers at once can oversubscribe the cores. future sets parallelly::availableCores() to 1 on its workers, and method = "rf" caps the threads of ranger and literanger at this value, so each chain uses one thread. This does not change the imputations. To use more threads per chain, set them yourself, e.g. mice(..., method = "rf", num.threads = 2) for ranger or n_thread = 2 for literanger. Other methods, and a multithreaded BLAS library, are not limited automatically; use fewer workers for these.

  • Memory. Each worker receives its own copy of the data and of the imputations of its chain. For large data, the memory use grows with the number of workers.

  • Number of cores. parallelly::availableCores() respects the limits set by schedulers such as Slurm and by containers, and is a safer choice than parallel::detectCores(). Use omit = 1 to keep a core free for the main process.

Close the workers when you are done:

plan(sequential)

References

Bengtsson, Henrik. 2021. “A Unifying Framework for Parallel and Distributed Processing in R Using Futures.” The R Journal 13 (2): 208–27. https://doi.org/10.32614/RJ-2021-048.
Volker, Thom Benjamin, and Gerko Vink. 2022. futuremice: The Future Starts Today. https://www.gerkovink.com/miceVignettes/futuremice/Vignette_futuremice.html.