[R] Lag representation in ccf() while zoo object is used?

Gabor Grothendieck ggrothendieck at gmail.com
Fri Jul 24 16:55:49 CEST 2009


On Fri, Jul 24, 2009 at 10:31 AM, Keith<kigiokli at gmail.com> wrote:
> Dear All,
>
> I have 2 time-series data sets and would like to check the cross
> correlation. These data sets were set as a zoo object, called data,
> and in general look like:
>
>                                        V1             V2
> 2007-01-01 00:00:00      0.0   0.176083
> 2007-01-01 01:00:00      0.0   0.176417
> 2007-01-01 02:00:00      0.0   0.175833
> 2007-01-01 03:00:00      0.0   0.175833
> 2007-01-01 04:00:00      0.3   0.176000
> 2007-01-01 05:00:00      1.8   0.176250
> 2007-01-01 06:00:00      2.0   0.177583
> 2007-01-01 07:00:00      0.2   0.178333
> 2007-01-01 08:00:00      0.3   0.178167
> 2007-01-01 09:00:00      3.2   0.178417
>
> When I applied the ccf method, ccf(data$V1, data$V2), I noticed the
> lag is every 3600 which is a little surprising to me. I was thinking
> the lag should be 1, but it seems the lag unit becomes 3600. I guess
> the number 3600 representing 3600 "seconds" because of the zoo object.

ccf reports in time units (as discussed in ?ccf) and POSIXct,
which is the time scale you have chosen works in seconds.
This seems unrelated to zoo.

> I am not sure if I'm right and would like someone here could certify
> this (or not). Besides, does anyone know any default argument to
> adjust the 3600 into 1 while plotting? The only idea I have is to
> divide the lag manually by 3600 and then plot it later.

Try changing your time scale from POSIXct to one measured in hours:

Lines <- "2007-01-01 00:00:00,0.0,0.176083
2007-01-01 01:00:00,0.0,0.176417
2007-01-01 02:00:00,0.0,0.175833
2007-01-01 03:00:00,0.0,0.175833
2007-01-01 04:00:00,0.3,0.176000
2007-01-01 05:00:00,1.8,0.176250
2007-01-01 06:00:00,2.0,0.177583
2007-01-01 07:00:00,0.2,0.178333
2007-01-01 08:00:00,0.3,0.178167
2007-01-01 09:00:00,3.2,0.178417"

library(zoo)
z <- read.zoo(textConnection(Lines), tz = "", sep = ",")

# zh is z but with time units in hours
zh <- z
time(zh) <- as.numeric(time(z)) / 3600

with(zh, ccf(V2, V3))


Equivalently one could have replaced the two lines
that define zh with this single line:
zh <- aggregate(z, as.numeric(time(z)) / 3600, force)




More information about the R-help mailing list