Time Series Decomposition

August 12, 2013

(This article was first published on Fear and Loathing in Data Science, and kindly contributed to R-bloggers)

In the last post on the changepoint package, I concluded with a brief example of time series decomposition with the “decompose” command.  After further reading, I discovered the “stl” command, which to me appears a superior method.  STL stands for “Seasonal Decomposition of Time Series by LOESS”.  By decomposition, we mean breaking it down into trend, seasonal and irregular (noise) components.

Let’s try it on the same data set as the past two week, looking at it from 2008 until now.

#put the data into a time series
house.ts = ts(Value, frequency=12, start=c(1968,1), end=c(2013,6))

#subset the time series from 2008 forward using window commd
> house2 = window(house.ts, start=c(2008,1), end=c(2013, 6))
> plot(house2)

# fit the stl model using only the s.window argument
> fit = stl(house2, s.window=”periodic”)
> plot(fit)

# another fit this time setting the t.window argument, which changes the number of lags used in the LOESS smoothing parameters
>  fit2 = stl(house2, s.window=”periodic”, t.window=15)
> plot(fit2)

By changing the smoothing parameter in fit2 with the t.window argument, we have put slightly more of the variation in the trend and less in the seasonal component (you can see the effect in R by plotting the different fits).  What is clear is the fact that examining the residuals in the remainder component shows us clear outliers in early 2008 and the summer of 2010.

We could mess around with the parameters further, but I don’t think it would dramatically improve what we already have here.

Now, here is the question.  If we make this time series stationary, will the seasonality still be relevant?


To leave a comment for the author, please follow the link and comment on their blog: Fear and Loathing in Data Science.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)