Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding lag at which cross correlation is maximum ccf( )

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?

like image 228
tan Avatar asked Apr 29 '12 02:04

tan


People also ask

What does the cross correlation function CCF measure?

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.

How do you interpret cross-correlation lags?

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.

How do you read a CCF plot?

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.

What is lag in correlation?

The correlation between two series where one of the series has a lag with reference to the other.


2 Answers

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)
} 
like image 53
tan Avatar answered Sep 20 '22 16:09

tan


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)
}
like image 43
nvogen Avatar answered Sep 20 '22 16:09

nvogen