
Forecasting using Structural Vector Autoregression
Source:R/forecast.R
      forecast.PosteriorBSVARMIX.RdSamples 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 'PosteriorBSVARMIX'
forecast(
  posterior,
  horizon = 1,
  exogenous_forecast = NULL,
  conditional_forecast = NULL
)Arguments
- posterior
- posterior estimation outcome - an object of class - PosteriorBSVARMIXobtained by running the- estimatefunction.
- horizon
- a positive integer, specifying the forecasting horizon. 
- exogenous_forecast
- a matrix of dimension - horizon x dcontaining forecasted values of the exogenous variables.
- conditional_forecast
- a - horizon x Nmatrix with forecasted values for selected variables. It should only contain- numericor- NAvalues. The entries with- NAvalues correspond to the values that are forecasted conditionally on the realisations provided as- numericvalues.
Value
A list of class Forecasts containing the
draws from the predictive density and for heteroskedastic models the draws
from the predictive density of structural shocks conditional standard
deviations and data. The output elements include:
- forecasts
- an - NxTxSarray with the draws from predictive density
- Y
- an \(NxT\) matrix with the data on dependent variables 
- forecast_mean
- an - NxTxSarray with the mean of the predictive density
- forecast_covariance
- an - NxTxSarray with the covariance of the predictive density
Author
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_mix$new(us_fiscal_lsuw, p = 1, M = 2)
#> The identification is set to the default option of lower-triangular structural matrix.
# run the burn-in
burn_in        = estimate(specification, 5)
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 5 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
# estimate the model
posterior      = estimate(burn_in, 10)
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 10 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
# sample from predictive density 1 year ahead
predictive     = forecast(posterior, 4)
# workflow with the pipe |>
############################################################
set.seed(123)
us_fiscal_lsuw |>
  specify_bsvar_mix$new(p = 1, M = 2) |>
  estimate(S = 5) |>
  estimate(S = 10) |>  
  forecast(horizon = 4) -> predictive
#> The identification is set to the default option of lower-triangular structural matrix.
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 5 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 10 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
  
# 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_mix$new(us_fiscal_lsuw, M = 2, exogenous = us_fiscal_ex)
#> The identification is set to the default option of lower-triangular structural matrix.
burn_in        = estimate(specification, 5)
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 5 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
posterior      = estimate(burn_in, 10)
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 10 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
# forecast 2 years ahead
predictive     = forecast(
                    posterior, 
                    horizon = 8,
                    exogenous_forecast = us_fiscal_ex_forecasts,
                    conditional_forecast = us_fiscal_cond_forecasts
                  )
summary(predictive)
#>  **************************************************|
#>  bsvars: Bayesian Structural Vector Autoregressions|
#>  **************************************************|
#>    Posterior summary of forecasts                  |
#>  **************************************************|
#> $variable1
#>        mean sd 5% quantile 95% quantile
#> 1 -8.914237  0   -8.914237    -8.914237
#> 2 -8.908921  0   -8.908921    -8.908921
#> 3 -8.903604  0   -8.903604    -8.903604
#> 4 -8.898288  0   -8.898288    -8.898288
#> 5 -8.892971  0   -8.892971    -8.892971
#> 6 -8.887655  0   -8.887655    -8.887655
#> 7 -8.882338  0   -8.882338    -8.882338
#> 8 -8.877022  0   -8.877022    -8.877022
#> 
#> $variable2
#>        mean         sd 5% quantile 95% quantile
#> 1 -9.802687 0.04668796   -9.868956    -9.742510
#> 2 -9.783475 0.04680419   -9.838697    -9.721589
#> 3 -9.792169 0.04063983   -9.852829    -9.745487
#> 4 -9.785433 0.03335361   -9.830007    -9.742060
#> 5 -9.772237 0.04104349   -9.839236    -9.725222
#> 6 -9.781855 0.05393974   -9.861461    -9.715581
#> 7 -9.776435 0.06132241   -9.876083    -9.697306
#> 8 -9.758157 0.06747919   -9.858273    -9.663606
#> 
#> $variable3
#>        mean          sd 5% quantile 95% quantile
#> 1 -7.055304 0.009658222   -7.070568    -7.043389
#> 2 -7.045265 0.011122839   -7.058188    -7.028240
#> 3 -7.048160 0.020136677   -7.079009    -7.025450
#> 4 -7.041407 0.017929766   -7.067371    -7.017038
#> 5 -7.036533 0.016761213   -7.060489    -7.014173
#> 6 -7.032319 0.020157293   -7.063000    -7.008982
#> 7 -7.026899 0.020066324   -7.057249    -7.002635
#> 8 -7.023969 0.020216934   -7.055470    -7.002394
#> 
# workflow with the pipe |>
############################################################
set.seed(123)
us_fiscal_lsuw |>
  specify_bsvar_mix$new(M = 2, 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()
#> The identification is set to the default option of lower-triangular structural matrix.
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 5 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
#> **************************************************|
#> bsvars: Bayesian Structural Vector Autoregressions|
#> **************************************************|
#>  Gibbs sampler for the SVAR-finiteMIX model             |
#> **************************************************|
#>  Progress of the MCMC simulation for 10 draws
#>     Every draw is saved via MCMC thinning
#>  Press Esc to interrupt the computations
#> **************************************************|
