# orthog = FALSE gives GIRF.
<- function(x, n.ahead = 20, cumulative = TRUE, orthog = FALSE){
psGIRF
<- colnames(x$y)
y.names <- y.names
impulse <- y.names
response
# Ensure n.ahead is an integer
<- abs(as.integer(n.ahead))
n.ahead
# Create arrays to hold calculations
# [1:nlags, 1:nvariables, shocked variable ]
= array(data = 0, dim = c(n.ahead,x$K,x$K),
IRF_o dimnames = list(NULL,y.names,y.names))
= array(data = 0, dim = c(n.ahead,x$K,x$K),
IRF_g dimnames = list(NULL,y.names,y.names))
= array(data = 0, dim = c(n.ahead,x$K,x$K))
IRF_g1
# Estimation of orthogonalised and generalised IRFs
<- Phi(x, n.ahead)
SpecMA <- ncol(x$datamat[, -c(1:x$K)])
params <- crossprod(resid(x))/(x$obs - params)
sigma.u <- t(chol(sigma.u))
P <- diag(sigma.u)
sig_jj
for (jj in 1:x$K){
<- matrix(0,x$K,1)
indx_ 1] <- 1
indx_[jj,
for (kk in 1:n.ahead){ #kk counts the lag
<- SpecMA[, ,kk]%*%P%*%indx_ # Peseran-Shin eqn 7 (OIRF)
IRF_o[kk, ,jj]
<- SpecMA[, ,kk]%*%sigma.u%*%indx_
IRF_g1[kk, ,jj] <- sig_jj[jj]^(-0.5)*IRF_g1[kk, ,jj] # Peseran-Shin eqn 10 (GIRF)
IRF_g[kk, ,jj]
}
}
if(orthog==TRUE){
<- IRF_o
irf else if(orthog==FALSE) {
} <- IRF_g
irf else {
} stop("\nError! Orthogonalised or generalised IRF?\n")
}
<- length(impulse)
idx <- list()
irs for (ii in 1:idx) {
<- matrix(irf[1:(n.ahead), response, impulse[ii]], nrow = n.ahead)
irs[[ii]] colnames(irs[[ii]]) <- response
if (cumulative) {
if (length(response) > 1)
<- apply(irs[[ii]], 2, cumsum)
irs[[ii]] if (length(response) == 1) {
<- matrix(cumsum(irs[[ii]]))
tmp colnames(tmp) <- response
<- tmp
irs[[ii]]
}
}
}names(irs) <- impulse
<- irs
result return(result)
}
Generalised impulse response function in R.
R
Econometrics
R code for creating generalised impulse response functions (GIRF) from VARS package outputs.
This function is a replacement for the vars package irf function, , that estimates Pesaran and Shin (1998) orthogonalised and generalised IRFs and is intended to be slotted into the vars package boot function, , to bootstrap confidence intervals for IRFs.
I referred to the Matlab code posted by Ken Nyholm.