Skip to content

Commit

Permalink
update globals and docs for fevd
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholas Clark committed Sep 18, 2024
1 parent 63f583d commit 2d0b304
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 26 deletions.
50 changes: 27 additions & 23 deletions R/fevd.mvgam.R
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ fevd.mvgam <- function(object,
validate_pos_integer(h)
trend_model <- attr(object$model_data, 'trend_model')
if(!trend_model %in% c('VAR', 'VARcor', 'VAR1', 'VAR1cor')){
stop('Only VAR(1) models currently supported for calculating IRFs',
stop('Only VAR(1) models currently supported for calculating FEVDs',
call. = FALSE)
}
beta_vars <- mcmc_chains(object$model_output, 'A')
Expand Down Expand Up @@ -107,32 +107,36 @@ fevd.mvgam <- function(object,
#' Forecast error variance decomposition
#' @noRd
gen_fevd <- function(x, h = 6, ...){

K <- x$K
ynames <- paste0('process_', 1:K)
msey <- var_fecov(x, h = h)
Phi <- var_phi(x, h = h)
mse <- matrix(NA, nrow = h, ncol = K)
Omega <- array(0, dim = c(h, K, K))
for(i in 1 : h){
mse[i, ] <- diag(msey[, , i])
temp <- matrix(0, K, K)
for(j in 1 : i){
temp <- temp + Phi[ , , j]^2
}
temp <- temp / mse[i, ]
for(j in 1 : K){
Omega[i, ,j] <- temp[j, ]
K <- x$K
ynames <- paste0('process_', 1:K)
msey <- var_fecov(x, h = h)
Psi <- var_psi(x, h = h)
mse <- matrix(NA, nrow = h, ncol = K)
Omega <- array(0, dim = c(h, K, K))
for(i in 1 : h){
mse[i, ] <- diag(msey[, , i])
temp <- matrix(0, K, K)
for(l in 1 : K){
for(m in 1 : K){
for(j in 1 : i){
temp[l, m] <- temp[l, m] + Psi[l , m, j]^2
}
}
}
result <- list()
for(i in 1 : K){
result[[i]] <- matrix(Omega[, , i], nrow = h, ncol = K)
colnames(result[[i]]) <- ynames
temp <- temp / mse[i, ]
for(j in 1 : K){
Omega[i, ,j] <- temp[j, ]
}
names(result) <- ynames
return(result)
}
result <- list()
for(i in 1 : K){
result[[i]] <- matrix(Omega[, , i], nrow = h, ncol = K)
colnames(result[[i]]) <- ynames
}
names(result) <- ynames
return(result)
}


#' Forecast error covariance matrix
#' @noRd
Expand Down
4 changes: 3 additions & 1 deletion R/globals.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ utils::globalVariables(c("y", "year", "smooth_vals", "smooth_num",
"name", "needed", "nmix_trendmap", "orig_formula",
"trial", "use_var1", "use_var1cor", "xcols_drop",
"time_lag", "dis_time", "maxt", "orig_rows",
"matches", "time.", "file_name", ".data"))
"matches", "time.", "file_name", ".data",
"horizon", "target", "Series", "evd", "mean_evd",
"total_evd"))
2 changes: 1 addition & 1 deletion man/fevd.mvgam.Rd

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

2 changes: 1 addition & 1 deletion man/plot.mvgam_fevd.Rd

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

Binary file modified src/mvgam.dll
Binary file not shown.

0 comments on commit 2d0b304

Please sign in to comment.