Skip to contents

Resampled predictions are commonly used for:

  1. Analyzing accuracy and stability of models

  2. As inputs to Ensemble methods (refer to the modeltime.ensemble package)

Usage

modeltime_fit_resamples(object, resamples, control = control_resamples())

Arguments

object

A Modeltime Table

resamples

An rset resample object. Used to generate sub-model predictions for the meta-learner. See timetk::time_series_cv() or rsample::vfold_cv() for making resamples.

control

A tune::control_resamples() object to provide control over the resampling process.

Value

A Modeltime Table (mdl_time_tbl) object with a column containing resample results (.resample_results)

Details

The function is a wrapper for tune::fit_resamples() to iteratively train and predict models contained in a Modeltime Table on resample objects. One difference between tune::fit_resamples() and modeltime_fit_resamples() is that predictions are always returned (i.e. control = tune::control_resamples(save_pred = TRUE)). This is needed for ensemble_model_spec().

Resampled Prediction Accuracy

Calculating Accuracy Metrics on models fit to resamples can help to understand the model performance and stability under different forecasting windows. See modeltime_resample_accuracy() for getting resampled prediction accuracy for each model.

Ensembles

Fitting and Predicting Resamples is useful in creating Stacked Ensembles using modeltime.ensemble::ensemble_model_spec(). The sub-model cross-validation predictions are used as the input to the meta-learner model.

Examples

library(tidymodels)
#> ── Attaching packages ────────────────────────────────────── tidymodels 1.3.0 ──
#>  broom        1.0.9      recipes      1.3.1
#>  dials        1.4.1      rsample      1.3.1
#>  dplyr        1.1.4      tibble       3.3.0
#>  ggplot2      3.5.2      tidyr        1.3.1
#>  infer        1.0.9      tune         2.0.0
#>  modeldata    1.5.1      workflows    1.3.0
#>  parsnip      1.3.3      workflowsets 1.1.1
#>  purrr        1.1.0      yardstick    1.3.2
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#>  purrr::discard() masks scales::discard()
#>  dplyr::filter()  masks stats::filter()
#>  dplyr::lag()     masks stats::lag()
#>  recipes::step()  masks stats::step()
library(modeltime)
library(timetk)
library(magrittr)
#> 
#> Attaching package: ‘magrittr’
#> The following object is masked from ‘package:tidyr’:
#> 
#>     extract
#> The following object is masked from ‘package:purrr’:
#> 
#>     set_names

# Make resamples
resamples_tscv <- training(m750_splits) %>%
    time_series_cv(
        assess      = "2 years",
        initial     = "5 years",
        skip        = "2 years",
        # Normally we do more than one slice, but this speeds up the example
        slice_limit = 1
    )
#> Using date_var: date

# \donttest{
# Fit and generate resample predictions
m750_models_resample <- m750_models %>%
    modeltime_fit_resamples(
        resamples = resamples_tscv,
        control   = control_resamples(verbose = TRUE)
    )
#> ── Fitting Resamples ────────────────────────────────────────────
#> 
#> • Model ID: 1 ARIMA(0,1,1)(0,1,1)[12]
#> i Slice1: preprocessor 1/1
#>A | warning: `keep_original_cols` was added to `step_dummy()` after this recipe was created.
#>                 Regenerate your recipe to avoid this warning.
#> i Slice1: preprocessor 1/1, model 1/1
#> frequency = 12 observations per 1 year
#> i Slice1: preprocessor 1/1, model 1/1 (predictions)
#> • Model ID: 2 PROPHET
#> i Slice1: preprocessor 1/1
#>A | warning: `keep_original_cols` was added to `step_dummy()` after this recipe was created.
#>                 Regenerate your recipe to avoid this warning.
#> i Slice1: preprocessor 1/1, model 1/1
#> Disabling weekly seasonality. Run prophet with weekly.seasonality=TRUE to override this.
#> Disabling daily seasonality. Run prophet with daily.seasonality=TRUE to override this.
#> i Slice1: preprocessor 1/1, model 1/1 (predictions)
#> • Model ID: 3 GLMNET
#> i Slice1: preprocessor 1/1
#>A | warning: `keep_original_cols` was added to `step_dummy()` after this recipe was created.
#>                 Regenerate your recipe to avoid this warning.
#> i Slice1: preprocessor 1/1, model 1/1
#> i Slice1: preprocessor 1/1, model 1/1 (predictions)
#> Warning: There were 9 warnings in `dplyr::mutate()`.
#> The first warning was:
#>  In argument: `.resample_results = purrr::pmap(...)`.
#> Caused by warning:
#> ! `keep_original_cols` was added to `step_dummy()` after this recipe was created.
#>  Regenerate your recipe to avoid this warning.
#>  Run `dplyr::last_dplyr_warnings()` to see the 8 remaining warnings.
#> 6.963 sec elapsed
#> 

# A new data frame is created from the Modeltime Table
#  with a column labeled, '.resample_results'
m750_models_resample
#> # Modeltime Table
#> # A tibble: 3 × 4
#>   .model_id .model     .model_desc             .resample_results
#>       <int> <list>     <chr>                   <list>           
#> 1         1 <workflow> ARIMA(0,1,1)(0,1,1)[12] <tibble [1 × 4]> 
#> 2         2 <workflow> PROPHET                 <tibble [1 × 4]> 
#> 3         3 <workflow> GLMNET                  <tibble [1 × 4]> 
# }