bvar: Bayesian Vector Autoregression Objects

View source: R/bvar.R

bvarR Documentation

Bayesian Vector Autoregression Objects

Description

bvar is used to create objects of class "bvar".

A plot function for objects of class "bvar".

Forecasting a Bayesian VAR object of class "bvar" with credible bands.

Usage

bvar(
  data = NULL,
  exogen = NULL,
  y,
  x = NULL,
  A0 = NULL,
  A = NULL,
  B = NULL,
  C = NULL,
  Sigma = NULL
)

## S3 method for class 'bvar'
plot(x, ci = 0.95, type = "hist", ...)

## S3 method for class 'bvar'
predict(object, ..., n.ahead = 10, new_x = NULL, new_d = NULL, ci = 0.95)

Arguments

data

the original time-series object of endogenous variables.

exogen

the original time-series object of unmodelled variables.

y

a time-series object of endogenous variables with T observations, usually, a result of a call to gen_var.

x

an object of class "bvar", usually, a result of a call to draw_posterior.

A0

either a K^2 \times S matrix of MCMC coefficient draws of structural parameters or a named list, where element coeffs contains a K^2 \times S matrix of MCMC coefficient draws of structural parameters and element lambda contains the corresponding draws of inclusion parameters in case variable selection algorithms were employed. For time varying parameter models the coefficient matrix must be TK^2 \times S. Draws of the error covariance matrix of the state equation can be provided as a K^2 \times S matrix in an additional list element.

A

either a pK^2 \times S matrix of MCMC coefficient draws of lagged endogenous variables or a named list, where element coeffs contains a pK^2 \times S matrix of MCMC coefficient draws of lagged endogenous variables and element lambda contains the corresponding draws of inclusion parameters in case variable selection algorithms were employed. For time varying parameter models the coefficient matrix must be pTK^2 \times S. Draws of the error covariance matrix of the state equation can be provided as a pK^2 \times S matrix in an additional list element.

B

either a ((1 + s)MK) \times S matrix of MCMC coefficient draws of unmodelled, non-deterministic variables or a named list, where element coeffs contains a ((1 + s)MK) \times S matrix of MCMC coefficient draws of unmodelled, non-deterministic variables and element lambda contains the corresponding draws of inclusion parameters in case variable selection algorithms were employed. For time varying parameter models the coefficient matrix must be (1 + s)TMK \times S. Draws of the error covariance matrix of the state equation can be provided as a (1 + s)MK \times S matrix in an additional list element.

C

either a KN \times S matrix of MCMC coefficient draws of deterministic terms or a named list, where element coeffs contains a KN \times S matrix of MCMC coefficient draws of deterministic terms and element lambda contains the corresponding draws of inclusion parameters in case variable selection algorithms were employed. For time varying parameter models the coefficient matrix must be TKN \times S. Draws of the error covariance matrix of the state equation can be provided as a KN \times S matrix in an additional list element.

Sigma

a K^2 \times S matrix of MCMC draws for the error variance-covariance matrix or a named list, where element coeffs contains a K^2 \times S matrix of MCMC draws for the error variance-covariance matrix and element lambda contains the corresponding draws of inclusion parameters in case variable selection algorithms were employed to the covariances. For time varying parameter models the coefficient matrix must be TK^2 \times S. Draws of the error covariance matrix of the state equation can be provided as a K^2 \times S matrix in an additional list element.

ci

a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95.

type

either "hist" (default) for histograms, "trace" for a trace plot or "boxplot" for a boxplot. Only used for parameter draws of constant coefficients.

...

additional arguments.

object

an object of class "bvar", usually, a result of a call to bvar or bvec_to_bvar.

n.ahead

number of steps ahead at which to predict.

new_x

an object of class ts of new non-deterministic, exogenous variables. The object must have the same frequency as the time series in object[["x"]] and must contain at least all necessary observations for the predicted period.

new_d

a matrix of new deterministic variables. Must have n.ahead rows.

Details

For the VARX model

A_0 y_t = \sum_{i = 1}^{p} A_i y_{t-i} + \sum_{i = 0}^{s} B_i x_{t - i} + C d_t + u_t

the function collects the S draws of a Gibbs sampler (after the burn-in phase) in a standardised object, where y_t is a K-dimensional vector of endogenous variables, A_0 is a K \times K matrix of structural coefficients. A_i is a K \times K coefficient matrix of lagged endogenous variabels. x_t is an M-dimensional vector of unmodelled, non-deterministic variables and B_i its corresponding coefficient matrix. d_t is an N-dimensional vector of deterministic terms and C its corresponding coefficient matrix. u_t is an error term with u_t \sim N(0, \Sigma_u).

For time varying parameter and stochastic volatility models the respective coefficients and error covariance matrix of the above model are assumed to be time varying, respectively.

The draws of the different coefficient matrices provided in A0, A, B, C and Sigma have to correspond to the same MCMC iterations.

For the VAR model

A_0 y_t = \sum_{i = 1}^{p} A_{i} y_{t-i} + \sum_{i = 0}^{s} B_{i} x_{t-i} + C D_t + u_t,

with u_t \sim N(0, \Sigma) the function produces n.ahead forecasts.

Value

An object of class "bvar" containing the following components, if specified:

data

the original time-series object of endogenous variables.

exogen

the original time-series object of unmodelled variables.

y

a K \times T matrix of endogenous variables.

x

a (pK + (1+s)M + N) \times T matrix of regressor variables.

A0

an S \times K^2 "mcmc" object of coefficient draws of structural parameters. In case of time varying parameters a list of such objects.

A0_lambda

an S \times K^2 "mcmc" object of inclusion parameters for structural parameters.

A0_sigma

an S \times K^2 "mcmc" object of the error covariance matrices of the structural parameters in a model with time varying parameters.

A

an S \times pK^2 "mcmc" object of coefficient draws of lagged endogenous variables. In case of time varying parameters a list of such objects.

A_lambda

an S \times pK^2 "mcmc" object of inclusion parameters for lagged endogenous variables.

A_sigma

an S \times pK^2 "mcmc" object of the error covariance matrices of coefficients of lagged endogenous variables in a model with time varying parameters.

B

an S \times ((1 + s)MK) "mcmc" object of coefficient draws of unmodelled, non-deterministic variables. In case of time varying parameters a list of such objects.

B_lambda

an S \times ((1 + s)MK) "mcmc" object of inclusion parameters for unmodelled, non-deterministic variables.

B_sigma

an S \times ((1 + s)MK) "mcmc" object of the error covariance matrices of coefficients of unmodelled, non-deterministic variables in a model with time varying parameters.

C

an S \times NK "mcmc" object of coefficient draws of deterministic terms. In case of time varying parameters a list of such objects.

C_lambda

an S \times NK "mcmc" object of inclusion parameters for deterministic terms.

C_sigma

an S \times NK "mcmc" object of the error covariance matrices of coefficients of deterministic terms in a model with time varying parameters.

Sigma

an S \times K^2 "mcmc" object of variance-covariance draws. In case of time varying parameters a list of such objects.

Sigma_lambda

an S \times K^2 "mcmc" object of inclusion parameters for error covariances.

Sigma_sigma

an S \times K^2 "mcmc" object of the error covariance matrices of the coefficients of the error covariance matrix of the measurement equation of a model with time varying parameters.

specifications

a list containing information on the model specification.

A time-series object of class "bvarprd".

References

Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.

Examples


# Get data
data("e1")
e1 <- diff(log(e1))
e1 <- window(e1, end = c(1978, 4))

# Generate model data
data <- gen_var(e1, p = 2, deterministic = "const")

# Add priors
model <- add_priors(data,
                    coef = list(v_i = 0, v_i_det = 0),
                    sigma = list(df = 0, scale = .00001))

# Set RNG seed for reproducibility 
set.seed(1234567)

iterations <- 400 # Number of iterations of the Gibbs sampler
# Chosen number of iterations and burnin should be much higher.
burnin <- 100 # Number of burn-in draws
draws <- iterations + burnin # Total number of MCMC draws

y <- t(model$data$Y)
x <- t(model$data$Z)
tt <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
m <- k * nrow(x) # Number of estimated coefficients

# Priors
a_mu_prior <- model$priors$coefficients$mu # Vector of prior parameter means
a_v_i_prior <- model$priors$coefficients$v_i # Inverse of the prior covariance matrix

u_sigma_df_prior <- model$priors$sigma$df # Prior degrees of freedom
u_sigma_scale_prior <- model$priors$sigma$scale # Prior covariance matrix
u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom

# Initial values
u_sigma_i <- diag(1 / .00001, k)

# Data containers for posterior draws
draws_a <- matrix(NA, m, iterations)
draws_sigma <- matrix(NA, k^2, iterations)

# Start Gibbs sampler
for (draw in 1:draws) {
 # 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)

 # Store draws
 if (draw > burnin) {
  draws_a[, draw - burnin] <- a
  draws_sigma[, draw - burnin] <- solve(u_sigma_i)
 }
}

# Generate bvar object
bvar_est <- bvar(y = model$data$Y, x = model$data$Z,
                 A = draws_a[1:18,], C = draws_a[19:21, ],
                 Sigma = draws_sigma)
                 

# Load data 
data("e1")
e1 <- diff(log(e1)) * 100

# Generate model
model <- gen_var(e1, p = 1, deterministic = 2,
                 iterations = 100, burnin = 10)
# Chosen number of iterations and burn-in should be much higher.

# Add priors
model <- add_priors(model)

# Obtain posterior draws
object <- draw_posterior(model)

# Plot draws
plot(object)


# Load data
data("e1")
e1 <- diff(log(e1)) * 100
e1 <- window(e1, end = c(1978, 4))

# Generate model data
model <- gen_var(e1, p = 0, deterministic = "const",
                 iterations = 100, burnin = 10)
# Chosen number of iterations and burnin should be much higher.

# Add prior specifications
model <- add_priors(model)

# Obtain posterior draws
object <- draw_posterior(model)

# Generate forecasts
bvar_pred <- predict(object, n.ahead = 10, new_d = rep(1, 10))

# Plot forecasts
plot(bvar_pred)


franzmohr/bvartools documentation built on Jan. 28, 2024, 4:06 a.m.