ap <- as_tsibble(AirPassengers)
ap |>
mutate(log_y = log(value)) |>
features(log_y, list(unitroot_nsdiffs, unitroot_ndiffs)) |>
fmt_tbl(digits = 0, caption = "Recommended differencing orders on log(AirPassengers).")| nsdiffs | ndiffs |
|---|---|
| 1 | 1 |
Module 3: ARIMA & SARIMA Models
After completing this note, you will be able to:
.innov visually (gg_tsresiduals()) and formally (ljung_box() with dof = p + q + P + Q).Selecting an appropriate \(\text{SARIMA}(p, d, q)(P, D, Q)_m\) model is an iterative diagnostic cycle rather than blind trial and error:
unitroot_nsdiffs) on the transformed series, apply \(\nabla_m^D\), and then test regular differencing (unitroot_ndiffs, based on KPSS) on the seasonally differenced series..innov for white-noise behavior via gg_tsresiduals() and ljung_box(..., dof = p + q + P + Q). If significant autocorrelation remains, refine the candidate set; once residuals look white, forecast on the original scale.The AirPassengers series (monthly international airline totals, 1949 to 1960, \(m = 12\)) displays both upward trend and multiplicative seasonality whose amplitude grows proportionally with the level.
ap <- as_tsibble(AirPassengers)
ap |>
mutate(log_y = log(value)) |>
features(log_y, list(unitroot_nsdiffs, unitroot_ndiffs)) |>
fmt_tbl(digits = 0, caption = "Recommended differencing orders on log(AirPassengers).")| nsdiffs | ndiffs |
|---|---|
| 1 | 1 |
ap |>
mutate(dd_log_y = log(value) |> difference(12) |> difference(1)) |>
filter(!is.na(dd_log_y)) |>
gg_tsdisplay(dd_log_y, plot_type = "partial", lag_max = 36) |>
ts_panel(
title = TeX(r"(AirPassengers: $\nabla\nabla_{12}\ln(y_t)$)", bold = TRUE),
subtitle = "Doubly differenced log series with sharp ACF spikes at lags 1 and 12"
)
Both unitroot_nsdiffs and unitroot_ndiffs return 1, confirming \(D = 1\) and \(d = 1\) on log(value). In Figure 1, the doubly differenced log series fluctuates around zero with constant variance, and the ACF exhibits clean negative spikes at lag 1 and lag 12 while the PACF decays at lags 1–3 and 12. That points directly to \(\text{SARIMA}(0, 1, 1)(0, 1, 1)_{12}\) alongside nearby \((p, q)(P, Q)\) neighbors, all sharing log(value), \(d = 1\), and \(D = 1\).
fit_ap <- ap |>
model(
m011_011 = ARIMA(log(value) ~ 0 + pdq(0, 1, 1) + PDQ(0, 1, 1)),
m110_011 = ARIMA(log(value) ~ 0 + pdq(1, 1, 0) + PDQ(0, 1, 1)),
m111_011 = ARIMA(log(value) ~ 0 + pdq(1, 1, 1) + PDQ(0, 1, 1)),
m011_110 = ARIMA(log(value) ~ 0 + pdq(0, 1, 1) + PDQ(1, 1, 0)),
m011_111 = ARIMA(log(value) ~ 0 + pdq(0, 1, 1) + PDQ(1, 1, 1))
)
glance(fit_ap) |>
select(.model, sigma2, log_lik, AICc, BIC) |>
arrange(AICc) |>
fmt_tbl(digits = 3, caption = "Candidate SARIMA models on log(AirPassengers), all with d = 1 and D = 1.")| .model | sigma2 | log_lik | AICc | BIC |
|---|---|---|---|---|
| m011_011 | 0.001 | 244.7 | -483.2 | -474.8 |
| m011_111 | 0.001 | 245.0 | -481.6 | -470.4 |
| m111_011 | 0.001 | 244.9 | -481.6 | -470.4 |
| m110_011 | 0.001 | 243.7 | -481.3 | -472.9 |
| m011_110 | 0.001 | 241.7 | -477.2 | -468.8 |
m011_011 (the Airline Model) wins on both AICc (-483.2) and BIC (-474.8). We verify its innovation residuals using gg_tsresiduals() and a Ljung-Box test at lag \(2m = 24\) with dof = 0 + 1 + 0 + 1 = 2.
fit_ap |>
select(m011_011) |>
gg_tsresiduals(lag_max = 24) |>
ts_panel(
title = bquote(bold("AirPassengers: Innovation Residuals for SARIMA(0,1,1)(0,1,1)"[12])),
subtitle = "Time plot, white-noise ACF (lags 1-24), and innovation residual distribution"
)
augment(fit_ap |> select(m011_011)) |>
features(.innov, ljung_box, lag = 24, dof = 2) |>
fmt_tbl(digits = 3, caption = "Ljung-Box test on AirPassengers innovation residuals (lag = 24, dof = 2).")| .model | lb_stat | lb_pvalue |
|---|---|---|
| m011_011 | 26.45 | 0.233 |
All residual autocorrelations stay within the 95% bands, and the Ljung-Box test gives \(Q^* = 26.4\) (\(p = 0.233\)), confirming white-noise innovations.
The USAccDeaths series (monthly U.S. accidental deaths, 1973 to 1978, \(n = 72\)) exhibits strong annual seasonality with roughly constant variance (no log transform needed). Watch the sequential order of Step 2: test \(D\) first, apply seasonal differencing, and then test \(d\) on the seasonally differenced series.
acc <- as_tsibble(USAccDeaths)
tibble(
D = acc |> features(value, unitroot_nsdiffs) |> pull(nsdiffs),
d_after_D = acc |> mutate(d12 = difference(value, 12)) |>
features(d12, unitroot_ndiffs) |> pull(ndiffs)
) |>
fmt_tbl(digits = 0, caption = "Sequential unit-root differencing checks for USAccDeaths.")| D | d_after_D |
|---|---|
| 1 | 1 |
Seasonal differencing (\(D = 1\)) followed by first differencing (\(d = 1\)) is indicated. (Note: if you ran unitroot_ndiffs on raw USAccDeaths without seasonal differencing first, you would miss the remaining non-stationarity in \(\nabla_{12} y_t\).) We inspect the ACF/PACF of \(\nabla \nabla_{12} y_t\), compare candidates that all share \((d = 1, D = 1)\) on the untransformed scale, and check residuals.
acc |>
mutate(dd_y = value |> difference(12) |> difference(1)) |>
filter(!is.na(dd_y)) |>
gg_tsdisplay(dd_y, plot_type = "partial", lag_max = 24) |>
ts_panel(
title = TeX(r"(USAccDeaths: $\nabla\nabla_{12} y_t$)", bold = TRUE),
subtitle = "Doubly differenced monthly deaths with ACF spikes at lags 1 and 12"
)
fit_acc <- acc |>
model(
m011_011 = ARIMA(value ~ 0 + pdq(0, 1, 1) + PDQ(0, 1, 1)),
m110_011 = ARIMA(value ~ 0 + pdq(1, 1, 0) + PDQ(0, 1, 1)),
m111_011 = ARIMA(value ~ 0 + pdq(1, 1, 1) + PDQ(0, 1, 1)),
m011_110 = ARIMA(value ~ 0 + pdq(0, 1, 1) + PDQ(1, 1, 0)),
m110_110 = ARIMA(value ~ 0 + pdq(1, 1, 0) + PDQ(1, 1, 0))
)
glance(fit_acc) |>
select(.model, sigma2, log_lik, AICc, BIC) |>
arrange(AICc) |>
fmt_tbl(digits = 2, caption = "Candidate SARIMA models for USAccDeaths (all d = 1, D = 1).")| .model | sigma2 | log_lik | AICc | BIC |
|---|---|---|---|---|
| m011_011 | 102860 | -425.4 | 857.3 | 863.1 |
| m111_011 | 104809 | -425.4 | 859.5 | 867.1 |
| m110_011 | 105977 | -426.6 | 859.7 | 865.5 |
| m011_110 | 113417 | -426.9 | 860.3 | 866.1 |
| m110_110 | 120328 | -428.6 | 863.7 | 869.5 |
augment(fit_acc |> select(m011_011)) |>
features(.innov, ljung_box, lag = 24, dof = 2) |>
fmt_tbl(digits = 3, caption = "Ljung-Box check on USAccDeaths SARIMA(0,1,1)(0,1,1)[12] residuals (dof = 2).")| .model | lb_stat | lb_pvalue |
|---|---|---|
| m011_011 | 26.09 | 0.248 |
SARIMA(0,1,1)(0,1,1)[12] achieves the lowest AICc (857.3) and BIC (863.1) and passes the Ljung-Box test (\(Q^* = 26.1, p = 0.248\)). Notice that we never compare AICc against a \(D = 0\) model, because changing \((d, D)\) changes both the response and the number of observations in the likelihood.
fit_acc |>
select(m011_011) |>
forecast(h = "2 years") |>
anchor_fc(acc) |>
autoplot(acc, color = ts_pal["navy"]) +
labs(
title = bquote(bold("USAccDeaths: 24-Month SARIMA(0,1,1)(0,1,1)"[12] ~ "Forecast")),
subtitle = "Point forecasts with 80% and 95% prediction intervals",
x = NULL, y = "Monthly deaths"
)
Exercise 1. Run the full Box-Jenkins workflow on Australian quarterly beer production (aus_production$Beer) from 1992 Q1 onward: determine \((D, d)\) sequentially, inspect the differenced ACF/PACF, fit four candidates with identical \((d, D)\), select the best model by AICc, and validate .innov with ljung_box().
beer <- aus_production |>
filter_index("1992 Q1" ~ .) |>
select(Quarter, Beer)
tibble(
D = beer |> features(Beer, unitroot_nsdiffs) |> pull(nsdiffs),
d_after_D = beer |> mutate(d4 = difference(Beer, 4)) |>
features(d4, unitroot_ndiffs) |> pull(ndiffs)
) |>
fmt_tbl(digits = 0)| D | d_after_D |
|---|---|
| 1 | 0 |
fit_beer <- beer |>
model(
m001_011 = ARIMA(Beer ~ 1 + pdq(0, 0, 1) + PDQ(0, 1, 1)),
m100_011 = ARIMA(Beer ~ 1 + pdq(1, 0, 0) + PDQ(0, 1, 1)),
m002_011 = ARIMA(Beer ~ 1 + pdq(0, 0, 2) + PDQ(0, 1, 1)),
m001_110 = ARIMA(Beer ~ 1 + pdq(0, 0, 1) + PDQ(1, 1, 0))
)
glance(fit_beer) |>
select(.model, sigma2, AICc, BIC) |>
arrange(AICc) |>
fmt_tbl(digits = 2)| .model | sigma2 | AICc | BIC |
|---|---|---|---|
| m001_011 | 149.3 | 558.1 | 566.5 |
| m100_011 | 149.7 | 558.3 | 566.7 |
| m002_011 | 151.6 | 560.4 | 570.8 |
| m001_110 | 183.3 | 570.3 | 578.7 |
augment(fit_beer |> select(m001_011)) |>
features(.innov, ljung_box, lag = 12, dof = 2) |>
fmt_tbl(digits = 3)| .model | lb_stat | lb_pvalue |
|---|---|---|
| m001_011 | 8.345 | 0.595 |
Sequential unit-root tests select \(D = 1\) and \(d = 0\) (no variance transform is needed since seasonal swings are stable after 1992). Among the fixed \((d = 0, D = 1)\) candidates with drift, SARIMA(0,0,1)(0,1,1)[4] achieves the lowest AICc (558.1) and BIC (566.5), and passes the Ljung-Box test at lag 12 (dof = 2, \(Q^* = 8.35, p = 0.595\)).
Exercise 2. Why must unitroot_nsdiffs() and unitroot_ndiffs() be run on log(value) rather than raw value when a series has multiplicative seasonality (such as AirPassengers), and why must dof be adjusted in ljung_box()?
ljung_box(): Fitting \(p + q + P + Q\) ARMA parameters absorbs autocorrelation from the sample residuals, shifting the null distribution of the Ljung-Box statistic from \(\chi^2_{\ell}\) to approximately \(\chi^2_{\ell - (p + q + P + Q)}\). Omitting dof produces overly large \(p\)-values and risks missing genuine model misspecification..innov.unitroot_ndiffs() on the seasonally differenced series rather than the raw data..innov with gg_tsresiduals() + plot_annotation() and ljung_box(..., dof = p + q + P + Q).