combine_forecasts: Combine multiple horizon-specific forecast models to produce...

Description Usage Arguments Value Methods and related functions Examples

View source: R/combine_forecasts.R

Description

The horizon-specific models can either be combined to (a) produce final forecasts for only those horizons at which they were trained (i.e., shorter-horizon models override longer-horizon models when producing final short-horizon h-step-ahead forecasts) or (b) produce final forecasts using any combination of horizon-specific models that minimized error over the validation/training dataset.

Usage

1
2
3
4
5
6
7
combine_forecasts(
  ...,
  type = c("horizon", "error"),
  aggregate = stats::median,
  data_error = list(NULL),
  metric = NULL
)

Arguments

...

One or more objects of class 'forecast_results' from running predict.forecast_model() on an input forward-looking forecast dataset. These are the forecasts from the horizon-specific direct forecasting models trained over the entire training dataset by setting create_windows(..., window_length = 0). If multiple models are passed in ... with the same direct forecast horizon, for type = 'horizon', forecasts for the same direct forecast horizon are combined with aggregate; for type = 'error', the model that minimizes the error metric at the given direct forecast horizon produces the forecast.

type

Default: 'horizon'. A character vector of length 1 that identifies the forecast combination method.

aggregate

Default median for type = 'horizon'. A function–without parentheses–that aggregates forecasts if more than one model passed in ... has the same direct forecast horizon and type = 'horizon'].

data_error

Optional. A list of objects of class 'validation_error' from running return_error() on a training dataset. The length and order of data_error should match the models passed in ....

metric

Required if data_error is given. A length 1 character vector naming the forecast error metric used to select the optimal model at each forecast horizon from the models passed in '...' e.g., 'mae'.

Value

An S3 object of class 'forecastML' with final h-step-ahead forecasts.

Forecast combination type:

Columns in returned 'forecastML' data.frame:

Methods and related functions

The output of combine_forecasts() has the following generic S3 methods

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Example with "type = 'horizon'".
data("data_seatbelts", package = "forecastML")

horizons <- c(1, 3, 12)
lookback <- 1:15

data_train <- create_lagged_df(data_seatbelts, type = "train", outcome_col = 1,
                               lookback = lookback, horizon = horizons)

windows <- create_windows(data_train, window_length = 0)

model_function <- function(data, my_outcome_col) {
  model <- lm(DriversKilled ~ ., data = data)
  return(model)
}

model_results <- train_model(data_train, windows, model_name = "LM", model_function)

data_forecast <- create_lagged_df(data_seatbelts, type = "forecast", outcome_col = 1,
                                  lookback = lookback, horizon = horizons)

prediction_function <- function(model, data_features) {
  x <- data_features
  data_pred <- data.frame("y_pred" = predict(model, newdata = x))
  return(data_pred)
}

data_forecasts <- predict(model_results, prediction_function = list(prediction_function),
                          data = data_forecast)

data_combined <- combine_forecasts(data_forecasts)

plot(data_combined)

Example output

Loading required package: dplyr

Attaching package:dplyrThe following objects are masked frompackage:stats:

    filter, lag

The following objects are masked frompackage:base:

    intersect, setdiff, setequal, union

forecastML documentation built on July 8, 2020, 7:27 p.m.