[R] ts vs zoo
phgrosjean at sciviews.org
Thu Oct 12 12:53:54 CEST 2006
Schweitzer, Markus wrote:
> thank you very much for the information.
> I guess I should have been more clear here.
> I was looking for the "monthly" or weekly trends within this one year
Then, always keep in mind that stl() is looking for periodic component
of a frequency = 1. This means you have to define the time unit so that
you catch it. For monthly periodic component, use "month" as time unit.
For weekly periodic component, use "week" as time unit. You must convert
your data accordingly. Also keep in mind that stl() decomposes your
series into a general trend, a periodic trend of frenquency 1, and
noise, using an ADDITIVE model. So, if the components are
multiplicative, you should use a different model.
Otherwise, there is much more than stl() in R! In particular, you could
look at any significant periodic component in your series by using
spectrum(), for instance... still considering that your series is long
enough, which is probably the case for looking at weekly periodic
signals on a one-year long series.
> to get there I now only took the zoo object "x" and made
> x<-ts(x, frequency=7) #to get 52 weeks(Periods) with 7 days each
> -> to get 12 periods e.g. months with 29,30 or 31 days, I guess I can
> only choose frequency=30
> I then can run stl
> It is just a pitty, that the "labeling" (jan 2005, feb 2005 ..) has
> So thank you for your hint with barplot and rollmean
> best regards, markus
> -----Original Message-----
> From: Achim Zeileis [mailto:Achim.Zeileis at wu-wien.ac.at]
> Sent: Donnerstag, 12. Oktober 2006 12:15
> To: Schweitzer, Markus
> Cc: R-help at stat.math.ethz.ch
> Subject: Re: [R] ts vs zoo
> 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
> 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
> x <- zoo(rnorm(365), seq(from = as.Date("2005-01-01"),
> to = as.Date("2005-12-31"), by = "1 day"))
>>> #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
> 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
> 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")
> R-help at stat.math.ethz.ch mailing list
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help