G'day Patrick, On Tue, 16 Jan 2024 09:19:40 +0100 Patrick Giraudoux <patrick.giraud...@univ-fcomte.fr> wrote:
[...] > So far so good, but when I lag one of the series, I cannot find the > same correlation as with ccf > > > cor(x[1:(length(x)-1)],y[2:length(y)]) [1] -0.7903428 > > ... where I expect -0.668 based on ccf > > Can anyone explain why ? The difference is explained by cff() seeing the complete data on x and y and calculating the sample means only once, which are then used in the calculations for each lag. cor() sees only the data you pass down, so calculates different estimates for the means of the two sequences. To illustrate: [...first execute your code...] R> xx <- x-mean(x) R> yy <- y-mean(y) R> n <- length(x) R> vx <- sum(xx^2)/n R> vy <- sum(yy^2)/n R> (c0 <- sum(xx*yy)/n/sqrt(vx*vy)) [1] -0.5948694 R> xx <- x[1:(length(x)-1)] - mean(x) R> yy <- y[2:length(y)] - mean(y) R> (c1 <- sum(xx*yy)/n/sqrt(vx*vy)) [1] -0.6676418 The help page of cff() points to MASS, 4ed, the more specific reference is p 389ff. :) Cheers, Berwin ______________________________________________ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.