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?
Solution given by Vikrant Shimpi at the R-Help mailing list https://stat.ethz.ch/pipermail/r-help/2010-July/245607.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)
}