diff --git a/DESCRIPTION b/DESCRIPTION index 64fa52b..ca380b4 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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' diff --git a/NAMESPACE b/NAMESPACE index 317ad22..e6fa50f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -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) @@ -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) diff --git a/R/irf.bgvarest.R b/R/irf.bgvarest.R new file mode 100644 index 0000000..ffe4f1e --- /dev/null +++ b/R/irf.bgvarest.R @@ -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) +} \ No newline at end of file diff --git a/R/irf.ctryvarest.R b/R/irf.ctryvarest.R index 97131b7..f6a9de5 100644 --- a/R/irf.ctryvarest.R +++ b/R/irf.ctryvarest.R @@ -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")) { diff --git a/R/plot.bgvarestirf.R b/R/plot.bgvarestirf.R new file mode 100644 index 0000000..58371b4 --- /dev/null +++ b/R/plot.bgvarestirf.R @@ -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]], ...) + } +} + + diff --git a/R/zzz.R b/R/zzz.R index 4dd1fca..45ccaf6 100644 --- a/R/zzz.R +++ b/R/zzz.R @@ -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) { diff --git a/man/irf.bgvarest.Rd b/man/irf.bgvarest.Rd new file mode 100644 index 0000000..c760fb8 --- /dev/null +++ b/man/irf.bgvarest.Rd @@ -0,0 +1,63 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/irf.bgvarest.R +\name{irf.bgvarest} +\alias{irf.bgvarest} +\title{Impulse Response Function for a GVAR Submodel} +\usage{ +\method{irf}{bgvarest}( + x, + impulse, + response, + n.ahead = 5, + ci = 0.95, + shock = 1, + type = "feir", + cumulative = FALSE, + keep_draws = FALSE, + period = NULL, + ... +) +} +\arguments{ +\item{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}}.} + +\item{impulse}{name of the impulse variable.} + +\item{response}{name of the response variable.} + +\item{n.ahead}{number of steps ahead.} + +\item{ci}{a numeric between 0 and 1 specifying the probability mass covered by the +credible intervals. Defaults to 0.95.} + +\item{shock}{size of the shock.} + +\item{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.} + +\item{cumulative}{logical specifying whether a cumulative IRF should be calculated.} + +\item{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.} + +\item{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.} + +\item{...}{further arguments passed to or from other methods.} +} +\value{ +A time-series object of class \code{"bvarirf"} and if \code{keep_draws = TRUE} a simple matrix. +} +\description{ +Computes the impulse response coefficients of an object of class \code{"ctryvarest"} for +\code{n.ahead} steps. +} +\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. +} diff --git a/man/irf.ctryvarest.Rd b/man/irf.ctryvarest.Rd index cf67151..23d4462 100644 --- a/man/irf.ctryvarest.Rd +++ b/man/irf.ctryvarest.Rd @@ -6,8 +6,8 @@ \usage{ \method{irf}{ctryvarest}( x, - impulse = NULL, - response = NULL, + impulse, + response, n.ahead = 5, ci = 0.95, shock = 1, diff --git a/man/plot.bgvarestirf.Rd b/man/plot.bgvarestirf.Rd new file mode 100644 index 0000000..a12719f --- /dev/null +++ b/man/plot.bgvarestirf.Rd @@ -0,0 +1,16 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/plot.bgvarestirf.R +\name{plot.bgvarestirf} +\alias{plot.bgvarestirf} +\title{Plotting Draws of a Bayesian Global VAR Models} +\usage{ +\method{plot}{bgvarestirf}(x, ...) +} +\arguments{ +\item{x}{an object of class \code{"bgvarestirf"}, usually, a result of a call to \code{\link{irf.bgvarest}}.} + +\item{...}{further graphical parameters.} +} +\description{ +A plot function for objects of class \code{"bgvarest"}. +}