1library(tidyquant)
library(plotly)- 1
-
In addition to the regular packages, here we’ll use
tidyquantandplotly
NEW - AI code assistant Elendil TA is now available! Click the icon in the navbar to start using it.
pbenavides
November 12, 2025
1library(tidyquant)
library(plotly)tidyquant and plotly
All these time series have different shapes, patterns, and so on. When modeling them, we need to take these characteristics into account. We seek to understand the underlying patterns in the data to make better forecasts.
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.
Transformations are used to stabilize the variance of a time series, making it easier to model and forecast. They can help to make the patterns in the data more apparent.
Transformations and adjustments help us simplify the patterns in our data, and can improve our forecasts’ accuracy.
Log transformations are often useful when the data presents an increasing/decreasing variation with the level of the series.
Log transformations are very interpretable: changes in a log value are percent changes on the original scale.
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}
In a Box-Cox transformation, the log is always a natural logarithm. The other case is just a power transformation with scaling.
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.
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
ℹ The deprecated feature was likely used in the fabletools package.
Please report the issue at <https://github.com/tidyverts/fabletools/issues>.
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}
where:
A decomposition splits the time series into its underlying components:
And what’s left of it we simply call it a “remainder component”.
In general, there are two types of decompositions:
y_t = T_t + S_t + R_t
y_t = T_t \times S_t \times R_t \\
A multiplicative decomposition is equivalent to an additive decomposition of the log-transformed series:
y_t = T_t \times S_t \times R_t
is equivalent to
\log(y_t) = \log(T_t) + \log(S_t) + \log(R_t)
One use of decomposition is to obtain a seasonally adjusted series, which is the original series with the seasonal component removed.
Seasonally adjusted series can be useful for: - Identifying and analyzing the trend-cycle component without the influence of seasonal fluctuations. - Making comparisons across different time periods without seasonal effects.
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)/23.
tsibble.
model() function, we specify the type of models we want to use.
model() function yields a mable4, 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.
It is not recommended to use classical decomposition for forecasting because of these issues.
STL (Seasonal and Trend decomposition using Loess) is a more advanced method for decomposing time series data5. It uses locally weighted regression (loess) to estimate the trend-cycle and seasonal components. STL is more flexible than classical decomposition and can handle changes in the seasonal component over time.
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.
fableThe code is basically the same as for the classical decomposition. We just need to change the model used inside the model() function.
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.
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.↩︎