Skip to content

bsvars/bvars

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

114 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

bvars bvars website

An R package for Bayesian Forecasting with Large Vector Autoregressions

R-CMD-check

Provides fast and efficient procedures for Bayesian estimation and forecasting using state-of-the-art Vector Autoregressions. This package includes the model proposed by Chan (2020), that is, a Bayesian Vector Autoregression with Minnesota priors and a flexible structure of the error term specification. The latter includes: conditional multivariate normal or Student’s t distributions, as well as homoskedastic or heteroskedastic specifications with a common volatility modelled by centred or non-centred Stochastic Volatility. Additionally, the package facilitates predictive analyses using density forecasting and forecast-error variance decompositions. All this is complemented by simple workflows, useful plots and summary functions, and comprehensive documentation. The ‘bvars’ package aligns with R packages ‘bsvars’ by Woźniak (2024), ‘bsvarSIGNs’ by Wang & Woźniak (2025), and ‘bpvars’ by Woźniak (2025) regarding objects, workflows, and code structure, and they constitute an integrated toolset.

bsvars.org website bsvars website bsvarSIGNs website bpvars website bvars website StealLikeBayes website

Features

Forecasting with Bayesian Vector Autoregressions

  • The bvars package includes state-of-the-art Vector Autoregressive models with Minnesota priors and a flexible structure of the error term specification. The model equations are:
       Y = A X + E          (VAR equation)
   E | X ~ MN(0, O, S)      (error term normality)
  • dependent variable matrix Y,
  • lagged dependent variable matrix X,
  • error term E,
  • autoregressive parameter matrix A,
  • error term covariance matrix S, and
  • diagonal matrix O allowing for heteroskedasticity and non-normality of the error term.
  • The error terms feature a zero-mean matrix-variate normal distribution with row-specific covariance matrix S and column-specific diagonal covariance matrix O of order T.
  • The parameters A and S follow a matrix-variate normal inverse Wishart prior featuring characteristics of the Minnesota priors.
  • The diagonal matrix O facilitates the following customisation of the error term specification:
    • conditional multivariate normal or Student’s t distributions,
    • homoskedastic or heteroskedastic specifications with a common volatility modelled by centred or non-centred stochastic volatility.

Simple workflows

  • Specify the models using the specify_bvar$new() function
  • Estimate the models using the estimate() method
  • Predict the future using the forecast() method
  • Compute forecast error variance decompositions using function compute_variance_decompositions()
  • Use plot() and summary() methods to gain the insights into the core of the empirical problem.

Fast and efficient computations

  • Extraordinary computational speed is obtained by combining
    • the application of frontier econometric and numerical techniques, and
    • the algorithms written in C++
  • It combines the best of two worlds: the ease of data analysis with R and fast C++ 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:
**************************************************|
bvars: Forecasting with Large                     |
       Bayesian Vector Autoregressions            |
**************************************************|
 Gibbs sampler for the BVAR model                 |
**************************************************|
 Progress of the MCMC simulation for 1000 draws
    Every draw is saved via MCMC thinning
 Press Esc to interrupt the computations
**************************************************|
0%   10   20   30   40   50   60   70   80   90   100%
[----|----|----|----|----|----|----|----|----|----|
*************************************

Start your Bayesian analysis of data

The beginnings are as easy as ABC:

library(bvars)                                          # load the package

spec = specify_bvar$new(                                # specify the model
  us_macro_chan,                                        # data
  p = 4,                                                # number of lags
  common_volatility = "ncSV",                           # heteroskedasticity
  distribution = "t",                                   # Student t error term
  stationary = rep(TRUE, ncol(us_macro_chan))           # Minnesota prior spec
)

burn = estimate(spec, S = 10000)                        # run the burn-in
post = estimate(burn, S = 10000)                        # estimate the model
summary(fore)                                           # estimation summary

fore = forecast(                                        # forecast the model 
  post,                                                 # estimation output
  horizon = 6                                           # forecast horizon
)

plot(fore)                                              # plot the forecasts
summary(fore)                                           # forecast summary forecasts

fevd = compute_variance_decompositions(
          post, horizon = 6)                            # compute variance decompositions
plot(fevd)                                              # plot variance decompositions

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

us_macro_chan |>                                        # data
  specify_bvar$new(p = 4) |>                            # specify the model
  estimate(S = 10000) |>                                # run the burn-in
  estimate(S = 10000) -> post                           # estimate the model

post |> forecast(horizon = 6) |> plot()                 # forecasting
post |> compute_variance_decompositions(horizon = 6) |> plot()

Now, you’re ready to analyse your model and forecasts!

The hexagonal logo

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

bvars website

Resources

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 development packages.

Once that’s done:

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

devtools::install_github("bsvars/bvars")

Development

The package is under intensive development. Your help is most welcome! Please, have a look at our issues to learn what we’re working on. Thank you!

About the authors

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 C++ using Rcpp.

Contributors