stick: Seasonality, Trend, and Irregular Contribution Kit

View source: R/stick.R

stickR Documentation

Seasonality, Trend, and Irregular Contribution Kit

Description

The function decomposes the variance of a time series into seasonal, trend and irregular parts based on an Analysis of Variance (ANOVA) of the series on the seasonal and trend factors and measures their contribution to the data.

Usage

stick(y, lags = frequency(y), ...)

## S3 method for class 'stick'
print(x, digits = 4, ...)

## S3 method for class 'stick'
plot(x, which = NULL, ask, ...)

Arguments

y

The vector or a ts object, containing the time series to analyse.

lags

The vector of seasonal lags (periodicities) in the data, e.g. lags=c(24,168) for the hourly data or lags=12 for the monthly one. Values not greater than one are dropped. Defaults to the frequency of y.

...

Other parameters passed to the aov() function.

x

The object of class "stick" generated by the stick() function.

digits

The number of digits to use when printing the strength of the components.

which

The vector of integers specifying which plots to produce: the values 1, ..., k-1 correspond to the seasonal plots for each of the seasonal lags (in the ascending order), while k corresponds to the trend plot (the last one). If NULL (the default), all the plots are produced.

ask

Logical; if TRUE, the user is asked to hit Enter before each new plot is drawn. The default only asks when the requested plots do not all fit on the current device layout (see par).

Details

stick() forms internally a data frame with the response y in the first column, a categorical (factor) variable for each of the provided seasonal lags and a "trend" factor in the last column. For a monthly series with lags=12, the seasonal factor takes values 1:12 repeated throughout the sample (the month of the year), while the trend factor takes values 1:ceiling(T/12), each repeated 12 times (the year). The trend factor is constructed based on the longest of the provided lags.

aov() is then applied to the model y ~ seasonal + trend and the strength of each component is measured as the share of the respective Sum of Squares in the total Sum of Squares. The irregular component corresponds to the share of the residual Sum of Squares. The strengths of all the components sum up to one.

The function implements the Seasonality, Trend, and Irregular (STI) classification of Hans Levenbach (see the reference below).

plot.stick() reproduces two types of plots, in the spirit of the STI decomposition. For every seasonal lag it draws a "seasonal plot": the series is reshaped into a matrix with one column per cycle (e.g. per year for monthly data) and the value within the cycle on the x-axis; each cycle is a grey line and the average seasonal profile (the mean across the cycles) is overlaid as a bold dashed black line. The final plot is the "trend plot", which uses the longest lag: each seasonal position (e.g. each month) is a grey line drawn across the cycles, and the average level per cycle (the trend) is overlaid as a bold dashed black line.

Value

stick() returns an object of class "stick", which contains:

  • y - The original data;

  • lags - The seasonal lags used in the analysis;

  • model - The fitted aov model;

  • anova - The ANOVA table (the first element of summary(model));

  • strength - The named vector with the strength of each seasonal component, the trend and the irregular component. The values sum up to one.

plot.stick() is called for its side effect of producing the plots and returns NULL invisibly.

Author(s)

Ivan Svetunkov, ivan@svetunkov.com

References

Levenbach, H. (2021). Four P's in a Pod: e-Commerce Forecasting and Planning for Supply Chain Practitioners. Independently published. ISBN 979-8461733575.

See Also

aov

Examples

# Generate a seasonal series and classify it
y <- ts(rnorm(120, 100 + 20 * sin(2 * pi * (1:120) / 12), 5), frequency=12)
stick(y)

# Produce all the plots
par(mfcol=c(1,2))
plot(stick(AirPassengers))
# Only the trend plot
plot(stick(AirPassengers), which=2)


greybox documentation built on Oct. 2, 2026, 5:08 p.m.