Josh Quigley
2007-Aug-02 05:45 UTC
[R] Using 'diff' on zoo vs zooreg classes (possible bug?)
Hello, Can anyone explain the following behaviour? To me it seems a bug, but maybe it is intentional. It seems that a diff on a zooreg class that is not _strictly_ regular only considers those entries that are 'deltat' apart. In the following, diff on the zooreg class only returns values where the index was one second apart. The example replicates by dev code, but I've also tested using plain old integer indices, and the behaviour is the same. Cheers, Josh. --- Example --- input <- " 2007-01-10 11:14:38 27.77 49.50 40.31000 24.07002 2007-01-10 11:14:39 27.77 49.50 40.31000 24.07000 2007-01-10 11:14:40 27.77 49.49 40.31000 24.07000 2007-01-10 11:14:43 27.77 49.49 40.31000 24.07000 2007-01-10 11:14:45 27.77 49.49 40.31000 24.07000 2007-01-10 11:14:46 27.77 49.49 40.31000 24.07000 2007-01-10 11:14:49 27.77 49.49 40.32000 24.07000 2007-01-10 11:14:50 27.77 49.49 40.32782 24.07000 2007-01-10 11:14:51 27.77 49.52 40.32782 24.07000 2007-01-10 11:14:52 27.76 49.53 40.32782 24.07000 2007-01-10 11:15:00 27.76 49.53 40.33000 24.07000 " dat <- read.table(textConnection(input)); dates <- as.POSIXlt(paste(dat[,1], dat[,2])); z <- zoo(dat[,3:6], order.by=as.POSIXct(dates)); zr <- zooreg(dat[,3:6], order.by=as.POSIXct(dates)); diff(z) # Produces correct/expected results diff(zr) # Not the result I would expect --- Output --- V3 V4 V5 V6 2007-01-10 11:14:39 0.00 0.00 0.00000 -2e-05 2007-01-10 11:14:40 0.00 -0.01 0.00000 0e+00 2007-01-10 11:14:43 0.00 0.00 0.00000 0e+00 2007-01-10 11:14:45 0.00 0.00 0.00000 0e+00 2007-01-10 11:14:46 0.00 0.00 0.00000 0e+00 2007-01-10 11:14:49 0.00 0.00 0.01000 0e+00 2007-01-10 11:14:50 0.00 0.00 0.00782 0e+00 2007-01-10 11:14:51 0.00 0.03 0.00000 0e+00 2007-01-10 11:14:52 -0.01 0.01 0.00000 0e+00 2007-01-10 11:15:00 0.00 0.00 0.00218 0e+00 2007-01-10 11:14:39 0.00 0.00 0.00000 -2e-05 2007-01-10 11:14:40 0.00 -0.01 0.00000 0e+00 2007-01-10 11:14:46 0.00 0.00 0.00000 0e+00 2007-01-10 11:14:50 0.00 0.00 0.00782 0e+00 2007-01-10 11:14:51 0.00 0.03 0.00000 0e+00 2007-01-10 11:14:52 -0.01 0.01 0.00000 0e+00
Gabor Grothendieck
2007-Aug-02 09:58 UTC
[R] Using 'diff' on zoo vs zooreg classes (possible bug?)
On 8/2/07, Josh Quigley <josh.quigley at tibra.com.au> wrote:> Hello, > > > Can anyone explain the following behaviour? To me it seems a bug, but maybe > it is intentional. > > It seems that a diff on a zooreg class that is not _strictly_ regular only > considers those entries that are 'deltat' apart. > > In the following, diff on the zooreg class only returns values where the > index was one second apart. The example replicates by dev code, but I've > also tested using plain old integer indices, and the behaviour is the same.Correct, that's how it is intended to work. diff(zr) is the same as zr - lag(zr, -1) and lag.zoo and lag.zooreg work differently. For zoo objects the lagged version is obtained by moving the time points to the adjacent time point that exists in the series but for zooreg objects lag the time is lagged by deltat. A key difference is that in zooreg one can lag a point to a time point that did not previously exist in the series and, in particular, can lag a series outside of the original time range. That can be important when performing dynamic regression, i.e. regression on lagged and differenced versions of the series, and is discussed briefly in: library(dyn) ?dyn In contrast to zooreg, the time points of a lagged zoo series will only be chosen from the time points of the original series. You can choose to make your object a zoo object rather than a zooreg object if you want the zoo rather than zooreg behavior, diff(as.zoo(zr)) Also note that diff.zooreg has an na.pad= argument that may be what you are looking for:> diff(zr, na.pad = TRUE)V3 V4 V5 V6 2007-01-10 11:14:38 NA NA NA NA 2007-01-10 11:14:39 0.00 0.00 0.00000 -2e-05 2007-01-10 11:14:40 0.00 -0.01 0.00000 0e+00 2007-01-10 11:14:43 NA NA NA NA 2007-01-10 11:14:45 NA NA NA NA 2007-01-10 11:14:46 0.00 0.00 0.00000 0e+00 2007-01-10 11:14:49 NA NA NA NA 2007-01-10 11:14:50 0.00 0.00 0.00782 0e+00 2007-01-10 11:14:51 0.00 0.03 0.00000 0e+00 2007-01-10 11:14:52 -0.01 0.01 0.00000 0e+00 2007-01-10 11:15:00 NA NA NA NA