Extract Seasonal & Trend: using decomposition in R

December 1, 2015 8 Comments math


How is the time series decomposition working? How does it split a time series into three other: Seasonality, Trends and Random? To understand time series decomposition, we will study the decompose( ) and STL( ) function from R language.

Understand decomposition

Decompose into more times series

A time series decomposition is a mathematical procedure which transform a time series into multiple different time series. The original time series is often computed (decompose) into 3 sub-time series:

  • Seasonal: patterns that repeat with fixed period of time. A website might receive more visit during weekends. This is a seasonality of 7 days.
  • Trend: the underlying trend of the metrics. A website who gain in popularity should have a general trend who go up.
  • Random: (also call “noise”, “Irregular” or “Remainder”) Is the residuals of the time series after allocation into the seasonal and trends time series.

Additive or multiplicative decomposition?

To get a successful decomposition, it is important to choose between the additive or multiplicative model. To choose the right model we need to look at the time series. Does the magnitude of the seasonality increase when the time series increase too?

additive-modelAustralian beer production – The seasonal variation looks constant. It doesn’t change when the time series increase. We should use the additive model.

multiplicative-modelAirline Passenger Numbers – When the time series increase the seasonal variation increase more and more. We should use the multiplicative model.

Time series = Seasonal + Trend + Random

Time series = Trend * Seasonal *Random

Based on the model, the decomposition formula varies a little.

Step-by-step: Time series decomposition

We’ll study the decompose( ) function in R. As a decomposition function, it takes a time series as parameter and decompose it into seasonal, trend and random time series. We’ll reproduce step-by-step the decompose( ) function in R to understand how it works. As the technique isn’t exactly the same with an additive and multiplicative model we will use two examples: the Australian beer production (additive) and the Airline Passenger Numbers (multiplicative).

Step 1: Import the data


A good example of additive time series is the beer production see previously. When the metrics values increase, the seasonality stays constant. It doesn’t vary that much.


The monthly Airline Passenger Numbers is a good example of multiplicative time series. The more passenger the more seasonality is observed.



Step 2: detect the trend

To detect the underlying trend, we smooth the time series using the “centred moving average“. To perform the decomposition, it is vital to use a moving window of the exact size of the seasonality. Therefore, to decompose a time series we need to know it seasonality period: weekly, monthly, etc… No worry if you don’t know it, you can detect the seasonality using Fourier transform.


The Australian beer production clearly follows an annual seasonality. As it is quarterly recorded, there is 4 data points recorded per year we use moving average windows of 4.


This is the same process used for the additive model. The airline passenger numbers seasonality also looks annually. As it is recorded monthly, we choose a moving average window of 12.



Step 3: detrend the time series

Removing the previously calculated trend from the time series will result into a new time series who clearly expose the seasonality.





Step 4: average seasonality

From the detrend time series, it’s easy to compute the average seasonality. We add the seasonality together and divide by the number of seasonality. Technically speaking, to average together the time series we feed the time series into a matrix. Then, we transform the matrix so each column contains the elements of the same period (same day, same month or same quarter…). Finally, we compute the mean of each column. Here is how to do it in R:


quarterly seasonality: we use a matrix of 4 rows.
The average seasonality is repeat 16 times to compare the graphic later (see below)


Monthly seasonality: we use a matrix of 12 rows.
The average seasonality is repeat 12 times to compare the graphic later (see below)



Step 5: random noise left

Previous steps already extract most data from the original time series. It only left with some “random” noise.


The additive formula define “Time series = Seasonal + Trend + Random” which means “Random = Time series – Seasonal – Trend”


The multiplicative formula define “Time series = Seasonal * Trend * Random” which means “Random = Time series / (Trend * Seasonal)”



Step 6: reconstruct the original signal

The decomposed time series can logically be re-composed using the model formula. This should reproduce the original signal. Some data are missing at the beginning and the end of the recomposed time series. This is due to the moving average windows who need to ingest some data point before producing average data points.


The additive formula define “Time series = Seasonal + Trend + Random” which means “Random = Time series – Seasonal – Trend”


The multiplicative formula define “Time series = Seasonal * Trend * Random” which means “Random = Time series / (Trend * Seasonal)”



DECOMPOSE( ) and STL(): Time series decomposition in R

To make our life easier, some R package provides decomposition with a single line of code. As expected, our step-by-step decomposition provides the exact as the DECOMPOSE( ) and the STL( ) function (see the graphic).


Only requirement: the seasonality is quarterly (frequency = 4)

Using the DECOMPOSE( ) function:


Only requirement: the seasonality is monthly (frequency = 12)



Now using the STL( ) function:



Often, the decomposition is used to removes the seasonal effect from a time series. It provided a cleaner way to understand the trend. For instance, lower ice-cream sales during winter don’t mean the company performing poorly. To know the answer, we need to remove the seasonality from the time series. Here, at Anomaly.io we detect anomalies, we use the seasonally adjusted time series to detect anomalies. We also use the random (also call reminder) sub-time series from the decomposed time series to detect anomalies and outliers.

Monitor & detect anomalies with Anomaly.io

  • PaoloRocca

    Nice post.. very well explained!! Thanks

  • Jenice Tom

    Great substance and content, but please proofread.

  • Aleksandras Urbonas

    Can I say ‘wow, this looks professional!’?
    Well thought of, and the layout is amazing.
    I wonder what your topic of interest is now that you have mastered time series :)

  • Jithin Sam Varghese

    Thank you! This was very helpful.

  • http://www.tombush.co.uk/ Tom Bush

    Thanks for this, really helpful. Just curious though — is there a reason you didn’t use stl() on the AirPassengers dataset?

  • Milan Jain

    Very nice article! Can you please suggest what to do when size of each cycle varies i.e. goes from large to small cycles?