-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ cpp wrapper of bsvars function + R method + doc + tests
- Loading branch information
1 parent
0dd79ba
commit b4faf0e
Showing
8 changed files
with
248 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,9 +50,72 @@ compute_structural_shocks.PosteriorBSVARSIGN <- function(posterior) { | |
Y = posterior$last_draw$data_matrices$Y | ||
X = posterior$last_draw$data_matrices$X | ||
|
||
ss = .Call(`_bsvarSIGNs_structural_shocks`, posterior_B, posterior_A, Y, X) | ||
ss = .Call(`_bsvarSIGNs_bsvarSIGNs_structural_shocks`, posterior_B, posterior_A, Y, X) | ||
class(ss) = "PosteriorShocks" | ||
|
||
return(ss) | ||
} # END compute_structural_shocks.PosteriorBSVARSIGN | ||
|
||
|
||
|
||
|
||
|
||
#' @method compute_fitted_values PosteriorBSVARSIGN | ||
#' | ||
#' @title Computes posterior draws from data predictive density | ||
#' | ||
#' @description Each of the draws from the posterior estimation of models from | ||
#' packages \pkg{bsvars} or \pkg{bsvarSIGNs} is transformed into | ||
#' a draw from the data predictive density. | ||
#' | ||
#' @param posterior posterior estimation outcome - an object of class | ||
#' \code{PosteriorBSVARSIGN} obtained by running the \code{estimate} function. | ||
#' | ||
#' @return An object of class \code{PosteriorFitted}, that is, an \code{NxTxS} | ||
#' array with attribute \code{PosteriorFitted} containing \code{S} draws from | ||
#' the data predictive density. | ||
#' | ||
#' @seealso \code{\link{estimate}}, \code{\link{summary}}, \code{\link{plot}} | ||
#' | ||
#' @author Xiaolei Wang \email{[email protected]} and Tomasz Woźniak \email{[email protected]} | ||
#' | ||
#' @examples | ||
#' # upload data | ||
#' data(oil) | ||
#' | ||
#' # specify the model and set seed | ||
#' set.seed(123) | ||
#' sign_irf = array(matrix(c(-1, -1, 1, rep(0, 6)), nrow = 3), dim = c(3, 3, 1)) | ||
#' specification = specify_bsvarSIGN$new(oil, sign_irf = sign_irf) | ||
#' | ||
#' # run the burn-in | ||
#' posterior = estimate(specification, 10) | ||
#' | ||
#' # compute draws from in-sample predictive density | ||
#' fitted = compute_fitted_values(posterior) | ||
#' | ||
#' # workflow with the pipe |> | ||
#' ############################################################ | ||
#' set.seed(123) | ||
#' oil |> | ||
#' specify_bsvarSIGN$new(sign_irf = sign_irf) |> | ||
#' estimate(S = 20) |> | ||
#' compute_fitted_values() -> fitted | ||
#' | ||
#' @export | ||
compute_fitted_values.PosteriorBSVARSIGN <- function(posterior) { | ||
|
||
posterior_A = posterior$posterior$A | ||
posterior_B = posterior$posterior$B | ||
|
||
N = dim(posterior_A)[1] | ||
T = dim(posterior$last_draw$data_matrices$X)[2] | ||
S = dim(posterior_A)[3] | ||
posterior_sigma = array(1, c(N, T, S)) | ||
X = posterior$last_draw$data_matrices$X | ||
|
||
fv = .Call(`_bsvarSIGNs_bsvarSIGNs_fitted_values`, posterior_A, posterior_B, posterior_sigma, X) | ||
class(fv) = "PosteriorFitted" | ||
|
||
return(fv) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
|
||
data(oil) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
specification_no1 <- specify_bsvarSIGN$new(oil) | ||
) | ||
run_no1 <- estimate(specification_no1, 3, 1, show_progress = FALSE) | ||
fv <- compute_fitted_values(run_no1) | ||
|
||
set.seed(1) | ||
suppressMessages( | ||
fv2 <- oil |> | ||
specify_bsvarSIGN$new() |> | ||
estimate(S = 3, thin = 1, show_progress = FALSE) |> | ||
compute_fitted_values() | ||
) | ||
|
||
|
||
|
||
expect_true( | ||
all(dim(fv) == dim(fv2)), | ||
info = "compute_fitted_values: same output dimentions for normal and pipe workflow." | ||
) | ||
|
||
expect_identical( | ||
fv[1,1,1], fv2[1,1,1], | ||
info = "compute_fitted_values: identical for normal and pipe workflow." | ||
) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters