Bug 15832 - Cross correlations in R with values >1
Summary: Cross correlations in R with values >1
Status: CLOSED FIXED
Alias: None
Product: R
Classification: Unclassified
Component: Accuracy (show other bugs)
Version: R 3.1.0
Hardware: x86_64/x64/amd64 (64-bit) Linux-Ubuntu
: P5 enhancement
Assignee: R-core
URL:
Depends on:
Blocks:
 
Reported: 2014-06-22 22:14 UTC by Jim Bowery
Modified: 2015-04-17 15:18 UTC (History)
3 users (show)

See Also:


Attachments
Original data -- reduced as per report (272.26 KB, text/csv)
2014-06-22 22:14 UTC, Jim Bowery
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jim Bowery 2014-06-22 22:14:46 UTC
Created attachment 1613 [details]
Original data -- reduced as per report

With the attached data, I ran:

    > x=ccf(a,a,na.action=na.pass)

As expected, 0 lag produced 1.000, but I got some values greater than 1.

       -5    -4    -3    -2    -1     0     1     2     3     4     5     6     7     8     9    10    11    12    13 
    1.000 1.000 1.000 1.001 1.001 1.000 1.001 1.001 1.000 1.000 1.000 0.999 0.999 0.999 0.999 0.998 0.998 0.997 0.997 

Is this a bug in ccf?

By request I minimized the data set down to a vector of 20 scalars.  In order to do so I used the library GenSA with the following result:

    > library(GenSA)
    > a <- read.table("~/a.csv", header=T, quote="\"")
      View(a)
    > b<-na.omit(a)
    > of<-function(p){v=b$x[p[1]:(p[1]+p[2]-1)];y=try(ccf(v,v,na.action=na.pass),TRUE);mx=ifelse(class(y)=="try-error",1,max(y$acf));return(ifelse(mx>1,p[2],as.numeric(length(b$x))))}
    > out<-GenSA(as.numeric(c(1,length(b$x))),of,as.numeric(c(1,20)),as.numeric(c(length(b$x)-1,length(b$x))))
    > out$par
    [1] 12514.27    20.00
    > e=b$x[12514.27:(12514.27+20)]
    > e
      [1] 1.48e-06 1.48e-06 1.49e-06 1.49e-06 1.51e-06 1.51e-06 1.51e-06 1.51e-06 1.51e-06 1.50e-06 1.50e-06 1.50e-06
     [13] 1.50e-06 1.49e-06 1.50e-06 1.50e-06 1.50e-06 1.50e-06 1.51e-06 1.50e-06 1.51e-06
    > r=ccf(e,e)
    > r

    Autocorrelations of series ‘X’, by lag

       -10     -9     -8     -7     -6     -5     -4     -3     -2     -1      0      1      2      3      4      5 
     0.111 -0.056 -0.167 -0.333 -0.333 -0.444 -0.278  0.000  0.333  0.556  1.000  0.556              0.333  0.000 -0.278 -0.444 
     6      7      8      9     10 
     -0.333 -0.333 -0.167 -0.056  0.111 
    > max(r$acf)
    [1] 1
    > max(r$acf)>1
    [1] TRUE
    > 


http://stackoverflow.com/questions/24256101/cross-correlations-in-r-with-values-1


  [1]: https://www.dropbox.com/s/ohn4wngilriogtn/a.csv
Comment 2 Martin Maechler 2015-04-17 15:18:30 UTC
An even smaller reproducible example,
using  acf() instead of  ccf() which calls acf() itself:

e <- rep(c(1.48e-6, 1.49e-6, 1.5e-6, 1.51e-6), c(2,3,9,7))
acf(e, plot=FALSE)$acf[1] - 1

> [1] 2.220446e-16

------------

Fix committed to R-devel  and  "R 3.2.0 patched" (which should be come R 3.2.1).