0% found this document useful (0 votes)
9 views4 pages

Ex. 12: (1) Decomposing Seasonal Time-Series Data Script

The document provides a script for decomposing seasonal and non-seasonal time series data using the AirPassengers dataset in R. It demonstrates how to perform Seasonal-Trend decomposition using Loess and visualize the trend, seasonal, and residual components. Additionally, it includes a method to forecast the next 12 months based on the STL decomposition results.

Uploaded by

Shenbaga Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

Ex. 12: (1) Decomposing Seasonal Time-Series Data Script

The document provides a script for decomposing seasonal and non-seasonal time series data using the AirPassengers dataset in R. It demonstrates how to perform Seasonal-Trend decomposition using Loess and visualize the trend, seasonal, and residual components. Additionally, it includes a method to forecast the next 12 months based on the STL decomposition results.

Uploaded by

Shenbaga Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Ex. 12: Program to decompose seasonal and non-seasonal time series data.

(1) Decomposing seasonal time-series data


Script
install.packages("forecast") # if not installed earlier

# Load necessary libraries


library(forecast) # For time series forecasting

# Load the built-in AirPassengers dataset


data("AirPassengers")
head(AirPassengers)

# Convert the dataset into a time series object


# Monthly data from 1949 to 1960
ts_data <- ts(AirPassengers, start = c(1949,1), frequency = 12)

# Perform Seasonal-Trend decomposition using Loess


stl_decomposed <- stl(ts_data, s.window = "periodic")

# Plot the decomposed components


plot(stl_decomposed)

trend <- stl_decomposed$time.series[, "trend"]


head(trend)

seasonal <- stl_decomposed$time.series[, "seasonal"]


head(seasonal)

residuals <- stl_decomposed$time.series[, "remainder"]


head(residuals)

# Plot the trend component


plot(trend, main = "Trend Component", col = "red", lwd = 2)

# Plot the seasonal component


plot(seasonal, main = "Seasonal Component", col = "blue", lwd = 2)

# Plot the residual component


plot(residuals, main = "Residual Component", col = "green", lwd = 2)

# Forecast the next 12 months based on STL decomposition


forecast_data <- forecast(stl_decomposed, h = 12)

# Plot the forecast


plot(forecast_data)
Output

You might also like