Last chance! 50% off unlimited learning
Sale ends in
bvar
is used to create objects of class "bvar"
.
Forecasting a Bayesian VAR object of class "bvar"
with credible bands.
bvar(
data = NULL,
exogen = NULL,
y = NULL,
x = NULL,
A0 = NULL,
A = NULL,
B = NULL,
C = NULL,
Sigma = NULL
)# S3 method for bvar
predict(object, ..., n.ahead = 10, new_x = NULL, new_d = NULL, ci = 0.95)
the original time-series object of endogenous variables.
the original time-series object of unmodelled variables.
a time-series object of endogenous variables,
usually, a result of a call to gen_var
.
a time-series object of gen_var
.
either a coeffs
contains a lambda
contains the corresponding draws of inclusion
parameters in case variable selection algorithms were employed.
either a coeffs
contains a lambda
contains the corresponding draws of inclusion
parameters in case variable selection algorithms were employed.
either a coeffs
contains a lambda
contains the corresponding draws of inclusion
parameters in case variable selection algorithms were employed.
either a coeffs
contains a lambda
contains the corresponding draws of inclusion
parameters in case variable selection algorithms were employed.
a coeffs
contains a lambda
contains the corresponding draws of inclusion
parameters in case variable selection algorithms were employed to the covariances.
an object of class "bvar"
, usually, a result of a call to
bvar
or bvec_to_bvar
.
additional arguments.
number of steps ahead at which to predict.
a matrix of new non-deterministic, exogenous variables. Must have n.ahead
rows.
a matrix of new deterministic variables. Must have n.ahead
rows.
a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95.
An object of class "bvar"
containing the following components, if specified:
the original time-series object of endogenous variables.
the original time-series object of unmodelled variables.
a
a
an
an
an
an
an
an
an
an
an
an
a list containing information on the model specification.
A time-series object of class "bvarprd".
For the VARX model
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
n.ahead
forecasts.
L<U+00FC>tkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
# NOT RUN {
# 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
e1 <- window(e1, end = c(1978, 4))
# Generate model data
model <- gen_var(e1, p = 2, deterministic = 2,
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)
# }
Run the code above in your browser using DataLab