I have simulated the behavior of two variables over the course of three weeks:

var_1 <- ts(c(25.1,21.8,15.6,28.0,25.8,26.2,29.9,30.6,28.3,22.1,20.2,20.5,18.4,12.0,8.1,8.6,8.2,9.17,8.8,9.7,10.4))


var_1 is the independent variable and I would like to see if var_2 is influenced by its fluctuations.

A: var_1 and var_2. B: running correlation

In the figure above (A), var_1 is in black and var_2 in red. Just looking at the curves, I would say that a relationship exists at least until around day 12, then some major lag occurs.

My first idea to highlight any similarity was to apply a running correlation test (fig. B above). I used the function running() from the R library gtools.

running(var_1, var_2, fun=cor, width=5)

The overlapping window has a width of 5 days. The 95% significance boundaries (dashed red lines) are calculated by running 999 simulations of randomly generated data sets with the same structure.

The correlation is rather good between day 6 and 12, but it is never statistically significant. Is there a more appropriate way to point out any common behavior?

Additionally, I tried to apply a cross correlation function to check for significant lags (R function ccf()).

ccf(var_1,var_2, main="")


It shows a strong correlation for lags -3 to 0 with tapering in both directions. If I interpret it correctly, I suppose I could say that it takes 0 to 3 days for var_2 to react to any change in var_1.

While this result is extremely interesting, I feel it does not describe well all the dynamics between the two curves, such as the good correlation limited to day 6 and 12. But, as seen above, the test that could do it does not show any statistical significance. So I am a bit puzzled as to what can be the most fitting method to describe these data.


You seem to have looked at spurious results by looking at correlations of absolute values rather than correlation of changes.

If so, then see these two links for an explanation (ignore otherwise): quant.stackexchange.com/questions/489/correlation-between-prices-or-returns & stats.stackexchange.com/a/133171/114856.

I write "seem" as you did not provide your code and I cannot reproduce your numbers.

#Attempt to reproduce
var_1 <- ts(c(25.1,21.8,15.6,28.0,25.8,26.2,29.9,30.6,28.3,22.1,20.2,20.5,18.4,12.0,8.1,8.6,8.2,9.17,8.8,9.7,10.4))
var_2 <- ts(c(-13.1,-7.5,0.1,-3.4,-6.0,-4.6,-0.1,4.8,4.3,-1.1,-6.5,-10.0,-9.2,-7.8,-7.6,-7.1,-11.4,-14.2,-19.6,-22.9,-23.5))
running(var_1, var_2, fun=cor, width=5, by=1, allow.fewer=TRUE, align=c("right"), simplify=TRUE)

#Same thing but on changes   (use non-log approach as neg values)
chg_1 <- diff(var_1)/var_1[-length(var_1)] 
chg_2 <- diff(var_2)/var_2[-length(var_2)] 
running(chg_1, chg_2, fun=cor, width=5, by=1, allow.fewer=TRUE, align=c("right"), simplify=TRUE)
  • $\begingroup$ Thanks for your answer. I added the running() code to the original post. $\endgroup$ – Steve_s Jul 6 '17 at 10:09
  • $\begingroup$ Could you please explain the meaning of diff(var_x)/var_x[-length(var_x)]? If I got it right, your answer focuses on the magnitude of changes between time series, is that correct? What I would like to highlight is that, e.g., both series show a positive trend from day 5 to 8, then a negative one from 8 to 11 and so on, regardless of the amplitude of those trends. Would using absolute values be correct in this case? $\endgroup$ – Steve_s Jul 6 '17 at 10:23
  • $\begingroup$ That code is running a rolling correlation between the inter-period changes of the two series, i.e. looking at correlations of relative values rather than absolute values. Correlations of absolute values yield spurious results. Please see the links I noted, its all in there; if still not clear those links have some other links as well. Its crucial to understand this before looking any further into the subject. Ignore the amplitude. Its not about amplitude - correlations do not measure amplitude, but rather direction. $\endgroup$ – Krug Jul 6 '17 at 10:39
  • $\begingroup$ By the way, check out the runCor function of TTR package. I like it better for rolling correlations than running. $\endgroup$ – Krug Jul 6 '17 at 10:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.