Learn R Programming

bivarhr (version 0.1.5)

predict_multistep: Multi-step Predictive Simulation for the Bivariate Hurdle Model

Description

Generates forward simulations for h future periods from a fitted bivariate hurdle negative binomial model (I/C), using posterior draws and dynamically updating the lagged history as new simulated values are added.

Usage

predict_multistep(fit_obj, DT, k, Tcut, h, ndraws = 800, seed = NULL)

Value

A list with two components:

pred_I

Numeric matrix of dimension S x h with simulated paths for I, where S is the number of posterior draws used.

pred_C

Numeric matrix of dimension S x h with simulated paths for C.

Arguments

fit_obj

A list returned by fit_one(), containing at least $fit (a CmdStanR fit object), $spec (model specification), and $controls (character vector of control variables).

DT

A data.frame or data.table with the covariates and original time series, including columns I, C, Regime, trans_PS, trans_SF, trans_FC and log_exposure50.

k

Integer; lag order used in the fitted model.

Tcut

Integer; last time index used as the starting point for prediction (historical window is 1:Tcut).

h

Integer; forecast horizon (number of steps ahead to simulate).

ndraws

Integer; maximum number of posterior draws to use for simulation (default 800). If larger than available draws, it is truncated.

seed

Optional integer; random seed passed to set.seed() for reproducibility of the simulation.

Details

For each selected posterior draw, the function iteratively simulates h future values of I and C. At each step:

  • The covariate vector is built from lagged outcomes (up to order k) and the corresponding row t of DT (trend terms, regime dummies, transition variables and controls).

  • The hurdle probabilities and negative-binomial means are computed from the draw-specific parameters.

  • New counts are sampled and appended to the local history so that subsequent steps use the updated lags.

Simulation stops early for a given path if Tcut + step > nrow(DT).

Examples

Run this code
# \donttest{
if (interactive() && requireNamespace("cmdstanr", quietly = TRUE)) {
  n <- 120
  DT <- data.table::data.table(
    I = rpois(n, 5), C = rpois(n, 3),
    Regime = factor(sample(c("A","B","C"), n, TRUE)),
    trans_PS = c(rep(1,5), rep(0,n-5)),
    trans_SF = c(rep(0,60), rep(1,5), rep(0,n-65)),
    trans_FC = rep(0, n),
    log_exposure50 = log(runif(n, 40, 60))
  )
  fit_obj <- fit_one(DT, k = 1, spec = "C")
  pred <- predict_multistep(fit_obj, DT, k = 1, Tcut = 100, h = 12,
                            ndraws = 500, seed = 123)
  str(pred$pred_I)
}
# }

Run the code above in your browser using DataLab