knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
This package provides useful and friendly functions to reproduce Diebold and Yilmaz (2012) papers. Its capabilities are not only for getting the directional indexes, but for plotting them too!
In this vignette we are showing how to get all results from Diebold and Yilmaz (2012).
Let's start loading the package and data. In order to confirm we have the correct data, let's get the Table 1 from the paper.
options(repos = list(CRAN="http://cran.rstudio.com/")) library(Spillover, quietly = TRUE) data(dy2012)
Once we are sure we have the correct data, let's get the spillover table, Table 2, this table is based on a VAR(4) and 10-day-ahead variance decomposition.
require(dplyr) dy2012 %>% dplyr::select(-Date) %>% VAR(p=4) %>% G.spillover(standardized = FALSE) %>% round(2)
Now, let's move on, and get the dynamic total spillover index:
total_dynamic <- total.dynamic.spillover(as.zoo(dy2012[,-1]), width = 200, index="generalized", p=4) require(ggplot2) tibble(Date= dy2012$Date[-c(1:199)], index = total_dynamic) %>% mutate(Date=as.Date(as.character(Date))) %>% ggplot(aes(x=Date, y=index)) + geom_line()+ labs(caption = "Fig 2. Total volatility spillovers, four asset classes.")+ theme(plot.caption = element_text(hjust = 0))
This part of the code simply gets all directional spillovers, then we need to plot them.
dy_results <- dynamic.spillover(data=dy2012, width=200, p=4)
In order to get Figure 3 from Diebold and Yilmaz (2012), we proceed as follows:
pp_from <- plotdy(data=dy_results, direction = "from") pp_from + labs(caption = "Fig 3. Directional volatility spillovers, FROM four asset classes.")+ theme(plot.caption = element_text(hjust = 0.5))
Is it difficult to plot directional TO? NO! It is as just easy as:
pp_to <- plotdy(dy_results, direction = "to") # Just for customization pp_from + labs(caption = "Fig. 4. Directional volatility spillovers, TO four asset classes.")+ theme(plot.caption = element_text(hjust = 0.5))
If you notice y scale is free, but we can get fix one using facet_wrap(~variables, scales = "fixed")
pp_net <- plotdy(dy_results, direction = "net") pp_net + labs(caption = "Fig. 5. Net volatility spillovers, four asset classes.")+ theme(plot.caption = element_text(hjust = 0.5))
pp_netpairwise <- plotdy(dy_results, direction = "net_pairwise") pp_netpairwise + labs(caption = "Fig. 6. Net pairwise volatility spillovers.")+ theme(plot.caption = element_text(hjust = 0.5))
Finally, we can add a new set of plots:
pp_from_to_pairwise <- plotdy(dy_results, direction = "from_to_pairwise")
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.