The package bvartools
implements some common functions used for Bayesian inference for mulitvariate time series models. It should give researchers maximum freedom in setting up an MCMC algorithm in R and keep calculation time limited at the same time. This is achieved by implementing posterior simulation functions in C++. Its main features are
bvar
and bvec
function collects the output of a Gibbs sampler in standardised objects, which can be used for further analysespredict
, irf
, fevd
for forecasting, impulse response analysis and forecast error variance decomposition, respectively.RcppArmadillo
package of Eddelbuettel and Sanderson (2014).1This vignette provides the code to set up and estimate a basic Bayesian VAR (BVAR) model with the bvartools
package.2 For this illustration the dataset E1 from Lütkepohl (2007) is used. It contains data on West German fixed investment, disposable income and consumption expenditures in billions of DM from 1960Q1 to 1982Q4.
The gen_var
function produces the inputs y
and x
for the BVAR estimator, where y
is a matrix of dependent variables and x
is the matrix of regressors for the model
\[y_t = A x_t + u_t,\] with \(u_t \sim N(0, \Sigma)\).
As in Lütkepohl (2007) only the first 73 observations are used.
We calculate frequentist VAR estimates using the standard formula \(y x' (x x')^{-1}\) to obtain a benchmark for the Bayesian estimator. The parameters are obtained by OLS:
A_freq <- tcrossprod(y, x) %*% solve(tcrossprod(x)) # Calculate estimates
round(A_freq, 3) # Round estimates and print
#> invest.1 income.1 cons.1 invest.2 income.2 cons.2 const
#> invest -0.320 0.146 0.961 -0.161 0.115 0.934 -0.017
#> income 0.044 -0.153 0.289 0.050 0.019 -0.010 0.016
#> cons -0.002 0.225 -0.264 0.034 0.355 -0.022 0.013
And \(\Sigma\) is calculated by
u_freq <- y - A_freq %*% x
u_sigma_freq <- tcrossprod(u_freq) / (ncol(y) - nrow(x))
round(u_sigma_freq * 10^4, 2)
#> invest income cons
#> invest 21.30 0.72 1.23
#> income 0.72 1.37 0.61
#> cons 1.23 0.61 0.89
These are the same values as in Lütkepohl (2007).
The following code is a Gibbs sampler for a simple VAR model with non-informative priors.
# Reset random number generator for reproducibility
set.seed(1234567)
iter <- 10000 # Number of iterations of the Gibbs sampler
burnin <- 5000 # Number of burn-in draws
store <- iter - burnin
t <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
m <- k * nrow(x) # Number of estimated coefficients
# Set (uninformative) priors
a_mu_prior <- matrix(0, m) # Vector of prior parameter means
a_v_i_prior <- diag(0, m) # Inverse of the prior covariance matrix
u_sigma_df_prior <- 0 # Prior degrees of freedom
u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix
u_sigma_df_post <- t + u_sigma_df_prior # Posterior degrees of freedom
# Initial values
u_sigma <- diag(.00001, k)
u_sigma_i <- solve(u_sigma)
# Data containers for posterior draws
draws_a <- matrix(NA, m, store)
draws_sigma <- matrix(NA, k^2, store)
# Start Gibbs sampler
for (draw in 1:iter) {
# Draw conditional mean parameters
a <- post_normal(y, x, u_sigma_i, a_mu_prior, a_v_i_prior)
# Draw variance-covariance matrix
u <- y - matrix(a, k) %*% x # Obtain residuals
u_sigma_scale_post <- solve(u_sigma_scale_prior + tcrossprod(u))
u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k)
u_sigma <- solve(u_sigma_i) # Invert Sigma_i to obtain Sigma
# Store draws
if (draw > burnin) {
draws_a[, draw - burnin] <- a
draws_sigma[, draw - burnin] <- u_sigma
}
}
After the Gibbs sampler has finished, point estimates can be obtained as the mean of the posterior draws:
A <- rowMeans(draws_a) # Obtain means for every row
A <- matrix(A, k) # Transform mean vector into a matrix
A <- round(A, 3) # Round values
dimnames(A) <- list(dimnames(y)[[1]], dimnames(x)[[1]]) # Rename matrix dimensions
A # Print
#> invest.1 income.1 cons.1 invest.2 income.2 cons.2 const
#> invest -0.321 0.143 0.971 -0.161 0.099 0.938 -0.016
#> income 0.044 -0.152 0.287 0.050 0.019 -0.008 0.016
#> cons -0.003 0.224 -0.262 0.033 0.357 -0.022 0.013
Sigma <- rowMeans(draws_sigma) # Obtain means for every row
Sigma <- matrix(Sigma, k) # Transform mean vector into a matrix
Sigma <- round(Sigma * 10^4, 2) # Round values
dimnames(Sigma) <- list(dimnames(y)[[1]], dimnames(y)[[1]]) # Rename matrix dimensions
Sigma # Print
#> invest income cons
#> invest 22.68 0.78 1.32
#> income 0.78 1.46 0.66
#> cons 1.32 0.66 0.95
The means of the coefficient draws are very close to the results of the frequentist estimatior, which would be expected with non-informative priors.
bvar
objectsThe bvar
function can be used to collect relevant output of the Gibbs sampler into a standardised object, which can be used by further functions such as predict
to obtain forecasts or irf
for impulse respons analysis.
Posterior draws can be thinned with function thin
:
Forecasts with credible bands can be obtained with the function predict
. If the model contains deterministic terms, new values can be provided in the argument new_D
. If no values are provided, the function sets them to zero. The number of rows of new_D
must be the same as the argument n.ahead
.
Currently, bvartools
supports forecast error, orthogonalised, and generalised impulse response functions.
FEIR <- irf(bvar_est, impulse = "income", response = "cons", n.ahead = 8)
plot(FEIR, main = "Forecast Error Impulse Response", xlab = "Period", ylab = "Response")
Default forecast error variance decomposition (FEVD) is based on orthogonalised impulse responses (OIR).
bvar_fevd_oir <- fevd(bvar_est, response = "cons")
plot(bvar_fevd_oir, main = "OIR-based FEVD of consumption")
It is also possible to calculate FEVDs, which are based on generalised impulse responses (GIR). Note that these do not automatically add up to unity.
bvar_fevd_gir <- fevd(bvar_est, response = "cons", type = "gir")
plot(bvar_fevd_gir, main = "GIR-based FEVD of consumption")
Eddelbuettel, D., & Sanderson C. (2014). RcppArmadillo: Accelerating R with high-performance C++ linear algebra. Computational Statistics and Data Analysis, 71, 1054-1063. https://doi.org/10.1016/j.csda.2013.02.005
Koop, G., Pesaran, M. H., & Potter, S.M. (1996). Impulse response analysis in nonlinear multivariate models. Journal of Econometrics 74(1), 119-147. https://doi.org/10.1016/0304-4076(95)01753-4
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Pesaran, H. H., & Shin, Y. (1998). Generalized impulse response analysis in linear multivariate models. Economics Letters, 58(1), 17-29. https://doi.org/10.1016/S0165-1765(97)00214-0
Sanderson, C., & Curtin, R. (2016). Armadillo: a template-based C++ library for linear algebra. Journal of Open Source Software, 1(2), 26. https://doi.org/10.21105/joss.00026
RcppArmadillo
is the Rcpp
bridge to the open source ‘Armadillo’ library of Sanderson and Curtin (2016).↩
Further examples about the use of the bvartools
package are available at https://www.r-econometrics.com/timeseriesintro/.↩