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)}})
ts_info(USgas)
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)
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")
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")
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)
The ts_polar function returns a polar plot representation for monthly and quarterly time series data:
ts_polar(USgas)