Site icon R-bloggers

Time Series Decomposition

[This article was first published on Fear and Loathing in Data Science, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.
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?

T.D.M.

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 about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.