View source: R/prediction_intervals.R
| bb_cvforecast | R Documentation |
Compute prediction intervals by applying the single season block bootstrap method to subsets of time series data using a rolling forecast origin.
bb_cvforecast(
object,
data,
yvar,
neighbour = 0,
predictor.vars,
h = 1,
season.period = 1,
m = 1,
num.futures = 1000,
level = c(80, 95),
forward = TRUE,
initial = 1,
window = NULL,
roll.length = 1,
exclude.trunc = NULL,
recursive = FALSE,
recursive_colNames = NULL,
na.rm = TRUE,
verbose = list(solver = FALSE, progress = FALSE),
...
)
object |
Fitted model object of class |
data |
Data set. Must be a data set of class |
yvar |
Name of the response variable as a character string. |
neighbour |
If multiple models are fitted: Number of neighbours of each
key (i.e. grouping variable) to be considered in model fitting to handle
smoothing over the key. Should be an |
predictor.vars |
A character vector of names of the predictor variables. |
h |
Forecast horizon. |
season.period |
Length of the seasonal period. |
m |
Multiplier. (Block size = |
num.futures |
Number of possible future sample paths to be generated. |
level |
Confidence level for prediction intervals. |
forward |
If |
initial |
Initial period of the time series where no cross-validation forecasting is performed. |
window |
Length of the rolling window. If |
roll.length |
Number of observations by which each rolling/expanding window should be rolled forward. |
exclude.trunc |
The names of the predictor variables that should not be truncated for stable predictions as a character string. (Since the nonlinear functions are estimated using splines, extrapolation is not desirable. Hence, if any predictor variable is treated non-linearly in the estimated model, will be truncated to be in the in-sample range before obtaining predictions. If any variables are listed here will be excluded from such truncation.) |
recursive |
Whether to obtain recursive forecasts or not (default -
|
recursive_colNames |
If |
na.rm |
logical; if |
verbose |
A named list controlling verbosity options. Defaults to
|
... |
Other arguments not currently used. |
An object of class bb_cvforecast, which is a list that
contains following elements:
x |
The original time series. |
method |
A character string "bb_cvforecast". |
fit_times |
The number of times the model is fitted in cross-validation. |
mean |
Point forecasts as a multivariate time series, where the
|
res |
The matrix of in-sample residuals produced in cross-validation.
The number of rows corresponds to |
model_fit |
Models fitted in cross-validation. |
level |
The confidence values associated with the prediction intervals. |
lower |
A list containing
lower bounds for prediction intervals for each level. Each element within
the list will be a multivariate time series with the same dimensional
characteristics as |
upper |
A list containing upper bounds
for prediction intervals for each level. Each element within the list will
be a multivariate time series with the same dimensional characteristics as
|
possible_futures |
A list of matrices containing future sample paths generated at each cross-validation step. |
cb_cvforecast
if(requireNamespace("gurobi", quietly = TRUE)){
library(dplyr)
library(ROI)
library(tibble)
library(tidyr)
library(tsibble)
# Simulate data
n = 1105
set.seed(123)
sim_data <- tibble(x_lag_000 = runif(n)) |>
mutate(
# Add x_lags
x_lag = lag_matrix(x_lag_000, 5)) |>
unpack(x_lag, names_sep = "_") |>
mutate(
# Response variable
y = (0.9*x_lag_000 + 0.6*x_lag_001 + 0.45*x_lag_003)^3 +
(0.35*x_lag_002 + 0.7*x_lag_005)^2 + rnorm(n, sd = 0.1),
# Add an index to the data set
inddd = seq(1, n)) |>
drop_na() |>
select(inddd, y, starts_with("x_lag")) |>
# Make the data set a `tsibble`
as_tsibble(index = inddd)
# Index variables
index.vars <- colnames(sim_data)[3:8]
# Training set
sim_train <- sim_data[1:1000, ]
# Test set
sim_test <- sim_data[1001:1100, ]
# Model fitting
smimodel_ppr <- model_smimodel(data = sim_train,
yvar = "y",
index.vars = index.vars,
initialise = "ppr")
# Block bootstrap prediction intervals (3-steps-ahead interval forecasts)
set.seed(12345)
smimodel_ppr_bb <- bb_cvforecast(object = smimodel_ppr,
data = sim_data,
yvar = "y",
predictor.vars = index.vars,
h = 3,
num.futures = 50,
window = 1000)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.