[R] as.zoo behavior (
Achim Zeileis
Achim.Zeileis at R-project.org
Sat Nov 4 02:37:44 CET 2006
Mark:
> temp<-ewma(actualdiff,.05),
ok, because ewma() is a wrapper for filter() this returns an object of
class "ts". Therefore you want to add the index again, but:
> rollmeandifflogbidask<-as.zoo(temp,index(actualdiff))
is not the right thing to do. as.zoo() is a coercion generic and its "ts"
method ignores all arguments except the first. Thus, index(actualdiff) is
*not* added and a "zooreg" object created (which is appropriate because
you supply an object of class "ts", a regular series. See
zoo:::as.zoo.ts
for the actual code).
You want to create a "zoo" series from scratch using the data from temp
and the index from actualdiff. Hence, the appropriate command is
zoo(coredata(temp), index(actualdiff))
But let's go back to the ewma() function. The NA in temp looks suspicious
(and stems from another implicit coercion from "zoo" to "ts"). I guess you
want:
ewma <- function(x, lambda = 1, init = 0) {
rval <- filter(lambda * coredata(x),
filter = (1-lambda),
method = "recursive",
init = init)
rval <- zoo(coredata(rval), index(x))
rval
}
And then you can do
ewma(actualdiff, 0.05)
and already get a nicely formatted "zoo" object back.
Z
More information about the R-help
mailing list