Learn R Programming

bsvars

An R package for Bayesian Estimation of Structural Vector Autoregressive Models

Provides fast and efficient procedures for Bayesian analysis of Structural Vector Autoregressions. This package estimates a wide range of models, including homo-, heteroskedastic, and non-normal specifications. Structural models can be identified by adjustable exclusion restrictions, time-varying volatility, or non-normality. They all include a flexible three-level equation-specific local-global hierarchical prior distribution for the estimated level of shrinkage for autoregressive and structural parameters. Additionally, the package facilitates predictive and structural analyses such as impulse responses, forecast error variance and historical decompositions, forecasting, verification of heteroskedasticity, non-normality, and hypotheses on autoregressive parameters, as well as analyses of structural shocks, volatilities, and fitted values. Beautiful plots, informative summary functions, and extensive documentation complement all this. The implemented techniques align closely with those presented in Lütkepohl, Shang, Uzeda, & Woźniak (2024), Lütkepohl & Woźniak (2020), and Song & Woźniak (2021).

Features

Structural Vector Autoregressions

  • All the models in the bsvars package consist of the Vector Autoregressive equation, with autoregressive parameters A and error terms E, and the structural equation with a structural matrix B and shocks U
    Y = AX + E           (VAR equation)
   BE = U                (structural equation)
  • The models are identified via exclusion restrictions, heteroskedasticity, or non-normality
  • The autoregressive parameters A and the structural matrix B feature a three-level local-global hierarchical prior that estimates the equation-specific level of shrinkage
  • In five models the structural shocks are conditionally normal with zero mean and diagonal covariance matrix with variances that are:
    • equal to one, that is, time invariant
    • time-varying following non-centred Stochastic Volatility
    • time-varying following centred Stochastic Volatility
    • time-varying with stationary Markov Switching
    • time-varying with sparse Markov Switching where the number of volatility regimes is estimated
  • In two more models non-normal structural shocks follow
    • a finite mixture of normal components and component-specific variances
    • a sparse mixture of normal components and component-specific variances where the number of states is estimated

Simple workflows

  • Specify the models using specify_bsvar_* functions, for instance, specify_bsvar_sv$new()
  • Estimate the models using the estimate() method
  • Predict the future using the forecast() method
  • Provide structural analyses using impulse responses, forecast error variance decompositions, historical decompositions, and structural shocks using functions compute_impulse_responses(), compute_variance_decompositions(), compute_historical_decompositions(), and compute_structural_shocks() respectively
  • Analyse the fitted values, time-varying volatility, and volatility regimes using functions compute_fitted_values(), compute_conditional_sd(), and compute_regime_probabilities() respectively
  • Use plot() and summary() methods to gain the insights into the core of the empirical problem.
  • Verify heteroskedasticity, non-normality, and hypotheses on autoregressive parameters using functions verify_volatility() and verify_autoregression()

Fast and efficient computations

  • Extraordinary computational speed is obtained by combining
    • the application of frontier econometric and numerical techniques, and
    • the implementation using compiled code written in cpp
  • It combines the best of two worlds: the ease of data analysis with R and fast cpp algorithms
  • The algorithms used here are very fast. But still, Bayesian estimation might take a little time. Look at our beautiful progress bar in the meantime:
**************************************************|
bsvars: Bayesian Structural Vector Autoregressions|
**************************************************|
 Gibbs sampler for the SVAR-SV model              |
   Non-centred SV model is estimated              |
**************************************************|
 Progress of the MCMC simulation for 1000 draws
    Every 10th draw is saved via MCMC thinning
 Press Esc to interrupt the computations
**************************************************|
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
*************************************

The hexagonal logo

This beautiful logo can be reproduced in R using this file.

Start your Bayesian analysis of data

The beginnings are as easy as ABC:

library(bsvars)                               # upload the package
data(us_fiscal_lsuw)                          # upload data
spec      = specify_bsvar_sv$new(us_fiscal_lsuw, p = 4)   # specify the model
burn_in   = estimate(spec, 1000)              # run the burn-in
out       = estimate(burn_in, 50000)          # estimate the model

fore      = forecast(out, horizon = 8)        # forecast 2 years ahead
plot(fore)                                    # plot the forecast

irfs      = compute_impulse_responses(out, 8) # compute impulse responses  
plot(irfs)                                    # plot the impulse responses

The bsvars package supports a simplified workflow using the |> pipe:

library(bsvars)                               # upload the package
data(us_fiscal_lsuw)                          # upload data
us_fiscal_lsuw |>
  specify_bsvar_sv$new(p = 4) |>              # specify the model
  estimate(S = 1000) |>                       # run the burn-in
  estimate(S = 50000) -> out                  # estimate the model

out |> forecast(horizon = 8) |> plot()        # compute and plot forecasts
out |> compute_impulse_responses(8) |> plot() # compute and plot impulse responses

Now, you’re ready to analyse your model!

Installation

The first time you install the package

You must have a cpp compiler. Follow the instructions from Section 1.3. by Eddelbuettel & François (2023). In short, for Windows: install RTools, for macOS: install Xcode Command Line Tools, and for Linux: install the standard developement packages.

Once that’s done:

Just open your R and type:

install.packages("bsvars")

The developer’s version of the package with the newest features can be installed by typing:

devtools::install_github("bsvars/bsvars")

Development

The package is under intensive development. Your help is most welcome! Please, have a look at the roadmap, discuss package features and applications, or report a bug. Thank you!

About the author

Tomasz is a Bayesian econometrician and a Senior Lecturer at the University of Melbourne. He develops methodology for empirical macroeconomic analyses and programs in R and cpp using Rcpp.

Copy Link

Version

Install

install.packages('bsvars')

Monthly Downloads

751

Version

3.1

License

GPL (>= 3)

Maintainer

Tomasz Woźniak

Last Published

July 14th, 2024

Functions in bsvars (3.1)

compute_fitted_values.PosteriorBSVART

Computes posterior draws from data predictive density
compute_fitted_values

Computes posterior draws from data predictive density
compute_historical_decompositions.PosteriorBSVARMSH

Computes posterior draws of historical decompositions
compute_historical_decompositions.PosteriorBSVARMIX

Computes posterior draws of historical decompositions
compute_historical_decompositions.PosteriorBSVARSV

Computes posterior draws of historical decompositions
compute_historical_decompositions.PosteriorBSVART

Computes posterior draws of historical decompositions
compute_impulse_responses.PosteriorBSVAR

Computes posterior draws of impulse responses
compute_fitted_values.PosteriorBSVARSV

Computes posterior draws from data predictive density
compute_historical_decompositions.PosteriorBSVAR

Computes posterior draws of historical decompositions
compute_regime_probabilities.PosteriorBSVARMSH

Computes posterior draws of regime probabilities
compute_impulse_responses

Computes posterior draws of impulse responses
compute_impulse_responses.PosteriorBSVARSV

Computes posterior draws of impulse responses
compute_historical_decompositions

Computes posterior draws of historical decompositions
compute_impulse_responses.PosteriorBSVART

Computes posterior draws of impulse responses
compute_impulse_responses.PosteriorBSVARMSH

Computes posterior draws of impulse responses
compute_impulse_responses.PosteriorBSVARMIX

Computes posterior draws of impulse responses
compute_structural_shocks.PosteriorBSVARMIX

Computes posterior draws of structural shocks
compute_structural_shocks

Computes posterior draws of structural shocks
compute_regime_probabilities

Computes posterior draws of regime probabilities
compute_variance_decompositions.PosteriorBSVARSV

Computes posterior draws of the forecast error variance decomposition
compute_structural_shocks.PosteriorBSVARSV

Computes posterior draws of structural shocks
compute_variance_decompositions.PosteriorBSVAR

Computes posterior draws of the forecast error variance decomposition
compute_structural_shocks.PosteriorBSVAR

Computes posterior draws of structural shocks
compute_structural_shocks.PosteriorBSVART

Computes posterior draws of structural shocks
compute_variance_decompositions.PosteriorBSVARMSH

Computes posterior draws of the forecast error variance decomposition
compute_regime_probabilities.PosteriorBSVARMIX

Computes posterior draws of regime probabilities
compute_variance_decompositions.PosteriorBSVART

Computes posterior draws of the forecast error variance decomposition
compute_structural_shocks.PosteriorBSVARMSH

Computes posterior draws of structural shocks
compute_variance_decompositions.PosteriorBSVARMIX

Computes posterior draws of the forecast error variance decomposition
estimate.PosteriorBSVARSV

Bayesian estimation of a Structural Vector Autoregression with Stochastic Volatility heteroskedasticity via Gibbs sampler
compute_variance_decompositions

Computes posterior draws of the forecast error variance decomposition
estimate.PosteriorBSVARMSH

Bayesian estimation of a Structural Vector Autoregression with Markov-switching heteroskedasticity via Gibbs sampler
estimate.PosteriorBSVART

Bayesian estimation of a homoskedastic Structural Vector Autoregression with t-distributed structural shocks via Gibbs sampler
estimate.PosteriorBSVARMIX

Bayesian estimation of a Structural Vector Autoregression with shocks following a finite mixture of normal components via Gibbs sampler
estimate.BSVART

Bayesian estimation of a homoskedastic Structural Vector Autoregression with t-distributed structural shocks via Gibbs sampler
estimate.BSVARMSH

Bayesian estimation of a Structural Vector Autoregression with Markov-switching heteroskedasticity via Gibbs sampler
estimate.PosteriorBSVAR

Bayesian estimation of a homoskedastic Structural Vector Autoregression via Gibbs sampler
estimate.BSVARSV

Bayesian estimation of a Structural Vector Autoregression with Stochastic Volatility heteroskedasticity via Gibbs sampler
estimate.BSVARMIX

Bayesian estimation of a Structural Vector Autoregression with shocks following a finite mixture of normal components via Gibbs sampler
forecast

Forecasting using Structural Vector Autoregression
plot.Forecasts

Plots fitted values of dependent variables
estimate.BSVAR

Bayesian estimation of a homoskedastic Structural Vector Autoregression via Gibbs sampler
estimate

Bayesian estimation of Structural Vector Autoregressions via Gibbs sampler
plot.PosteriorFEVD

Plots forecast error variance decompositions
forecast.PosteriorBSVARMIX

Forecasting using Structural Vector Autoregression
normalise_posterior

Waggoner & Zha (2003) row signs normalisation of the posterior draws for matrix \(B\)
forecast.PosteriorBSVARMSH

Forecasting using Structural Vector Autoregression
forecast.PosteriorBSVAR

Forecasting using Structural Vector Autoregression
forecast.PosteriorBSVART

Forecasting using Structural Vector Autoregression
plot.PosteriorFitted

Plots fitted values of dependent variables
plot.PosteriorIR

Plots impulse responses
specify_bsvar_mix

R6 Class representing the specification of the BSVAR model with a zero-mean mixture of normals model for structural shocks.
plot_ribbon

Plots the median and an interval between two specified percentiles for a sequence of K random variables
forecast.PosteriorBSVARSV

Forecasting using Structural Vector Autoregression
plot.PosteriorShocks

Plots structural shocks
specify_bsvar_msh

R6 Class representing the specification of the BSVAR model with Markov Switching Heteroskedasticity.
plot.PosteriorRegimePr

Plots estimated regime probabilities
specify_bsvar

R6 Class representing the specification of the homoskedastic BSVAR model
plot.PosteriorHD

Plots historical decompositions
specify_data_matrices

R6 Class Representing DataMatricesBSVAR
specify_posterior_bsvar_msh

R6 Class Representing PosteriorBSVARMSH
plot.PosteriorSigma

Plots structural shocks' conditional standard deviations
specify_bsvar_sv

R6 Class representing the specification of the BSVAR model with Stochastic Volatility heteroskedasticity.
specify_posterior_bsvar

R6 Class Representing PosteriorBSVAR
specify_posterior_bsvar_mix

R6 Class Representing PosteriorBSVARMIX
specify_identification_bsvars

R6 Class Representing IdentificationBSVARs
specify_bsvar_t

R6 Class representing the specification of the BSVAR model with t-distributed structural shocks.
specify_posterior_bsvar_t

R6 Class Representing PosteriorBSVART
specify_prior_bsvar

R6 Class Representing PriorBSVAR
specify_posterior_bsvar_sv

R6 Class Representing PosteriorBSVARSV
specify_starting_values_bsvar_t

R6 Class Representing StartingValuesBSVART
specify_prior_bsvar_sv

R6 Class Representing PriorBSVARSV
specify_starting_values_bsvar

R6 Class Representing StartingValuesBSVAR
specify_starting_values_bsvar_mix

R6 Class Representing StartingValuesBSVARMIX
specify_starting_values_bsvar_sv

R6 Class Representing StartingValuesBSVARSV
specify_starting_values_bsvar_msh

R6 Class Representing StartingValuesBSVARMSH
summary.Forecasts

Provides posterior summary of Forecasts
specify_prior_bsvar_mix

R6 Class Representing PriorBSVARMIX
specify_prior_bsvar_msh

R6 Class Representing PriorBSVARMSH
summary.PosteriorFitted

Provides posterior summary of variables' fitted values
summary.PosteriorHD

Provides posterior summary of historical decompositions
summary.PosteriorBSVAR

Provides posterior summary of homoskedastic Structural VAR estimation
summary.PosteriorBSVARMIX

Provides posterior summary of non-normal Structural VAR estimation
specify_prior_bsvar_t

R6 Class Representing PriorBSVART
summary.PosteriorRegimePr

Provides posterior summary of regime probabilities
summary.PosteriorIR

Provides posterior summary of impulse responses
us_fiscal_ex

A 3-variable system of exogenous variables for the US fiscal model for the period 1948 Q1 -- 2024 Q1
summary.SDDRidMIX

Provides summary of verifying shocks' normality
summary.PosteriorSigma

Provides posterior summary of structural shocks' conditional standard deviations
summary.PosteriorShocks

Provides posterior summary of structural shocks
summary.SDDRautoregression

Provides summary of verifying hypotheses about autoregressive parameters
summary.PosteriorBSVART

Provides posterior summary of Structural VAR with t-distributed shocks estimation
summary.PosteriorFEVD

Provides posterior summary of forecast error variance decompositions
summary.SDDRvolatility

Provides summary of verifying homoskedasticity
summary.PosteriorBSVARSV

Provides posterior summary of heteroskedastic Structural VAR estimation
summary.SDDRidSV

Provides summary of verifying homoskedasticity
summary.SDDRidT

Provides summary of verifying shocks' normality
us_fiscal_lsuw

A 3-variable US fiscal system for the period 1948 Q1 -- 2024 Q1
summary.PosteriorBSVARMSH

Provides posterior summary of heteroskedastic Structural VAR estimation
summary.SDDRidMSH

Provides summary of verifying homoskedasticity
verify_autoregression.PosteriorBSVART

Verifies hypotheses involving autoregressive parameters
verify_autoregression

Verifies hypotheses involving autoregressive parameters
verify_autoregression.PosteriorBSVAR

Verifies hypotheses involving autoregressive parameters
verify_volatility.PosteriorBSVAR

Verifies heteroskedasticity of structural shocks equation by equation
verify_volatility.PosteriorBSVARMIX

Verifies heteroskedasticity of structural shocks equation by equation
verify_volatility.PosteriorBSVARMSH

Verifies heteroskedasticity of structural shocks equation by equation
verify_identification.PosteriorBSVART

Verifies identification through heteroskedasticity or non-normality of of structural shocks
verify_autoregression.PosteriorBSVARSV

Verifies hypotheses involving autoregressive parameters
verify_volatility.PosteriorBSVARSV

Verifies heteroskedasticity of structural shocks equation by equation
verify_autoregression.PosteriorBSVARMSH

Verifies hypotheses involving autoregressive parameters
verify_identification

Verifies identification through heteroskedasticity or non-normality of of structural shocks
verify_autoregression.PosteriorBSVARMIX

Verifies hypotheses involving autoregressive parameters
verify_identification.PosteriorBSVAR

Verifies identification through heteroskedasticity or non-normality of of structural shocks
verify_volatility

Verifies heteroskedasticity of structural shocks equation by equation
verify_identification.PosteriorBSVARMSH

Verifies identification through heteroskedasticity or non-normality of of structural shocks
verify_identification.PosteriorBSVARSV

Verifies identification through heteroskedasticity or non-normality of of structural shocks
verify_identification.PosteriorBSVARMIX

Verifies identification through heteroskedasticity or non-normality of of structural shocks
compute_conditional_sd.PosteriorBSVARMSH

Computes posterior draws of structural shock conditional standard deviations
compute_conditional_sd.PosteriorBSVARMIX

Computes posterior draws of structural shock conditional standard deviations
compute_conditional_sd.PosteriorBSVARSV

Computes posterior draws of structural shock conditional standard deviations
compute_fitted_values.PosteriorBSVARMIX

Computes posterior draws from data predictive density
compute_fitted_values.PosteriorBSVAR

Computes posterior draws from data predictive density
compute_conditional_sd

Computes posterior draws of structural shock conditional standard deviations
compute_conditional_sd.PosteriorBSVART

Computes posterior draws of structural shock conditional standard deviations
compute_fitted_values.PosteriorBSVARMSH

Computes posterior draws from data predictive density
bsvars-package

Bayesian Estimation of Structural Vector Autoregressive Models
compute_conditional_sd.PosteriorBSVAR

Computes posterior draws of structural shock conditional standard deviations