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).
The main function is check_seasonality()
. This function analyzes numeric columns in a data frame for seasonal patterns.
data
: A data frame containing numeric columns to be tested for seasonality.
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.
# 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)
The check_seasonality
function performs the following steps:
- Selects numeric columns from the input data frame.
- Applies seasonality test with monthly frequency.
- Returns both detailed test results and a simplified list of seasonal variables.
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.
The function conducts several tests to detect seasonality:
- QS Test (Quadratic Seasonality): Tests for deterministic seasonality based on fitting quadratic functions to seasonal components.
- Kruskal-Wallis Test: A non-parametric test that checks if samples from different seasons come from the same distribution.
- Friedman Test: Another non-parametric test that looks for differences between seasons across multiple observations.
- Periodogram Test: Analyzes the spectral density of the time series to identify periodic components.
- 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.
x
: The time series to be testedfreq
: 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)
Seasonal adjustment is the process of removing the seasonal component from a time series to better understand the underlying trend and cyclical patterns.
- Improved Analysis: Allows for proper comparison of adjacent time periods without seasonal influence.
- Trend Identification: Makes it easier to identify the true underlying trend in the data.
- Better Forecasting: Enables more accurate forecasting by separating predictable seasonal components from other variations.
- Policy Decision Making: Provides clearer signals for economic and business decision-making.
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
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
A versatile method that:
- Uses Loess smoothing for decomposition
- Handles non-linear trends
- Can accommodate changing seasonal patterns over time
A properly seasonally adjusted series should:
- Show no remaining seasonal patterns
- Preserve the original trend and cycle
- Have no correlation between seasonal components and the adjusted series
The check_seasonality()
function depends on the following R packages:
dplyr
purrr
tidyr
seastests