Skip to contents

Creates an Ensemble Model using Mean/Median Averaging

Usage

ensemble_average(object, type = c("mean", "median"))

Arguments

object

A Modeltime Table

type

Specify the type of average ("mean" or "median")

Value

A mdl_time_ensemble object.

Details

The input to an ensemble_average() model is always a Modeltime Table, which contains the models that you will ensemble.

Averaging Methods

The average method uses an un-weighted average using type of either:

  • "mean": Performs averaging using mean(x, na.rm = TRUE) to aggregate each underlying models forecast at each timestamp

  • "median": Performs averaging using stats::median(x, na.rm = TRUE) to aggregate each underlying models forecast at each timestamp

Examples

# \donttest{
library(tidymodels)
#> ── Attaching packages ────────────────────────────────────── tidymodels 1.1.1 ──
#>  broom        1.0.5      recipes      1.0.9
#>  dials        1.2.0      rsample      1.2.0
#>  dplyr        1.1.4      tibble       3.2.1
#>  ggplot2      3.4.4      tidyr        1.3.0
#>  infer        1.0.5      tune         1.1.2
#>  modeldata    1.2.0      workflows    1.1.3
#>  parsnip      1.1.1      workflowsets 1.0.1
#>  purrr        1.0.2      yardstick    1.2.0
#> ── Conflicts ───────────────────────────────────────── tidymodels_conflicts() ──
#>  purrr::discard() masks scales::discard()
#>  dplyr::filter()  masks stats::filter()
#>  dplyr::lag()     masks stats::lag()
#>  recipes::step()  masks stats::step()
#>  Use suppressPackageStartupMessages() to eliminate package startup messages
library(modeltime)
library(modeltime.ensemble)
library(dplyr)
library(timetk)

# Make an ensemble from a Modeltime Table
ensemble_fit <- m750_models %>%
    ensemble_average(type = "mean")

ensemble_fit
#> ── Modeltime Ensemble ───────────────────────────────────────────
#> Ensemble of 3 Models (MEAN) 
#> 
#> # Modeltime Table
#> # A tibble: 3 × 3
#>   .model_id .model     .model_desc            
#>       <int> <list>     <chr>                  
#> 1         1 <workflow> ARIMA(0,1,1)(0,1,1)[12]
#> 2         2 <workflow> PROPHET                
#> 3         3 <workflow> GLMNET                 

# Forecast with the Ensemble
modeltime_table(
    ensemble_fit
) %>%
    modeltime_forecast(
        new_data    = testing(m750_splits),
        actual_data = m750
    ) %>%
    plot_modeltime_forecast(
        .interactive = FALSE,
        .conf_interval_show = FALSE
    )
#> Warning: There were 2 warnings in `dplyr::mutate()`.
#> The first warning was:
#>  In argument: `.nested.col = purrr::map2(...)`.
#> Caused by warning:
#> ! There was 1 warning in `dplyr::mutate()`.
#>  In argument: `.nested.col = purrr::map2(...)`.
#> 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 1 remaining warning.

# }