#' @export
#' @title connectedness measure for classical VAR
#' @param mydata data
#' @param rolling_window size of the rolling window to estimate the VAR. If the rollingwindow is 0, the whole dataset is estimated at once.
#' @param nolags number of lags in the VAR model
#' @param nhor horizon for the forecast erro variance decomposition
#' @return an S3-object of the class spillover_table or spillover_series
cv_connectedness <- function(mydata, rolling_window = 0,nolags = 6,nhor = 12){
# Load required Namespace for the vars packag
chck <- requireNamespace("vars",quietly = TRUE)
if(!chck){
stop("Please install the vars package!")
}
if(rolling_window == 0){
# Estimate the whole thing
tmp <- vars::VAR(y=mydata, p = nolags)
Alpha <- vars::Acoef(tmp)
Sigma <- summary(tmp)$covres
# Get VMA representation
vma <- cv_var2vma(Alpha,nhor,nolags,tmp$K)
# Get GFEVD
fe <- gfevd(vma,Sigma,nhor)
# Calculate spillovers from generalized gfevd
spill_over <- array(0,dim=c(tmp$K,tmp$K))
for(ii in 1:tmp$K){
fe_sum <- sum(fe[ii,])
spill_over[ii,] <- fe[ii,] / fe_sum
}
colnames(spill_over) <- colnames(mydata)
rownames(spill_over) <- colnames(mydata)
spillover <- structure(list(spillover = spill_over,
nvar = tmp$K,
nhor = nhor,
nolags = nolags),class="spillover_table")
}
else{
# Calculate Spillovers with rolling window
total_length <- dim(mydata)[1] - rolling_window + 1
K <- dim(mydata)[2]
spillover_series <- array(0,dim=c(K,K,total_length))
for(ii in 1:total_length){
print(ii)
sub_series <- mydata[ii:(ii + rolling_window - 1),]
# Estimate the sub series
tmp <- vars::VAR(y=sub_series, p = nolags)
Alpha <- vars::Acoef(tmp)
Sigma <- summary(tmp)$covres
# Get VMA representation
vma <- cv_var2vma(Alpha,nhor,nolags,tmp$K)
# Get GFEVD
fe <- gfevd(vma,Sigma,nhor)
# Calculate spillovers from generalized gfevd
spill_over <- array(0,dim=c(tmp$K,tmp$K))
for(ii in 1:tmp$K){
fe_sum <- sum(fe[ii,])
spill_over[ii,] <- fe[ii,] / fe_sum
}
spillover_series[,,ii] <- spill_over
}
spillover <- structure(list(spillover_series = spillover_series,
nvar = K,
nhor = nhor,
nolags = nolags),class = "spillover_series")
}
return(spillover)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.