forecast.PosteriorBSVART: Forecasting using Structural Vector Autoregression

View source: R/forecast.R

forecast.PosteriorBSVARTR Documentation

Forecasting using Structural Vector Autoregression

Description

Samples from the joint predictive density of all of the dependent variables for models from packages bsvars, bsvarSIGNs or bvarPANELs at forecast horizons from 1 to horizon specified as an argument of the function.

Usage

## S3 method for class 'PosteriorBSVART'
forecast(
  posterior,
  horizon = 1,
  exogenous_forecast = NULL,
  conditional_forecast = NULL
)

Arguments

posterior

posterior estimation outcome - an object of class PosteriorBSVART obtained by running the estimate function.

horizon

a positive integer, specifying the forecasting horizon.

exogenous_forecast

a matrix of dimension horizon x d containing forecasted values of the exogenous variables.

conditional_forecast

a horizon x N matrix with forecasted values for selected variables. It should only contain numeric or NA values. The entries with NA values correspond to the values that are forecasted conditionally on the realisations provided as numeric values.

Value

A list of class Forecasts containing the draws from the predictive density and data. The output list includes element:

forecasts

an NxTxS array with the draws from predictive density

Y

an NxT matrix with the data on dependent variables

Author(s)

Tomasz Woźniak wozniak.tom@pm.me

Examples

# upload data
data(us_fiscal_lsuw)

# specify the model and set seed
set.seed(123)
specification  = specify_bsvar_t$new(us_fiscal_lsuw, p = 1)

# run the burn-in
burn_in        = estimate(specification, 5)

# estimate the model
posterior      = estimate(burn_in, 10)

# sample from predictive density 1 year ahead
predictive     = forecast(posterior, 4)

# workflow with the pipe |>
############################################################
set.seed(123)
us_fiscal_lsuw |>
  specify_bsvar_t$new(p = 1) |>
  estimate(S = 5) |> 
  estimate(S = 10) |> 
  forecast(horizon = 4) -> predictive

# conditional forecasting using a model with exogenous variables
############################################################
data(us_fiscal_ex_forecasts)      # upload exogenous variables future values
data(us_fiscal_cond_forecasts)    # upload a matrix with projected ttr

#' set.seed(123)
specification  = specify_bsvar_t$new(us_fiscal_lsuw, exogenous = us_fiscal_ex)
burn_in        = estimate(specification, 5)
posterior      = estimate(burn_in, 10)

# forecast 2 years ahead
predictive     = forecast(
                    posterior, 
                    horizon = 8,
                    exogenous_forecast = us_fiscal_ex_forecasts,
                    conditional_forecast = us_fiscal_cond_forecasts
                  )
summary(predictive)

# workflow with the pipe |>
############################################################
set.seed(123)
us_fiscal_lsuw |>
  specify_bsvar_t$new(exogenous = us_fiscal_ex) |>
  estimate(S = 5) |> 
  estimate(S = 10) |> 
  forecast(
    horizon = 8,
    exogenous_forecast = us_fiscal_ex_forecasts,
    conditional_forecast = us_fiscal_cond_forecasts
  ) |> plot()
  

bsvars documentation built on Oct. 24, 2024, 5:11 p.m.