calculate_intervals: Calculate bootstrap prediction intervals for forecasts

Description Usage Arguments Value Examples

View source: R/calculate_intervals.R

Description

The residuals from model training/fit are sampled i.i.d. for (a) each direct forecast horizon for a single time series and (b) each combination of direct forecast horizon and group for multiple time series.

Usage

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
calculate_intervals(
  forecasts,
  residuals,
  index = NULL,
  outcome = NULL,
  keys = NULL,
  levels = c(0.95),
  times = 100L,
  weights = NULL,
  keep_samples = FALSE
)

Arguments

forecasts

A data.frame of forecasts.

residuals

A data.frame of residuals (e.g., residuals(data_fit))

index

Optional for forecasts from combine_forecasts(). A string giving the name of the date column in forecasts.

outcome

Optional for forecasts from combine_forecasts(). A string giving the name of the forecast column in forecasts.

keys

Optional. For grouped time series, a character vector giving the column name(s) of the group columns. The key identifies unique time series of residuals for bootstrap sampling. For direct forecasting, a single time series will have one group per direct forecast horizon.

levels

A numeric vector with 1 or more forecast prediction intervals. A level of .95, for example, will return the 0.25 and .975 quantiles of the bootstrapped forecast distribution at each forecast horizon.

times

Integer. The number of bootstrap samples.

weights

Not implemented.

keep_samples

Boolean. If TRUE, a data.frame of times bootstrapped forecasts is returned in addition to the calculated forecast prediction intervals. The samples are in the list slot named 'samples'.

Value

If forecasts is an object of class 'forecast_results', a forecast_results object with a new column for each lower- and upper-bound forecast in levels. If forecasts is a data.frame, the function return will be the same but without forecastML attributes. If, keep_samples is TRUE, a named list of length 2 is returned with 'forecasts' and 'samples'.

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
34
35
36
37
38
## Not run: 
  data("data_seatbelts", package = "forecastML")

  data_train <- create_lagged_df(data_seatbelts, type = "train", method = "direct",
                                 outcome_col = 1, lookback = 1:15,
                                 horizons = c(1, 6, 12))

  windows <- create_windows(data_train, window_length = 0)

  model_fn <- function(data) {
    model <- lm(DriversKilled ~ ., data)
  }

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

  predict_fn <- function(model, data) {
    data_pred <- as.data.frame(predict(model, data))
  }

  data_fit <- predict(model_results, prediction_function = list(predict_fn), data = data_train)

  residuals <- residuals(data_fit)

  data_forecast <- create_lagged_df(data_seatbelts, type = "forecast",
                                    method = "direct", outcome_col = 1,
                                    lookback = 1:15, horizons = c(1, 6, 12))

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

  data_forecasts <- combine_forecasts(data_forecasts)

  data_forecasts <- calculate_intervals(data_forecasts, residuals, times = 30)

  plot(data_forecasts)

## End(Not run)

nredell/forecastML documentation built on June 14, 2020, 5:12 p.m.