tan*_*tan 13 r time-series correlation
我有两个时间序列,我用它ccf来找到它们之间的互相关.
ccf(ts1, ts2)列出所有时间滞后的互相关.如何在不手动查看数据的情况下找到导致最大相关性的滞后?
tan*_*tan 18
发表答案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)
}
Run Code Online (Sandbox Code Playgroud)
nvo*_*gen 11
我以为我会重做上面的函数但让它找到返回原始相关性(正或负)的绝对最大相关性.我也达到了(差不多)滞后的数量.
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)
}
Run Code Online (Sandbox Code Playgroud)