[R] ts vs zoo

Achim Zeileis Achim.Zeileis at wu-wien.ac.at
Thu Oct 12 12:14:57 CEST 2006


Markus,

several comments:

> > I have lots of data in zoo format and would like to do some time
> > series analysis. (using library(zoo), library(ts) )

The "ts" package has been integrated into the "stats" package for a
long time now...

> > My data is usually from one year, and I try for example  stl() to
> > find some seasonalities or trends.

As pointed out by Philippe, this is not what STL is made for. In STL you
try to find seasonality patterns by loess smoothing the seasonality of
subsequent years. If you have observations from just one year, there is
just one seasonality pattern (at least if you look for monthly or
quaterly patterns).

> > I have now accepted, that I might have to convert my series into ts
> > () but still I am not able to execute the comand since stl() is not
> > satisfied

And there are reasons for this: you need to have a regular time series
with a certain frequency so that STL is applicable. (One could argue
that "ts" is not the only format for regular time series but typically
you can easily coerce back and forth between "ts" and "zoo"/"zooreg".

> > x<-zoo(rnorm(365), as.Date("2005-01-01"):as.Date("2005-12-31"))

I don't think that this is what you want. Look at time(x). I guess you
mean
  x <- zoo(rnorm(365), seq(from = as.Date("2005-01-01"),
    to = as.Date("2005-12-31"), by = "1 day"))

> > x<-as.ts(x)
> > #x<-as.ts(x, frequency=12)  #this has no effect frequency is not

Here, it seems to me that you want to aggregate to monthly data, this
can be done via
  x2 <- aggregate(x, as.yearmon, mean)

This is now (by default) a regular series with frequency 12
  frequency(x2)

and hence it can be easily coereced to "ts" and back (with almost no
loss of information):
  as.zoo(as.ts(x2))

However, calling stl(as.ts(x2)) still complains that there are not
enough periods because this is just a single year, i.e., only a single
seasonality pattern. To look at this, you could do
   barplot(x2)

For looking at the trend you could use a simple running mean
  plot(x)
  lines(rollmean(x, 14), 2)
or you could also use loess() or some other smoother...

For more details on the "zoo" package, see
  vignette("zoo", package = "zoo")

Best,
Z



More information about the R-help mailing list