R/compute_fitted_values.R

Defines functions print.PosteriorFitted compute_fitted_values.PosteriorBSVART compute_fitted_values.PosteriorBSVARSV compute_fitted_values.PosteriorBSVARMIX compute_fitted_values.PosteriorBSVARHMSH compute_fitted_values.PosteriorBSVARMSH compute_fitted_values.PosteriorBSVAREXH compute_fitted_values.PosteriorBSVAR compute_fitted_values

Documented in compute_fitted_values compute_fitted_values.PosteriorBSVAR compute_fitted_values.PosteriorBSVAREXH compute_fitted_values.PosteriorBSVARHMSH compute_fitted_values.PosteriorBSVARMIX compute_fitted_values.PosteriorBSVARMSH compute_fitted_values.PosteriorBSVARSV compute_fitted_values.PosteriorBSVART

#' @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
#' 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' fitted         = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar$new(p = 1) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> fitted
#' 
#' @export
compute_fitted_values <- function(posterior) {
  UseMethod("compute_fitted_values", posterior)
}




#' @method compute_fitted_values PosteriorBSVAR
#' 
#' @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{PosteriorBSVAR} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar$new(us_fiscal_lsuw, p = 1)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' fitted         = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar$new(p = 1) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> fitted
#' 
#' @export
compute_fitted_values.PosteriorBSVAR <- function(posterior) {

  Y               = posterior$last_draw$data_matrices$Y
  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 = sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X

  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)

  return(fv)
}






#' @method compute_fitted_values PosteriorBSVAREXH
#' 
#' @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{PosteriorBSVAREXH} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_exh$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' csd     = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_exh$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> csd
#'   
#' @export
compute_fitted_values.PosteriorBSVAREXH <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = posterior$posterior$sigma * sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X
  
  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)
  
  return(fv)
}



#' @method compute_fitted_values PosteriorBSVARMSH
#' 
#' @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{PosteriorBSVARMSH} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_msh$new(us_fiscal_lsuw, M = 2)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' csd     = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_msh$new(M = 2) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> csd
#'   
#' @export
compute_fitted_values.PosteriorBSVARMSH <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = posterior$posterior$sigma * sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X

  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)

  return(fv)
}









#' @method compute_fitted_values PosteriorBSVARHMSH
#' 
#' @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{PosteriorBSVARHMSH} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_hmsh$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' csd     = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_hmsh$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> csd
#'   
#' @export
compute_fitted_values.PosteriorBSVARHMSH <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = posterior$posterior$sigma * sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X
  
  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)
  
  return(fv)
}











#' @method compute_fitted_values PosteriorBSVARMIX
#' 
#' @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{PosteriorBSVARMIX} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_mix$new(us_fiscal_lsuw, M = 2)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' csd     = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_mix$new(M = 2) |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> csd
#'   
#' @export
compute_fitted_values.PosteriorBSVARMIX <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = posterior$posterior$sigma * sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X
  
  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)
  
  return(fv)
}








#' @method compute_fitted_values PosteriorBSVARSV
#' 
#' @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{PosteriorBSVARSV} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_sv$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' csd     = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_sv$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> csd
#'   
#' @export
compute_fitted_values.PosteriorBSVARSV <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = posterior$posterior$sigma * sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X
  
  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)
  
  return(fv)
}





#' @method compute_fitted_values PosteriorBSVART
#' 
#' @title Computes posterior draws from data predictive density
#'
#' @description Each of the draws from the posterior estimation of the model is 
#' transformed into a draw from the data predictive density. 
#' 
#' @param posterior posterior estimation outcome - an object of class 
#' \code{PosteriorBSVART} 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}}
#'
#' @author Tomasz Woźniak \email{wozniak.tom@pm.me}
#' 
#' @examples
#' specification  = specify_bsvar_t$new(us_fiscal_lsuw)
#' burn_in        = estimate(specification, 5)
#' posterior      = estimate(burn_in, 5)
#' 
#' # compute draws from in-sample predictive density
#' fitted         = compute_fitted_values(posterior)
#' 
#' # workflow with the pipe |>
#' ############################################################
#' us_fiscal_lsuw |>
#'   specify_bsvar_t$new() |>
#'   estimate(S = 5) |> 
#'   estimate(S = 5) |> 
#'   compute_fitted_values() -> fitted
#' 
#' @export
compute_fitted_values.PosteriorBSVART <- function(posterior) {
  
  Y               = posterior$last_draw$data_matrices$Y
  posterior_A     = posterior$posterior$A
  posterior_B     = posterior$posterior$B
  posterior_sigma = sqrt(posterior$posterior$lambda)
  X               = posterior$last_draw$data_matrices$X
  
  fv              = .Call(`_bsvars_bsvars_fitted_values`, posterior_A, posterior_B, posterior_sigma, X)
  class(fv)       = "PosteriorFitted"
  S               = dim(posterior_A)[3]      
  dimnames(fv)    = list(rownames(Y), colnames(Y), 1:S)
  
  return(fv)
}

#' @export
print.PosteriorFitted <- function(x, ...) {
  cat("Posterior draws from the data predictive density\n")
  cat("An array of dimensions:", paste(dim(x), collapse = " x "), "\n")
  invisible(x)
}

Try the bsvars package in your browser

Any scripts or data that you put into this service are public.

bsvars documentation built on Aug. 22, 2026, 5:09 p.m.