Learn R Programming

RolWinMulCor (version 0.4.0)

rolwincor_1win: Estimate and plot the Rolling Window Correlation for the bi-variate case

Description

The rolwincor_1win function estimates the rolling (running) window correlation between TWO time series (bi-variate case) sampled on identical time points for ONLY ONE window-length and plots the correlation coefficients and their respective p-values. To carry out the computational implementation we follow to Telford (2013) and Polanco-Mart<U+00ED>nez (2019). The rolwincor_1win function is highly flexible since this contain a great number of parameters to control the estimation of correlation and the features of the plot output. For example, rolwincor_1win function contain parameters to remove the (linear) trend contained in the time series under analysis, to choose different p-value correction methods (which are used to address the multiple comparison problem), or to personalise the plot output. A list of parameters are described in the following lines.

Usage

rolwincor_1win(inputdata, varnametsX="", varnametsY="", units="", 
               coltsX="black", coltsY="blue", colCOEF="black", 
               colPVAL="gray", CEXLAB=1.15, CEXAXIS=1.05, LWDtsX=1, 
               LWDtsY=1, LWDcoef=1, LWDpval=1, CorMethod="spearman", 
               widthwin=3, Align="center", pvalcorectmethod="BH", 
               rmltrd="Y", Scale="Y", device="screen", Hfig=1200, 
               Wfig=900, resfig=150, Hpdf=5.15, Wpdf=13.5, 
               HeigWin1=2.05, HeigWin2=2.75, ofilename)

Arguments

inputdata

Matrix of 3 columns: time, first variable (e.g. \(X\)), and second variable (e.g. \(Y\)).

varnametsX, varnametsY

Names of the first (e.g. \(X\)) and second (e.g. \(Y\)) variable. Please note that the names of these variables MUST be defined.

units

Time's unit (e.g. days, weeks, years, etc.) for the variables under analysis. Please note that the units MUST be defined.

coltsX, coltsY

The colors to be used when the first and second variables are plotted, by default the colors are ``black'' and ``blue,'' respectively, but other colors can be used.

colCOEF, colPVAL

The colors to be used when the correlation coefficients and their corresponding p-values are plotted, by default the colors are ``black'' and ``gray,'' but other colors can be used.

CEXLAB, CEXAXIS

These parameters are used to plot the sizes of the X-axis and Y-axis labels and X- and Y-axis, by default these parameters have values of 1.15 and 1.05, respectively, but it is possible to use other values.

LWDtsX, LWDtsY

Line-widths for the first and the second variable when these are plotted, by default these have values of 1, but other values (widths) can be used.

LWDcoef, LWDpval

The line-widths to be used when the correlation coefficients and their respective p-values are plotted, by default these parameters have a value of 1, but it is possible to use other values.

CorMethod

The method used to estimate the correlations, by default is ``spearman,'' but other options (``pearson'' and ``kendall'') are available (please look at: R>?cor.test).

widthwin

Window's size to compute the rolling window correlations, this MUST be odd (i.e. of the form 2m + 1, where m is a natural number), the minimum value is 3 (the default value), but this information MUST be provided.

Align

To align the rolling object, RolWinMulCor ONLY uses the ``center'' option (please look at: R>?running) to ensure that variations in the correlation are aligned with the variations in the relationship of the time series under study rather than being shifted (Polanco-Mart<U+00ED>nez, 2019).

pvalcorectmethod

The p-value correction method to be used, by default the method of Benjamini and Hochberg (BH) (1995) is used since this is less conservative and performs much better than Bonferroni, but other six methods (e.g. Holm, Bonferroni, etc.) are available (please look at: R>?p.adjust).

rmltrd

Remove (by default is ``Y'' or ``y''; please use ``N'' or ``n'' otherwise) the linear trend in the two time series under analysis.

Scale

Scale (by default is ``Y'' or ``y''; please use ``N'' or ``n'' otherwise) is used to ``normalize'' or ``standardize'' the time series under analysis.

device

Kind of plot output (please look at: R>?device), there are five options: ``png'', ``jpeg/jpg'', ``eps'', ``pdf'' and ``screen.'' By default device is ``screen.''

Hfig, Wfig

Plot's height and width (for the device) for ``png'' and ``jpg'' format (look at R>?png or R>?jpg), by default Hfig and Wfig have values of 1200 and 900, but it is possible to use other values.

resfig

Image resolution (in ``ppi'') for the plot in ``png'' and ``jpg'' format (look at R>?png, R>?jpg, or R>?jpeg), by default this has a value of 150, but it is possible to use other image resolutions.

Hpdf, Wpdf

Plot's height and width (for the device) for ``pdf'' or ``eps'' format (>R?pdf or >R?postscript), by default Hpdf and Wpdf have values of 5.15 and 13.5, but other values can be used.

HeigWin1, HeigWin2

Proportion of window's size to plot the time series under analysis (HeigWin1) and the rolling window correlation coefficients and p-values (HeigWin2) (look at: R>?layout to get more information about ``layout''). By default HeigWin1 and HeigWin2 have values of 2.05 and 2.75, but other values can be used.

ofilename

Output file name.

Value

Outputs:

Plot output: screen or 'plot_bivariate_' + 'ofilename + .png, .jpg, .eps or .pdf'.

Numerical output: a list containing Correlation_coefficients, P_values_corrected, and P_values_not_corrected, which are self-explanatory.

Details

The rolwincor_1win function estimates the rolling window correlation between TWO time series (bi-variate case) sampled on identical time points for ONLY ONE window-length and plots the rolling correlation coefficients and their respective p-values. rolwincor_1win uses the functions running (package:gtools), the native R functions cor, cor.test, and p.adjust (package:stats), and some pieces of code written specifically to our R RolWinMulCor package.

References

Benjamini, Y., and Hochberg, Y. (1995). Controlling the false discovery rate: a practical and powerful approach to multiple testing. Journal of the Royal Statistical Society Series B, 57 (1), 289-300. <URL: https://rss.onlinelibrary.wiley.com/doi/10.1111/j.2517-6161.1995.tb02031.x>.

Polanco-Mart<U+00ED>nez, J. M. (2019). Dynamic relationship analysis between NAFTA stock markets using nonlinear, nonparametric, non-stationary methods. Nonlinear Dynamics, 97(1), 369-389. <URL: https://doi.org/10.1007/s11071-019-04974-y>.

Telford, R.: Running correlations -- running into problems (2013). <URL: https://quantpalaeo.wordpress.com/2013/01/04/running-correlations-running-into-problems/>.

Examples

Run this code
# NOT RUN {
 # Loading packages
 library("RolWinMulCor") 
 library("gtools") 
 library("pracma") 
 # Loading data set  
 data(synthetic_data)
 # Testing the function 'rolwincor_1win' (bi-variate case)
 # Window length = 21 and plot output in screen format 
 test1_rolwincor_1win <- rolwincor_1win(synthetic_data[,1:3], varnametsX="X", 
                          varnametsY="Y", units="NU", widthwin=21, 
                          device="screen")
 # Window length = 51 and plot output in screen format 
 test2_rolwincor_1win <- rolwincor_1win(synthetic_data[,1:3], varnametsX="X", 
                          varnametsY="Y", units="NU", widthwin=51, 
                          device="screen")
 # Window length = 51 and plot output in PDF format 
 
# }
# NOT RUN {
  test3_rolwincor_1win <- rolwincor_1win(synthetic_data[,1:3], varnametsX="X", 
                          varnametsY="Y", units="NU", widthwin=51, 
                          device="pdf", ofilename="test3")
# }

Run the code above in your browser using DataLab