Open In App

STL Decomposition of Time Series Using R

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

STL (Seasonal and Trend decomposition using Loess) is a method used to analyze time series data by separating it into meaningful components. It is commonly applied in fields like economics, weather analysis and demand forecasting to better understand underlying patterns. It is a robust and versatile method for decomposing a time series into three components:

  1. Seasonal: The repeating pattern or cycle in the data.
  2. Trend: The long-term progression in the data.
  3. Residual: The remaining variation in the data after removing the seasonal and trend components.

Why Use STL?

The STL method uses Loess (Local Regression) to estimate the seasonal and trend components, therefore:

  • It can handle any type of seasonality (hourly, daily, weekly, etc.).
  • It is robust to outliers.
  • It allows the seasonal component to change over time.
  • It provides a clear decomposition which can be useful for forecasting and understanding the underlying patterns.

Implementation of STL Decomposition in R

We will use STL decomposition to analyze time series by splitting the data into meaningful components, which helps in detecting trends, patterns and anomalies in R programming language.

1. Installing and Loading Required Packages

We install and load the necessary libraries to perform STL decomposition and plot its result.

  • install.packages: Installs the required R package.
  • library: Loads the specified package into the R session.
R
install.packages("forecast")
install.packages("ggplot2")

library(forecast)
library(ggplot2)

2. Loading the Time Series Dataset

We use the built-in AirPassengers dataset, which contains monthly airline passenger counts from 1949 to 1960.

  • data: Loads a dataset available in R.
  • AirPassengers: A built-in time series object with monthly passenger counts.
R
data("AirPassengers")
ts_data <- AirPassengers

3. Performing STL Decomposition

We decompose the time series into seasonal, trend and remainder components using the STL method.

  • stl: Performs seasonal-trend decomposition using Loess.
  • s.window: Sets the smoothing parameter; "periodic" keeps the seasonal pattern fixed.
R
stl_decomp <- stl(ts_data, s.window = "periodic")

4. Visualizing the STL Decomposition

We generate a multi-panel plot showing the original time series along with its components.

  • autoplot: Automatically plots objects like time series decompositions.
  • stl_decomp: The object containing seasonal, trend and remainder components.
R
autoplot(stl_decomp)

Output:

composition
STL Trend of Time Series Using R

5. Extracting the STL Components

We extract each component from the STL object for further analysis or custom plotting.

  • stl_decomp$time.series: Accesses the decomposed components.
  • seasonal: Refers to the seasonal variation in the data.
  • trend: Captures the long-term direction.
  • remainder: The noise or residual part of the series.
R
seasonal_component <- stl_decomp$time.series[, "seasonal"]
trend_component <- stl_decomp$time.series[, "trend"]
residual_component <- stl_decomp$time.series[, "remainder"]

6. Plotting the Trend Component Separately

We visualize the trend component alone to understand the overall long-term movement in the dataset.

  • plot: Generates a line plot for the selected time series.
  • trend_component: The extracted trend from STL decomposition.
  • main, xlab, ylab: Add labels and title to the plot.
R
plot(trend_component, main = "Trend Component", ylab = "Passengers", xlab = "Time")

Output:

trendcomponent
STL Trend of Time Series Using R

The output shows a smooth upward curve representing the long-term trend in airline passengers from 1949 to 1960. It filters out seasonal effects and noise for a clearer view of overall growth.


Similar Reads