I want to compare two time series in R and see if they are cross-correlated (with ccf).

I believe that they have to be stationary? How that my data is stationary in R and are there any other requirements before I can do ccf?

up vote 2 down vote accepted

The sample cross correlation function is useful to identify which variable is leading or lagging. You can learn more about it here. Note that if you have non-stationary data you may find some spurious correlation between the two series, so you must first check if this is the case.

To check if a series is stationary you can use unit root tests. The most common is the Augmented Dickey Fuller test. It can be implemented in R with the urca package using the following code:

library(urca)
adf <- ur.df(x, type = "drift", lags = 10, selectlags = "AIC")
summary(adf)

If the null hypothesis of unit root is rejected for both series you are good to go. If you have doubts about interpreting the ADF test results take a look at this question. If the series are non-stationary one way of addressing this is to differentiate them and do the test again until they are stationary. Note that you usually are not able to do major interpretations with the results of the cross correlation function, but it is a good tool to help you fit your model.

  • Thanks for the answer. I just tried to execute that command and it gave me "x is not a vector or matrix". I had just read in x as a series of numbers (from a csv file). – dorien Jan 22 '16 at 0:08
  • oh, think I got it: x <- matrix(unlist(z=x), ncol = 1) – dorien Jan 22 '16 at 0:11
  • How do i determine the number of lags? And if the data is non-stationary, do you happen to have a suggestion on how I can prove a correlation between two time series? – dorien Jan 22 '16 at 9:24
  • Using the code above you set just the maximum number of lags to 10 and the test select automatically by the AIC criteria. If all series are non stationary, differentiate them and test again. If they became stationary, then they all are I(1). In this case you can use the Johansenn test to check if there is a long run relationship between them. In case there are not, just run the ccf on the differentiated series. – Regis A. Ely Jan 22 '16 at 11:35
  • Thanks you. Should I also test for autocorrelation in order to use cross-correlation? – dorien Jan 24 '16 at 10:58

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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