Skip to contents

Generate a prediction interval from arbitrary model types using bootstrap resampling. predict_boots() generates n bootstrap resamples, fits a model to each resample (creating n models), then creates n predictions for each observation in new_data.

Usage

predict_boots(
  workflow,
  n = 2000,
  training_data,
  new_data,
  interval = c("prediction", "confidence"),
  verbose = FALSE,
  ...
)

Arguments

workflow

An un-fitted workflow object.

n

An integer for the number of bootstrap resampled models that will be created.

training_data

A tibble or dataframe of data to be resampled and used for training.

new_data

A tibble or dataframe used to make predictions.

interval

One of prediction, confidence. Specifies the interval type to be generated.

verbose

A logical. Defaults to FALSE. If set to TRUE, prints progress of training to console.

...

Additional params passed to rsample::bootstraps().

Value

A tibble with a column indicating the row index of each observation in new_data and a nested list of the model predictions for each observation.

Details

Since predict_boots() fits a new model to each resample, the argument workflow must not yet be fit. Any tuned hyperparameters must be finalized prior to calling predict_boots().

Examples

if (FALSE) {
library(tidymodels)

# setup a workflow without fitting
wf <-
  workflow() %>%
  add_recipe(recipe(qsec ~ wt, data = mtcars)) %>%
  add_model(linear_reg())

# fit and predict 2000 bootstrap resampled models to mtcars
set.seed(123)
wf %>%
  predict_boots(n = 2000, training_data = mtcars, new_data = mtcars)
}