[R-SIG-Finance] Ops on zoo objects

Ian Seow ianseow at gmail.com
Mon Jan 7 16:40:15 CET 2008


Amazing!! Thank you very much Gabor for your excellent suggestion...
certainly saved me lots of hair-pulling :) Btw, are there any
resources where I can read up about OOP with R? Esp. the syntax used
here in this solution... it's pretty new to me... so far most of the R
code I've encountered are mainly to do with statistical analysis,
haven't really come across examples on encapsulation, inheritance,
polymorphism etc etc with R. Thanks again for the great help!

Best regards
Ian Seow
GIC


On 1/7/08, Gabor Grothendieck <ggrothendieck at gmail.com> 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!
>
> 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
>



More information about the R-SIG-Finance mailing list