Finding lag at which cross correlation is maximum ccf( )

后端 未结 4 2107
野的像风
野的像风 2020-12-23 17:55

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. H

4条回答
  •  萌比男神i
    2020-12-23 18:40

    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)
    }
    

提交回复
热议问题