Time series can have distinct patterns:
Trend: A long-term increase/decrease in the data.
Seasonal: Fluctuations in the time series with a fixed and known period1.
Cycles: More commonly known as “Business cycles”, refer to rises and falls that are not of a fixed frequency2.
Changes in variability: Changes in the spread of the data over time, i. e., an increase/decrease in the variance as the level of the series increases/decreases.
A time series can be decomposed into the following components:
Seasonal component (S): The repeating short-term cycle in the series.
Trend-cycle component (T): The long-term progression of the series.
Residual component (R): The residuals or “noise” left after removing the seasonal and trend-cycle components.
\[ w_t= \begin{cases}\log \left(y_t\right) & \text { if } \lambda=0 \\ \left(\operatorname{sign}\left(y_t\right)\left|y_t\right|^\lambda-1\right) / \lambda & \text { otherwise }\end{cases} \]
What happens when \(\lambda = 1\)?
You should choose a value of \(\lambda\) that makes the size of the seasonal variation the same throughout the series.
We can use the guerrero
feature to choose an optimal lambda.
Is the Mexican economy really that similar Australia’s economy? Is Iceland’s economy really that small?
The population sizes of these countries are very different.
\[ x_t = \frac{y_t}{z_t} * z_{2010} \]
\[ y_t = T_t + S_t + R_t \]
\[ y_t = T_t \times S_t \times R_t \\ \]
In a classical decomposition, the trend-cycle component is estimated using a moving average. Then, the seasonal component is estimated by averaging the detrended values for each season. Finally, the remainder component is obtained by subtracting the trend-cycle and seasonal components from the original series.
An \(m\) order moving average is given by:
\[ \hat{T}_{t}=\frac{1}{m} \sum_{j=-k}^{k} y_{t+j} \]
where \(k = (m-1)/2\)3.
mexretail_dcmp <- mexretail |>
model(
classical = classical_decomposition(y, type = "additive")
) |>
components()
mexretail_dcmp
tsibble
.
model()
function, we specify the type of models we want to use.
model()
function yields a mable
4, which is a table that contains the fitted models for each time series in the tsibble
.
components()
function is used to extract the components of the decomposition (trend-cycle, seasonal, and remainder) from the fitted models in the mable
. It also provides the seasonally adjusted series.
STL cannot automatically handle calendar or holiday variations.
It only provides methods for additive models. If your data has multiplicative seasonality, you should log-transform the data before applying STL.
fable
fable
fable
fable
fable
mexretail |>
model(
stl = STL(y ~
trend(window = NULL) +
season(window = "periodic"),
robust = TRUE)
) |>
components() |>
autoplot()
STL()
function, we can specify the formula for the decomposition, or don’t specify it at all. See ?STL
for more details.
trend()
function is used to specify the trend component of the decomposition. The window
argument controls the smoothness of the trend component. A larger window results in a smoother trend.
season()
function is used to specify the seasonal component of the decomposition. The window
argument controls the smoothness of the seasonal component. Setting it to “periodic” means that the seasonal component will be fixed over time.
robust
argument, when set to TRUE
, makes the STL decomposition more robust to outliers in the data, so the effect of such values is sent to the residual component.
Writing formulas in R
In R, we use “\(\sim\)” instead of “\(=\)” in formula specification, i.e., \(y \sim mx + b\).
A time series can have multiple seasonal patterns.
They usually last at least 2 years.
In R, you can compute any moving average by using the slider::slide_dbl()
function.
short for “model table”
There are other decomposition methods primarily used by official statistics agencies, such as X-11, X-12-ARIMA, and TRAMO/SEATS. However, these methods are not as widely used in the forecasting community as STL. For more on these, see this.
Time Series Forecasting