forecast.dynamic_fit: Forecast a fitted dynamic model

View source: R/forecast.R

forecast.dynamic_fitR Documentation

Forecast a fitted dynamic model

Description

Returns posterior predictive forecasts for the H periods after the end of the series. The forecasts are obtained by forward simulation from the stored posterior draws, so any horizon can be requested after fitting. If the model was fitted with horizon >= 1, the forecast draws stored in the fit are returned unless horizon, forecast_offset or forecast_trials is supplied.

Usage

## S3 method for class 'dynamic_fit'
forecast(
  object,
  horizon = NULL,
  forecast_offset = NULL,
  forecast_trials = NULL,
  probs = c(0.025, 0.5, 0.975),
  seed = NULL,
  ...
)

Arguments

object

A "dynamic_fit" object.

horizon

Forecast horizon H (a positive integer). If NULL (default), the forecast stored in the fit is returned; the fit must then have been created with horizon >= 1.

forecast_offset

Known offset over the forecast horizon, in the same format as in fit_dynamic_model(). Defaults to 0; a warning is issued if the model was fitted with a non-zero offset but no forecast_offset is supplied.

forecast_trials

Binomial and multinomial families: the number of trials (binomial) or the total count per period (multinomial) over the forecast horizon, of length 1 (recycled) or horizon. Defaults to the last observed number of trials (binomial) or the last non-zero row total (multinomial).

probs

Quantile probabilities for the summary. Default c(0.025, 0.5, 0.975).

seed

Optional random seed for the forward simulation. The previous state of the global random number generator is restored afterwards.

...

Unused.

Details

For every stored posterior draw, the latent path is propagated from the last in-sample state z_n with z_{n+h} = \mu + \rho z_{n+h-1} + \varepsilon_{n+h}, drawing the increments from the fitted innovation structure (Gaussian with the drawn variance, Student-t with the drawn scale and degrees of freedom, the drawn scale mixture, or the stochastic-volatility process continued from its last in-sample log-variance). A response is then drawn from the observation model at each simulated state. The intervals therefore reflect parameter, state and innovation uncertainty.

Zero inflation. For fits with zeros = "inflated" the response forecast is unconditional: each draw includes the zero-inflation gate, so structural zeros are reproduced and the draws are directly comparable to future observations (like the in-sample yrep, unlike yrep_open). The latent forecast summary in ⁠$latent⁠ is gate-free by construction.

Multinomial family. Response forecasts are multinomial draws with the totals given by forecast_trials. The summaries are in long format with a category column. summary and final cover all K categories on the count scale, prob gives the forecast category shares, and latent the K - 1 ALR series. draws is a ⁠draws x H x K⁠ array and final_draws a ⁠draws x K⁠ matrix.

Value

An object of class "dynamic_forecast": a list with summary (one row per horizon 1, \dots, H, on the response scale), latent (summary of the forecast latent path), draws (the predictive draws, draws x H), latent_draws (the latent forecast draws), final (the single-row summary of the final H-step-ahead forecast), final_draws (the predictive draws at horizon H), the forecast horizon, and the forecast_offset / forecast_trials that were used. For the multinomial family additionally prob (share summary) and prob_draws (⁠draws x H x K⁠ share draws); see Details for the layout.

See Also

fit_dynamic_model(), plot_forecast()

Examples

sim <- simulate_dynamic_poisson(60, 0.2, 2, seed = 1)
fit <- fit_dynamic_model(sim$y, nsave = 300, nburn = 200, seed = 1)
fc <- forecast(fit, horizon = 8)
fc$summary
fc$final

DynCount documentation built on Sept. 28, 2026, 5:10 p.m.