I don't understand the output of stl. As a simple example: y <- numeric(1:365) y[250] = 1 stl <- stl(ts(y, frequency=7), s.window="periodic") This returns without error but the results are puzzling to me. If you plot the results it is probably easiest to visualize what I mean. plot(stl) This shows the original data (a single spike at 250). A trend (which also shows a bump at 250). It is the rest that I have a question on. For the "seasonal" component it seems to show a sinusoid like wave with a period roughly a week (7 days) long all with the same amplitude. I can't see how a single spike can generate a "seasonal" component that is periodic for every period in the data. Finally the "remainder" portion of the data generated seems to show just what I want, a representation of the input. But if this is ruly the remainder (data - (trend + seasonal)) then shouldn't it have all entries close to zero? Please help me with my misunderstanding if you have any experience with stl. Finally it has been suggested that in order to find an overall formula to represent the data a model will need to be constructed. I unfortunately don't have any experience in developing a model. Any hints on where to start? Thank you. Kevin
I can't reproduce this because the data has two points 0 and one at the ends of the data set, and I get an na.fail error. There is no periodic part to this data- it doesn't seem because there are only two points. stephen On Tue, Sep 2, 2008 at 11:38 AM, <rkevinburton at charter.net> wrote:> I don't understand the output of stl. As a simple example: > > y <- numeric(1:365) > y[250] = 1 > > stl <- stl(ts(y, frequency=7), s.window="periodic") > > This returns without error but the results are puzzling to me. If you plot the results it is probably easiest to visualize what I mean. > > plot(stl) > > This shows the original data (a single spike at 250). A trend (which also shows a bump at 250). It is the rest that I have a question on. For the "seasonal" component it seems to show a sinusoid like wave with a period roughly a week (7 days) long all with the same amplitude. I can't see how a single spike can generate a "seasonal" component that is periodic for every period in the data. Finally the "remainder" portion of the data generated seems to show just what I want, a representation of the input. But if this is ruly the remainder (data - (trend + seasonal)) then shouldn't it have all entries close to zero? Please help me with my misunderstanding if you have any experience with stl. > > Finally it has been suggested that in order to find an overall formula to represent the data a model will need to be constructed. I unfortunately don't have any experience in developing a model. Any hints on where to start? > > Thank you. > > Kevin > > ______________________________________________ > R-help at r-project.org 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. >-- Stephen Sefick Research Scientist Southeastern Natural Sciences Academy Let's not spend our time and resources thinking about things that are so little or so large that all they really do for us is puff us up and make us feel like gods. We are mammals, and have not exhausted the annoying little problems of being mammals. -K. Mullis
Trying your example: y <- numeric(365) y y[250] = 1 y y.stl <- stl(ts(y, frequency=7), s.window="periodic") First of all, pay attention to the axes on your plot - the scales are different for each panel. Your seasonal component is quite small in magnitude compared to everything else. Also, if you are unsure that data = seasonal + trend + remainder, just try apply(y.stl$time.series, 1, sum) which adds up the three components. This will get you back your original time series. The problem with your example is that you need to be giving sensible data to the stl procedure. How does data with a bunch of zeros and one 1 represent anything with weekly periodicity? For example, try the following plot: library(lattice) xyplot(y ~ 1:365 | factor(rep(1:7, 53)[1:365])) This groups your data into all Mondays, all Tuesdays, etc. Do you see anything here indicating periodicity? It was your specification of frequency=7 that created the cyclical pattern you see in the seasonal component. The STL procedure has a step where it smooths, in this case, each day of the week, and then strings each of those fitted values back together. In the case of this data, it gets a positive value for day 5 (refer to lattice plot above), and hence the seasonal pattern you see. If you read the documentation, you will see that s.window="periodic" causes the mean to be taken for each day of the week, which forces the day of the week to be constant. If you would like the seasonal to be able to adapt, try something like: y.stl <- stl(ts(y, frequency=7), s.window=9, s.degree=1) plot(y.stl) This will use local linear fitting to each week day and allow the seasonal to evolve over time. You can play with s.window argument. If you provided this example just as an example, hopefully this explanation will be helpful. However, if this is what your data actually looks like, I don't see how stl will do you any good.