Parallelize 'fwb' functions
Henrik Bengtsson
Source:vignettes/futurize-81-fwb.md
futurize-81-fwb.Rmd
+
=

The futurize package allows you to easily turn
sequential code into parallel code by piping the sequential code to the
futurize() function. Easy!
Introduction
This vignette demonstrates how to use this approach to parallelize
fwb functions
such as fwb() and vcovFWB().
The fwb package implements the fractional weighted bootstrap (also known as the Bayesian bootstrap). Rather than resampling units to include in bootstrap samples, random weights are drawn and applied to a weighted estimator. Given the resampling nature of bootstrapping, the algorithm is an excellent candidate for parallelization.
Example: Fractional weighted bootstrap
The fwb() function produces fractional weighted
bootstrap samples of a statistic applied to data. For example, consider
bootstrapping a linear model on the mtcars dataset:
library(fwb)
## Draw 999 bootstrap samples of the regression coefficients
set.seed(123)
b <- fwb(mtcars, statistic = function(data, w) {
fit <- lm(mpg ~ wt + am, data = data, weights = w)
coef(fit)
}, R = 999)Here fwb() evaluates sequentially, but we can easily
make it evaluate in parallel by piping to futurize():
library(fwb)
library(futurize)
set.seed(123)
b <- fwb(mtcars, statistic = function(data, w) {
fit <- lm(mpg ~ wt + am, data = data, weights = w)
coef(fit)
}, R = 999) |> futurize()This will distribute the 999 bootstrap samples across the available parallel workers, given that we have set up parallel workers, e.g.
plan(multisession)The built-in multisession backend parallelizes on your
local computer and works on all operating systems. There are other parallel
backends to choose from, including alternatives to parallelize
locally as well as distributed across remote machines, e.g.
plan(future.mirai::mirai_multisession)and
plan(future.batchtools::batchtools_slurm)Supported Functions
The following fwb functions are supported by
futurize():