Seasonal adjustment of high-frequency series

Introduction

Besides X-13ARIMA and TRAMO-SEATS (see vignette("vignette", package = "fable.rjd3")), fable.rjd3 gives access to the decompositions of high-frequency series (daily, hourly, half-hourly…) of the rjd3highfreq package. These series usually have several periodicities, some of them non-integer (a year is 365.25 days on average), that the classical methods cannot handle.

Model Method Periodicities Forecasts
AIRLINE_DECOMPOSITION() Extended (fractional) airline model (rjd3highfreq::multi_airline_decomposition()) one or several yes
MSTL() Multiple STL (rjd3highfreq::mstl()) one or several no
ISTL() Iterative STL (rjd3highfreq::istl()) one or several no
STLPLUS() STL (rjd3highfreq::stlplus()) one no
X11PLUS() Extended X-11 (rjd3highfreq::x11plus()) one no

All of them can be combined with a pre-adjustment of the series (outliers, holidays and user-defined regressors) estimated with an extended airline model (rjd3highfreq::fractional_airline_estimation()): the decomposition is then done on the linearized series, and the effects removed by the pre-adjustment are put back in the components.

rjd3highfreq must be installed (it is only suggested by fable.rjd3).

library(fable.rjd3)
library(dplyr)
#> 
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#> 
#>     filter, lag
#> The following objects are masked from 'package:base':
#> 
#>     intersect, setdiff, setequal, union

Daily series

We use the number of daily births in France (rjd3toolkit::Births), over the last four years. The series must be a tsibble without gaps.

births <- as_tsibble(tail(rjd3toolkit::Births, 365 * 4), index = date)
births
#> # A tsibble: 1,460 x 2 [1D]
#>    date       births
#>    <date>      <int>
#>  1 2021-01-02   1439
#>  2 2021-01-03   1427
#>  3 2021-01-04   1824
#>  4 2021-01-05   1878
#>  5 2021-01-06   1820
#>  6 2021-01-07   1762
#>  7 2021-01-08   1716
#>  8 2021-01-09   1434
#>  9 2021-01-10   1416
#> 10 2021-01-11   1693
#> # ℹ 1,450 more rows

Periodicities

The periodicities are deduced from the index: c(7, 365.25) for a daily series (weekly and annual periodicities), k * c(1, 7) for a series with k observations per day (daily and weekly periodicities: the annual periodicity, k * 365.25, must be added with period()). They can be set with the special period(). STLPLUS() and X11PLUS() handle only one periodicity: by default the smallest one is used.

mb <- model(
    births,
    airline = AIRLINE_DECOMPOSITION(births, log = TRUE),
    mstl = MSTL(births),
    x11 = X11PLUS(births ~ period(7))
)
mb
#> # A mable: 1 x 3
#>                airline              mstl        x11
#>                <model>           <model>    <model>
#> 1 <Airline(7, 365.25)> <MSTL(7, 365.25)> <X-11+(7)>

Decomposition

components() returns a dable with one seasonal component per periodicity (seasonal_week, seasonal_year, or seasonal with a single periodicity). The decomposition is multiplicative with log = TRUE (for AIRLINE_DECOMPOSITION()) or multiplicative = TRUE (the default of the other models), additive otherwise. With "auto", the choice is made with rjd3highfreq::loglevel().

dcmp <- components(mb)
dcmp
#> # A dable: 4,380 x 9 [1D]
#> # Key:     .model [3]
#> # :        births = trend * seasonal_week * seasonal_year * irregular
#>    .model  date       births trend seasonal_week seasonal_year irregular
#>    <chr>   <date>      <int> <dbl>         <dbl>         <dbl>     <dbl>
#>  1 airline 2021-01-02   1439 1788.         0.881         0.926     0.986
#>  2 airline 2021-01-03   1427 1788.         0.870         0.925     0.991
#>  3 airline 2021-01-04   1824 1788.         1.02          0.989     1.01 
#>  4 airline 2021-01-05   1878 1788.         1.08          0.963     1.01 
#>  5 airline 2021-01-06   1820 1788.         1.05          0.963     1.00 
#>  6 airline 2021-01-07   1762 1788.         1.06          0.939     0.991
#>  7 airline 2021-01-08   1716 1788.         1.06          0.922     0.980
#>  8 airline 2021-01-09   1434 1788.         0.882         0.930     0.978
#>  9 airline 2021-01-10   1416 1788.         0.870         0.929     0.980
#> 10 airline 2021-01-11   1693 1788.         1.02          0.941     0.987
#> # ℹ 4,370 more rows
#> # ℹ 2 more variables: season_adjust <dbl>, seasonal <dbl>
library(ggtime)
dcmp |>
    filter(.model == "airline") |>
    ggplot2::autoplot()

Pre-adjustment

The pre-adjustment is defined with specials of the formula:

  • preadjust(): outlier detection (outliers = c("ao", "ls", "wo") and critical_value = 6 by default) and other options of rjd3highfreq::fractional_airline_estimation();
  • holidays(calendar): holidays regressors of a national calendar (rjd3toolkit::national_calendar());
  • xreg(..., regeffect = "Irregular"): regressors taken from the columns of the tsibble (variables written directly in the formula are handled by xreg()).
french_calendar <- rjd3toolkit::national_calendar(list(
    rjd3toolkit::special_day("NEWYEAR"),
    rjd3toolkit::special_day("EASTERMONDAY"),
    rjd3toolkit::special_day("MAYDAY"),
    rjd3toolkit::fixed_day(5, 8),
    rjd3toolkit::special_day("ASCENSION"),
    rjd3toolkit::special_day("WHITMONDAY"),
    rjd3toolkit::fixed_day(7, 14),
    rjd3toolkit::special_day("ASSUMPTION"),
    rjd3toolkit::special_day("ALLSAINTSDAY"),
    rjd3toolkit::special_day("ARMISTICE"),
    rjd3toolkit::special_day("CHRISTMAS")
))

mb_pre <- model(
    births,
    airline = AIRLINE_DECOMPOSITION(
        births ~ preadjust(critical_value = 5) + holidays(french_calendar),
        log = TRUE
    ),
    mstl = MSTL(births ~ preadjust(critical_value = 5) + holidays(french_calendar))
)

The effects removed by the pre-adjustment are allocated to the components following the JDemetra+ conventions:

  • level shifts (LS) are added to the trend;
  • additive outliers (AO) and switch outliers (WO) are added to the irregular;
  • the holidays are put in a separate calendar component, which is removed from the seasonally adjusted series;
  • user-defined regressors go to the component given by regeffect ("Irregular", "Trend" or "Calendar").
components(mb_pre) |>
    filter(.model == "airline")
#> # A dable: 1,460 x 9 [1D]
#> # Key:     .model [1]
#> # :        births = trend * seasonal_week * seasonal_year * calendar *
#> #   irregular
#>    .model date       births trend seasonal_week seasonal_year calendar irregular
#>    <chr>  <date>      <int> <dbl>         <dbl>         <dbl>    <dbl>     <dbl>
#>  1 airli… 2021-01-02   1439 1784.         0.880         0.925        1     0.991
#>  2 airli… 2021-01-03   1427 1784.         0.869         0.925        1     0.995
#>  3 airli… 2021-01-04   1824 1784.         1.02          0.992        1     1.01 
#>  4 airli… 2021-01-05   1878 1784.         1.08          0.969        1     1.01 
#>  5 airli… 2021-01-06   1820 1784.         1.05          0.966        1     1.00 
#>  6 airli… 2021-01-07   1762 1784.         1.06          0.937        1     0.994
#>  7 airli… 2021-01-08   1716 1784.         1.06          0.918        1     0.988
#>  8 airli… 2021-01-09   1434 1784.         0.880         0.925        1     0.987
#>  9 airli… 2021-01-10   1416 1784.         0.869         0.925        1     0.988
#> 10 airli… 2021-01-11   1693 1784.         1.02          0.936        1     0.992
#> # ℹ 1,450 more rows
#> # ℹ 1 more variable: season_adjust <dbl>

The coefficients of the airline model and of the regressors are given by tidy(), and the dates of the outliers by outliers().

tidy(mb_pre) |>
    filter(.model == "airline")
#> # A tibble: 14 × 6
#>    .model  term           estimate std.error statistic   p.value
#>    <chr>   <chr>             <dbl>     <dbl>     <dbl>     <dbl>
#>  1 airline theta            -0.266   0.0237     -11.2  2.57e- 29
#>  2 airline btheta(7)         0.727   0.0223      32.6  4.57e-233
#>  3 airline btheta(365.25)    0.950   0.00772    123.   0        
#>  4 airline 05-08            -0.119   0.0211      -5.65 1.58e-  8
#>  5 airline 07-14            -0.119   0.0176      -6.77 1.27e- 11
#>  6 airline NEWYEAR          -0.224   0.0294      -7.64 2.13e- 14
#>  7 airline EASTERMONDAY     -0.119   0.0146      -8.10 5.61e- 16
#>  8 airline MAYDAY           -0.151   0.0210      -7.21 5.51e- 13
#>  9 airline ASCENSION        -0.134   0.0147      -9.08 1.13e- 19
#> 10 airline WHITMONDAY       -0.161   0.0146     -11.0  3.72e- 28
#> 11 airline ASSUMPTION       -0.125   0.0173      -7.25 4.27e- 13
#> 12 airline ALLSAINTSDAY     -0.141   0.0153      -9.19 3.92e- 20
#> 13 airline ARMISTICE        -0.111   0.0174      -6.40 1.51e- 10
#> 14 airline CHRISTMAS        -0.217   0.0216     -10.0  1.01e- 23
outliers(mb_pre)
#> # A tsibble: 0 x 3 [?]
#> # Key:       .model [0]
#> # ℹ 3 variables: .model <chr>, date <date>, births <int>
report(select(mb_pre, airline))
#> Series: births 
#> Model: Airline(7, 365.25) + preadj. 
#> Method: Airline, periodicities: 7, 365.25
#> Decomposition: multiplicative
#> Pre-adjustment: 11 regressor(s), 0 outlier(s)
#> 
#> Coefficients:
#>                  Estimate Std. Error  z value  Pr(>|z|)    
#> theta          -0.2663618  0.0236963 -11.2406 < 2.2e-16 ***
#> btheta(7)       0.7274811  0.0223176  32.5967 < 2.2e-16 ***
#> btheta(365.25)  0.9504988  0.0077205 123.1142 < 2.2e-16 ***
#> 05-08          -0.1193173  0.0211080  -5.6527 1.579e-08 ***
#> 07-14          -0.1190842  0.0175849  -6.7720 1.270e-11 ***
#> NEWYEAR        -0.2243851  0.0293597  -7.6426 2.128e-14 ***
#> EASTERMONDAY   -0.1185902  0.0146452  -8.0976 5.607e-16 ***
#> MAYDAY         -0.1514426  0.0209987  -7.2120 5.514e-13 ***
#> ASCENSION      -0.1335251  0.0147125  -9.0756 < 2.2e-16 ***
#> WHITMONDAY     -0.1611431  0.0146461 -11.0024 < 2.2e-16 ***
#> ASSUMPTION     -0.1254168  0.0173071  -7.2466 4.275e-13 ***
#> ALLSAINTSDAY   -0.1405817  0.0152970  -9.1902 < 2.2e-16 ***
#> ARMISTICE      -0.1113192  0.0173825  -6.4041 1.513e-10 ***
#> CHRISTMAS      -0.2172343  0.0216357 -10.0406 < 2.2e-16 ***
#> 
#> log likelihood = 2885.81, AIC = -5741.63, AICc = -5741.29, BIC = -5662.42

User-defined regressors

births <- births |>
    mutate(lockdown = as.numeric(date >= as.Date("2021-01-01") & date <= as.Date("2021-01-31")))
mb_xreg <- model(
    births,
    airline = AIRLINE_DECOMPOSITION(
        births ~ holidays(french_calendar) + xreg(lockdown, regeffect = "Trend"),
        log = TRUE
    )
)
tidy(mb_xreg)
#> # A tibble: 15 × 6
#>    .model  term           estimate std.error statistic   p.value
#>    <chr>   <chr>             <dbl>     <dbl>     <dbl>     <dbl>
#>  1 airline theta           -0.272    0.0233     -11.6  2.42e- 31
#>  2 airline btheta(7)        0.793    0.0210      37.7  2.30e-310
#>  3 airline btheta(365.25)   0.953    0.00740    129.   0        
#>  4 airline 05-08           -0.119    0.0212      -5.63 1.79e-  8
#>  5 airline 07-14           -0.119    0.0177      -6.70 2.03e- 11
#>  6 airline NEWYEAR         -0.223    0.0295      -7.56 4.18e- 14
#>  7 airline EASTERMONDAY    -0.120    0.0148      -8.14 3.96e- 16
#>  8 airline MAYDAY          -0.152    0.0211      -7.21 5.44e- 13
#>  9 airline ASCENSION       -0.132    0.0148      -8.88 6.44e- 19
#> 10 airline WHITMONDAY      -0.160    0.0148     -10.9  1.94e- 27
#> 11 airline ASSUMPTION      -0.125    0.0174      -7.16 8.00e- 13
#> 12 airline ALLSAINTSDAY    -0.141    0.0154      -9.16 5.37e- 20
#> 13 airline ARMISTICE       -0.111    0.0175      -6.35 2.09e- 10
#> 14 airline CHRISTMAS       -0.215    0.0216      -9.98 1.85e- 23
#> 15 airline lockdown        -0.0697   0.0110      -6.32 2.58e- 10

Forecasts

AIRLINE_DECOMPOSITION() can forecast and backcast the series: the forecasts of the linearized series are combined with the regression effects (holidays, level shifts, and user-defined regressors whose future values are taken from new_data). rjd3highfreq does not provide the standard errors of the forecasts yet, so only point forecasts are returned (degenerate distributions).

fc <- forecast(select(mb_pre, airline), h = 28)
fc
#> # A fable: 28 x 4 [1D]
#> # Key:     .model [1]
#>    .model  date         births .mean
#>    <chr>   <date>       <dist> <dbl>
#>  1 airline 2025-01-01 1358.818 1359.
#>  2 airline 2025-01-02  1697.67 1698.
#>  3 airline 2025-01-03 1719.785 1720.
#>  4 airline 2025-01-04 1477.322 1477.
#>  5 airline 2025-01-05  1413.45 1413.
#>  6 airline 2025-01-06 1612.554 1613.
#>  7 airline 2025-01-07 1702.224 1702.
#>  8 airline 2025-01-08 1699.912 1700.
#>  9 airline 2025-01-09 1700.318 1700.
#> 10 airline 2025-01-10 1718.771 1719.
#> # ℹ 18 more rows
backcast(select(mb_pre, airline), h = 7)
#> # A tibble: 7 × 3
#>   .model  date           dist
#>   <chr>   <date>       <dist>
#> 1 airline 2020-12-26 1504.245
#> 2 airline 2020-12-27 1492.434
#> 3 airline 2020-12-28 1762.848
#> 4 airline 2020-12-29 1847.444
#> 5 airline 2020-12-30 1799.525
#> 6 airline 2020-12-31 1806.209
#> 7 airline 2021-01-01 1434.304
future <- new_data(births, 28) |>
    mutate(lockdown = 0)
forecast(mb_xreg, new_data = future)
#> # A fable: 28 x 5 [1D]
#> # Key:     .model [1]
#>    .model  date         births .mean lockdown
#>    <chr>   <date>       <dist> <dbl>    <dbl>
#>  1 airline 2025-01-01   1362.6 1363.        0
#>  2 airline 2025-01-02 1698.912 1699.        0
#>  3 airline 2025-01-03  1723.25 1723.        0
#>  4 airline 2025-01-04  1480.95 1481.        0
#>  5 airline 2025-01-05 1417.413 1417.        0
#>  6 airline 2025-01-06 1618.302 1618.        0
#>  7 airline 2025-01-07 1710.069 1710.        0
#>  8 airline 2025-01-08  1707.68 1708.        0
#>  9 airline 2025-01-09 1707.478 1707.        0
#> 10 airline 2025-01-10 1724.996 1725.        0
#> # ℹ 18 more rows
fc |>
    ggplot2::autoplot(tail(births, 120))

The other models have no forecasting method. Their components can be forecasted separately with fabletools::decomposition_model(), which requires an additive decomposition (for a multiplicative one, decompose the log of the series):

fit <- model(
    births,
    dcmp = fabletools::decomposition_model(
        MSTL(log(births) ~ period(7), multiplicative = FALSE),
        fable::NAIVE(season_adjust)
    )
)
forecast(fit, h = 7)
#> # A fable: 7 x 4 [1D]
#> # Key:     .model [1]
#>   .model date                  births .mean
#>   <chr>  <date>                <dist> <dbl>
#> 1 dcmp   2025-01-01 t(N(7.5, 0.0019)) 1722.
#> 2 dcmp   2025-01-02 t(N(7.5, 0.0039)) 1745.
#> 3 dcmp   2025-01-03 t(N(7.5, 0.0058)) 1768.
#> 4 dcmp   2025-01-04 t(N(7.3, 0.0077)) 1490.
#> 5 dcmp   2025-01-05 t(N(7.2, 0.0096)) 1407.
#> 6 dcmp   2025-01-06  t(N(7.4, 0.012)) 1656.
#> 7 dcmp   2025-01-07  t(N(7.5, 0.013)) 1755.

Fitted values and residuals (fitted(), residuals(), augment()) come from the airline model: the pre-adjustment model, or the model of AIRLINE_DECOMPOSITION().

Infra-daily series

rjd3toolkit::Electricity contains the half-hourly electricity consumption in France. Here we only use the last eight weeks. With infra-daily data, the default periodicities are the daily (48) and weekly (336) ones: the annual periodicity (17532) must be added explicitly with period(). It is rarely relevant on a few years of data (it is then estimated on very few cycles) and much longer to compute; an annual effect is usually better studied on the daily aggregated series.

electricity <- rjd3toolkit::Electricity |>
    tail(48 * 7 * 8) |>
    transmute(
        time = as.POSIXct(paste(Date, Hours), tz = "UTC"),
        consumption = Consumtion
    ) |>
    as_tsibble(index = time)

mb_elec <- model(
    electricity,
    airline = AIRLINE_DECOMPOSITION(consumption, log = TRUE),
    mstl = MSTL(consumption)
)
components(mb_elec)
#> # A dable: 5,376 x 8 [30m] <UTC>
#> # Key:     .model [2]
#> # :        consumption = trend * seasonal_day * seasonal_week * irregular
#>    .model  time                consumption  trend seasonal_day seasonal_week
#>    <chr>   <dttm>                    <int>  <dbl>        <dbl>         <dbl>
#>  1 airline 2023-11-06 00:00:00       48626 52475.        0.959         0.967
#>  2 airline 2023-11-06 00:30:00       47668 52478.        0.938         0.968
#>  3 airline 2023-11-06 01:00:00       45374 52482.        0.890         0.972
#>  4 airline 2023-11-06 01:30:00       45145 52487.        0.884         0.973
#>  5 airline 2023-11-06 02:00:00       44514 52493.        0.872         0.973
#>  6 airline 2023-11-06 02:30:00       44162 52500.        0.863         0.975
#>  7 airline 2023-11-06 03:00:00       42829 52508.        0.838         0.974
#>  8 airline 2023-11-06 03:30:00       42269 52517.        0.827         0.973
#>  9 airline 2023-11-06 04:00:00       41568 52527.        0.814         0.973
#> 10 airline 2023-11-06 04:30:00       41511 52539.        0.810         0.976
#> # ℹ 5,366 more rows
#> # ℹ 2 more variables: irregular <dbl>, season_adjust <dbl>
components(mb_elec) |>
    filter(.model == "mstl") |>
    ggplot2::autoplot()

Limitations

  • The index must be a Date (daily or weekly series), a POSIXct (infra-daily series, with a constant interval: use a time zone without daylight saving time, such as UTC) or a yearweek; otherwise the periodicities must be given with period().
  • Holidays are only available for daily and infra-daily series.
  • The forecasts are point forecasts, and are only available for AIRLINE_DECOMPOSITION().
  • Backcasts are not available with user-defined regressors.
  • MSTL() and ISTL() use by default 15 robustness iterations (nouterloop = 15), whereas forecast::mstl() and feasts::STL() use none: with long series and long periodicities, nouterloop = 1 divides the computation time by about seven (nouterloop = 0 fails with rjd3highfreq 3.0.1). Even then, these functions are much slower than their stats::stl()-based counterparts when the annual periodicity of infra-daily data is used.
  • The computation time and the memory used by the Java routines grow quickly with the length of the series and of the periodicities; the Java heap size can be increased with options(java.parameters = "-Xmx4g") before loading the package.