References:

-Ghulame Rubbaniy, Ali Awais Khalid & Aristeidis Samitas (2021) Are Cryptos Safe-Haven Assets during Covid-19? Evidence from Wavelet Coherence Analysis, Emerging Markets Finance and Trade, 57:6, 1741-1756, DOI: 10.1080/1540496X.2021.1897004.

-Ghulame Rubbaniy, Ali Awais Khalid, Konstantinos Syriopoulos, Aristeidis Samitas, (2021). Safe-haven properties of soft commodities during times of Covid-19, Journal of Commodity Markets, 100223.

-Rubbaniy, G., Khalid, A.A., Rizwan, M.F. and Ali, S. (2021), “Are ESG stocks safe-haven during COVID-19?”, Studies in Economics and Finance, Vol. ahead-of-print No. ahead-of-print.

-Rubbaniy, Ghulame and Khalid, Ali Awais and Tessema, Abiot, Do Stock Market Fear and Economic Policy Uncertainty Co-Move with COVID-19 Fear? Evidence from the US and UK (May 11, 2021). Available at SSRN: https://ssrn.com/abstract=3843666

# Turning the package on for wavelet coherence.  If you haven't installed
# yet, then go to Packages --> Click on Install --> Search for 'biwavelet'
# --> Ok
library(biwavelet)
## Warning: package 'biwavelet' was built under R version 4.1.1
## biwavelet 0.20.21 loaded.
# Import your data(Change the windows slashes(\) to  to R studio(/) format)
Data <- read.csv("C:/Users/aliaw/Documents/Wavelet_Codes/Data_Stocks.csv")
# Attach your data so that you can access variables directly using their
# names
attach(Data)

MSCI World Islamic index Returns (series A) and S&P 500 Index Returns(Series B)

#Assigning variables to A and B

A=MSCI_IS_World_ri # MSCI Islamic World index Returns
B=SP500_ri # S&P 500 Index Returns

nrands=10 #Number of iterations. Higher is better (>500)
# Define two sets of variables with time stamps
DATE=1:length(A)

WA = cbind(DATE, MSCI_IS_World_ri)
WB = cbind(DATE, SP500_ri)

# Specify the number of iterations. The more, the better (>1000).  For the
# purpose of this tutorial, we just set it = 10
nrands = 10

Esimation of Wavelet Coherence (10 monte carlo simulations)

#Main calculations
wtc.AB = wtc(WA, WB, nrands = nrands)
## 
  |                                                                            
  |                                                                      |   0%
  |                                                                            
  |=======                                                               |  10%
  |                                                                            
  |==============                                                        |  20%
  |                                                                            
  |=====================                                                 |  30%
  |                                                                            
  |============================                                          |  40%
  |                                                                            
  |===================================                                   |  50%
  |                                                                            
  |==========================================                            |  60%
  |                                                                            
  |=================================================                     |  70%
  |                                                                            
  |========================================================              |  80%
  |                                                                            
  |===============================================================       |  90%
  |                                                                            
  |======================================================================| 100%

Plotting Graph and adding Grid lines and Tag the dates(Years from 2017-2020)

# Plotting a graph
par(oma = c(0, 0, 0, 1), mar = c(5, 4, 5, 5) + 0.1)
plot(wtc.AB, plot.phase = TRUE, lty.coi = 1, col.coi = "grey", lwd.coi = 2, 
    lwd.sig = 2, arrow.lwd = 0.03, arrow.len = 0.12, ylab = "Scale", xlab = "Period", 
    plot.cb = TRUE, main = "Wavelet Coherence: MSCI Islamic Index vs S&P500 Index ")

# Adding grid lines
n = length(WA[, 1])
abline(v = seq(250, n, 250), h = 1:4, col = "brown", lty = 1, lwd = 1)
# Defining x labels
axis(side = 3, at = c(seq(250, n, 250)), labels = c(seq(2017, 2020, 1)))

Interpretation: