[R-SIG-Finance] Ops on zoo objects

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Mon Jan 7 17:45:41 CET 2008


On Mon, 7 Jan 2008, Gabor Grothendieck wrote:

> On Jan 7, 2008 6:12 AM, Ian Seow <ianseow at gmail.com> wrote:
> > Hi guys, currently the zoo package extends the group generic functions
> > Ops to perform operations only for the **intersection** of the indexes
> > of the objects. Was just wondering if anyone has came across an
> > elegant/simple solution for operations on the union of the indexes?
> > I'm trying to look for a clean way of adding the signals of my
> > different trading models which return zoo time-series with indexes
> > which are not necessary the same. Unfortunately, the only way I can
> > think of is to merge the zoo objects, obtain the union of the indexes,
> > prepend / append each zoo object with zeros, and then add them. This
> > looks really ugly especially when there are many objects to be added
> > :(
> >
> > Brief example:
> >
> > > model1.output
> >
> >               EURUSD   USDJPY
> > 2007-12-27 -19.470868 32.79096
> > 2007-12-28 -17.159300 32.79096
> > 2008-01-02 -17.162815 32.79096
> > 2008-01-03  -5.701861 10.93032
> > 2008-01-04  -5.701861 10.93032
> >
> > > model2.output
> >
> >               EURUSD   USDJPY
> > 2007-12-27 -20.470868 11.79096
> > 2007-12-28 -20.159300 11.79096
> > 2008-01-02 -20.162815 11.79096
> >
> > model1.output + model2.output would return a zoo object starting from
> > 2007-12-27 to 2008-01-02. :(
> > Any tips on this would be greatly appreciated!
> > Thanks!

In addition to Gabor's more general solution, you could also take a
somewhat simpler (but less general) solution and simply extend both series
to the same time grid:
  merge(model1.output, model2.output)
and then compute with the results from that.

In this particular case, it would be sufficient to just extend
model2.ouput, e.g., via
  model2.output <- merge(zoo(,time(model1.output)), model2.output)

hth,
Z

> It would be enough to define a subclass of zoo with its own
> constructor function and
> an as function for conversions:
>
> > Ops.zooall <- function (e1, e2)
> + {
> +     e <- if (missing(e2)) {
> +         NextMethod(.Generic)
> +     }
> +     else if (any(nchar(.Method) == 0)) {
> +         NextMethod(.Generic)
> +     }
> +     else {
> + merge(e1, e2, all = TRUE, fill = 0, retclass = NULL)
> +         NextMethod(.Generic)
> +     }
> +     if (is.null(attr(e, "index")))
> + zooall(e, index(e1), attr(e1, "frequency"))
> +     else
> + e
> + }
> >
> > zooall <- function(x = NULL, order = index(x), frequency = NULL) {
> + structure(zoo(x = x, order = order, frequency = frequency),
> + class = c("zooall", "zoo"))
> + }
> >
> > as.zooall <- function(x, ...) UseMethod("as.zooall")
> > as.zooall.zoo <- function(x, ...) structure(x, class = c("zooall", "zoo"))
> >
> > # now using your zoo data:
> >
> > model1.output <-
> + structure(c(-19.470868, -17.1593, -17.162815, -5.701861, -5.701861,
> + 32.79096, 32.79096, 32.79096, 10.93032, 10.93032), .Dim = c(5L,
> + 2L), .Dimnames = list(NULL, c("V2", "V3")), index = structure(c(13874,
> + 13875, 13880, 13881, 13882), class = "Date"), class = "zoo")
> >
> > model2.output <-
> + structure(c(-20.470868, -20.1593, -20.162815, 11.79096, 11.79096,
> + 11.79096), .Dim = c(3L, 2L), .Dimnames = list(NULL, c("V2", "V3"
> + )), index = structure(c(13874, 13875, 13880), class = "Date"), class = "zoo")
> >
> > model1.output.all <- as.zooall(model1.output)
> > model2.output.all <- as.zooall(model2.output)
> > model1.output.all + model2.output.all
>
> 2007-12-27 -39.941736 44.58192
> 2007-12-28 -37.318600 44.58192
> 2008-01-02 -37.325630 44.58192
> 2008-01-03  -5.701861 10.93032
> 2008-01-04  -5.701861 10.93032
> >
> > model1.output + model2.output
>                   V2       V3
> 2007-12-27 -39.94174 44.58192
> 2007-12-28 -37.31860 44.58192
> 2008-01-02 -37.32563 44.58192
>
> _______________________________________________
> R-SIG-Finance at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
> -- Subscriber-posting only.
> -- If you want to post, subscribe first.
>
>



More information about the R-SIG-Finance mailing list