Seasonality in R

Seasonality analysis is one of the core elements of the descriptive analysis process of time series data. This process is typically done with the use of data visualization tools and summary statistics methods. The TSstudio package provides a set of functions for seasonality plots.

Load Packages

packages <- c("tidyverse", "forecast", "TSstudio")

packages <- lapply(packages, FUN=function(x) {

if(!require(x, character.only=TRUE)) {

install.packages(x)

library(x, character.only=TRUE)}})

Time Series Info

ts_info(USgas)

Decomposition of time series

The ts_decompose function is an interactive wrapper for the decompose function from the stats package. It provides a decomposition of the series into seasonal, trend, and irregular components. The decomposition of the series is done by using a moving average. The following example demonstrated the decomposition of the US monthly consumption of natural gas (USgas):

ts_decompose(USgas)

Seasonality plot

The ts_seasonal function provides three different seasonal views of the series for low-frequency time series objects (i.e., monthly, quarterly, half-year). The type of seasonal view defined by the type argument. The default view, type=normal, split and plot the series by its full cycle units (which is typically years). This allows comparing the changes of the series from year to year by the series frequency units (e.g. the month of the year), and therefore, to identify seasonal patterns:

ts_seasonal(USgas, type="normal")

Note that the color scale of the series cycles lines (which in the case above represents the years) is set by chronological order. The second view, type=cycle, split and plot the series by its frequency units over time. This enables to identify if there is a hierarchy relationship between the frequency units of the series, or how strong the seasonal pattern of the series:

ts_seasonal(USgas, type="cycle")

In the plot above, you can notice that while the series is trending up, the hierarchy between the different months kept over time (e.g., typically, the peak of the consumption occurs during January). The third and last view, type=box, returns a box-plot of each frequency unit:

ts_seasonal(USgas, type="box")

As each view may reveal different patterns, it would make more sense to plot all the three together. This can be done by setting type=all:

ts_seasonal(USgas, type="all")

Note that some of the variations seen in those seasonal plots, especially in the box view, related to the series trend. In some cases, it would make more sense to remove the trend from the series and replot it. The next example demonstrated the plot of the series without the trend. A simple detrending of the series is done with the decompose function:

USgas_detrend <- USgas - decompose(USgas)$trend

ts_seasonal(USgas_detrend, type="all")

Time series heatmap

Another way to look at seasonality is with the use of heatmap. The ts_heatmap function returns heatmap of the series object:

ts_heatmap(USgas)

The color argument defines the heatmap color scale supporting the RColorBrewer colors palettes. For example, we can modify the heatmap above to red scale by using the Reds colors palette:

ts_heatmap(USgas, color="Reds")

Surface plot

The ts_surface function provides a 3D plot for low-frequency time-series data (e.g., monthly), by plotting the cycles (years), frequency units (months) and the corresponding values of the series:

ts_surface(USgas)

Polar plot

The ts_polar function returns a polar plot representation for monthly and quarterly time series data:

ts_polar(USgas)

Sabalico Logo
Sabalytics Logo
Senty Logo
SEO Guide Logo
World Map Logo
rStatistics Logo
Day Map Logo
Time Zone Logo
Galaxy View Logo
Periodic Table Logo
My Location Logo
My Weather Logo
Sprite Sheet Logo
Barcode Generator Logo
Test Speed Logo
Website Tools Logo
Image Tools Logo
Color Tools Logo
Text Tools Logo
Finance Tools Logo
File Tools Logo
Data Tools Logo
History of Humanity - History Archive Logo
History of Humanity - History Mysteries Logo
History of Humanity - Ancient Mesopotamia Logo
History of Humanity - Egypt History Logo
History of Humanity - Persian Empire Logo
History of Humanity - Greek History Logo
History of Humanity - Alexander the Great Logo
History of Humanity - Roman History Logo
History of Humanity - Punic Wars Logo
History of Humanity - Golden Age of Piracy Logo
History of Humanity - Revolutionary War Logo
History of Humanity - Mafia History Logo