Skip to content

This repository contains code written in R for analyzing and handling seasonality in data.

Notifications You must be signed in to change notification settings

g-arruda/seasonality

Folders and files

NameName
Last commit message
Last commit date

Latest commit

fa068fa · Mar 16, 2025

History

2 Commits
Mar 16, 2025
Mar 16, 2025

Repository files navigation

Seasonality Analysis Package

Description

The check_seasonality() analyze numeric columns in a data frame for seasonal patterns using the seastests::isSeasonal() function. The test is performed with a monthly frequency (freq = 12).

Usage

The main function is check_seasonality(). This function analyzes numeric columns in a data frame for seasonal patterns.

Function: check_seasonality

Arguments

  • data: A data frame containing numeric columns to be tested for seasonality.

Returns

A list containing two elements:

  • season_test_result: A data frame with test results for each numeric variable.
  • season_vars: A character vector containing names of variables that exhibit seasonal patterns.

Example

# Load the required libraries
library(dplyr)
library(purrr)
library(tidyr)
library(seastests)
# Create a sample data frame
data <- data.frame(
    date = seq.Date(
        from = as.Date("2020-01-01"),
        by = "month", length.out = 24
    ),
    sales = rnorm(24),
    temperature = sin(seq(0, 4 * pi, length.out = 24))
)
# Check for seasonality
results <- check_seasonality(data)
# Print the variables with seasonal patterns
print(results$season_vars)

Details

The check_seasonality function performs the following steps:

  1. Selects numeric columns from the input data frame.
  2. Applies seasonality test with monthly frequency.
  3. Returns both detailed test results and a simplified list of seasonal variables.

About the isSeasonal() Function

The isSeasonal() function from the seastests package is a powerful tool for detecting seasonal patterns in time series data. It implements multiple statistical tests to determine whether a series contains seasonal components.

How isSeasonal() Works

The function conducts several tests to detect seasonality:

  1. QS Test (Quadratic Seasonality): Tests for deterministic seasonality based on fitting quadratic functions to seasonal components.
  2. Kruskal-Wallis Test: A non-parametric test that checks if samples from different seasons come from the same distribution.
  3. Friedman Test: Another non-parametric test that looks for differences between seasons across multiple observations.
  4. Periodogram Test: Analyzes the spectral density of the time series to identify periodic components.
  5. Wavelets Analysis: Decomposes the time series to identify patterns at different scales.

The function combines results from these tests to make a determination about the presence of seasonality. By default, it requires at least two tests to be positive for a series to be classified as seasonal.

Parameters for isSeasonal()

  • x: The time series to be tested
  • freq: The frequency of seasonality (e.g., 12 for monthly data)
  • test: Which tests to perform ("combined", "qs", "kw", "friedman", "periodogram", or "wavelets")
  • significance: Significance level for the tests (default is 0.05)

Theory Behind Seasonal Adjustment

Seasonal adjustment is the process of removing the seasonal component from a time series to better understand the underlying trend and cyclical patterns.

Why Seasonal Adjustment Matters

  1. Improved Analysis: Allows for proper comparison of adjacent time periods without seasonal influence.
  2. Trend Identification: Makes it easier to identify the true underlying trend in the data.
  3. Better Forecasting: Enables more accurate forecasting by separating predictable seasonal components from other variations.
  4. Policy Decision Making: Provides clearer signals for economic and business decision-making.

Common Seasonal Adjustment Methods

1. Time Series Decomposition

The classical approach decomposes a time series (Y) into multiple components:

Y = T × S × C × I (multiplicative model) or Y = T + S + C + I (additive model)

Where:

  • T: Trend component
  • S: Seasonal component
  • C: Cyclical component
  • I: Irregular component

2. X-13ARIMA-SEATS

This is an advanced method developed by the U.S. Census Bureau that:

  • Uses moving averages to estimate seasonal factors
  • Incorporates ARIMA modeling
  • Handles outliers, calendar effects, and trading day adjustments
  • Provides diagnostics for the quality of adjustment

3. STL (Seasonal and Trend decomposition using Loess)

A versatile method that:

  • Uses Loess smoothing for decomposition
  • Handles non-linear trends
  • Can accommodate changing seasonal patterns over time

Evaluating Seasonal Adjustment

A properly seasonally adjusted series should:

  1. Show no remaining seasonal patterns
  2. Preserve the original trend and cycle
  3. Have no correlation between seasonal components and the adjusted series

Dependencies

The check_seasonality() function depends on the following R packages:

  • dplyr
  • purrr
  • tidyr
  • seastests

About

This repository contains code written in R for analyzing and handling seasonality in data.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages