[R] ts vs zoo
Achim Zeileis
Achim.Zeileis at wu-wien.ac.at
Thu Oct 12 13:04:42 CEST 2006
On Thu, 12 Oct 2006 12:26:42 +0200 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
> period.
>
> to get there I now only took the zoo object "x" and made
>
>
> x<-as.ts(x)
> 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
> gone.
But you can do the following
x <- zoo(rnorm(365), seq(from = as.Date("2005-01-01"),
to = as.Date("2005-12-31"), by = "1 day"))
xt <- ts(as.ts(x), frequency = 7)
xt_stl <- stl(xt, s.window = 28, t.window = 28)
xz_stl <- as.zoo(xt_stl$time.series)
time(xz_stl) <- time(x)
plot(xz_stl)
i.e., convert the extracted series back to "zoo" and add the original
index for plotting.
Z
> 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
>
> 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