Skip to content

Commit

Permalink
Merge pull request #34 from bsvars/33-change-examples
Browse files Browse the repository at this point in the history
33 change examples
  • Loading branch information
adamwang15 authored Jul 16, 2024
2 parents 661ef88 + 4c1e098 commit b7cf782
Show file tree
Hide file tree
Showing 29 changed files with 192 additions and 258 deletions.
4 changes: 1 addition & 3 deletions R/bsvarSIGNs-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@
#'
#' Antolín-Díaz & Rubio-Ramírez (2018) Narrative Sign Restrictions for SVARs, American Economic Review, 108(10), 2802-29, <doi:10.1257/aer.20161852>.
#' @examples
#' sign_irf = matrix(NA, 5, 5)
#' sign_irf[2, 1] = 1
#' sign_irf[1, 1] = 0
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' spec = specify_bsvarSIGN$new(optimism * 100,
#' p = 4,
#' sign_irf = sign_irf)
Expand Down
48 changes: 24 additions & 24 deletions R/compute.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -37,7 +37,7 @@
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 20) |>
#' compute_structural_shocks() -> ss
Expand Down Expand Up @@ -81,12 +81,12 @@ compute_structural_shocks.PosteriorBSVARSIGN <- function(posterior) {
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -97,7 +97,7 @@ compute_structural_shocks.PosteriorBSVARSIGN <- function(posterior) {
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 20) |>
#' compute_fitted_values() -> fitted
Expand Down Expand Up @@ -151,12 +151,12 @@ compute_fitted_values.PosteriorBSVARSIGN <- function(posterior) {
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -167,7 +167,7 @@ compute_fitted_values.PosteriorBSVARSIGN <- function(posterior) {
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 10) |>
#' compute_impulse_responses(horizon = 8) -> ir
Expand Down Expand Up @@ -223,12 +223,12 @@ compute_impulse_responses.PosteriorBSVARSIGN <- function(posterior, horizon, sta
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -239,7 +239,7 @@ compute_impulse_responses.PosteriorBSVARSIGN <- function(posterior, horizon, sta
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 10) |>
#' compute_historical_decompositions() -> hd
Expand Down Expand Up @@ -300,12 +300,12 @@ compute_historical_decompositions.PosteriorBSVARSIGN <- function(posterior, show
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -316,7 +316,7 @@ compute_historical_decompositions.PosteriorBSVARSIGN <- function(posterior, show
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 10) |>
#' compute_variance_decompositions(horizon = 8) -> fevd
Expand Down Expand Up @@ -365,12 +365,12 @@ compute_variance_decompositions.PosteriorBSVARSIGN <- function(posterior, horizo
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # run the burn-in
#' posterior = estimate(specification, 10)
Expand All @@ -381,7 +381,7 @@ compute_variance_decompositions.PosteriorBSVARSIGN <- function(posterior, horizo
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 10) |>
#' compute_conditional_sd() -> csd
Expand Down
22 changes: 0 additions & 22 deletions R/data.R
Original file line number Diff line number Diff line change
@@ -1,26 +1,4 @@

#' @title A 3-variable oil price data, from 1971 to 2016
#'
#' @description A sample data to identify oil demand and supply shocks.
#'
#' @usage data(oil)
#'
#' @format A matrix and a \code{ts} object with time series of over five hundred observations on 3 variables:
#' \describe{
#' \item{opg}{monthly growth rate of global oil production}
#' \item{eai}{monthly index of real economic activity}
#' \item{rop}{monthly real oil price}
#' }
#'
#' The series are as described by Antolín-Díaz & Rubio-Ramírez (2018) in section IV-A.
#'
#' @references
#' Antolín-Díaz & Rubio-Ramírez (2018) Narrative Sign Restrictions for SVARs, American Economic Review, 108(10), 2802-29, <doi:10.1257/aer.20161852>.
#'
#' @source
#' Replication package, \url{https://www.aeaweb.org/articles?id=10.1257/aer.20161852}
"oil"


#' @title A 5-variable US business cycle data, from 1955 Q1 to 2004 Q4
#'
Expand Down
15 changes: 5 additions & 10 deletions R/estimate.BSVARSIGN.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,15 @@
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # restrictions as in Antolín-Díaz & Rubio-Ramírez (2018)
#' sign_narrative = matrix(c(2, -1, 3, 2, 236, 0), ncol = 6)
#' sign_irf = array(matrix(c(-1, -1, 1, 1, 1, 1, 1, -1, 1), nrow = 3),
#' dim = c(3, 3, 1))
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#'
#' # specify the model and set seed
#' set.seed(123)
#' specification = specify_bsvarSIGN$new(oil,
#' p = 12,
#' sign_irf = sign_irf,
#' sign_narrative = sign_narrative
#' )
#' specification = specify_bsvarSIGN$new(optimism * 100,
#' p = 12,
#' sign_irf = sign_irf)
#' posterior = estimate(specification, S = 10)
#'
#' @export
Expand Down
17 changes: 9 additions & 8 deletions R/forecast.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@
#'
#' @examples
#' # upload data
#' data(oil)
#' data(optimism)
#'
#' # specify the model and set seed
#' set.seed(123)
#' sign_irf = array(matrix(c(-1, -1, 1, rep(NA, 6)), nrow = 3), dim = c(3, 3, 1))
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf)
#' sign_irf = matrix(c(0, 1, rep(NA, 23)), 5, 5)
#' specification = specify_bsvarSIGN$new(optimism, sign_irf = sign_irf)
#'
#' # estimate the model
#' posterior = estimate(specification, 20)
Expand All @@ -49,22 +49,23 @@
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 20) |>
#' forecast(horizon = 4) -> predictive
#'
#' # conditional forecasting 2 quarters ahead conditioning on
#' # provided future values for the Gross Domestic Product
#' ############################################################
#' cf = matrix(NA , 2, 3)
#' cf[,3] = tail(oil, 1)[3] # conditional forecasts equal to the last gdp observation
#' predictive = forecast(posterior, 2, conditional_forecast = cf)
#' cf = matrix(NA , 2, 5)
#' # # conditional forecasts equal to the last consumption observation
#' cf[,3] = tail(optimism, 1)[3]
#' predictive = forecast(posterior, 2, conditional_forecast = cf)
#'
#' # workflow with the pipe |>
#' ############################################################
#' set.seed(123)
#' oil |>
#' optimism |>
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |>
#' estimate(S = 10) |>
#' forecast(horizon = 2, conditional_forecast = cf) -> predictive
Expand Down
Loading

0 comments on commit b7cf782

Please sign in to comment.