I have 2 time series and I am using ccf
to find the cross correlation between them.
ccf(ts1, ts2)
lists the cross-correlations for all time lags. How can I find the lag which results in maximum correlation without manually looking at the data?
The cross-correlation function (CCF) identifies lags or leads of the two time-series. The CCF is defined as the set of correlations between xt+h and yt for h=0, ±1, ±2, ±3, and so on. A negative value for h is a correlation between the x-variable at a time before t and the y-variable at time t.
The lag refers to how far the series are offset, and its sign determines which series is shifted. Note that as the lag increases, the number of possible matches decreases because the series “hang out” at the ends and do not overlap.
A plot of the X data vs. the Y data at lag 𝑘 may show a positive or negative trend. If the slope is positive, the cross correlation is positive; if there is a negative slope, the cross correlation is negative.
The correlation between two series where one of the series has a lag with reference to the other.
Posting the answer http://r.789695.n4.nabble.com/ccf-function-td2288257.html
Find_Max_CCF<- function(a,b)
{
d <- ccf(a, b, plot = FALSE)
cor = d$acf[,,1]
lag = d$lag[,,1]
res = data.frame(cor,lag)
res_max = res[which.max(res$cor),]
return(res_max)
}
I thought I'd redo the above function but have it find the absolute max correlation that returns the original correlation (positive or negative). I also maxed out (nearly) the number of lags.
Find_Abs_Max_CCF<- function(a,b)
{
d <- ccf(a, b, plot = FALSE, lag.max = length(a)-5)
cor = d$acf[,,1]
abscor = abs(d$acf[,,1])
lag = d$lag[,,1]
res = data.frame(cor,lag)
absres = data.frame(abscor,lag)
absres_max = res[which.max(absres$abscor),]
return(absres_max)
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With