Learn R Programming

bivarhr (version 0.1.5)

rolling_oos: Rolling Out-of-Sample Forecast Evaluation

Description

Computes rolling out-of-sample (OOS) forecast accuracy for the selected bivariate hurdle model by repeatedly truncating the sample at different cut points Tcut, generating multi-step-ahead predictive distributions, and summarizing them via RMSE for I and C.

Usage

rolling_oos(
  best_fit,
  DT,
  h = 5,
  cuts = seq(round(0.6 * nrow(DT)), round(0.9 * nrow(DT)), length.out = 5)
)

Value

A data.frame with one row per Tcut and columns:

  • Tcut: training end index.

  • RMSE_I: rolling OOS RMSE for series I.

  • RMSE_C: rolling OOS RMSE for series C.

Arguments

best_fit

A fitted model object as returned by fit_one(), containing at least:

  • $fit: CmdStanR fit object with posterior draws.

  • $des: design matrices used by the model.

  • $k: lag order used in the fit.

This object is passed directly to predict_multistep().

DT

A data.frame or data.table containing the original time series and covariates used to fit the model, including at least columns I and C.

h

Integer; maximum forecast horizon (number of steps ahead) requested at each cut. For a given Tcut, the effective horizon is min(h, nrow(DT) - Tcut).

cuts

Numeric vector of time indices (training end points) at which to perform the rolling evaluation. By default, a grid of five equally spaced cut points between 60\ used: seq(round(0.6 * nrow(DT)), round(0.9 * nrow(DT)), length.out = 5).

Details

For each Tcut in cuts, the function:

  1. Calls predict_multistep() with fit_obj = best_fit, the full DT, lag k = best_fit$k, and horizon h_eff = min(h, nrow(DT) - Tcut) to obtain posterior predictive paths pred_I and pred_C.

  2. Computes the posterior-mean forecast for each step (mI, mC) as the column means of pred_I and pred_C.

  3. Extracts the realized outcomes yI = I[(Tcut + 1):(Tcut + h_eff)] and analogously for yC.

  4. Computes RMSE for each series: RMSE_I = sqrt(mean((yI - mI)^2)), RMSE_C = sqrt(mean((yC - mC)^2)).

Progress is reported via progressr. The resulting table is written as "rolling_oos.csv" in the directory specified by a global character scalar dir_csv.

Examples

Run this code
# \donttest{
# Minimal synthetic example illustrating the expected data structure:
set.seed(123)
DT <- data.frame(
  id = rep(1:10, each = 2),
  t  = rep(1:2, times = 10),
  I  = rpois(20, lambda = 0.5),
  C  = rpois(20, lambda = 1.0)
)

# Directory for CSV output (in practice, use a persistent path chosen
# by the user):
dir_csv <- file.path(tempdir(), "bivarhr_oos_csv")

# Typical workflow (commented out to avoid heavy computation and
# external dependencies such as CmdStan during R CMD check):
#
# best_fit <- fit_one(
#   data = DT,
#   k    = 2,
#   spec = "C"
# )
#
# oos_res <- rolling_oos(
#   fit     = best_fit,
#   data    = DT,
#   h       = 6,
#   dir_csv = dir_csv
# )
# print(oos_res)
# }

Run the code above in your browser using DataLab