Forecasting using Structural Vector Autoregression
Source:R/forecast.R
forecast.PosteriorBSVARMIX.Rd
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 'PosteriorBSVARMIX'
forecast(
posterior,
horizon = 1,
exogenous_forecast = NULL,
conditional_forecast = NULL
)
Arguments
- posterior
posterior estimation outcome - an object of class
PosteriorBSVARMIX
obtained by running theestimate
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 containnumeric
orNA
values. The entries withNA
values correspond to the values that are forecasted conditionally on the realisations provided asnumeric
values.
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
NxTxS
array with the draws from predictive density- forecasts_sigma
provided only for heteroskedastic models, an
NxTxS
array with the draws from the predictive density of structural shocks conditional standard deviations- Y
an \(NxT\) matrix with the data on dependent variables
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.822846 0.03970738 -9.886112 -9.784667
#> 2 -9.806706 0.03909839 -9.862611 -9.757952
#> 3 -9.801070 0.05643508 -9.870298 -9.726569
#> 4 -9.796551 0.07759272 -9.922286 -9.721255
#> 5 -9.785176 0.08714302 -9.929234 -9.699514
#> 6 -9.799670 0.08553221 -9.932941 -9.694298
#> 7 -9.786979 0.09465144 -9.929065 -9.672518
#> 8 -9.777957 0.11395437 -9.937104 -9.617393
#>
#> $variable3
#> mean sd 5% quantile 95% quantile
#> 1 -7.068890 0.04990418 -7.159712 -7.028088
#> 2 -7.050519 0.03783268 -7.092040 -6.994934
#> 3 -7.035814 0.05847281 -7.089360 -6.942734
#> 4 -7.035925 0.06546260 -7.130703 -6.946988
#> 5 -7.034157 0.08565502 -7.142984 -6.902916
#> 6 -7.032694 0.07662933 -7.124635 -6.916280
#> 7 -7.003395 0.08954214 -7.121886 -6.873249
#> 8 -7.003338 0.09556267 -7.133180 -6.882801
#>
# 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
#> **************************************************|