I have a TS object in R with 78 variables. These all represent different macroeconomic indicators. The last variable is a balance of some kind that I want to compare each of these indicators. Currently I have been using the ccf() function to do this.

I am wondering about the stationarity of my TS and have a few questions:

When comparing time series using ccf(), do both time series need to be stationary?

  • Currently my independent variable (the balance I am comparing to the macroeconomic indicators) is not very stationary, with an adf test p-value of 0.17.

If they do both need to be stationary as I suspect, what is the easiest way to do this in R? Some of these 78 variables are stationary and some are not.

To Summarize my three questions are:

  1. When comparing time series using ccf(), do both time series need to be stationary?
  2. If they do both need to be stationary as I suspect, what is the easiest way to do this in R?
  3. If the diff() function is a good way to accomplish this, can I difference only one variable to bring the adf-test p-value down to below 0.05? Or do both variables need to be differenced to compare them?
  • $\begingroup$ I'm running out but: ccf needs stationarity. differencing might help. it might not. they both need to be I(0) (i.e: stationary ). but sometimes you can have cointegrated variables. 78 variables is a lot !!!! $\endgroup$
    – mlofton
    Jul 25, 2019 at 13:58
  • $\begingroup$ @mlofton just to clarify. I am only doing 78 comparisons total, or 78 calls to ccf(). Good to know they both need to be stationary though. My main question now is if I can compare a differenced TS to a non-differenced TS, if they both have a adf p-value below 0.05 $\endgroup$ Jul 25, 2019 at 14:11
  • $\begingroup$ hi: I don't follow the last sentence. what has an adf pvalue of below 0.05 and what are you comparing ? $\endgroup$
    – mlofton
    Jul 26, 2019 at 12:10
  • $\begingroup$ @mlofton Hi, my independent TS (the balance I am comparing to the Macro indicators) has an adf pvalue above 0.05 (meaning it is not stationary). I am wondering how I can make this variable stationary, so that I can compare it with the other variables (The macro indicators). I have thought about taking the difference of the balance number, and then comparing it to the other TS but I'm not sure if that is correct. The main problem is that I have variables which are stationary, and some that are not, and I am confused as to how I should compare them properly. $\endgroup$ Jul 26, 2019 at 14:08
  • $\begingroup$ fortunately, there's a whole theory on that and engle-granger got the nobel prize for it. It's the idea of cointegration which says that it is possible for two variables to be I(1) ( i.e: not stationary ) and, if they are "cointegrated", the two variables then have an error correction representation which can be used model the relation between the levels of the two variables and the changes in the levels. I couldn't possibly do the topic justice in a comment. But, if you google for "error correction representation" or "cointegration", there is a ton of material written on it. $\endgroup$
    – mlofton
    Jul 27, 2019 at 18:34

1 Answer 1


Here is what I think you could do. I will first describe the overall idea and then provide a reproducible code that you can use for your case.

First, according to Box and Jenkins (Box, G. E., Jenkins, G. M., Reinsel, G. C., & Ljung, G. M. (2015). Time series analysis: forecasting and control. John Wiley & Sons.), cross correlation function will not work meaningfully if there is a pronounced autocorrelation in the input ("independent") series. So, before applying ccf it makes sense to check if the input is autocorrelated. If it is not, then ccf is safe to apply. Fortunately, Box & Jenkins offer a solution in the case when input is in fact autocorrelated (including when non-stationary), which is based on the idea of pre-whitening. Here is the idea: a) You fit a suitable ARIMA model to the input, b) you then get the residuals from the model fit from previous step; c) then you take that particular model and fit it to your dependent variable (i.e. the output), and get the residuals from that model fit; d) finally, you cross-correlate the residuals from step (b) with the residuals from step (c) to get an accurate relationship between the underlying dynamics of your original series.

Now then, applying this to your case and based on your description, the independent variable in your case is the "balance" and you have 78 dependent variables. Here is a reproducible example to explain how this can be done in your case using R. For simplicity, I am limiting myself to only 2 dependent variables (instead of 78). Note also, that I am using the prewhiten function from the TSA package in R.


N=100#let's say there are 100 observations in each series
num.indep.ts=2#you will have 78 here

x=arima.sim(n = N, list(ar = c(0.995))) #This will make the input close to unit-root nonstationary


for (i in 1:num.indep.ts){

  y=arima.sim(n = N, list(ar = c(i/N)))
  y.table=cbind(y.table, y)}

#the 0.05 threshold is somewhat arbitrary below

if (adf.result$p.value<0.05){ 

for (i in 1:num.indep.ts)
{ccf(x, y.table[,i])}
  for (i in 1:num.indep.ts) {prewhiten(x, y.table[,i])}}

Note, that a very readable exposition to the ideas that I outlined above is in Bisgaard, S., & Kulahci, M. (2006). Quality Quandaries: Studying input-output relationships, part I. Quality Engineering, 18(2), 273-281.

  • $\begingroup$ Hi: I read your R code and you A) test each independent variable for stationarity, If the variable is stationary, you run the ccf on the response and independent series. If not, you pre-whiten. I imagine that this could be useful if one restricts themselves to the stationary case. My point is that he doesn't need stationarity of the two variables. He can work with two I(1) variables if he uses the EG methodology. In the multivariate case, he can use the Johansen machinery and the details can be found in a text or notes. Depending on the level of the OP, enders text possibly or Hamilton's. $\endgroup$
    – mlofton
    Jul 27, 2019 at 23:48
  • $\begingroup$ So pre-whitening will help when the input (independant variable) is autocorrelated. However, does this still work when the dependant variable is also autocorrelated? What about when the dependant is stationary? Also, is this program still viable when the independant is stationary and the dependant is non-stationary/autocorrelated? I guess the best question to answer here is In what scenarios is pre-whitening the optimal choice? $\endgroup$ Jul 30, 2019 at 13:12

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.