Cross-Correlation Function and Granger Causality

When it comes to dealing with time series analysis, one of the most important things we wish to determine is whether one time series “Granger-causes” another; i.e. is there a strong correlation between a time series and another given a number of lags. The way we can detect this is through measuring cross-correlation.

For instance, one time series could serve as a leading or lagging indicator whereby the effect of a change in one time series transfers to the other time series several periods later. This is quite common in economic data – e.g. an economic shock having an effect on GDP two quarters later, for instance.

But how do we measure the lag where this is significant? One very handy way of doing so in R is using what is called the “ccf” (cross-correlation) function. Essentially, running this function allows us to determine the lag at which the correlation between two time series is strongest and warrants further investigation.

Two important things that we must ensure when we run a cross-correlation is that our time series is stationary and that once we have chosen the suitable lag, we are then able to detect and correct for serial correlation if necessary. If you are unfamiliar with the above, then I recommend first reviewing two of my previous posts on stationarity and serial correlation:

The sample dataset I use for this example is the campus gym dataset from Kaggle. Essentially, we wish to determine if there is a correlation between the change in temperature every 10 minutes and the number of people using a campus gym.

When we plot the number of people and temperature for every 10 minutes, we see that there is evidence of possible trend for both variables and thus non-stationarity may be present:

plot1

plot2

Let us now formally test for stationarity using the ADF and KPSS tests:

While our ADF test indicates stationarity, the KPSS test conversely predicts that a unit root is present. In this regard, in spite of the inconsistency, the plots still indicate a trend and I therefore choose to difference the two series:

We now see that while the ADF test still shows a p-value below 0.05, the KPSS test now shows a p-value above 0.05, indicating trend stationarity. Moreover, when we plot the ACF for the two differenced variables, stationarity is indicated:

acf1

acf2

Let us now plot the cross-correlation (CCF function) for the two differenced variables:

cross-correlation

From the plot, we see that we have the strongest cross-correlations at lag 0 and lag -39. So, let’s go ahead and run regressions on these:

We see that when we have run both of the regressions and corrected for serial correlation, we have a positive and significant independent variable at lag 0 but a negative and insignificant independent variable at lag -39. In this regard, the output suggests that the correlation is indeed strongest and significant when no lag is present.

Of course, it could be the case that when using the cross-correlation function, we observe the strongest correlations at a certain lag – this did not happen in this instance. However, when looking at correlations across time series models, it always helps to experiment with correlations across different periods to obtain the strongest predictive model.

Get your free data science course for R and Python
                                                                                                                                                  Subscribe to my mailing list, and receive my data science course, "Regression Analysis and Data Structuring Methods: R and Python", along with weekly data science newsletters.
Your details will not be shared with any third party, and will be used solely to send you the free course and weekly data science newsletters.