<!--
%\VignetteIndexEntry{Parallelize 'boot' functions}
%\VignetteAuthor{Henrik Bengtsson}
%\VignetteKeyword{R}
%\VignetteKeyword{package}
%\VignetteKeyword{boot}
%\VignetteKeyword{vignette}
%\VignetteKeyword{futurize}
%\VignetteEngine{futurize::selfonly}
-->

<div class="logos">
<img src="../man/figures/cran-boot-logo.webp" alt="The 'boot' image">
<span>+</span>
<img src="../man/figures/futurize-logo.webp" alt="The 'futurize' hexlogo">
<span>=</span>
<img src="../man/figures/future-logo.webp" alt="The 'future' logo">
</div>

The **futurize** package allows you to easily turn sequential code
into parallel code by piping the sequential code to the `futurize()`
function. Easy!


# TL;DR

```r
library(futurize)
plan(multisession)
library(boot)

ratio <- function(pop, w) sum(w * pop$x) / sum(w * pop$u)
b <- boot(bigcity, statistic = ratio, R = 999, stype = "w") |> futurize()
```


# Introduction

This vignette demonstrates how to use this approach to parallelize **[boot]**
functions such as `boot()`, `censboot()`, and `tsboot()`.

The **[boot]** package is one of the "recommended" R packages, meaning
it is officially endorsed by the R Core Team, well maintained, and
installed by default with R. The package generates bootstrap samples
and provides statistical methods around them. Given the resampling
nature of bootstrapping, the algorithms are excellent candidates for
parallelization.


## Example: Bootstrap sampling

The core function `boot()` produces bootstrap samples of a statistic
applied to data. For example, consider the `bigcity` dataset, which
contains populations of 49 large U.S. cities in 1920 (`u`) and 1930
(`x`):

```r
library(boot)

## Draw 999 bootstrap samples of the population data. For each
## sample, calculate the ratio of mean-1930 over mean-1920 populations
ratio <- function(pop, w) sum(w * pop$x) / sum(w * pop$u)
b <- boot(bigcity, statistic = ratio, R = 999, stype = "w")
```

Here `boot()` evaluates sequentially, but we can easily make it
evaluate in parallel by piping to `futurize()`:

```r
library(futurize)
library(boot)

ratio <- function(pop, w) sum(w * pop$x) / sum(w * pop$u)
b <- boot(bigcity, statistic = ratio, R = 999, stype = "w") |> futurize()
```

This will distribute the 999 bootstrap samples across the available
parallel workers, given that we have set up parallel workers, e.g.

```r
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.

```r
plan(future.mirai::mirai_multisession)
```

and

```r
plan(future.batchtools::batchtools_slurm)
```


## Example: Time series bootstrap

The `tsboot()` function generates bootstrap samples from time series
data. For example, here we fit autoregressive models to bootstrap
replicates of the `lynx` time series:

```r
library(futurize)
plan(multisession)
library(boot)

## Fit AR models to bootstrap replicates of the lynx time series
lynx_fun <- function(tsb) {
    ar_fit <- ar(tsb, order.max = 25)
    c(ar_fit$order, mean(tsb), tsb)
}

lynx_boot <- tsboot(log(lynx), lynx_fun, R = 99, l = 20, sim = "geom") |> futurize()
```


# Supported Functions

The following **boot** functions are supported by `futurize()`:

* `boot()`
* `censboot()`
* `tsboot()`


# Without futurize: Manual PSOCK cluster setup

For comparison, here is what it takes to parallelize `boot()` using
the **parallel** package directly, without **futurize**:

```r
library(boot)
library(parallel)

ratio <- function(pop, w) sum(w * pop$x) / sum(w * pop$u)

## Set up a PSOCK cluster
ncpus <- 4L
cl <- makeCluster(ncpus)

## Run bootstrapping in parallel
b <- boot(bigcity, statistic = ratio, R = 999, stype = "w",
          parallel = "snow", ncpus = ncpus, cl = cl)

## Tear down the cluster
stopCluster(cl)
```

This requires you to manually create and manage the cluster
lifecycle. If you forget to call `stopCluster()`, or if your code
errors out before reaching it, you leak background R processes. You
also have to decide upfront how many CPUs to use and what cluster
type to use. Switching to another parallel backend, e.g. a Slurm
cluster, would require a completely different setup. With
**futurize**, all of this is handled for you - just pipe to
`futurize()` and control the backend with `plan()`.


[boot]: https://cran.r-project.org/package=boot
[other parallel backends]: https://www.futureverse.org/backends.html
