# Comparing Numerous Macroeconomic Time Series in R with Varying Stationarity

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?
• 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 !!!! – mlofton Jul 25 '19 at 13:58
• @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 – Alexander Mays Jul 25 '19 at 14:11
• hi: I don't follow the last sentence. what has an adf pvalue of below 0.05 and what are you comparing ? – mlofton Jul 26 '19 at 12:10
• @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. – Alexander Mays Jul 26 '19 at 14:08
• 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. – mlofton Jul 27 '19 at 18:34

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.

library(forecast)
library(tseries)
library(TSA)

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
plot(x)

y.table=c()

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