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. 


*

*I have thought about using diff() to take the difference of my independant variable and comparing it with the dependants as seen here: https://towardsdatascience.com/cross-correlation-of-currency-pairs-in-r-ccf-d27eec2d4b91.

*However in that example both of the variables are differenced then compared. In mine only some would need to be differenced as only some have an adf-test p-value above 0.05.


To Summarize my three questions are: 


*

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

*If they do both need to be stationary as I suspect, what is the easiest way to do this in R?

*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? 

 A: 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)
adf.result=adf.test(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

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

for (i in 1:num.indep.ts)
{ccf(x, y.table[,i])}
  }else{
  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. 
