Question

I have multiple time series and want to study whether certain events happen at relatively the same time in the different series. For example say I have x1 and x2 which are both time-series but from different sources, and I want to know whether x1 increases/decreases at relatively the same time as x2. How would you go about that. I know I can use correlation to measure the general relationship.

But is there a way to isolate periods during which they were moving together, from periods during which they deviated?

Was it helpful?

Solution

Here is an example were we calculate the rolling correlation in a 30 day window, and then plot the result:

library(zoo)
roll.corrs <- rollapplyr(dates, 30, function(index) cor(df1[index, "x1"], df1[index, "x2"]), fill=NA)
library(ggplot2)
qplot(as.Date(dates), roll.corrs)

or, shorter:

z <- zoo(df1, as.Date(rownames(df1)))
r <- rollapplyr(z, 30, function(x) cor(x[,1], x[,2]), by.column = FALSE) 
autoplot(r)

The qplot output shows: enter image description here

You can clearly see how there are in particular two sections with particularly high correlations between the time series. The data is purposefully designed to have periods of high and low correlation. Here is the data:

base <- c(rep(1, 100), 1:50, rep(50, 100), 50:1, rep(1, 65))
dates <- as.character(seq(as.Date("2013-01-01"), len=365, by="1 day"))

set.seed(1)
df1 <- data.frame(  
  x1=base+rnorm(365, 0, 5),
  x2=base+rnorm(365, 0, 5),
  row.names=dates
)

OTHER TIPS

Have you tried looking at the correlation within subintervals of your overall timeseries? Then you have to decide what size to make the subintervals, perhaps set a condition for "sufficient correlation", etc.


Edit - another idea - one could define a function F(tau) which is the correlation between the two timeseries in the [0,tau] subinterval, and look at the derivative of it? But this maybe assumes that it's well-correlated around T = 0 anyway.

Maybe one could find "seed" times where the two are well correlated, and then grow the intervals around it by defining such G(width) as correlation in [seed, seed + width]) and similarly examining the derivative with respect to width?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top