Skip to content

Commit

Permalink
Impulse responses for submodels
Browse files Browse the repository at this point in the history
  • Loading branch information
franzmohr committed Jun 23, 2023
1 parent cf9e35d commit 03a71cb
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 3 deletions.
2 changes: 2 additions & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,10 @@ Collate:
'get_submodel_specifications.R'
'gfevd.R'
'girf.R'
'irf.bgvarest.R'
'irf.ctryvarest.R'
'plot.bgvarest.R'
'plot.bgvarestirf.R'
'plot.bgvarfevd.R'
'plot.bgvarirf.R'
'plot.bgvarprd.R'
Expand Down
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ S3method(add_priors,gvarsubmodels)
S3method(add_priors,gvecsubmodels)
S3method(draw_posterior,gvarsubmodels)
S3method(draw_posterior,gvecsubmodels)
S3method(irf,bgvarest)
S3method(irf,ctryvarest)
S3method(plot,bgvarest)
S3method(plot,bgvarestirf)
S3method(plot,bgvarfevd)
S3method(plot,bgvarirf)
S3method(plot,bgvarprd)
Expand Down Expand Up @@ -42,5 +44,6 @@ exportPattern("^[[:alpha:]]+")
importFrom(Rcpp,sourceCpp)
importFrom(bvartools,add_priors)
importFrom(bvartools,draw_posterior)
importFrom(bvartools,irf)
importFrom(coda,thin)
useDynLib(bgvars, .registration = TRUE)
46 changes: 46 additions & 0 deletions R/irf.bgvarest.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#' Impulse Response Function for a GVAR Submodel
#'
#' Computes the impulse response coefficients of an object of class \code{"ctryvarest"} for
#' \code{n.ahead} steps.
#'
#' @param x an object of class \code{"ctryvarest"}, usually, a result of a call to
#' \code{\link{draw_posterior.gvarsubmodels}} or \code{\link{ctryvec_to_ctryvar}}.
#' @param impulse name of the impulse variable.
#' @param response name of the response variable.
#' @param n.ahead number of steps ahead.
#' @param ci a numeric between 0 and 1 specifying the probability mass covered by the
#' credible intervals. Defaults to 0.95.
#' @param shock size of the shock.
#' @param type type of the impulse response. Possible choices are forecast error \code{"feir"}
#' (default), orthogonalised \code{"oir"}, structural \code{"sir"}, generalised \code{"gir"},
#' and structural generalised \code{"sgir"} impulse responses.
#' @param cumulative logical specifying whether a cumulative IRF should be calculated.
#' @param keep_draws logical specifying whether the function should return all draws of
#' the posterior impulse response function. Defaults to \code{FALSE} so that
#' the median and the credible intervals of the posterior draws are returned.
#' @param period integer. Index of the period, for which the IR should be generated.
#' Only used for TVP or SV models. Default is \code{NULL}, so that the posterior draws of the last time period
#' are used.
#' @param ... further arguments passed to or from other methods.
#'
#' @return A time-series object of class \code{"bvarirf"} and if \code{keep_draws = TRUE} a simple matrix.
#'
#' @references
#'
#' Lütkepohl, H. (2006). \emph{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. \emph{Economics Letters, 58}, 17-29.
#'
#' @export
irf.bgvarest <- function(x, impulse, response, n.ahead = 5, ci = .95, shock = 1,
type = "feir", cumulative = FALSE, keep_draws = FALSE, period = NULL, ...) {

result <- list()
for (i in 1:length(x)) {
result[[i]] <- irf(x[[i]], impulse = impulse, response = response, n.ahead = n.ahead, ci = ci, shock = shock,
type = type, cumulative, keep_draws, period, ...)
}

class(result) <- c("bgvarestirf", "list")
return(result)
}
2 changes: 1 addition & 1 deletion R/irf.ctryvarest.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#' Pesaran, H. H., Shin, Y. (1998). Generalized impulse response analysis in linear multivariate models. \emph{Economics Letters, 58}, 17-29.
#'
#' @export
irf.ctryvarest <- function(x, impulse = NULL, response = NULL, n.ahead = 5, ci = .95, shock = 1,
irf.ctryvarest <- function(x, impulse, response, n.ahead = 5, ci = .95, shock = 1,
type = "feir", cumulative = FALSE, keep_draws = FALSE, period = NULL, ...) {

if (!type %in% c("feir", "oir", "gir", "sir", "sgir")) {
Expand Down
15 changes: 15 additions & 0 deletions R/plot.bgvarestirf.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#' Plotting Draws of a Bayesian Global VAR Models
#'
#' A plot function for objects of class \code{"bgvarest"}.
#'
#' @param x an object of class \code{"bgvarestirf"}, usually, a result of a call to \code{\link{irf.bgvarest}}.
#' @param ... further graphical parameters.
#'
#' @export
plot.bgvarestirf <- function(x, ...) {
for (i in 1:length(x)) {
plot(x[[i]], ...)
}
}


1 change: 1 addition & 0 deletions R/zzz.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#' @importFrom coda thin
#' @importFrom bvartools add_priors
#' @importFrom bvartools draw_posterior
#' @importFrom bvartools irf

# Unload the DLL when the package is unloaded
.onUnload <- function (libpath) {
Expand Down
63 changes: 63 additions & 0 deletions man/irf.bgvarest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions man/irf.ctryvarest.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions man/plot.bgvarestirf.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 03a71cb

Please sign in to comment.