Dear all, given two numeric vectors x and y, the ACF(x) at lag k is cor(x(t),x(t+k)) while the CCF(x,y) at lag k is cor(x(t),y(t-k)). See below for a simple example. > set.seed(1) > x <- rnorm(10) > y <- rnorm(10)> x[1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 0.4874291 0.7383247 0.5757814 -0.3053884> y[1] 1.51178117 0.38984324 -0.62124058 -2.21469989 1.12493092 -0.04493361 -0.01619026 0.94383621 0.82122120 0.59390132> x <- x - mean(x); > y <- y -mean(y);## ACF OF x(t)> print(acf(x,lag=2,plot=F),digits=5))Autocorrelations of series 'x', by lag 0 1 2 1.00000 -0.26486 -0.25352 ## For Instance ACF(x) at lag 1 is Corr(x(t),x(t+1)): > sum(x[1:9]*x[2:10])/sum(x^2) [1] -0.2648633 ## whereas CCF(x,y) at lag 1 is Corr(x(t),y(t-1)) > ccf(x,y,lag=2,plot=F) Autocorrelations of series 'X', by lag -2 -1 0 1 2 -0.139 0.593 -0.377 -0.382 0.116 > sum(x[1:9]*y[2:10])/sqrt(sum(x^2)*sum(y^2)) [1] 0.5930216> sum(x[2:10]*y[1:9])/sqrt(sum(x^2)*sum(y^2))[1] -0.3822885 from a quick survey on textbooks (Brockwell and Davis, Chatfield, Fuller) it looks like different authors use different conventions so that I think that it would be nice to clarify this in the documentation. Ciao Simone> R.version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status beta major 2 minor 4.0 year 2006 month 09 day 20 svn rev 39421 language R version.string R version 2.4.0 beta (2006-09-20 r39421)> Sys.getlocale()[1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United States.1252;LC_MONETARY=English_United States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252" ______________________________________________________ Simone Giannerini Dipartimento di Scienze Statistiche "Paolo Fortunati" Universita' di Bologna Via delle belle arti 41 - 40126 Bologna, ITALY Tel: +39 051 2098262 Fax: +39 051 232153
Using "ts" objects these can be written in the following consistent manner:> sum(ts(x) * lag(ts(x)) / sum(ts(x)^2))[1] -0.2648633> sum(ts(x) * lag(ts(y)) / sqrt(sum(ts(y)^2) * sum(ts(x)^2)))[1] 0.5930216 On 10/2/06, Simone Giannerini <sgiannerini at gmail.com> wrote:> Dear all, > > given two numeric vectors x and y, the ACF(x) at lag k is > cor(x(t),x(t+k)) while the CCF(x,y) at lag k is cor(x(t),y(t-k)). See > below for a simple example. > > > set.seed(1) > > x <- rnorm(10) > > y <- rnorm(10) > > x > [1] -0.6264538 0.1836433 -0.8356286 1.5952808 0.3295078 -0.8204684 > 0.4874291 0.7383247 0.5757814 -0.3053884 > > y > [1] 1.51178117 0.38984324 -0.62124058 -2.21469989 1.12493092 > -0.04493361 -0.01619026 0.94383621 0.82122120 0.59390132 > > x <- x - mean(x); > > y <- y -mean(y); > > ## ACF OF x(t) > > print(acf(x,lag=2,plot=F),digits=5)) > > Autocorrelations of series 'x', by lag > > 0 1 2 > 1.00000 -0.26486 -0.25352 > > ## For Instance ACF(x) at lag 1 is Corr(x(t),x(t+1)): > > > sum(x[1:9]*x[2:10])/sum(x^2) > [1] -0.2648633 > > ## whereas CCF(x,y) at lag 1 is Corr(x(t),y(t-1)) > > ccf(x,y,lag=2,plot=F) > > Autocorrelations of series 'X', by lag > > -2 -1 0 1 2 > -0.139 0.593 -0.377 -0.382 0.116 > > > sum(x[1:9]*y[2:10])/sqrt(sum(x^2)*sum(y^2)) > > [1] 0.5930216 > > > sum(x[2:10]*y[1:9])/sqrt(sum(x^2)*sum(y^2)) > [1] -0.3822885 > > from a quick survey on textbooks (Brockwell and Davis, Chatfield, > Fuller) it looks like different authors use different conventions so > that I think that it would be nice to clarify this in the > documentation. > > Ciao > > Simone > > > R.version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status beta > major 2 > minor 4.0 > year 2006 > month 09 > day 20 > svn rev 39421 > language R > version.string R version 2.4.0 beta (2006-09-20 r39421) > > > Sys.getlocale() > [1] "LC_COLLATE=English_United States.1252;LC_CTYPE=English_United > States.1252;LC_MONETARY=English_United > States.1252;LC_NUMERIC=C;LC_TIME=English_United States.1252" > > > ______________________________________________________ > > Simone Giannerini > Dipartimento di Scienze Statistiche "Paolo Fortunati" > Universita' di Bologna > Via delle belle arti 41 - 40126 Bologna, ITALY > Tel: +39 051 2098262 Fax: +39 051 232153 > > ______________________________________________ > R-devel at r-project.org mailing list > stat.ethz.ch/mailman/listinfo/r-devel >