Skip to content

Commit

Permalink
Add IRF method for VAR
Browse files Browse the repository at this point in the history
Ref: #363
  • Loading branch information
mitchelloharawild committed Sep 15, 2024
1 parent 933816c commit 70b418d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Generated by roxygen2: do not edit by hand

S3method(IRF,VAR)
S3method(breusch_godfrey,TSLM)
S3method(components,ETS)
S3method(fitted,AR)
Expand Down Expand Up @@ -136,5 +137,6 @@ importFrom(stats,residuals)
importFrom(stats,sd)
importFrom(stats,time)
importFrom(stats,ts)
importFrom(tsibble,as_tsibble)
importFrom(utils,tail)
useDynLib(fable, .registration = TRUE)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## New features

* Added generate() method for VAR models
* Added `generate()` and `IRF()` methods for VAR models.

# fable 0.3.4

Expand Down
27 changes: 27 additions & 0 deletions R/var.R
Original file line number Diff line number Diff line change
Expand Up @@ -429,3 +429,30 @@ generate.VAR <- function(x, new_data, specials, ...){
new_data[colnames(coef)] <- split(.sim, col(.sim))
new_data
}

#' @export
IRF.VAR <- function(x, new_data, specials, impulse = NULL, ortho = FALSE, ...) {
new_data$.innov <- matrix(0, nrow = nrow(new_data), ncol = ncol(x$last_obs),
dimnames = dimnames(x$last_obs))
# Zero out end of data
x$last_obs[seq_along(x$last_obs)] <- 0

# Remove regressors
n_ar <- x$spec$p*ncol(x$coef)
if(nrow(x$coef) > n_ar) {
x$coef[seq(n_ar + 1, nrow(x$coef)),] <- 0
}

# Add shocks
new_data$.innov[1, impulse] <- 1

# Orthogonalised shocks
if(ortho) {
# Use Cholesky decomposition to orthogonalise the shocks / innovations
new_data$.innov <- new_data$.innov %*% chol(x$fit$sigma2[[1L]])
}

irf <- generate(x, new_data, specials)
irf$.innov <- NULL
irf
}

0 comments on commit 70b418d

Please sign in to comment.