0

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:

  1. 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)
  1. 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

Jaap
  • 81,064
  • 34
  • 182
  • 193
Arg
  • 35
  • 1
  • 4

1 Answers1

1

Invoke rollapplyr over the indexes like this:

library(zoo)

set.seed(123)

ret1 <- rnorm(10)
ret2 <- rnorm(10)

rollapplyr(seq_along(ret1), 5, function(ix) cor(ret1[ix], ret2[ix]), fill = NA)
##  [1]         NA         NA         NA         NA -0.2440453  0.6974227
##  [7]  0.7729576  0.9777006  0.8008661  0.8428961

or cbind the return vectors together and use by.column = FALSE :

ret <- cbind(ret1, ret2)
rollapplyr(ret, 5, function(x) cor(x[, 1], x[, 2]), by.column = FALSE, fill = NA)
##  [1]         NA         NA         NA         NA -0.2440453  0.6974227
##  [7]  0.7729576  0.9777006  0.8008661  0.8428961

Normally time is ascending. If you create a ts, zoo or xts time series object that is the convention used.

G. Grothendieck
  • 254,981
  • 17
  • 203
  • 341
  • How though can I call in my function variables from the two different dataframes? For example I used it like this: `rollapplyr(seq_along(df1[,1]), 5, function(x,y) cor(x,y), df1$returns, df2$returns, fill = NA)` But i am getting an error. Of course this applies to the first solution, without binding the variables (I'll use that if I don't manage to find a solution with the separate dataframes) Thank you for the help! – Arg Jul 27 '20 at 21:42
  • `ret1 <- df1$returns; ret2 <- df2$returns` – G. Grothendieck Jul 28 '20 at 01:21