library("zoo")
## 
## Attaching package: 'zoo'
## The following objects are masked from 'package:base':
## 
##     as.Date, as.Date.numeric
library("knitr")
library("ggplot2")
library("reshape2")
library("devtools")
## Loading required package: usethis
library("RColorBrewer")
library("ConnectednessApproach")
## 
## Please cite as:
##  Gabauer, David (2022). ConnectednessApproach.
##  R package version 1.0.0. https://CRAN.R-project.org/package=ConnectednessApproach
colors = c("black","steelblue4","firebrick")
palette(colors)

Please use the latest version

#install_github("GabauerDavid/ConnectednessApproach")
# Quantile-on-quantile connectedness approach
m = 5
quantiles = seq(0.05, 0.95,length.out=m)
print(quantiles)

nlag = 1
t = nrow(data)
window.size = 200 + nlag
t0 = t - window.size + 1 + nlag
NET = TCI = array(NA, c(t0,m,m), dimnames=list(1:t0,quantiles,quantiles))
for (i in 1:m) {
  print(i)
  for (j in 1:m) {
    print(paste0("-", j))
    dca = suppressMessages(ConnectednessApproach(data, nlag=nlag, nfore=20, 
                                model="QVAR", connectedness="Time",
                                window.size=window.size, corrected=TRUE,
                                VAR_config=list(QVAR=list(tau=c(quantiles[i], quantiles[j])))))
    TCI[,i,j] = dca$TCI
    NET[,i,j] = dca$NET[,1]
  }
}

TCI_agg = round(apply(TCI,2:3,mean),1)
NET_agg = round(apply(NET,2:3,mean),1)
melted_TCI_agg = data.frame(melt(TCI_agg))

Dynamic quantile-on-quantile total connectedness

ggplot(data = melted_TCI_agg, aes(x=Var2, y=Var1, fill=value)) + 
  geom_tile(color = "black") + 
  geom_text(aes(label = value), color = "black", size = 4) + 
  labs(x=NAMES[2],y=NAMES[1],color="",title="") + guides(fill=guide_legend(title="TCI")) + 
  scale_fill_gradientn(colours=c("white", "steelblue4"), limits=c(min(melted_TCI_agg[,3]), max(abs(melted_TCI_agg))))

Net quantile-on-quantile total directional connectedness

melted_NET_agg = data.frame(melt(NET_agg))
ggplot(data = melted_NET_agg, aes(x=Var2, y=Var1, fill=value)) + 
  geom_tile(color = "black") + 
  geom_text(aes(label = value), color = "black", size = 4) + 
  labs(x=NAMES[2],y=NAMES[1],color="",title="") +guides(fill=guide_legend(title="NET")) + 
  scale_fill_gradientn(colours=c("gold4", "white", "steelblue4"), limits=c(-max(abs(melted_NET_agg)), max(abs(melted_NET_agg))))

net = FROM = TO = array(NA,c(t0,m))
nTCI = pTCI = array(NA,c(t0,1))
for (i in 1:t0) {
  pTCI[i,] = mean(diag(TCI[i,,]))
  nTCI[i,] = mean(diag(TCI[i,m:1,]))
  FROM[i,] = rowMeans(TCI[i,,])
  TO[i,] = colMeans(TCI[i,,])
  net[i,] = TO[i,] - FROM[i,]
}
dates = as.Date(tail(index(data), t0))

TCI_df = data.frame(Date=dates,"Reverse TCI"=nTCI,"Direct TCI"=pTCI, "ΔTCI"=nTCI-pTCI)
colnames(TCI_df) = c("Date","Reverse TCI","Direct TCI",paste0("\u0394 TCI"))
TCI_melted = melt(TCI_df, id.vars = "Date")

Directly related and indirectly related total connectedness

ggplot(data = TCI_melted, aes(x=Date, y=value, color = variable)) +
  geom_line() +
  labs(x = "", y = "", title = "", color="") + geom_hline(yintercept=0, linetype="dotted") + 
  theme(legend.position="bottom") + ylim(-100,110)  + scale_x_date(expand = c(0,0)) + scale_y_continuous(expand = c(0,12))
## Scale for y is already present.
## Adding another scale for y, which will replace the existing scale.