I have 2 data frames, separate ones, where I have the log daily returns of some stocks. For example:
Index Date Close Volume Open High Low Return
1259 27/07/2015 627.26 2673801 621 634.3 620.5 NA
1258 28/07/2015 628 1713684 632.83 632.83 623.31 1.18E-03
1257 29/07/2015 631.93 1573146 628.8 633.36 622.65 6.24E-03
1256 30/07/2015 632.59 1472286 630 635.22 622.05 1.04E-03
1255 31/07/2015 625.61 1705286 631.38 632.91 625.5 -1.11E-02
Suppose this is df1 and the second df2 is similar. Variable of the returns have the same name (df1$Return, df2$Return)
I am trying to create a rolling correlation for example of 5 days window. I have some questions, as I am pretty new to this:
- What is the easiest way to do that with separate data frames? I searched a bit and try to understand how to apply the rollapply but with no success. Can somebody explain how I can create the function and then apply it?
Something like:
corx <- function(x) cor(df1$return, df2$return)
mycors <- rollapply(c(df1$return, df2$return),5, corx)
- Also I had a general question, what is the best practice of sorting the time series date, ascending or descending? I suppose this might have an effect on applying the rollapply , so I would be interested to understand.
Thank you in advance