Title: | Bayesian Inference of Vector Autoregressive and Error Correction Models |
Version: | 0.2.4 |
Date: | 2024-01-05 |
Description: | Assists in the set-up of algorithms for Bayesian inference of vector autoregressive (VAR) and error correction (VEC) models. Functions for posterior simulation, forecasting, impulse response analysis and forecast error variance decomposition are largely based on the introductory texts of Chan, Koop, Poirier and Tobias (2019, ISBN: 9781108437493), Koop and Korobilis (2010) <doi:10.1561/0800000013> and Luetkepohl (2006, ISBN: 9783540262398). |
License: | GPL-2 | GPL-3 [expanded from: GPL (≥ 2)] |
Depends: | R (≥ 3.4.0), coda, Matrix |
Imports: | grDevices, graphics, methods, parallel, Rcpp (≥ 0.12.14), stats |
LinkingTo: | Rcpp, RcppArmadillo |
Encoding: | UTF-8 |
RoxygenNote: | 7.2.3 |
URL: | https://github.com/franzmohr/bvartools |
BugReports: | https://github.com/franzmohr/bvartools/issues |
Suggests: | knitr, rmarkdown |
VignetteBuilder: | knitr |
Collate: | 'RcppExports.R' 'add_priors.R' 'add_priors.bvarmodel.R' 'add_priors.bvecmodel.R' 'add_priors.dfmodel.R' 'bvar.R' 'bvar_fill_helper.R' 'bvarpost.R' 'bvartools-package.R' 'bvec.R' 'bvec_to_bvar.R' 'bvecpost.R' 'data.R' 'dfm.R' 'dfmpost.R' 'draw_posterior.R' 'draw_posterior.bvarmodel.R' 'draw_posterior.bvecmodel.R' 'draw_posterior.dfmodel.R' 'fevd.R' 'fevd.bvar.R' 'gen_dfm.R' 'gen_var.R' 'gen_vec.R' 'get_regressor_names.R' 'inclusion_prior.R' 'irf.R' 'irf.bvar.R' 'minnesota_prior.R' 'plot.bvar.R' 'plot.bvarfevd.R' 'plot.bvarirf.R' 'plot.bvarlist.R' 'plot.bvarprd.R' 'plot.bvec.R' 'plot.dfm.R' 'post_normal_covar_const.R' 'post_normal_covar_tvp.R' 'predict.bvar.R' 'summary.bvar.R' 'print.summary.bvar.R' 'summary.bvec.R' 'print.summary.bvec.R' 'ssvs_prior.R' 'summary.bvarlist.R' 'summary.dfm.R' 'thin.bvar.R' 'thin.bvarlist.R' 'thin.bvec.R' 'thin.dfm.R' 'tvpribbon.R' 'zzz.R' |
NeedsCompilation: | yes |
Packaged: | 2024-01-08 08:34:17 UTC; franz |
Author: | Franz X. Mohr [aut, cre] (ORCiD: 0009-0003-8890-7781) |
Maintainer: | Franz X. Mohr <franz.x.mohr@outlook.com> |
Repository: | CRAN |
Date/Publication: | 2024-01-08 12:10:02 UTC |
bvartools: Bayesian Inference of Vector Autoregressive and Error Correction Models
Description
Assists in the set-up of algorithms for Bayesian inference of vector autoregressive (VAR) and error correction (VEC) models. Functions for posterior simulation, forecasting, impulse response analysis and forecast error variance decomposition are largely based on the introductory texts of Chan, Koop, Poirier and Tobias (2019, ISBN: 9781108437493), Koop and Korobilis (2010) doi:10.1561/0800000013 and Luetkepohl (2006, ISBN: 9783540262398).
Author(s)
Maintainer: Franz X. Mohr franz.x.mohr@outlook.com (0009-0003-8890-7781)
See Also
Useful links:
Add Priors to Bayesian Models A generic function used to generate prior specifications for a list of models. The function invokes particular methods which depend on the class of the first argument.
Description
Add Priors to Bayesian Models
A generic function used to generate prior specifications for a list of models. The function invokes particular methods which depend on the class of the first argument.
Usage
add_priors(object, ...)
Arguments
object |
an object of class |
... |
arguments passed forward to method. |
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Obtain data matrices
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
# Chosen number of iterations and burn-in draws should be much higher.
# Add prior specifications
model <- add_priors(model)
Add Priors for a Vector Autoregressive Models
Description
Adds prior specifications to a list of models, which was produced by
function gen_var
.
Usage
## S3 method for class 'bvarmodel'
add_priors(
object,
coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01),
sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04),
ssvs = NULL,
bvs = NULL,
...
)
Arguments
object |
a list, usually, the output of a call to |
coef |
a named list of prior specifications for the coefficients of the
models. For the default specification all prior means are set to zero and the diagonal elements of
the inverse prior variance-covariance matrix are set to 1 for coefficients corresponding to non-deterministic
and structural terms. For deterministic coefficients the prior variances are set to 10 via |
sigma |
a named list of prior specifications for the error variance-covariance matrix of the models. For the default specification of an inverse Wishart distribution the prior degrees of freedom are set to the number of endogenous variables and the prior variances to 1. See 'Details'. |
ssvs |
optional; a named list of prior specifications for the SSVS algorithm. Not allowed for TVP models. See 'Details'. |
bvs |
optional; a named list of prior specifications for the BVS algorithm. See 'Details'. |
... |
further arguments passed to or from other methods. |
Details
The arguments of the function require named lists. Possible specifications are described in the following. Note that it is important to specify the priors in the correct list. Otherwise, the provided specification will be disregarded and default values will be used.
Argument coef
can contain the following elements
v_i
a numeric specifying the prior precision of the coefficients. Default is 1.
v_i_det
a numeric specifying the prior precision of coefficients corresponding to deterministic terms. Default is 0.1.
coint_var
a logical specifying whether the prior mean of the first own lag of an endogenous variable should be set to 1. Default is
FALSE
.const
a numeric or character specifying the prior mean of coefficients, which correspond to the intercept. If a numeric is provided, all prior means are set to this value. If
const = "mean"
, the mean of the respective endogenous variable is used as prior mean. Ifconst = "first"
, the first values of the respective endogenous variable is used as prior mean.minnesota
a list of length 4 containing parameters for the calculation of the Minnesota prior, where the element names must be
kappa0
,kappa1
,kappa2
andkappa3
. For the endogenous variablei
the prior variance of thel
th lag of regressorj
is obtained as\frac{\kappa_{0}}{l^2} \textrm{ for own lags of endogenous variables,}
\frac{\kappa_{0} \kappa_{1}}{l^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for endogenous variables other than own lags,}
\frac{\kappa_{0} \kappa_{2}}{(l+1)^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for exogenous variables,}
\kappa_{0} \kappa_{3} \sigma_{i}^2 \textrm{ for deterministic terms,}
where
\sigma_{i}
is the residual standard deviation of variablei
of an unrestricted LS estimate. For exogenous variables\sigma_{i}
is the sample standard deviation.max_var
a numeric specifying the maximum prior variance that is allowed for non-deterministic coefficients.
shape
a numeric specifying the prior shape parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 3.
rate
a numeric specifying the prior rate parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 0.0001.
rate_det
a numeric specifying the prior rate parameter of the error term of the state equation for coefficients, which correspond to deterministic terms. Only used for models with time varying parameters. Default is 0.01.
If minnesota
is specified, v_i
and v_i_det
are ignored.
Argument sigma
can contain the following elements:
df
an integer or character specifying the prior degrees of freedom of the error term. Only used, if the prior is inverse Wishart. Default is
"k"
, which indicates the amount of endogenous variables in the respective model."k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If an integer is provided, the degrees of freedom are set to this value in all models.scale
a numeric specifying the prior error variance of endogenous variables. Default is 1.
shape
a numeric or character specifying the prior shape parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with constant volatility the default is
"k"
, which indicates the amount of endogenous variables in the respective country model."k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If a numeric is provided, the shape parameters are set to this value in all models. For models with stochastic volatility this prior refers to the error variance of the state equation.rate
a numeric specifying the prior rate parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with stochastic volatility this prior refers to the error variance of the state equation.
mu
numeric of the prior mean of the initial state of the log-volatilities. Only used for models with time varying volatility.
v_i
numeric of the prior precision of the initial state of the log-volatilities. Only used for models with time varying volatility.
sigma_h
numeric of the initial draw for the variance of the log-volatilities. Only used for models with time varying volatility.
constant
numeric of the constant, which is added before taking the log of the squared errors. Only used for models with time varying volatility.
covar
logical indicating whether error covariances should be estimated. Only used in combination with an inverse gamma prior or stochastic volatility, for which
shape
andrate
must be specified.
df
and scale
must be specified for an inverse Wishart prior. shape
and rate
are required for an inverse gamma prior. For structural models or models with stochastic volatility
only a gamma prior specification is allowed.
Argument ssvs
can contain the following elements:
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
tau
a numeric vector of two elements containing the prior standard errors of restricted variables (
\tau_0
) as its first element and unrestricted variables (\tau_1
) as its second.semiautomatic
an numeric vector of two elements containing the factors by which the standard errors associated with an unconstrained least squares estimate of the model are multiplied to obtain the prior standard errors of restricted (
\tau_0
) and unrestricted (\tau_1
) variables, respectively. This is the semiautomatic approach described in George et al. (2008).covar
logical indicating if SSVS should also be applied to the error covariance matrix as in George et al. (2008).
exclude_det
logical indicating if deterministic terms should be excluded from the SSVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
Either tau
or semiautomatic
must be specified.
The argument bvs
can contain the following elements
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
covar
logical indicating if BVS should also be applied to the error covariance matrix.
exclude_det
logical indicating if deterministic terms should be excluded from the BVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
If either ssvs$minnesota
or bvs$minnesota
is specified, prior inclusion probabilities
are calculated in a Minnesota-like fashion as
\frac{\kappa_1}{l} | for own lags of endogenous variables, |
\frac{\kappa_2}{l} | for other endogenous variables, |
\frac{\kappa_3}{1 + l} | for exogenous variables, |
\kappa_{4} | for deterministic variables, |
for lag l
with \kappa_1
, \kappa_2
, \kappa_3
, \kappa_4
as the first, second,
third and forth element in ssvs$minnesota
or bvs$minnesota
, respectively.
Value
A list of country models.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
data("e1")
e1 <- diff(log(e1)) * 100
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
model <- add_priors(model)
Add Priors for Vector Error Correction Models
Description
Adds prior specifications to a list of models, which was produced by
function gen_vec
.
Usage
## S3 method for class 'bvecmodel'
add_priors(
object,
coef = list(v_i = 1, v_i_det = 0.1, shape = 3, rate = 1e-04, rate_det = 0.01),
coint = list(v_i = 0, p_tau_i = 1, shape = 3, rate = 1e-04, rho = 0.999),
sigma = list(df = "k", scale = 1, mu = 0, v_i = 0.01, sigma_h = 0.05, constant = 1e-04),
ssvs = NULL,
bvs = NULL,
...
)
Arguments
object |
a list, usually, the output of a call to |
coef |
a named list of prior specifications for coefficients that do not
determine the cointegration space. For the default specification all prior means are set to zero
and the diagonal elements of the inverse prior variance-covariance matrix are set to 1
for coefficients corresponding to non-deterministic terms. For deterministic coefficients the prior
variances are set to 10 by |
coint |
a named list of prior specifications for coefficients determining the cointegration space of VEC models. See 'Details'. |
sigma |
a named list of prior specifications for the error variance-covariance matrix of the models. For the default specification of an inverse Wishart distribution the prior degrees of freedom are set to the number of endogenous variables plus the rank of the cointegration matrix. The prior variance is to 1. See 'Details'. |
ssvs |
optional; a named list of prior specifications for the SSVS algorithm. Not allowed for TVP models. See 'Details'. |
bvs |
optional; a named list of prior specifications for the BVS algorithm. See 'Details'. |
... |
further arguments passed to or from other methods. |
Details
The arguments of the function require named lists. Possible specifications are described in the following. Note that it is important to specify the priors in the correct list. Otherwise, the provided specification will be disregarded and default values will be used.
Argument coef
contains the following elements
v_i
a numeric specifying the prior precision of the coefficients. Default is 1.
v_i_det
a numeric specifying the prior precision of coefficients that correspond to deterministic terms. Default is 0.1.
const
a character specifying the prior mean of coefficients, which correspond to the intercept. If
const = "mean"
, the means of the series of endogenous variables are used as prior means. Ifconst = "first"
, the first values of the series of endogenous variables are used as prior means.minnesota
a list of length 4 containing parameters for the calculation of the Minnesota prior, where the element names must be
kappa0
,kappa1
,kappa2
andkappa3
. For the endogenous variablei
the prior variance of thel
th lag of regressorj
is obtained as\frac{\kappa_{0}}{l^2} \textrm{ for own lags of endogenous variables,}
\frac{\kappa_{0} \kappa_{1}}{l^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for endogenous variables other than own lags,}
\frac{\kappa_{0} \kappa_{2}}{(l+1)^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for exogenous variables,}
\kappa_{0} \kappa_{3} \sigma_{i}^2 \textrm{ for deterministic terms,}
where
\sigma_{i}
is the residual standard deviation of variablei
of an unrestricted LS estimate. For exogenous variables\sigma_{i}
is the sample standard deviation.The function only provides priors for the non-cointegration part of the model. However, the residual standard errors
\sigma_i
are based on an unrestricted LS regression of the endogenous variables on the error correction term and the non-cointegration regressors.max_var
a numeric specifying the maximum prior variance that is allowed for non-deterministic coefficients.
shape
a numeric specifying the prior shape parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 3.
rate
a numeric specifying the prior rate parameter of the error term of the state equation. Only used for models with time varying parameters. Default is 0.0001.
rate_det
a numeric specifying the prior rate parameter of the error term of the state equation for coefficients, which correspond to deterministic terms. Only used for models with time varying parameters. Default is 0.01.
If minnesota
is specified, elements v_i
and v_i_det
are ignored.
Argument coint
can contain the following elements:
v_i
numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. Default is 0.
p_tau_i
numeric of the diagonal elements of the inverse prior matrix of the central location of the cointegration space
sp(\beta)
. Default is 1.shape
an integer specifying the prior degrees of freedom of the error term of the state equation of the loading matrix
\alpha
. Default is 3.rate
a numeric specifying the prior variance of error term of the state equation of the loading matrix
\alpha
. Default is 0.0001.rho
a numeric specifying the autocorrelation coefficient of the state equation of
\beta
. It must be smaller than 1. Default is 0.999. Note that in contrast to Koop et al. (2011)\rho
is not drawn in the Gibbs sampler of this package yet.
Argument sigma
can contain the following elements:
df
an integer or character specifying the prior degrees of freedom of the error term. Only used, if the prior is inverse Wishart. Default is
"k"
, which indicates the amount of endogenous variables in the respective model."k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If an integer is provided, the degrees of freedom are set to this value in all models. In all cases the rankr
of the cointegration matrix is automatically added.scale
a numeric specifying the prior error variance of endogenous variables. Default is 1.
shape
a numeric or character specifying the prior shape parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with constant volatility the default is
"k"
, which indicates the amount of endogenous variables in the respective country model."k + 3"
can be used to set the prior to the amount of endogenous variables plus 3. If a numeric is provided, the shape parameters are set to this value in all models. For models with stochastic volatility this prior refers to the error variance of the state equation.rate
a numeric specifying the prior rate parameter of the error term. Only used, if the prior is inverse gamma or if time varying volatilities are estimated. For models with stochastic volatility this prior refers to the error variance of the state equation.
mu
numeric of the prior mean of the initial state of the log-volatilities. Only used for models with time varying volatility.
v_i
numeric of the prior precision of the initial state of the log-volatilities. Only used for models with time varying volatility.
sigma_h
numeric of the initial draw for the variance of the log-volatilities. Only used for models with time varying volatility.
constant
numeric of the constant, which is added before taking the log of the squared errors. Only used for models with time varying volatility.
covar
logical indicating whether error covariances should be estimated. Only used in combination with an inverse gamma prior or stochastic volatility, for which
shape
andrate
must be specified.
df
and scale
must be specified for an inverse Wishart prior. shape
and rate
are required for an inverse gamma prior. For structural models or models with stochastic volatility
only a gamma prior specification is allowed.
Argument ssvs
can contain the following elements:
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
tau
a numeric vector of two elements containing the prior standard errors of restricted variables (
\tau_0
) as its first element and unrestricted variables (\tau_1
) as its second.semiautomatic
an numeric vector of two elements containing the factors by which the standard errors associated with an unconstrained least squares estimate of the model are multiplied to obtain the prior standard errors of restricted (
\tau_0
) and unrestricted (\tau_1
) variables, respectively. This is the semiautomatic approach described in George et al. (2008).covar
logical indicating if SSVS should also be applied to the error covariance matrix as in George et al. (2008).
exclude_det
logical indicating if deterministic terms should be excluded from the SSVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
Either tau
or semiautomatic
must be specified.
The argument bvs
can contain the following elements
inprior
a numeric between 0 and 1 specifying the prior probability of a variable to be included in the model.
covar
logical indicating if BVS should also be applied to the error covariance matrix.
exclude_det
logical indicating if deterministic terms should be excluded from the BVS algorithm.
minnesota
a numeric vector of length 4 containing parameters for the calculation of the Minnesota-like inclusion priors. See below.
If either ssvs$minnesota
or bvs$minnesota
is specified, prior inclusion probabilities
are calculated in a Minnesota-like fashion as
\frac{\kappa_1}{l} | for own lags of endogenous variables, |
\frac{\kappa_2}{l} | for other endogenous variables, |
\frac{\kappa_3}{1 + l} | for exogenous variables, |
\kappa_{4} | for deterministic variables, |
for lag l
with \kappa_1
, \kappa_2
, \kappa_3
, \kappa_4
as the first, second,
third and forth element in ssvs$minnesota
or bvs$minnesota
, respectively.
Value
A list of models.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Get data
data("e6")
# Create model
model <- gen_vec(e6, p = 4, r = 1,
const = "unrestricted", seasonal = "unrestricted",
iterations = 100, burnin = 10)
# Chosen number of iterations and burnin should be much higher.
# Add priors
model <- add_priors(model)
Add Priors to Dynamic Factor Model
Description
Adds prior specifications to a list of models, which was produced by
function gen_dfm
.
Usage
## S3 method for class 'dfmodel'
add_priors(
object,
lambda = list(v_i = 0.01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = 0.01),
sigma_v = list(shape = 5, rate = 4),
...
)
Arguments
object |
a list, usually, the output of a call to |
lambda |
a named list of prior specifications for the factor loadings in the measurement equation. For the default specification the diagonal elements of the inverse prior variance-covariance matrix are set to 0.01. The variances need to be specified as precisions, i.e. as inverses of the variances. |
sigma_u |
a named list of prior specifications for the error variance-covariance matrix. See 'Details'. |
a |
a named list of prior specifications for the coefficients of the transition equation. For the default specification the diagonal elements of the inverse prior variance-covariance matrix are set to 0.01. The variances need to be specified as precisions, i.e. as inverses of the variances. |
sigma_v |
a named list of prior specifications for the error variance-covariance matrix. See 'Details'. |
... |
further arguments passed to or from other methods. |
Details
Argument lambda
can only contain the element v_i
, which is a numeric specifying the prior
precision of the loading factors of the measurement equation. Default is 0.01.
The function assumes an inverse gamma prior for the errors of the measurement equation.
Argument sigma_u
can contain the following elements:
shape
a numeric or character specifying the prior shape parameter of the error terms of the measurement equation. Default is 5.
rate
a numeric specifying the prior rate parameter of the error terms of the measurement equation. Default is 4.
Argument a
can only contain the element v_i
, which is a numeric specifying the prior
precision of the coefficients of the transition equation. Default is 0.01.
The function assumes an inverse gamma prior for the errors of the transition equation.
Argument sigma_v
can contain the following elements:
shape
a numeric or character specifying the prior shape parameter of the error terms of the transition equation. Default is 5.
rate
a numeric specifying the prior rate parameter of the error terms of the transition equation. Default is 4.
Value
A list of models.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 5000, burnin = 1000)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model,
lambda = list(v_i = .01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = .01),
sigma_v = list(shape = 5, rate = 4))
FRED-QD data
Description
The data set contains quarterly time series for 196 US macroeconomic variables from 1959Q3 to 2015Q3. It was produced from file "freddata_Q.csv" of the data sets associated with Chan, Koop, Poirier and Tobias (2019). Raw data are available at https://web.ics.purdue.edu/~jltobias/second_edition/Chapter18/code_for_exercise_4/freddata_Q.csv.
Usage
data("bem_dfmdata")
Format
A named time-series object with 225 rows and 196 variables. A detailed explanation of the variables can be found in McCracken and Ng (2016).
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
McCracken, M. W., & Ng, S. (2016). FRED-MD: A monthly database for macroeconomic research. Journal of Business & Economic Statistics 34(4), 574-589. doi:10.1080/07350015.2015.1086655
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 |
x |
an object of class |
A0 |
either a |
A |
either a |
B |
either a |
C |
either a |
Sigma |
a |
ci |
a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95. |
type |
either |
... |
additional arguments. |
object |
an object of class |
n.ahead |
number of steps ahead at which to predict. |
new_x |
an object of class |
new_d |
a matrix of new deterministic variables. Must have |
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 |
x |
a |
A0 |
an |
A0_lambda |
an |
A0_sigma |
an |
A |
an |
A_lambda |
an |
A_sigma |
an |
B |
an |
B_lambda |
an |
B_sigma |
an |
C |
an |
C_lambda |
an |
C_sigma |
an |
Sigma |
an |
Sigma_lambda |
an |
Sigma_sigma |
an |
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)
Posterior Simulation for BVAR Models
Description
Produces draws from the posterior distributions of Bayesian VAR models.
Usage
bvarpost(object)
Arguments
object |
an object of class |
Details
The function implements commonly used posterior simulation algorithms for Bayesian VAR models with both constant and time varying parameters (TVP) as well as stochastic volatility. It can produce posterior draws for standard BVAR models with independent normal-Wishart priors, which can be augmented by stochastic search variable selection (SSVS) as proposed by Geroge et al. (2008) or Bayesian variable selection (BVS) as proposed in Korobilis (2013). Both SSVS or BVS can also be applied to the covariances of the error term.
The implementation follows the descriptions in Chan et al. (2019), George et al. (2008) and Korobilis (2013). For all approaches the SUR form of a VAR model is used to obtain posterior draws. The algorithm is implemented in C++ to reduce calculation time.
The function also supports structural BVAR models, where the structural coefficients are estimated from contemporary endogenous variables, which corresponds to the so-called (A-model). Currently, only specifications are supported, where the structural matrix contains ones on its diagonal and all lower triangular elements are freely estimated. Since posterior draws are obtained based on the SUR form of the VAR model, the structural coefficients are drawn jointly with the other coefficients.
Value
An object of class "bvar"
.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Examples
# Get data
data("e1")
e1 <- diff(log(e1)) * 100
# Create model
model <- gen_var(e1, p = 2, deterministic = "const",
iterations = 50, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add priors
model <- add_priors(model)
# Obtain posterior draws
object <- bvarpost(model)
Bayesian Vector Error Correction Objects
Description
'bvec' is used to create objects of class "bvec"
.
A plot function for objects of class "bvec"
.
Usage
bvec(
y,
alpha = NULL,
beta = NULL,
beta_x = NULL,
beta_d = NULL,
r = NULL,
Pi = NULL,
Pi_x = NULL,
Pi_d = NULL,
w = NULL,
w_x = NULL,
w_d = NULL,
Gamma = NULL,
Upsilon = NULL,
C = NULL,
x = NULL,
x_x = NULL,
x_d = NULL,
A0 = NULL,
Sigma = NULL,
data = NULL,
exogen = NULL
)
## S3 method for class 'bvec'
plot(x, ci = 0.95, type = "hist", ...)
Arguments
y |
a time-series object of differenced endogenous variables,
usually, a result of a call to |
alpha |
a |
beta |
a |
beta_x |
a |
beta_d |
a |
r |
an integer of the rank of the cointegration matrix. |
Pi |
a |
Pi_x |
a |
Pi_d |
a |
w |
a time-series object of lagged endogenous variables in levels, which enter the
cointegration term, usually, a result of a call to |
w_x |
a time-series object of lagged unmodelled, non-deterministic variables in levels, which enter the
cointegration term, usually, a result of a call to |
w_d |
a time-series object of deterministic terms, which enter the
cointegration term, usually, a result of a call to |
Gamma |
a |
Upsilon |
an |
C |
an |
x |
an object of class |
x_x |
a time-series object of |
x_d |
a time-series object of |
A0 |
either a |
Sigma |
a |
data |
the original time-series object of endogenous variables. |
exogen |
the original time-series object of unmodelled variables. |
ci |
interval used to calculate credible bands for time-varying parameters. |
type |
either |
... |
further graphical parameters. |
Details
For the vector error correction model with unmodelled exogenous variables (VECX)
A_0 \Delta y_t = \Pi^{+} \begin{pmatrix} y_{t-1} \\ x_{t-1} \\ d^{R}_{t-1} \end{pmatrix} +
\sum_{i = 1}^{p-1} \Gamma_i \Delta y_{t-i} +
\sum_{i = 0}^{s-1} \Upsilon_i \Delta x_{t-i} +
C^{UR} d^{UR}_t + u_t
the function collects the S
draws of a Gibbs sampler in a standardised object,
where \Delta y_t
is a K-dimensional vector of differenced endogenous variables
and A_0
is a K \times K
matrix of structural coefficients.
\Pi^{+} = \left[ \Pi, \Pi^{x}, \Pi^{d} \right]
is
the coefficient matrix of the error correction term, where
y_{t-1}
, x_{t-1}
and d^{R}_{t-1}
are the first lags of endogenous,
exogenous variables in levels and restricted deterministic terms, respectively.
\Pi
, \Pi^{x}
, and \Pi^{d}
are the corresponding coefficient matrices, respectively.
\Gamma_i
is a coefficient matrix of lagged differenced endogenous variabels.
\Delta x_t
is an M-dimensional vector of unmodelled, non-deterministic variables
and \Upsilon_i
its corresponding coefficient matrix. d_t
is an
N^{UR}
-dimensional vector of unrestricted deterministics and C^{UR}
the 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 alpha
, beta
,
Pi
, Pi_x
, Pi_d
, A0
, Gamma
, Ypsilon
,
C
and Sigma
have to correspond to the same MCMC iteration.
Value
An object of class "gvec"
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 time-series object of differenced endogenous variables. |
w |
a time-series object of lagged endogenous variables in levels, which enter the cointegration term. |
w_x |
a time-series object of lagged unmodelled, non-deterministic variables in levels, which enter the cointegration term. |
w_d |
a time-series object of deterministic terms, which enter the cointegration term. |
x |
a time-series object of |
x_x |
a time-series object of |
x_d |
a time-series object of |
A0 |
an |
A0_lambda |
an |
A0_sigma |
an |
alpha |
an |
beta |
an |
beta_x |
an |
beta_d |
an |
Pi |
an |
Pi_x |
an |
Pi_d |
an |
Gamma |
an |
Gamma_lamba |
an |
Gamma_sigma |
an |
Upsilon |
an |
Upsilon_lambda |
an |
Upsilon_sigma |
an |
C |
an |
C_lambda |
an |
C_sigma |
an |
Sigma |
an |
Sigma_lambda |
an |
Sigma_sigma |
an |
specifications |
a list containing information on the model specification. |
Examples
# Load data
data("e6")
# Generate model
data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted")
# Obtain data matrices
y <- t(data$data$Y)
w <- t(data$data$W)
x <- t(data$data$X)
# Reset random number generator for reproducibility
set.seed(1234567)
iterations <- 400 # Number of iterations of the Gibbs sampler
# Chosen number of iterations should be much higher, e.g. 30000.
burnin <- 100 # Number of burn-in draws
draws <- iterations + burnin
r <- 1 # Set rank
tt <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
k_w <- nrow(w) # Number of regressors in error correction term
k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms
k_alpha <- k * r # Number of elements in alpha
k_beta <- k_w * r # Number of elements in beta
k_gamma <- k * k_x
# Set uninformative priors
a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means
a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix
v_i <- 0
p_tau_i <- diag(1, k_w)
u_sigma_df_prior <- r # Prior degrees of freedom
u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix
u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom
# Initial values
beta <- matrix(c(1, -4), k_w, r)
u_sigma_i <- diag(1 / .0001, k)
g_i <- u_sigma_i
# Data containers
draws_alpha <- matrix(NA, k_alpha, iterations)
draws_beta <- matrix(NA, k_beta, iterations)
draws_pi <- matrix(NA, k * k_w, iterations)
draws_gamma <- matrix(NA, k_gamma, iterations)
draws_sigma <- matrix(NA, k^2, iterations)
# Start Gibbs sampler
for (draw in 1:draws) {
# Draw conditional mean parameters
temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i,
v_i = v_i, p_tau_i = p_tau_i, g_i = g_i,
gamma_mu_prior = a_mu_prior,
gamma_v_i_prior = a_v_i_prior)
alpha <- temp$alpha
beta <- temp$beta
Pi <- temp$Pi
gamma <- temp$Gamma
# Draw variance-covariance matrix
u <- y - Pi %*% w - matrix(gamma, k) %*% x
u_sigma_scale_post <- solve(tcrossprod(u) +
v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha))
u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k)
u_sigma <- solve(u_sigma_i)
# Update g_i
g_i <- u_sigma_i
# Store draws
if (draw > burnin) {
draws_alpha[, draw - burnin] <- alpha
draws_beta[, draw - burnin] <- beta
draws_pi[, draw - burnin] <- Pi
draws_gamma[, draw - burnin] <- gamma
draws_sigma[, draw - burnin] <- u_sigma
}
}
# Number of non-deterministic coefficients
k_nondet <- (k_x - 4) * k
# Generate bvec object
bvec_est <- bvec(y = data$data$Y, w = data$data$W,
x = data$data$X[, 1:6],
x_d = data$data$X[, 7:10],
Pi = draws_pi,
Gamma = draws_gamma[1:k_nondet,],
C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),],
Sigma = draws_sigma)
# Load data
data("e6")
# Generate model
model <- gen_vec(data = e6, p = 2, r = 1, const = "unrestricted",
iterations = 20, 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)
Transform a VEC Model to a VAR in Levels
Description
An object of class "bvec"
is transformed to a VAR in level representation.
Usage
bvec_to_bvar(object)
Arguments
object |
an object of class |
Value
An object of class "bvar"
.
References
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("e6")
# Generate model
data <- gen_vec(e6, p = 4, r = 1, const = "unrestricted", season = "unrestricted")
# Obtain data matrices
y <- t(data$data$Y)
w <- t(data$data$W)
x <- t(data$data$X)
# Reset random number generator for reproducibility
set.seed(1234567)
iterations <- 100 # Number of iterations of the Gibbs sampler
# Chosen number of iterations should be much higher, e.g. 30000.
burnin <- 100 # Number of burn-in draws
draws <- iterations + burnin
r <- 1 # Set rank
tt <- ncol(y) # Number of observations
k <- nrow(y) # Number of endogenous variables
k_w <- nrow(w) # Number of regressors in error correction term
k_x <- nrow(x) # Number of differenced regressors and unrestrictec deterministic terms
k_alpha <- k * r # Number of elements in alpha
k_beta <- k_w * r # Number of elements in beta
k_gamma <- k * k_x
# Set uninformative priors
a_mu_prior <- matrix(0, k_x * k) # Vector of prior parameter means
a_v_i_prior <- diag(0, k_x * k) # Inverse of the prior covariance matrix
v_i <- 0
p_tau_i <- diag(1, k_w)
u_sigma_df_prior <- r # Prior degrees of freedom
u_sigma_scale_prior <- diag(0, k) # Prior covariance matrix
u_sigma_df_post <- tt + u_sigma_df_prior # Posterior degrees of freedom
# Initial values
beta <- matrix(c(1, -4), k_w, r)
u_sigma_i <- diag(1 / .0001, k)
g_i <- u_sigma_i
# Data containers
draws_alpha <- matrix(NA, k_alpha, iterations)
draws_beta <- matrix(NA, k_beta, iterations)
draws_pi <- matrix(NA, k * k_w, iterations)
draws_gamma <- matrix(NA, k_gamma, iterations)
draws_sigma <- matrix(NA, k^2, iterations)
# Start Gibbs sampler
for (draw in 1:draws) {
# Draw conditional mean parameters
temp <- post_coint_kls(y = y, beta = beta, w = w, x = x, sigma_i = u_sigma_i,
v_i = v_i, p_tau_i = p_tau_i, g_i = g_i,
gamma_mu_prior = a_mu_prior,
gamma_v_i_prior = a_v_i_prior)
alpha <- temp$alpha
beta <- temp$beta
Pi <- temp$Pi
gamma <- temp$Gamma
# Draw variance-covariance matrix
u <- y - Pi %*% w - matrix(gamma, k) %*% x
u_sigma_scale_post <- solve(tcrossprod(u) +
v_i * alpha %*% tcrossprod(crossprod(beta, p_tau_i) %*% beta, alpha))
u_sigma_i <- matrix(rWishart(1, u_sigma_df_post, u_sigma_scale_post)[,, 1], k)
u_sigma <- solve(u_sigma_i)
# Update g_i
g_i <- u_sigma_i
# Store draws
if (draw > burnin) {
draws_alpha[, draw - burnin] <- alpha
draws_beta[, draw - burnin] <- beta
draws_pi[, draw - burnin] <- Pi
draws_gamma[, draw - burnin] <- gamma
draws_sigma[, draw - burnin] <- u_sigma
}
}
# Number of non-deterministic coefficients
k_nondet <- (k_x - 4) * k
# Generate bvec object
bvec_est <- bvec(y = data$data$Y, w = data$data$W,
x = data$data$X[, 1:6],
x_d = data$data$X[, 7:10],
Pi = draws_pi,
Gamma = draws_gamma[1:k_nondet,],
C = draws_gamma[(k_nondet + 1):nrow(draws_gamma),],
Sigma = draws_sigma)
# Thin posterior draws
bvec_est <- thin(bvec_est, thin = 5)
# Transfrom VEC output to VAR output
bvar_form <- bvec_to_bvar(bvec_est)
Posterior Simulation for BVEC Models
Description
Produces draws from the posterior distributions of Bayesian VEC models.
Usage
bvecpost(object)
Arguments
object |
an object of class |
Details
The function implements posterior simulation algorithms proposed in Koop et al. (2010)
and Koop et al. (2011), which place identifying restrictions on the cointegration space.
Both algorithms are able to employ Bayesian variable selection (BVS) as proposed in Korobilis (2013).
The algorithm of Koop et al. (2010) is also able to employ stochastic search variable selection (SSVS)
as proposed by Geroge et al. (2008).
Both SSVS and BVS can also be applied to the covariances of the error term. However, the algorithms
cannot be applied to cointegration related coefficients, i.e. to the loading matrix \alpha
or
the cointegration matrix beta
.
The implementation primarily follows the description in Koop et al. (2010). Chan et al. (2019), George et al. (2008) and Korobilis (2013) were used to implement the variable selection algorithms. For all approaches the SUR form of a VEC model is used to obtain posterior draws. The algorithm is implemented in C++ to reduce calculation time.
The function also supports structural BVEC models, where the structural coefficients are estimated from contemporary endogenous variables, which corresponds to the so-called (A-model). Currently, only specifications are supported, where the structural matrix contains ones on its diagonal and all lower triangular elements are freely estimated. Since posterior draws are obtained based on the SUR form of the VEC model, the structural coefficients are drawn jointly with the other coefficients. No identifying restrictions are made regarding the cointegration matrix.
Value
An object of class "bvec"
.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Examples
# Get data
data("e6")
# Create model
model <- gen_vec(e6, p = 4, r = 1,
const = "unrestricted", seasonal = "unrestricted",
iterations = 100, burnin = 10)
# Chosen number of iterations and burnin should be much higher.
# Add priors
model <- add_priors(model)
# Obtain posterior draws
object <- bvecpost(model)
Bayesian Variable Selection
Description
bvs
employs Bayesian variable selection as proposed by Korobilis (2013)
to produce a vector of inclusion parameters for the coefficient matrix
of a VAR model.
Usage
bvs(y, z, a, lambda, sigma_i, prob_prior, include = NULL)
Arguments
y |
a |
z |
a |
a |
an M-dimensional vector of parameter draws. If time varying parameters are used,
an |
lambda |
an |
sigma_i |
the inverse variance-covariance matrix. If the variance-covariance matrix
is time varying, a |
prob_prior |
an M-dimensional vector of prior inclusion probabilities. |
include |
an integer vector specifying the positions of variables, which should be
included in the BVS algorithm. If |
Details
The function employs Bayesian variable selection as proposed
by Korobilis (2013) to produce a vector of inclusion parameters, which are
the diagonal elements of the inclusion matrix \Lambda
for the VAR model
y_t = Z_t \Lambda a_t + u_t,
where u_t \sim N(0, \Sigma_{t})
.
y_t
is a K-dimensional vector of endogenous variables and
Z_t = x_t^{\prime} \otimes I_K
is a K \times M
matrix of regressors with
x_t
as a vector of regressors.
Value
A matrix of inclusion parameters on its diagonal.
References
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Examples
# Load data
data("e1")
data <- diff(log(e1)) * 100
# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
z <- temp$data$SUR
tt <- ncol(y)
m <- ncol(z)
# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(0.1, m)
# Prior for inclusion parameter
prob_prior <- matrix(0.5, m)
# Initial value of Sigma
sigma <- tcrossprod(y) / tt
sigma_i <- solve(sigma)
lambda <- diag(1, m)
z_bvs <- z %*% lambda
a <- post_normal_sur(y = y, z = z_bvs, sigma_i = sigma_i,
a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
lambda <- bvs(y = y, z = z, a = a, lambda = lambda,
sigma_i = sigma_i, prob_prior = prob_prior)
Bayesian Dynamic Factor Model Objects
Description
dfm
is used to create objects of class "dfm"
.
A plot function for objects of class "dfm"
.
Usage
dfm(x, lambda = NULL, fac, sigma_u = NULL, a = NULL, sigma_v = NULL)
## S3 method for class 'dfm'
plot(x, ci = 0.95, ...)
Arguments
x |
an object of class |
lambda |
an |
fac |
an |
sigma_u |
an |
a |
a |
sigma_v |
an |
ci |
interval used to calculate credible bands. |
... |
further graphical parameters. |
Details
The function produces a standardised object from S draws of a Gibbs sampler (after the burn-in phase) for the dynamic factor model (DFM) with measurement equation
x_t = \lambda f_t + u_t,
where
x_t
is an M \times 1
vector of observed variables,
f_t
is an N \times 1
vector of unobserved factors and
\lambda
is the corresponding M \times N
matrix of factor loadings.
u_t
is an M \times 1
error term.
The transition equation is
f_t = \sum_{i=1}^{p} A_i f_{t - i} + v_t,
where
A_i
is an N \times N
coefficient matrix and
v_t
is an N \times 1
error term.
Value
An object of class "dfm"
containing the following components, if specified:
x |
the standardised time-series object of observable variables. |
lambda |
an |
factor |
an |
sigma_u |
an |
a |
an |
sigma_v |
an |
specifications |
a list containing information on the model specification. |
Examples
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 20, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model,
lambda = list(v_i = .01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = .01),
sigma_v = list(shape = 5, rate = 4))
# Obtain posterior draws
object <- dfmpost(model)
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 20, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model,
lambda = list(v_i = .01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = .01),
sigma_v = list(shape = 5, rate = 4))
# Obtain posterior draws
object <- draw_posterior(model)
# Plot factors
plot(object)
Posterior Simulation for Dynamic Factor Models
Description
Produces draws from the posterior distributions of Bayesian dynamic factor models.
Usage
dfmpost(object)
Arguments
object |
an object of class |
Details
The function implements the posterior simulation algorithm for Bayesian dynamic factor models.
The implementation follows the description in Chan et al. (2019) and C++ is used to reduce calculation time.
Value
An object of class "dfm"
.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Examples
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 20, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model,
lambda = list(v_i = .01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = .01),
sigma_v = list(shape = 5, rate = 4))
# Obtain posterior draws
object <- dfmpost(model)
Posterior Simulation
Description
Forwards model input to posterior simulation functions. This is a generic function.
Usage
draw_posterior(object, ...)
Arguments
object |
a list of model specifications. Usually, the output of a call to
|
... |
arguments passed forward to method. |
Posterior Simulation
Description
Forwards model input to posterior simulation functions.
Usage
## S3 method for class 'bvarmodel'
draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
Arguments
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each model in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
Value
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
result in an object of class "bvar"
.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate model
model <- gen_var(e1, p = 1:2, 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)
Posterior Simulation for Vector Error Correction Models
Description
Forwards model input to posterior simulation functions for vector error correction models.
Usage
## S3 method for class 'bvecmodel'
draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
Arguments
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each list element in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
Value
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
be "bvec"
.
References
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224–242. doi:10.1080/07474930903382208
Koop, G., León-González, R., & Strachan R. W. (2011). Bayesian inference in a time varying cointegration model. Journal of Econometrics, 165(2), 210–220. doi:10.1016/j.jeconom.2011.07.007
Examples
# Load data
data("e6")
e6 <- e6 * 100
# Generate model
model <- gen_vec(e6, p = 1, r = 1, const = "restricted",
iterations = 10, 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)
Posterior Simulation
Description
Forwards model input to posterior simulation functions.
Usage
## S3 method for class 'dfmodel'
draw_posterior(object, FUN = NULL, mc.cores = NULL, ...)
Arguments
object |
a list of model specifications, which should be passed on
to function |
FUN |
the function to be applied to each list element in argument |
mc.cores |
the number of cores to use, i.e. at most how many child processes will be run simultaneously. The option is initialized from environment variable MC_CORES if set. Must be at least one, and parallelization requires at least two cores. |
... |
further arguments passed to or from other methods. |
Value
For multiple models a list of objects of class bvarlist
.
For a single model the object has the class of the output of the applied posterior
simulation function. In case the package's own functions are used, this will
be "bvar"
, "bvec"
or "dfm"
.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate model
model <- gen_var(e1, p = 1:2, 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)
West German economic time series data
Description
The data set contains quarterly, seasonally adjusted time series for West German fixed investment, disposable income, and consumption expenditures in billions of DM from 1960Q1 to 1982Q4. It was produced from file E1 of the data sets associated with Lütkepohl (2007). Raw data are available at http://www.jmulti.de/download/datasets/e1.dat and were originally obtained from Deutsche Bundesbank.
Usage
data("e1")
Format
A named time-series object with 92 rows and 3 variables:
- invest
fixed investment.
- income
disposable income.
- cons
consumption expenditures.
References
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
German interest and inflation rate data
Description
The data set contains quarterly, seasonally unadjusted time series for German long-term interest and inflation rates from 1972Q2 to 1998Q4. It was produced from file E6 of the data sets associated with Lütkepohl (2007). Raw data are available at http://www.jmulti.de/download/datasets/e6.dat and were originally obtained from Deutsche Bundesbank and Deutsches Institut für Wirtschaftsforschung.
Usage
data("e6")
Format
A named time-series object with 107 rows and 2 variables:
- R
nominal long-term interest rate (Umlaufsrendite).
- Dp
\Delta
log of GDP deflator.
Details
The data cover West Germany until 1990Q2 and all of Germany aferwards. The values refer to the last month of a quarter.
References
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Forecast Error Variance Decomposition A generic function used to calculate forecast error varianc decompositions.
Description
A plot function for objects of class "bvarfevd".
Usage
fevd(object, ...)
## S3 method for class 'bvarfevd'
plot(x, ...)
Arguments
object |
an object of class |
... |
further graphical parameters. |
x |
an object of class "bvarfevd", usually, a result of a call to |
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# 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)
# Obtain FEVD
vd <- fevd(object, response = "cons")
# Plot
plot(vd)
Forecast Error Variance Decomposition
Description
Produces the forecast error variance decomposition of a Bayesian VAR model.
Usage
## S3 method for class 'bvar'
fevd(
object,
response = NULL,
n.ahead = 5,
type = "oir",
normalise_gir = FALSE,
period = NULL,
...
)
Arguments
object |
an object of class |
response |
name of the response variable. |
n.ahead |
number of steps ahead. |
type |
type of the impulse responses used to calculate forecast error variable decompositions.
Possible choices are orthogonalised |
normalise_gir |
logical. Should the GIR-based FEVD be normalised? |
period |
integer. Index of the period, for which the variance decomposition should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
Details
The function produces forecast error variance decompositions (FEVD) for the VAR model
A_0 y_t = \sum_{i = 1}^{p} A_{i} y_{t-i} + u_t,
with u_t \sim N(0, \Sigma)
. For non-structural models matrix A_0
is set to the identiy matrix
and can therefore be omitted, where not relevant.
If the FEVD is based on the orthogonalised impulse resonse (OIR), the FEVD will be calculated as
\omega^{OIR}_{jk, h} = \frac{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i P e_k )^2}{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i \Sigma \Phi_i^{\prime} e_j )},
where \Phi_i
is the forecast error impulse response for the i
th period,
P
is the lower triangular Choleski decomposition of the variance-covariance
matrix \Sigma
, e_j
is a selection vector for the response variable and
e_k
a selection vector for the impulse variable.
If type = "sir"
, the structural FEVD will be
calculated as
\omega^{SIR}_{jk, h} = \frac{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i A_0^{-1} e_k )^2}{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i A_0^{-1} A_0^{-1\prime} \Phi_i^{\prime} e_j )},
where \sigma_{jj}
is the diagonal element of the j
th variable of the variance covariance matrix.
If type = "gir"
, the generalised FEVD will be
calculated as
\omega^{GIR}_{jk, h} = \frac{\sigma^{-1}_{jj} \sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i \Sigma e_k )^2}{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i \Sigma \Phi_i^{\prime} e_j )},
where \sigma_{jj}
is the diagonal element of the j
th variable of the variance covariance matrix.
If type = "sgir"
, the structural generalised FEVD will be
calculated as
\omega^{SGIR}_{jk, h} = \frac{\sigma^{-1}_{jj} \sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i A_0^{-1} \Sigma e_k )^2}{\sum_{i = 0}^{h-1} (e_j^{\prime} \Phi_i A_0^{-1} \Sigma A_0^{-1\prime} \Phi_i^{\prime} e_j )}
.
Since GIR-based FEVDs do not add up to unity, they can be normalised by setting normalise_gir = TRUE
.
Value
A time-series object of class "bvarfevd"
.
References
Lütkepohl, H. (2006). 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, 17-29.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate models
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
# Add priors
model <- add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
# Obtain FEVD
vd <- fevd(object, response = "cons")
# Plot FEVD
plot(vd)
Dynamic Factor Model Input
Description
gen_dfm
produces the input for the estimation of a dynamic factor model (DFM).
Usage
gen_dfm(x, p = 2, n = 1, iterations = 50000, burnin = 5000)
Arguments
x |
a time-series object of stationary endogenous variables. |
p |
an integer vector of the lag order of the measurement equation. See 'Details'. |
n |
an integer vector of the number of factors. See 'Details'. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
Details
The function produces the variable matrices of dynamic factor models (DFM) with measurement equation
x_t = \lambda f_t + u_t,
where
x_t
is an M \times 1
vector of observed variables,
f_t
is an N \times 1
vector of unobserved factors and
\lambda
is the corresponding M \times N
matrix of factor loadings.
u_t
is an M \times 1
error term.
The transition equation is
f_t = \sum_{i=1}^{p} A_i f_{t - i} + v_t,
where
A_i
is an N \times N
coefficient matrix and
v_t
is an N \times 1
error term.
If integer vectors are provided as arguments p
or n
, the function will
produce a distinct model for all possible combinations of those specifications.
Value
An object of class 'dfmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
References
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2007). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 5000, burnin = 1000)
Vector Autoregressive Model Input
Description
gen_var
produces the input for the estimation of a vector autoregressive (VAR) model.
Usage
gen_var(
data,
p = 2,
exogen = NULL,
s = NULL,
deterministic = "const",
seasonal = FALSE,
structural = FALSE,
tvp = FALSE,
sv = FALSE,
fcst = NULL,
iterations = 50000,
burnin = 5000
)
Arguments
data |
a time-series object of endogenous variables. |
p |
an integer vector of the lag order (default is |
exogen |
an optional time-series object of external regressors. |
s |
an optional integer vector of the lag order of the external regressors (default is |
deterministic |
a character specifying which deterministic terms should
be included. Available values are |
seasonal |
logical. If |
structural |
logical indicating whether data should be prepared for the estimation of a structural VAR model. |
tvp |
logical indicating whether the model parameters are time varying. |
sv |
logical indicating whether time varying error variances should be estimated by employing a stochastic volatility algorithm. |
fcst |
integer. Number of observations saved for forecasting evaluation. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
Details
The function produces the data matrices for vector autoregressive (VAR) models, which can also include unmodelled, non-deterministic variables:
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,
where
y_t
is a K-dimensional vector of endogenous variables,
A_0
is a K \times K
coefficient matrix of contemporaneous endogenous variables,
A_i
is a K \times K
coefficient matrix of endogenous variables,
x_t
is an M-dimensional vector of exogenous regressors and
B_i
its corresponding K \times M
coefficient matrix.
D_t
is an N-dimensional vector of deterministic terms and
C
its corresponding K \times N
coefficient matrix.
p
is the lag order of endogenous variables, s
is the lag
order of exogenous variables, and u_t
is an error term.
If an integer vector is provided as argument p
or s
, the function will
produce a distinct model for all possible combinations of those specifications.
If tvp
is TRUE
, the respective coefficients
of the above model are assumed to be time varying. If sv
is TRUE
,
the error covariance matrix is assumed to be time varying.
Value
An object of class 'bvarmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
References
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2019). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("e1")
e1 <- diff(log(e1))
# Generate model data
data <- gen_var(e1, p = 0:2, deterministic = "const")
Vector Error Correction Model Input
Description
gen_vec
produces the input for the estimation of a vector error correction (VEC) model.
Usage
gen_vec(
data,
p = 2,
exogen = NULL,
s = 2,
r = NULL,
const = NULL,
trend = NULL,
seasonal = NULL,
structural = FALSE,
tvp = FALSE,
sv = FALSE,
fcst = NULL,
iterations = 50000,
burnin = 5000
)
Arguments
data |
a time-series object of endogenous variables. |
p |
an integer vector of the lag order of the series in the (levels) VAR. Thus, the
resulting model's lag will be |
exogen |
an optional time-series object of external regressors. |
s |
an optional integer vector of the lag order of the exogenous variables of the series
in the (levels) VAR. Thus, the resulting model's lag will be |
r |
an integer vector of the cointegration rank. See 'Details'. |
const |
a character specifying whether a constant term enters the error correction
term ( |
trend |
a character specifying whether a trend term enters the error correction
term ( |
seasonal |
a character specifying whether seasonal dummies should be included in the error
correction term ( |
structural |
logical indicating whether data should be prepared for the estimation of a structural VAR model. |
tvp |
logical indicating whether the model parameters are time varying. |
sv |
logical indicating whether time varying error variances should be estimated by employing a stochastic volatility algorithm. |
fcst |
integer. Number of observations saved for forecasting evaluation. |
iterations |
an integer of MCMC draws excluding burn-in draws (defaults to 50000). |
burnin |
an integer of MCMC draws used to initialize the sampler (defaults to 5000). These draws do not enter the computation of posterior moments, forecasts etc. |
Details
The function produces the variable matrices of vector error correction (VEC) models, which can also include exogenous variables:
\Delta y_t = \Pi w_t + \sum_{i=1}^{p-1} \Gamma_{i} \Delta y_{t - i} +
\sum_{i=0}^{s-1} \Upsilon_{i} \Delta x_{t - i} +
C^{UR} d^{UR}_t + u_t,
where
\Delta y_t
is a K \times 1
vector of differenced endogenous variables,
w_t
is a (K + M + N^{R}) \times 1
vector of cointegration variables,
\Pi
is a K \times (K + M + N^{R})
matrix of cointegration parameters,
\Gamma_i
is a K \times K
coefficient matrix of endogenous variables,
\Delta x_t
is a M \times 1
vector of differenced exogenous regressors,
\Upsilon_i
is a K \times M
coefficient matrix of exogenous regressors,
d^{UR}_t
is a N \times 1
vector of deterministic terms, and
C^{UR}
is a K \times N^{UR}
coefficient matrix of deterministic terms
that do not enter the cointegration term.
p
is the lag order of endogenous variables and s
is the lag
order of exogenous variables of the corresponding VAR model.
u_t
is a K \times 1
error term.
If an integer vector is provided as argument p
, s
or r
, the function will
produce a distinct model for all possible combinations of those specifications.
If tvp
is TRUE
, the respective coefficients
of the above model are assumed to be time varying. If sv
is TRUE
,
the error covariance matrix is assumed to be time varying.
Value
An object of class 'bvecmodel'
, which contains the following elements:
data |
A list of data objects, which can be used for posterior simulation. Element
|
model |
A list of model specifications. |
References
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("e6")
# Generate model data
data <- gen_vec(e6, p = 4, const = "unrestricted", season = "unrestricted")
Prior Inclusion Probabilities
Description
Prior inclusion probabilities as required for stochastic search variable selection (SSVS) à la George et al. (2008) and Bayesian variable selection (BVS) à la Korobilis (2013).
Usage
inclusion_prior(
object,
prob = 0.5,
exclude_deterministics = TRUE,
minnesota_like = FALSE,
kappa = c(0.8, 0.5, 0.5, 0.8)
)
Arguments
object |
an object of class |
prob |
a numeric specifying the prior inclusion probability of all model parameters. |
exclude_deterministics |
logical. If |
minnesota_like |
logical. If |
kappa |
a numeric vector of four elements containing the prior inclusion probabilities
of coefficients that correspond to own lags of endogenous variables, to endogenous variables,
which do not correspond to own lags, to exogenous variables and deterministic terms, respectively.
Only used if |
Details
If minnesota_like = TRUE
, prior inclusion probabilities \underline{\pi}_1
are calculated as
\frac{\kappa_1}{r} | for own lags of endogenous variables, |
\frac{\kappa_2}{r} | for other endogenous variables, |
\frac{\kappa_3}{1 + r} | for exogenous variables, |
\kappa_{4} | for deterministic variables, |
for lag r
with \kappa_1
, \kappa_2
, \kappa_3
, \kappa_4
as the first, second,
third and forth element in kappa
, respectively.
For vector error correction models the function generates prior inclusion probabilities for differenced variables and unrestricted deterministc terms as described above. For variables in the error correction term prior inclusion probabilites are calculated as
\kappa_1 | fow own levels of endogenous variables, |
\kappa_2 | for levels of other endogenous variables, |
\kappa_3 | for levels of exogenous variables, |
\kappa_4 | for deterministic variables. |
Value
A list containing a matrix of prior inclusion probabilities and an integer vector specifying the positions of variables, which should be included in the variable selction algorithm.
References
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Korobilis, D. (2013). VAR forecasting using Bayesian variable selection. Journal of Applied Econometrics, 28(2), 204–230. doi:10.1002/jae.1271
Examples
# Prepare data
data("e1")
# Generate model input
object <- gen_var(e1)
# Obtain inclusion prior
pi_prior <- inclusion_prior(object)
Impulse Response Function A generic function used to calculate impulse response functions.
Description
A plot function for objects of class "bvarirf".
Usage
irf(x, ...)
## S3 method for class 'bvarirf'
plot(x, ...)
Arguments
x |
an object of class "bvarirf", usually, a result of a call to |
... |
further graphical parameters. |
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate model data
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model)
# Optain posterior draws
object <- draw_posterior(model)
# Calculate IR
ir <- irf(object, impulse = "invest", response = "cons")
# Plot IR
plot(ir)
Impulse Response Function
Description
Computes the impulse response coefficients of an object of class "bvar"
for
n.ahead
steps.
Usage
## S3 method for class 'bvar'
irf(
x,
impulse = NULL,
response = NULL,
n.ahead = 5,
ci = 0.95,
shock = 1,
type = "feir",
cumulative = FALSE,
keep_draws = FALSE,
period = NULL,
...
)
Arguments
x |
an object of class |
impulse |
name of the impulse variable. |
response |
name of the response variable. |
n.ahead |
number of steps ahead. |
ci |
a numeric between 0 and 1 specifying the probability mass covered by the credible intervals. Defaults to 0.95. |
shock |
size of the shock. |
type |
type of the impulse response. Possible choices are forecast error |
cumulative |
logical specifying whether a cumulative IRF should be calculated. |
keep_draws |
logical specifying whether the function should return all draws of
the posterior impulse response function. Defaults to |
period |
integer. Index of the period, for which the IR should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
Details
The function produces different types of impulse responses for the VAR model
A_0 y_t = \sum_{i = 1}^{p} A_{i} y_{t-i} + u_t,
with u_t \sim N(0, \Sigma)
.
Forecast error impulse responses \Phi_i
are obtained by recursions
\Phi_i = \sum_{j = 1}^{i} \Phi_{i-j} A_j, i = 1, 2,...,h
with \Phi_0 = I_K
.
Orthogonalised impulse responses \Theta^o_i
are calculated as \Theta^o_i = \Phi_i P
,
where P is the lower triangular Choleski decomposition of \Sigma
.
Structural impulse responses \Theta^s_i
are calculated as \Theta^s_i = \Phi_i A_0^{-1}
.
(Structural) Generalised impulse responses for variable j
, i.e. \Theta^g_ji
are calculated as
\Theta^g_{ji} = \sigma_{jj}^{-1/2} \Phi_i A_0^{-1} \Sigma e_j
, where \sigma_{jj}
is the variance
of the j^{th}
diagonal element of \Sigma
and e_i
is a selection vector containing
one in its j^{th}
element and zero otherwise. If the "bvar"
object does not contain draws
of A_0
, it is assumed to be an identity matrix.
Value
A time-series object of class "bvarirf"
and if keep_draws = TRUE
a simple matrix.
References
Lütkepohl, H. (2006). 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, 17-29.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# 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)
# Obtain IR
ir <- irf(object, impulse = "invest", response = "cons")
# Plot IR
plot(ir)
Durbin and Koopman Simulation Smoother
Description
An implementation of the Kalman filter and backward smoothing algorithm proposed by Durbin and Koopman (2002).
Usage
kalman_dk(y, z, sigma_u, sigma_v, B, a_init, P_init)
Arguments
y |
a |
z |
a |
sigma_u |
the constant |
sigma_v |
the constant |
B |
an |
a_init |
an M-dimensional vector of initial states. |
P_init |
an |
Details
The function uses algorithm 2 from Durbin and Koopman (2002) to produce
a draw of the state vector a_t
for t = 1,...,T
for a state space model
with measurement equation
y_t = Z_t a_t + u_t
and transition equation
a_{t + 1} = B_t a_{t} + v_t,
where u_t \sim N(0, \Sigma_{u,t})
and v_t \sim N(0, \Sigma_{v,t})
.
y_t
is a K-dimensional vector of endogenous variables and
Z_t = z_t^{\prime} \otimes I_K
is a K \times M
matrix of regressors with
z_t
as a vector of regressors.
The algorithm takes into account Jarociński (2015), where a possible missunderstanding in the implementation of the algorithm of Durbin and Koopman (2002) is pointed out. Following that note the function sets the mean of the initial state to zero in the first step of the algorithm.
Value
A M \times T+1
matrix of state vector draws.
References
Durbin, J., & Koopman, S. J. (2002). A simple and efficient simulation smoother for state space time series analysis. Biometrika, 89(3), 603–615.
Jarociński, M. (2015). A note on implementing the Durbin and Koopman simulation smoother. Computational Statistics and Data Analysis, 91, 1–3. doi:10.1016/j.csda.2015.05.001
Examples
# Load data
data("e1")
data <- diff(log(e1))
# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
z <- temp$data$SUR
k <- nrow(y)
tt <- ncol(y)
m <- ncol(z)
# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(0.1, m)
a_Q <- diag(.0001, m)
# Initial value of Sigma
sigma <- tcrossprod(y) / tt
sigma_i <- solve(sigma)
# Initial values for Kalman filter
y_init <- y * 0
a_filter <- matrix(0, m, tt + 1)
# Initialise the Kalman filter
for (i in 1:tt) {
y_init[, i] <- y[, i] - z[(i - 1) * k + 1:k,] %*% a_filter[, i]
}
a_init <- post_normal_sur(y = y_init, z = z, sigma_i = sigma_i,
a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
y_filter <- matrix(y) - z %*% a_init
y_filter <- matrix(y_filter, k) # Reshape
# Kalman filter and backward smoother
a_filter <- kalman_dk(y = y_filter, z = z, sigma_u = sigma,
sigma_v = a_Q, B = diag(1, m),
a_init = matrix(0, m), P_init = a_Q)
a <- a_filter + matrix(a_init, m, tt + 1)
Calculates the log-likelihood of a multivariate normal distribution.
Description
Calculates the log-likelihood of a multivariate normal distribution.
Usage
loglik_normal(u, sigma)
Arguments
u |
a |
sigma |
a |
Details
The log-likelihood is calculated for each vector in period t
as
-\frac{K}{2} \ln 2\pi - \frac{1}{2} \ln |\Sigma_t| -\frac{1}{2} u_t^\prime \Sigma_t^{-1} u_t
,
where u_t = y_t - \mu_t
.
Examples
# Load data
data("e1")
e1 <- diff(log(e1))
# Generate VAR model
data <- gen_var(e1, p = 2, deterministic = "const")
y <- t(data$data$Y)
x <- t(data$data$Z)
# LS estimate
ols <- tcrossprod(y, x) %*% solve(tcrossprod(x))
# Residuals
u <- y - ols %*% x # Residuals
# Covariance matrix
sigma <- tcrossprod(u) / ncol(u)
# Log-likelihood
loglik_normal(u = u, sigma = sigma)
Minnesota Prior
Description
Calculates the Minnesota prior for a VAR model.
Usage
minnesota_prior(
object,
kappa0 = 2,
kappa1 = 0.5,
kappa2 = NULL,
kappa3 = 5,
max_var = NULL,
coint_var = FALSE,
sigma = "AR"
)
Arguments
object |
an object of class |
kappa0 |
a numeric specifying the prior variance of coefficients that correspond to own lags of endogenous variables. |
kappa1 |
a numeric specifying the size of the prior variance of endogenous
variables, which do not correspond to own lags, relative to argument |
kappa2 |
a numeric specifying the size of the prior variance of non-deterministic exogenous
variables relative to argument |
kappa3 |
a numeric specifying the size of the prior variance of deterministic
terms relative to argument |
max_var |
a positive numeric specifying the maximum prior variance that is allowed for
coefficients of non-deterministic variables. If |
coint_var |
a logical specifying whether the model is a cointegrated VAR model, for which the prior means of first own lags should be set to one. |
sigma |
either |
Details
The function calculates the Minnesota prior of a VAR model. For the endogenous variable
i
the prior variance of the l
th lag of regressor j
is obtained as
\frac{\kappa_{0}}{l^2} \textrm{ for own lags of endogenous variables,}
\frac{\kappa_{0} \kappa_{1}}{l^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for endogenous variables other than own lags,}
\frac{\kappa_{0} \kappa_{2}}{(l + 1)^2} \frac{\sigma_{i}^2}{\sigma_{j}^2} \textrm{ for exogenous variables,}
\kappa_{0} \kappa_{3} \sigma_{i}^2 \textrm{ for deterministic terms,}
where \sigma_{i}
is the residual standard deviation of variable i
of an unrestricted
LS estimate. For exogenous variables \sigma_{i}
is the sample standard deviation.
For VEC models the function only provides priors for the non-cointegration part of the model. The
residual standard errors \sigma_i
are based on an unrestricted LS regression of the
endogenous variables on the error correction term and the non-cointegration regressors.
Value
A list containing a matrix of prior means and the precision matrix of the cofficients and the inverse variance-covariance matrix of the error term, which was obtained by an LS estimation.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias, J. L. (2020). Bayesian Econometric Methods (2nd ed.). Cambridge: University Press.
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("e1")
data <- diff(log(e1))
# Generate model input
object <- gen_var(data)
# Obtain Minnesota prior
prior <- minnesota_prior(object)
Plotting Posterior Draws of Bayesian VAR or VEC Models
Description
A plot function for objects of class "bvarlist"
.
Usage
## S3 method for class 'bvarlist'
plot(x, ci = 0.95, type = "hist", model = NULL, ...)
Arguments
x |
an object of class |
ci |
interval used to calculate credible bands for time-varying parameters. |
type |
either |
model |
numeric or integer indicating for which models in argument |
... |
further graphical parameters. |
Plotting Forecasts of BVAR Models
Description
A plot function for objects of class "bvarprd"
.
Usage
## S3 method for class 'bvarprd'
plot(x, n.pre = NULL, ...)
Arguments
x |
an object of class "bvarprd", usually, a result of a call to |
n.pre |
number of plotted observations that precede the forecasts. If |
... |
further graphical parameters. |
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate model data
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
# Add prior specifications
model <- add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
# Calculate forecasts
pred <- predict(object, new_d = rep(1, 10))
# Plot forecasts
plot(pred)
Posterior Draw for Cointegration Models
Description
Produces a draw of coefficients for cointegration models with a prior on the cointegration space as proposed in Koop et al. (2010) and a draw of non-cointegration coefficients from a normal density.
Usage
post_coint_kls(
y,
beta,
w,
sigma_i,
v_i,
p_tau_i,
g_i,
x = NULL,
gamma_mu_prior = NULL,
gamma_v_i_prior = NULL
)
Arguments
y |
a |
beta |
a |
w |
a |
sigma_i |
an inverse of the |
v_i |
a numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. |
p_tau_i |
an inverted |
g_i |
a |
x |
a |
gamma_mu_prior |
a |
gamma_v_i_prior |
an inverted |
Details
The function produces posterior draws of the coefficient
matrices \alpha
, \beta
and \Gamma
for the model
y_{t} = \alpha \beta^{\prime} w_{t-1} + \Gamma z_{t} + u_{t},
where y_{t}
is a K-dimensional vector of differenced endogenous variables.
w_{t}
is an M \times 1
vector of variables in the cointegration term,
which include lagged values of endogenous and exogenous variables in levels and
restricted deterministic terms. z_{t}
is an N-dimensional vector of
differenced endogenous and exogenous explanatory variabes as well as unrestricted
deterministic terms. The error term is u_t \sim \Sigma
.
Draws of the loading matrix \alpha
are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix is
\overline{V}_{\alpha} = \left[\left(v^{-1} (\beta^{\prime} P_{\tau}^{-1} \beta) \otimes G_{-1}\right) + \left(ZZ^{\prime} \otimes \Sigma^{-1} \right) \right]^{-1}
and the posterior mean by
\overline{\alpha} = \overline{V}_{\alpha} + vec(\Sigma^{-1} Y Z^{\prime}),
where Y
is a K \times T
matrix of differenced endogenous variables and
Z = \beta^{\prime} W
with W
as an M \times T
matrix of
variables in the cointegration term.
For a given prior mean vector \underline{\Gamma}
and prior covariance matrix \underline{V_{\Gamma}}
the posterior covariance matrix of non-cointegration coefficients in \Gamma
is obtained by
\overline{V}_{\Gamma} = \left[ \underline{V}_{\Gamma}^{-1} + \left(X X^{\prime} \otimes \Sigma^{-1} \right) \right]^{-1}
and the posterior mean by
\overline{\Gamma} = \overline{V}_{\Gamma} \left[ \underline{V}_{\Gamma}^{-1} \underline{\Gamma} + vec(\Sigma^{-1} Y X^{\prime}) \right],
where X
is an M \times T
matrix of
explanatory variables, which do not enter the cointegration term.
Draws of the cointegration matrix \beta
are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix of the unrestricted cointegration
matrix B
is
\overline{V}_{B} = \left[\left(A^{\prime} G^{-1} A \otimes v^{-1} P_{\tau}^{-1} \right) + \left(A^{\prime} \Sigma^{-1} A \otimes WW^{\prime} \right) \right]^{-1}
and the posterior mean by
\overline{B} = \overline{V}_{B} + vec(W Y_{B}^{-1} \Sigma^{-1} A),
where Y_{B} = Y - \Gamma X
and A = \alpha (\alpha^{\prime} \alpha)^{-\frac{1}{2}}
.
The final draws of \alpha
and \beta
are calculated using
\beta = B (B^{\prime} B)^{-\frac{1}{2}}
and
\alpha = A (B^{\prime} B)^{\frac{1}{2}}
.
Value
A named list containing the following elements:
alpha |
a draw of the |
beta |
a draw of the |
Pi |
a draw of the |
Gamma |
a draw of the |
References
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224-242. doi:10.1080/07474930903382208
Examples
# Load data
data("e6")
# Generate model data
temp <- gen_vec(e6, p = 1, r = 1)
y <- t(temp$data$Y)
ect <- t(temp$data$W)
k <- nrow(y) # Endogenous variables
tt <- ncol(y) # Number of observations
# Initial value of Sigma
sigma <- tcrossprod(y) / tt
sigma_i <- solve(sigma)
# Initial values of beta
beta <- matrix(c(1, -4), k)
# Draw parameters
coint <- post_coint_kls(y = y, beta = beta, w = ect, sigma_i = sigma_i,
v_i = 0, p_tau_i = diag(1, k), g_i = sigma_i)
Posterior Draw for Cointegration Models
Description
Produces a draw of coefficients for cointegration models in SUR form with a prior on the cointegration space as proposed in Koop et al. (2010) and a draw of non-cointegration coefficients from a normal density.
Usage
post_coint_kls_sur(
y,
beta,
w,
sigma_i,
v_i,
p_tau_i,
g_i,
x = NULL,
gamma_mu_prior = NULL,
gamma_v_i_prior = NULL,
svd = FALSE
)
Arguments
y |
a |
beta |
a |
w |
a |
sigma_i |
the inverse of the constant |
v_i |
a numeric between 0 and 1 specifying the shrinkage of the cointegration space prior. |
p_tau_i |
an inverted |
g_i |
a |
x |
a |
gamma_mu_prior |
a |
gamma_v_i_prior |
an inverted |
svd |
logical. If |
Details
The function produces posterior draws of the coefficient
matrices \alpha
, \beta
and \Gamma
for the model
y_{t} = \alpha \beta^{\prime} w_{t-1} + \Gamma z_{t} + u_{t},
where y_{t}
is a K-dimensional vector of differenced endogenous variables.
w_{t}
is an M \times 1
vector of variables in the cointegration term,
which include lagged values of endogenous and exogenous variables in levels and
restricted deterministic terms. z_{t}
is an N-dimensional vector of
differenced endogenous and exogenous explanatory variabes as well as unrestricted
deterministic terms. The error term is u_t \sim \Sigma
.
Draws of the loading matrix \alpha
are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix is
\overline{V}_{\alpha} = \left[\left(v^{-1} (\beta^{\prime} P_{\tau}^{-1} \beta) \otimes G_{-1}\right) + \left(ZZ^{\prime} \otimes \Sigma^{-1} \right) \right]^{-1}
and the posterior mean by
\overline{\alpha} = \overline{V}_{\alpha} + vec(\Sigma^{-1} Y Z^{\prime}),
where Y
is a K \times T
matrix of differenced endogenous variables and
Z = \beta^{\prime} W
with W
as an M \times T
matrix of
variables in the cointegration term.
For a given prior mean vector \underline{\Gamma}
and prior covariance matrix \underline{V_{\Gamma}}
the posterior covariance matrix of non-cointegration coefficients in \Gamma
is obtained by
\overline{V}_{\Gamma} = \left[ \underline{V}_{\Gamma}^{-1} + \left(X X^{\prime} \otimes \Sigma^{-1} \right) \right]^{-1}
and the posterior mean by
\overline{\Gamma} = \overline{V}_{\Gamma} \left[ \underline{V}_{\Gamma}^{-1} \underline{\Gamma} + vec(\Sigma^{-1} Y X^{\prime}) \right],
where X
is an M \times T
matrix of
explanatory variables, which do not enter the cointegration term.
Draws of the cointegration matrix \beta
are obtained using the prior on the cointegration space
as proposed in Koop et al. (2010). The posterior covariance matrix of the unrestricted cointegration
matrix B
is
\overline{V}_{B} = \left[\left(A^{\prime} G^{-1} A \otimes v^{-1} P_{\tau}^{-1} \right) + \left(A^{\prime} \Sigma^{-1} A \otimes WW^{\prime} \right) \right]^{-1}
and the posterior mean by
\overline{B} = \overline{V}_{B} + vec(W Y_{B}^{-1} \Sigma^{-1} A),
where Y_{B} = Y - \Gamma X
and A = \alpha (\alpha^{\prime} \alpha)^{-\frac{1}{2}}
.
The final draws of \alpha
and \beta
are calculated using
\beta = B (B^{\prime} B)^{-\frac{1}{2}}
and
\alpha = A (B^{\prime} B)^{\frac{1}{2}}
.
Value
A named list containing the following elements:
alpha |
a draw of the |
beta |
a draw of the |
Pi |
a draw of the |
Gamma |
a draw of the |
References
Koop, G., León-González, R., & Strachan R. W. (2010). Efficient posterior simulation for cointegrated models with priors on the cointegration space. Econometric Reviews, 29(2), 224-242. doi:10.1080/07474930903382208
Examples
# Load data
data("e6")
# Generate model data
temp <- gen_vec(e6, p = 1, r = 1)
y <- t(temp$data$Y)
ect <- t(temp$data$W)
k <- nrow(y) # Endogenous variables
tt <- ncol(y) # Number of observations
# Initial value of Sigma
sigma <- tcrossprod(y) / tt
sigma_i <- solve(sigma)
# Initial values of beta
beta <- matrix(c(1, -4), k)
# Draw parameters
coint <- post_coint_kls_sur(y = y, beta = beta, w = ect,
sigma_i = sigma_i, v_i = 0, p_tau_i = diag(1, nrow(ect)),
g_i = sigma_i)
Posterior Draw from a Normal Distribution
Description
Produces a draw of coefficients from a normal posterior density.
Usage
post_normal(y, x, sigma_i, a_prior, v_i_prior)
Arguments
y |
a |
x |
an |
sigma_i |
the inverse of the |
a_prior |
a |
v_i_prior |
the inverse of the |
Details
The function produces a vectorised posterior draw a
of the
K \times M
coefficient matrix A
for the model
y_{t} = A x_{t} + u_{t},
where y_{t}
is a K-dimensional vector of endogenous variables,
x_{t}
is an M-dimensional vector of explanatory variabes
and the error term is u_t \sim \Sigma
.
For a given prior mean vector \underline{a}
and prior covariance matrix \underline{V}
the posterior covariance matrix is obtained by
\overline{V} = \left[ \underline{V}^{-1} + \left(X X^{\prime} \otimes \Sigma^{-1} \right) \right]^{-1}
and the posterior mean by
\overline{a} = \overline{V} \left[ \underline{V}^{-1} \underline{a} + vec(\Sigma^{-1} Y X^{\prime}) \right],
where Y
is a K \times T
matrix of the endogenous variables and X
is an M \times T
matrix of
the explanatory variables.
Value
A vector.
References
Lütkepohl, H. (2006). New introduction to multiple time series analysis (2nd ed.). Berlin: Springer.
Examples
# Load data
data("e1")
data <- diff(log(e1))
# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
x <- t(temp$data$Z)
k <- nrow(y)
tt <- ncol(y)
m <- k * nrow(x)
# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(0.1, m)
# Initial value of inverse Sigma
sigma_i <- solve(tcrossprod(y) / tt)
# Draw parameters
a <- post_normal(y = y, x = x, sigma_i = sigma_i,
a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
Posterior Simulation of Error Covariance Coefficients
Description
Produces posterior draws of constant error covariance coefficients.
Usage
post_normal_covar_const(y, u_omega_i, prior_mean, prior_covariance_i)
Arguments
y |
a |
u_omega_i |
matrix of error variances of the measurement equation.
Either a |
prior_mean |
vector of prior means. In case of TVP, this vector is used as initial condition. |
prior_covariance_i |
inverse prior covariance matrix. In case of TVP, this matrix is used as initial condition. |
Details
For the multivariate model A_0 y_t = u_t
with u_t \sim N(0, \Omega_t)
the function produces a draw of the lower triangular part of A_0
similar as in
Primiceri (2005), i.e., using
y_t = Z_t \psi + u_t,
where
Z_{t} = \begin{bmatrix} 0 & \dotsm & \dotsm & 0 \\ -y_{1, t} & 0 & \dotsm & 0 \\ 0 & -y_{[1,2], t} & \ddots & \vdots \\ \vdots & \ddots & \ddots & 0 \\ 0 & \dotsm & 0 & -y_{[1,...,K-1], t} \end{bmatrix}
and y_{[1,...,K-1], t}
denotes the first to (K-1)
th elements of the vector y_t
.
Value
A matrix.
References
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies, 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
Examples
# Load example data
data("e1")
y <- log(t(e1))
# Generate artificial draws of other matrices
u_omega_i <- diag(1, 3)
prior_mean <- matrix(0, 3)
prior_covariance_i <- diag(0, 3)
# Obtain posterior draw
post_normal_covar_const(y, u_omega_i, prior_mean, prior_covariance_i)
Posterior Simulation of Error Covariance Coefficients
Description
Produces posterior draws of time varying error covariance coefficients.
Usage
post_normal_covar_tvp(y, u_omega_i, v_sigma_i, psi_init)
Arguments
y |
a |
u_omega_i |
matrix of error variances of the measurement equation.
Either a |
v_sigma_i |
matrix of error variances of the state equation.
Either an |
psi_init |
a vector of inital values of the state equation. |
Details
For the multivariate model A_{0,t} y_t = u_t
with u_t \sim N(0, \Omega_t)
the function produces a draw of the lower triangular part of A_{0,t}
similar as in
Primiceri (2005), i.e., using
y_t = Z_t \psi_t + u_t,
where
Z_{t} = \begin{bmatrix} 0 & \dotsm & \dotsm & 0 \\ -y_{1, t} & 0 & \dotsm & 0 \\ 0 & -y_{[1,2], t} & \ddots & \vdots \\ \vdots & \ddots & \ddots & 0 \\ 0 & \dotsm & 0 & -y_{[1,...,K-1], t} \end{bmatrix}
and y_{[1,...,K-1], t}
denotes the first to (K-1)
th elements of the vector y_t
.
The algorithm of Chan and Jeliazkov (2009) is used to obtain time varying coefficients.
Value
A matrix.
References
Chan, J., & Jeliazkov, I. (2009). Efficient simulation and integrated likelihood estimation in state space models. International Journal of Mathematical Modelling and Numerical Optimisation, 1(1/2), 101–120. doi:10.1504/IJMMNO.2009.030090
Primiceri, G. E. (2005). Time varying structural vector autoregressions and monetary policy. The Review of Economic Studies, 72(3), 821–852. doi:10.1111/j.1467-937X.2005.00353.x
Examples
# Load example data
data("e1")
y <- log(t(e1))
# Generate artificial draws of other matrices
u_omega_i <- diag(1, 3)
v_sigma_i <- diag(1000, 3)
psi_init <- matrix(0, 3)
# Obtain posterior draw
post_normal_covar_tvp(y, u_omega_i, v_sigma_i, psi_init)
Posterior Draw from a Normal Distribution
Description
Produces a draw of coefficients from a normal posterior density for a model with seemingly unrelated regresssions (SUR).
Usage
post_normal_sur(y, z, sigma_i, a_prior, v_i_prior, svd = FALSE)
Arguments
y |
a |
z |
a |
sigma_i |
the inverse of the constant |
a_prior |
a |
v_i_prior |
the inverse of the |
svd |
logical. If |
Details
The function produces a posterior draw of the coefficient vector a
for the model
y_{t} = Z_{t} a + u_{t},
where u_t \sim N(0, \Sigma_{t})
.
y_t
is a K-dimensional vector of endogenous variables and
Z_t = z_t^{\prime} \otimes I_K
is a K \times KM
matrix of regressors with
z_t
as a vector of regressors.
For a given prior mean vector \underline{a}
and prior covariance matrix \underline{V}
the posterior covariance matrix is obtained by
\overline{V} = \left[ \underline{V}^{-1} + \sum_{t=1}^{T} Z_{t}^{\prime} \Sigma_{t}^{-1} Z_{t} \right]^{-1}
and the posterior mean by
\overline{a} = \overline{V} \left[ \underline{V}^{-1} \underline{a} + \sum_{t=1}^{T} Z_{t}^{\prime} \Sigma_{t}^{-1} y_{t} \right].
Value
A vector.
Examples
# Load data
data("e1")
data <- diff(log(e1))
# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
z <- temp$data$SUR
k <- nrow(y)
tt <- ncol(y)
m <- ncol(z)
# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(0.1, m)
# Initial value of inverse Sigma
sigma_i <- solve(tcrossprod(y) / tt)
# Draw parameters
a <- post_normal_sur(y = y, z = z, sigma_i = sigma_i,
a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
Stochastic Search Variable Selection
Description
ssvs
employs stochastic search variable selection as proposed by George et al. (2008)
to produce a draw of the precision matrix of the coefficients in a VAR model.
Usage
ssvs(a, tau0, tau1, prob_prior, include = NULL)
Arguments
a |
an M-dimensional vector of coefficient draws. |
tau0 |
an M-dimensional vector of prior standard deviations for restricted
coefficients in vector |
tau1 |
an M-dimensional vector of prior standard deviations for unrestricted
coefficients in vector |
prob_prior |
an M-dimensional vector of prior inclusion probabilites for the coefficients
in vector |
include |
an integer vector specifying the positions of coefficients in vector |
Details
The function employs stochastic search variable selection (SSVS) as proposed
by George et al. (2008) to produce a draw of the diagonal inverse prior covariance matrix
\underline{V}^{-1}
and the corresponding vector of inclusion parameters \lambda
of the vectorised coefficient matrix a = vec(A)
for the VAR model
y_t = A x_t + u_t,
where y_{t}
is a K-dimensional vector of endogenous variables,
x_{t}
is a vector of explanatory variabes
and the error term is u_t \sim \Sigma
.
Value
A named list containing two components:
v_i |
an |
lambda |
an M-dimensional vector of inclusion parameters. |
References
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Examples
# Load data
data("e1")
data <- diff(log(e1))
# Generate model data
temp <- gen_var(data, p = 2, deterministic = "const")
y <- t(temp$data$Y)
x <- t(temp$data$Z)
k <- nrow(y)
tt <- ncol(y)
m <- k * nrow(x)
# Obtain SSVS priors using the semiautomatic approach
priors <- ssvs_prior(temp, semiautomatic = c(0.1, 10))
tau0 <- priors$tau0
tau1 <- priors$tau1
# Prior for inclusion parameter
prob_prior <- matrix(0.5, m)
# Priors
a_mu_prior <- matrix(0, m)
a_v_i_prior <- diag(c(tau1^2), m)
# Initial value of Sigma
sigma_i <- solve(tcrossprod(y) / tt)
# Draw parameters
a <- post_normal(y = y, x = x, sigma_i = sigma_i,
a_prior = a_mu_prior, v_i_prior = a_v_i_prior)
# Run SSVS
lambda <- ssvs(a = a, tau0 = tau0, tau1 = tau1,
prob_prior = prob_prior)
Stochastic Search Variable Selection Prior
Description
Calculates the priors for a Bayesian VAR model, which employs stochastic search variable selection (SSVS).
Usage
ssvs_prior(object, tau = c(0.05, 10), semiautomatic = NULL)
Arguments
object |
an object of class |
tau |
a numeric vector of two elements containing the prior standard errors of restricted
variables ( |
semiautomatic |
an optional numeric vector of two elements containing the factors by which
the standard errors associated with an unconstrained least squares estimate of the VAR model are
multiplied to obtain the prior standard errors of restricted ( |
Value
A list containing the vectors of prior standard deviations for restricted and unrestricted variables, respectively.
References
George, E. I., Sun, D., & Ni, S. (2008). Bayesian stochastic search for VAR model restrictions. Journal of Econometrics, 142(1), 553–580. doi:10.1016/j.jeconom.2007.08.017
Examples
# Prepare data
data("e1")
data <- diff(log(e1))
# Generate model input
object <- gen_var(data)
# Obtain SSVS prior
prior <- ssvs_prior(object, semiautomatic = c(.1, 10))
Stochastic Volatility
Description
Produces a draw of log-volatilities.
Usage
stoch_vol(y, h, sigma, h_init, constant)
Arguments
y |
a |
h |
a |
sigma |
a numeric of the variance of the log-volatilites. |
h_init |
a numeric of the initial state of log-volatilities. |
constant |
a numeric of the constant that should be added to |
Details
The function is a wrapper for function stochvol_ksc1998
.
Value
A vector of log-volatility draws.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Kim, S., Shephard, N., & Chib, S. (1998). Stochastic volatility. Likelihood inference and comparison with ARCH models. Review of Economic Studies 65(3), 361–393. doi:10.1111/1467-937X.00050
Examples
data("us_macrodata")
y <- matrix(us_macrodata[, "r"])
# Initialise log-volatilites
h_init <- matrix(log(var(y)))
h <- matrix(rep(h_init, length(y)))
# Obtain draw
stoch_vol(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
Stochastic Volatility
Description
Produces a draw of log-volatilities.
Usage
stochvol_ksc1998(y, h, sigma, h_init, constant)
Arguments
y |
a |
h |
a |
sigma |
a |
h_init |
a |
constant |
a |
Details
For each column in y
the function produces a posterior
draw of the log-volatility h
for the model
y_{t} = e^{\frac{1}{2}h_t} \epsilon_{t},
where \epsilon_t \sim N(0, 1)
and h_t
is assumed to evolve according to a random walk
h_t = h_{t - 1} + u_t,
with u_t \sim N(0, \sigma^2)
.
The implementation is based on the algorithm of Kim, Shephard and Chip (1998) and performs the following steps:
Perform the transformation
y_t^* = ln(y_t^2 + constant)
.Obtain a sample from the seven-component normal mixture for approximating the log-
\chi_1^2
distribution.Obtain a draw of log-volatilities.
The implementation follows the code provided on the website to the textbook by Chan, Koop, Poirier, and Tobias (2019).
Value
A vector of log-volatility draws.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Kim, S., Shephard, N., & Chib, S. (1998). Stochastic volatility. Likelihood inference and comparison with ARCH models. Review of Economic Studies 65(3), 361–393. doi:10.1111/1467-937X.00050
Examples
data("us_macrodata")
y <- matrix(us_macrodata[, "r"])
# Initialise log-volatilites
h_init <- matrix(log(var(y)))
h <- matrix(rep(h_init, length(y)))
# Obtain draw
stochvol_ksc1998(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
Stochastic Volatility
Description
Produces a draw of log-volatilities based on Omori, Chib, Shephard and Nakajima (2007).
Usage
stochvol_ocsn2007(y, h, sigma, h_init, constant)
Arguments
y |
a |
h |
a |
sigma |
a |
h_init |
a |
constant |
a |
Details
For each column in y
the function produces a posterior
draw of the log-volatility h
for the model
y_{t} = e^{\frac{1}{2}h_t} \epsilon_{t},
where \epsilon_t \sim N(0, 1)
and h_t
is assumed to evolve according to a random walk
h_t = h_{t - 1} + u_t,
with u_t \sim N(0, \sigma^2)
.
The implementation follows the algorithm of Omori, Chib, Shephard and Nakajima (2007) and performs the following steps:
Perform the transformation
y_t^* = ln(y_t^2 + constant)
.Obtain a sample from the ten-component normal mixture for approximating the log-
\chi_1^2
distribution.Obtain a draw of log-volatilities.
The implementation is an adaption of the code provided on the website to the textbook by Chan, Koop, Poirier, and Tobias (2019).
Value
A vector of log-volatility draws.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.
Omori, Y., Chib, S., Shephard, N., & Nakajima, J. (2007). Stochastic volatiltiy with leverage. Fast and efficient likelihood inference. Journal of Econometrics 140(2), 425–449. doi:10.1016/j.jeconom.2006.07.008
Examples
data("us_macrodata")
y <- matrix(us_macrodata[, "r"])
# Initialise log-volatilites
h_init <- matrix(log(var(y)))
h <- matrix(rep(h_init, length(y)))
# Obtain draw
stochvol_ocsn2007(y - mean(y), h, matrix(.05), h_init, matrix(0.0001))
Summarising Bayesian VAR Coefficients
Description
summary method for class "bvar"
.
Usage
## S3 method for class 'bvar'
summary(object, ci = 0.95, period = NULL, ...)
## S3 method for class 'summary.bvar'
print(x, digits = max(3L, getOption("digits") - 3L), ...)
Arguments
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
period |
integer. Index of the period, for which the summary statistics should be generated.
Only used for TVP or SV models. Default is |
... |
further arguments passed to or from other methods. |
x |
an object of class |
digits |
the number of significant digits to use when printing. |
Value
summary.bvar
returns a list of class "summary.bvar"
,
which contains the following components:
coefficients |
A list of various summary statistics of the posterior draws of the VAR coefficients. |
sigma |
A list of various summary statistics of the posterior draws of the variance-covariance matrix. |
specifications |
a list containing information on the model specification. |
Summarising Bayesian VAR or VEC Models
Description
summary method for class "bvarlist"
.
Usage
## S3 method for class 'bvarlist'
summary(object, ...)
Arguments
object |
an object of class |
... |
further arguments passed to or from other methods. |
Details
The log-likelihood for the calculation of the information criteria is obtained by
LL = \frac{1}{R} \sum_{i = 1}^{R} \left( \sum_{t = 1}^{T} -\frac{K}{2} \ln 2\pi - \frac{1}{2} \ln |\Sigma_t^{(i)}| -\frac{1}{2} (u_t^{{(i)}\prime} (\Sigma_t^{(i)})^{-1} u_t^{(i)} \right)
,
where u_t = y_t - \mu_t
. The Akaike, Bayesian and Hannan–Quinn (HQ) information criteria are calculated as
AIC = 2 (Kp + Ms + N) - 2 LL
,
BIC = (Kp + Ms + N) ln(T) - 2 LL
and
HQ = 2 (Kp + Ms + N) ln(ln(T)) - 2 LL
, respectively,
where K
is the number of endogenous variables, p
the number of lags of endogenous variables,
M
the number of exogenous variables, s
the number of lags of exogenous variables,
N
the number of deterministic terms and T
the number of observations.
Value
summary.bvarlist
returns a table of class "summary.bvarlist"
.
Summarising Bayesian VEC Coefficients
Description
summary method for class "bvec"
.
Usage
## S3 method for class 'bvec'
summary(object, ci = 0.95, period = NULL, ...)
## S3 method for class 'summary.bvec'
print(x, digits = max(3L, getOption("digits") - 3L), ...)
Arguments
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
period |
integer. Index of the period of a TVP VEC, for which a summary should be generated.
Only used for TVP models. Default is |
... |
further arguments passed to or from other methods. |
x |
an object of class |
digits |
the number of significant digits to use when printing. |
Value
summary.bvec
returns a list of class "summary.bvec"
,
which contains the following components:
coefficients |
A list of various summary statistics of the posterior draws of the VAR coefficients. |
sigma |
A list of various summary statistics of the posterior draws of the variance-covariance matrix. |
specifications |
a list containing information on the model specification. |
Summarising Bayesian Dynamic Factor Models
Description
summary method for class "dfm"
.
Usage
## S3 method for class 'dfm'
summary(object, ci = 0.95, ...)
Arguments
object |
an object of class |
ci |
a numeric between 0 and 1 specifying the probability of the credible band. Defaults to 0.95. |
... |
further arguments passed to or from other methods. |
Value
summary.dfm
returns a list of class "summary.dfm"
,
which contains the following components:
lambda |
A list of various summary statistics of the posterior draws of the factor loadings. |
factor |
A list of various summary statistics of the posterior draws of the factors. |
sigma_u |
A list of various summary statistics of the posterior draws of the variance matrix of the measurement equation. |
a |
A list of various summary statistics of the posterior draws of the factor loadings. |
sigma_v |
A list of various summary statistics of the posterior draws of the variance matrix of the transition equation. |
specifications |
a list containing information on the model specification. |
Thinning Posterior Draws
Description
Thins the MCMC posterior draws in an object of class "bvar"
.
Usage
## S3 method for class 'bvar'
thin(x, thin = 10, ...)
Arguments
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
Value
An object of class "bvar"
.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Obtain data matrices
model <- gen_var(e1, p = 2, deterministic = 2,
iterations = 100, burnin = 10)
# Chosen number of iterations and burn-in draws should be much higher.
# Add prior specifications
model <- add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
object <- thin(object)
Thinning Posterior Draws
Description
Thins the MCMC posterior draws in an object of class "bvarlist"
.
Usage
## S3 method for class 'bvarlist'
thin(x, thin = 10, ...)
Arguments
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
Value
An object of class "bvarlist"
.
Examples
# Load data
data("e1")
e1 <- diff(log(e1)) * 100
# Generate multiple model matrices
model <- gen_var(e1, p = 1:2, deterministic = 2,
iterations = 100, burnin = 10)
# Add prior specifications
model <- add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
# Thin
object <- thin(object)
Thinning Posterior Draws
Description
Thins the MCMC posterior draws in an object of class "bvec"
.
Usage
## S3 method for class 'bvec'
thin(x, thin = 10, ...)
Arguments
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
Value
An object of class "bvec"
.
Examples
# Load data
data("e6")
# Generate model data
model <- gen_vec(e6, p = 2, r = 1,
const = "unrestricted", seasonal = "unrestricted",
iterations = 100, burnin = 10)
# Add prior specifications
model <- add_priors(model)
# Obtain posterior draws
object <- draw_posterior(model)
# Thin
object <- thin(object)
Thinning Posterior Draws
Description
Thins the MCMC posterior draws in an object of class "dfm"
.
Usage
## S3 method for class 'dfm'
thin(x, thin = 10, ...)
Arguments
x |
an object of class |
thin |
an integer specifying the thinning interval between successive values of posterior draws. |
... |
further arguments passed to or from other methods. |
Value
An object of class "dfm"
.
Examples
# Load data
data("bem_dfmdata")
# Generate model data
model <- gen_dfm(x = bem_dfmdata, p = 1, n = 1,
iterations = 20, burnin = 10)
# Number of iterations and burnin should be much higher.
# Add prior specifications
model <- add_priors(model,
lambda = list(v_i = .01),
sigma_u = list(shape = 5, rate = 4),
a = list(v_i = .01),
sigma_v = list(shape = 5, rate = 4))
# Obtain posterior draws
object <- draw_posterior(model)
# Plot factors
object <- thin(object, thin = 2)
US macroeconomic data
Description
The data set contains quarterly time series for the US CPI inflation rate, unemployment rate, and Fed Funds rate from 1959Q2 to 2007Q4. It was produced from file "US_macrodata.csv" of the data sets associated with Chan, Koop, Poirier and Tobias (2019). Raw data are available at https://web.ics.purdue.edu/~jltobias/second_edition/Chapter20/code_for_exercise_1/US_macrodata.csv.
Usage
data("us_macrodata")
Format
A named time-series object with 195 rows and 3 variables:
- Dp
CPI inflation rate.
- u
unemployment rate.
- r
Fed Funds rate.
References
Chan, J., Koop, G., Poirier, D. J., & Tobias J. L. (2019). Bayesian econometric methods (2nd ed.). Cambridge: Cambridge University Press.