# Preparing data for cross-correlation time series

I have several data sets with air quality measurements for 20 locations. The measurements were done per second over a period of two weeks, 5 locations per period (because there were only 5 instruments).

The data looks stationary when plotted (see plot for 1 period). I want to calculate the cross-correlation between the relevant time series, to see if there is a temporal correlation between the measured concentrations at the various sites.

It does not feel useful to analyze the data per second. What is the best way to go with this? I tried to use hourly average. Or should I smooth the data? I read something about modeling the data (How to use Pearson correlation correctly with time series), but that sounds rather complicated.

Edit: I am using R.

• If you want to calculate the cross-correlation between the relevant time series why do you not feel [it is] useful to analyze the data per second? – Benjamin Christoffersen Jan 5 '19 at 10:27
• Because when measuring air pollutants (in this case ultrafine particles) the measurements tend to have a lot of irrelevant peaks which are considered artefacts, whereas the mean/moving average over a longer period is considered a good representative of the actual measurements. I guess I just kind of answered my own question? :) – Qeshet Jan 5 '19 at 19:34

## 1 Answer

When attempting to detect cross-correlation between two time series, the first thing you should do is make sure the time series are stationary (i.e. have a constant mean, variance, and autocorrelation).

The reason this is important is because a correlation is looking to measure a linear relationship between two variables. Presence of a time series trend interferes with gauging a true correlation between two time series variables, i.e. is it a true correlation or simply due to chance.

In this regard, firstly use the Dickey-Fuller test to screen for stationarity (it would help if you specify the software package you are using, I am using Python in this instance). Suppose you have two time series x and y:

xdf = ts.adfuller(x, 1)
ydf = ts.adfuller(y, 1)


Here's some sample output:

xdf
(-3.0704779047168596, 0.028816508715839483, 0, 106, {'1%': -3.4936021509366793, '5%': -2.8892174239808703, '10%': -2.58153320754717}, -723.247574137278)
ydf
(-2.949959856756157, 0.03983919029636401, 1, 105, {'1%': -3.4942202045135513, '5%': -2.889485291005291, '10%': -2.5816762131519275}, -815.3639322514784)


In this instance, we have p-values below 0.05, so the series do not need to be differenced for stationarity. In the case that we did, it would be necessary to difference the series. The following tutorial might help you.

Now, it is a matter of calculating the cross-correlation between x and y, and generating the lags:

# Calculate correlations
cc1 = np.correlate(x - x.mean(), y - y.mean())[0] # Remove means
cc1 /= (len(x) * x.std() * y.std()) #Normalise by number of points and product of standard deviations
cc2 = np.corrcoef(x, y)[0, 1]
print(cc1, cc2)


Upon obtaining the cross-correlation coefficient, the lags can be generated and the autocorrelations calculated:

# Generating lags
lg = 108
x = np.random.randn(lg)

• The links are dead – Nick Dec 22 '19 at 14:21
• My apologies. Those links were outdated, and I have linked to the new ones above. – Michael Grogan Dec 23 '19 at 22:28
• Hi Michael, your answer has been of great help to me but the links are now dead again - could you update please? – JDraper Feb 11 at 22:03
• Hi Michael, would it be appropriate to 'blend' time series. For instance if one series was differenced for stationarity and the other was not would you be able to combined them in the cross correlation analysis? – JDraper Feb 11 at 22:10
• Unfortunately the link is no longer active, but the previous answer provided should be sufficient for you to implement the model. It would not be particularly appropriate to blend time series in this manner - as having a non-stationary series would only indicate that there is a cross-correlation between two series at a particular point in time. However, we would have no way of knowing if the cross-correlation is generalisable across all time periods. – Michael Grogan Feb 13 at 11:40