# Re: [R] ts vs zoo

From: Schweitzer, Markus <Markus.Schweitzer_at_hilti.com>
Date: Thu 12 Oct 2006 - 10:26:42 GMT

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

best regards, markus

Markus,

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

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

R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. Received on Thu Oct 12 20:37:05 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 https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.