Re: [R] ts vs zoo

From: Achim Zeileis <>
Date: Thu 12 Oct 2006 - 10:14:57 GMT


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):

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


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")

Z mailing list PLEASE do read the posting guide and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 12 20:42:01 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Thu 12 Oct 2006 - 11:30:09 GMT.

Mailing list information is available at Please read the posting guide before posting to the list.