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. enter image description here

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.

  • $\begingroup$ 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? $\endgroup$ Commented Jan 5, 2019 at 10:27
  • 2
    $\begingroup$ 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? :) $\endgroup$
    – Qeshet
    Commented Jan 5, 2019 at 19:34

1 Answer 1


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:

(-3.0704779047168596, 0.028816508715839483, 0, 106, {'1%': -3.4936021509366793, '5%': -2.8892174239808703, '10%': -2.58153320754717}, -723.247574137278)
(-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)
  • $\begingroup$ The links are dead $\endgroup$
    – Nick
    Commented Dec 22, 2019 at 14:21
  • $\begingroup$ My apologies. Those links were outdated, and I have linked to the new ones above. $\endgroup$ Commented Dec 23, 2019 at 22:28
  • $\begingroup$ Hi Michael, your answer has been of great help to me but the links are now dead again - could you update please? $\endgroup$
    – JDraper
    Commented Feb 11, 2021 at 22:03
  • $\begingroup$ 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? $\endgroup$
    – JDraper
    Commented Feb 11, 2021 at 22:10
  • $\begingroup$ 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. $\endgroup$ Commented Feb 13, 2021 at 11:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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