12

I have zoo object with 10000+ rows.

> head(tt)
                      A             B
2007-01-04  0.005945924  0.0021167475
2007-01-05 -0.004201991 -0.0080020024
2007-01-08  0.001740897  0.0045804104
2007-01-09  0.000000000 -0.0008163931
2007-01-10 -0.004503531  0.0032615812
2007-01-11 -0.005841138  0.0043863282

I have tried variations of the following line, but to no avail.

rollapply(tt, 21, function(x) cor(x[,1],x[,2]))

Every entry gave correlation of 1, looks like it's picking up the 1 off the diagonal of the correlation matrix.

2013-11-25  1  1
2013-11-26  1  1
2013-11-27  1  1
2013-11-29  1  1
2013-12-02  1  1
2013-12-03  1  1

What I really want is -0.4649, like the following

> cor(tt)
           A          B
A  1.0000000 -0.4649881
B -0.4649881  1.0000000
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
simon
  • 139
  • 1
  • 1
  • 5

3 Answers3

14

For your simple case, you could use TTR::runCor.

set.seed(21)
x <- rnorm(30)
y <- rnorm(30)
z <- zoo(cbind(x,y),Sys.Date()-1:30)
tail(rollapplyr(z, 21, function(x) cor(x[,1],x[,2]), by.column=FALSE))
tail(runCor(z[,1],z[,2],21))
Joshua Ulrich
  • 173,410
  • 32
  • 338
  • 418
9

Try something like this:

x<-rnorm(100)
y<-rnorm(100)
rollapply(data.frame(x,y), 21 ,function(x) cor(x[,1],x[,2]), by.column=FALSE)

In other words, I think you may just need the by.column=FALSE argument. Works with a zoo object too

rollapply(zoo(cbind(x,y),Sys.Date()-1:100), 21 ,function(x) cor(x[,1],x[,2]), by.column=FALSE)

Edit to address a question from the comment about adding another column.

You can specify the columns you want to use in the cor function.

z<-rnorm(100)
rollapply(zoo(cbind(x,y,z),Sys.Date()-1:100), 21 ,function(x) cor(x[,1],x[,3]), by.column=FALSE)
rollapply(zoo(cbind(x,y,z),Sys.Date()-1:100), 21 ,function(x) cor(x[,2],x[,3]), by.column=FALSE)

by.column=FALSE indicates that the function should not be applied to each column separately. If by.column=TRUE, then the function will be applied to each column separately, and this is the default behavior.

Jota
  • 17,281
  • 7
  • 63
  • 93
  • it works! what exactly does the by.column option do? if i was to have a 3x3 cor matrix, how would one go about picking up the right correlation? – simon Dec 20 '13 at 00:57
  • @Jota, thanks for the comments, I asked the same question about more columns to Joshua Ulrich and then saw your amendments so I just deleted it, apoloiges. If I had 100 columns of data is there a more efficient way to run the cor function rather than specifying each pair? Could this be achieved using one of the apply family functions? – TheGoat Mar 08 '17 at 21:33
  • 1
    You can use this function in `rollapply` to handle any number of columns: `function(x) cor(x)[lower.tri(cor(x))]` – G. Grothendieck Oct 03 '17 at 13:43
0

Here's how to compute 6 month running correlations using tq_transmute_xy from the tidyquant package:

library(tidyquant)
    
x<-rnorm(100)
y<-rnorm(100)
    
zoo(cbind(x,y),Sys.Date()-1:100)
    
tq_transmute_xy(x = x, y = y, 
                mutate_fun = runCor,
                n = 6,
                col_rename = "rolling.corr.6")
asachet
  • 6,620
  • 2
  • 30
  • 74
Gucci148
  • 1,977
  • 1
  • 13
  • 4