Skip to main content

Note 10: Dynamic Regression, ARIMA Errors & Harmonic Seasonality

Module 2: Stationarity & Regression

Learning Objectives

  • Formulate regression with ARIMA errors and explain how it differs from ARMAX and inconsistent lagged-\(y\) OLS.
  • Explain how automatic differencing applies jointly to both response and predictors when \(d \ge 1\).
  • Build dynamic harmonic regression models with Fourier terms for long seasonal periods and select harmonic order \(K\) via AICc.

1. Regression with ARIMA Errors

In Note 09, adding temperature curvature and calendar dummies to electricity demand still left autocorrelated residuals. Regression with ARIMA errors solves this by allowing the regression disturbance \(\eta_t\) to follow an \(\text{ARIMA}(p, d, q)\) process while keeping the innovations \(\varepsilon_t\) white noise:

\[ y_t = \beta_0 + \mathbf{x}_t^\top \boldsymbol{\beta} + \eta_t, \qquad \phi(B)(1 - B)^d \eta_t = \theta(B) \varepsilon_t, \qquad \varepsilon_t \sim \text{WN}(0, \sigma^2), \]

where \(B y_t = y_{t-1}\) is the backshift operator, \(\phi(B) = 1 - \phi_1 B - \dots - \phi_p B^p\), and \(\theta(B) = 1 + \theta_1 B + \dots + \theta_q B^q\).

Three conceptual distinctions are essential:

  1. Contrast with ARMAX: An ARMAX model writes \(\phi(B) y_t = \mathbf{x}_t^\top \boldsymbol{\beta} + \theta(B)\varepsilon_t\). Dividing by \(\phi(B)\) mixes the AR dynamics directly into every covariate, making \(\boldsymbol{\beta}\) difficult to interpret directly as contemporaneous marginal effects. In regression with ARIMA errors, \(\boldsymbol{\beta}\) retains standard regression meaning while \(\eta_t\) absorbs residual serial dependence.
  2. Why plain OLS with lagged \(y_{t-1}\) fails: Adding \(y_{t-1}\) as a regressor in TSLM() when errors \(u_t\) remain autocorrelated induces correlation between \(y_{t-1}\) and \(u_t\) (through \(u_{t-1}\)), rendering OLS biased and inconsistent. Manual lagged-\(y\) regression also complicates multi-step forecasting because future values of \(y_{t+h-1}\) are unobserved.
  3. Joint differencing when \(d = 1\): Multiplying \(y_t = \beta_0 + \mathbf{x}_t^\top \boldsymbol{\beta} + \eta_t\) by \((1 - B)\) gives: \[\nabla y_t = (\nabla \mathbf{x}_t)^\top \boldsymbol{\beta} + \nabla \eta_t.\] When fable::ARIMA() selects \(d \ge 1\), it automatically differences both \(y_t\) and every regressor in \(\mathbf{x}_t\), guarding against spurious regression while estimating \(\boldsymbol{\beta}\) and \((\phi, \theta)\) jointly by maximum likelihood.

2. Worked Example: U.S. Consumption and Income

Compare plain TSLM(Consumption ~ Income) against dynamic regression ARIMA(Consumption ~ Income) on us_change.

fit_us <- us_change |>
  model(
    OLS = TSLM(Consumption ~ Income),
    DynReg = ARIMA(Consumption ~ Income)
  )

fit_us |>
  select(DynReg) |>
  tidy() |>
  select(term, estimate, std.error, statistic, p.value) |>
  fmt_tbl(digits = 3, caption = "Maximum-likelihood estimates for ARIMA(Consumption ~ Income): LM with ARIMA(1,0,2) errors.")
Maximum-likelihood estimates for ARIMA(Consumption ~ Income): LM with ARIMA(1,0,2) errors.
term estimate std.error statistic p.value
ar1 0.707 0.107 6.622 0.000
ma1 -0.617 0.122 -5.069 0.000
ma2 0.207 0.074 2.790 0.006
Income 0.198 0.046 4.275 0.000
intercept 0.595 0.085 6.997 0.000

Notice that fable::ARIMA() selects an \(\text{ARIMA}(1, 0, 2)\) error structure (\(p + q = 3\), so dof = 3 in the Ljung-Box test on .innov). Compare the white-noise diagnostics of OLS versus dynamic regression:

bind_rows(
  fit_us |> select(OLS) |> augment() |> features(.innov, ljung_box, lag = 10, dof = 0),
  fit_us |> select(DynReg) |> augment() |> features(.innov, ljung_box, lag = 10, dof = 3)
) |>
  fmt_tbl(digits = 4, caption = "Ljung-Box test (h = 10): OLS fails severely, while ARIMA(1,0,2) errors yield clean white-noise innovations.")
Ljung-Box test (h = 10): OLS fails severely, while ARIMA(1,0,2) errors yield clean white-noise innovations.
.model lb_stat lb_pvalue
OLS 35.078 0.0001
DynReg 5.451 0.6051
fit_us |>
  select(DynReg) |>
  gg_tsresiduals() |>
  ts_panel(
    title = "Innovation Diagnostics: Dynamic Regression with ARIMA(1,0,2) Errors",
    subtitle = "Consumption ~ Income: ACF spikes fall inside Bartlett bounds"
  )
Figure 1: Innovation residual diagnostics for dynamic regression ARIMA(Consumption ~ Income), showing uncorrelated, approximately normal innovations.

3. Dynamic Harmonic Regression (Fourier Terms)

When seasonal periods are long (weekly \(m \approx 52.18\), daily \(m \approx 365.25\), or half-hourly), seasonal dummy variables require hundreds of indicators and seasonal ARIMA becomes computationally intractable. Dynamic harmonic regression models smooth periodic seasonality with \(K \le m/2\) pairs of Fourier sine and cosine terms:

\[ y_t = \beta_0 + \mathbf{x}_t^\top \boldsymbol{\beta} + \sum_{k=1}^{K} \left[ \alpha_k \sin\!\left(\frac{2\pi k t}{m}\right) + \beta_k \cos\!\left(\frac{2\pi k t}{m}\right) \right] + \eta_t, \]

where \(\eta_t\) follows a non-seasonal ARIMA process (PDQ(0, 0, 0)), since the Fourier basis already captures the periodic seasonal shape. Increasing \(K\) allows sharper seasonal curves at the cost of \(2K\) additional regression parameters; we select \(K\) by minimizing AICc.

Let us apply dynamic harmonic regression to a full annual cycle (2013, 365 daily observations) of daily Victoria electricity demand (vic_elec), combining centered quadratic temperature, Day_Type, and annual Fourier harmonics for \(K = 1, \dots, 6\) with AR(1) error dynamics (pdq(1, 0, 0) + PDQ(0, 0, 0)).

vic_daily <- vic_elec |>
  filter(year(Time) == 2013) |>
  index_by(Date = as_date(Time)) |>
  summarise(
    Demand = sum(Demand) / 1e3,
    Temperature = max(Temperature),
    Holiday = any(Holiday)
  ) |>
  mutate(
    Temp_c = Temperature - mean(Temperature),
    Day_Type = case_when(
      Holiday ~ "Holiday",
      wday(Date) %in% 2:6 ~ "Weekday",
      TRUE ~ "Weekend"
    )
  )

fit_harm <- vic_daily |>
  model(
    K1 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 1) + pdq(1, 0, 0) + PDQ(0, 0, 0)),
    K2 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 2) + pdq(1, 0, 0) + PDQ(0, 0, 0)),
    K3 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 3) + pdq(1, 0, 0) + PDQ(0, 0, 0)),
    K4 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 4) + pdq(1, 0, 0) + PDQ(0, 0, 0)),
    K5 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 5) + pdq(1, 0, 0) + PDQ(0, 0, 0)),
    K6 = ARIMA(Demand ~ Temp_c + I(Temp_c^2) + Day_Type + fourier(period = "year", K = 6) + pdq(1, 0, 0) + PDQ(0, 0, 0))
  )

glance(fit_harm) |>
  select(.model, sigma2, AICc, BIC) |>
  arrange(AICc) |>
  fmt_tbl(digits = 2, caption = "Harmonic order selection (K = 1..6) for daily Victoria electricity demand (2013).")
Harmonic order selection (K = 1..6) for daily Victoria electricity demand (2013).
.model sigma2 AICc BIC
K4 55.23 2518 2575
K5 55.50 2522 2586
K3 56.58 2524 2574
K6 55.80 2526 2598
K2 58.28 2533 2575
K1 58.92 2535 2569

K = 4 achieves the lowest AICc (adding harmonics beyond \(K = 4\) increases complexity faster than it improves log-likelihood). Plotting observed versus fitted values confirms tight tracking across winter heating peaks, summer cooling spikes, and weekend drops.

fit_harm |>
  select(K4) |>
  augment() |>
  ggplot(aes(x = Date)) +
  geom_line(aes(y = Demand, colour = "Observed"), linewidth = 0.5, alpha = 0.7) +
  geom_line(aes(y = .fitted, colour = "Harmonic Fit (K = 4)"), linewidth = 0.65) +
  scale_colour_manual(values = c("Observed" = unname(ts_pal["slate"]),
                                 "Harmonic Fit (K = 4)" = unname(ts_pal["navy"]))) +
  labs(
    title = "Dynamic Harmonic Regression: Victoria Electricity Demand",
    subtitle = TeX(r"(Annual Fourier ($K = 4$), quadratic temperature, Day Type, and AR(1) errors)"),
    x = "Date", y = "Daily Demand (GWh)", colour = NULL
  )
Figure 2: Observed versus fitted daily Victoria electricity demand (2013) from dynamic harmonic regression with K = 4 annual Fourier pairs, quadratic temperature, Day_Type, and AR(1) errors.

Practice Exercises

Exercise 1: Dynamic Harmonic Regression on Australian Gas Production

Filter aus_production from 1980 Q1 onward. Fit dynamic harmonic regressions on log(Gas) with fourier(K = 1) and fourier(K = 2) (setting PDQ(0, 0, 0) since \(m = 4\) limits \(K \le 2\)). Compare AICc and run a Ljung-Box test (lag = 12, with dof equal to the number of ARMA parameters) on the .innov residuals of the K = 2 model.

Solution

With quarterly data (\(m = 4\)), \(K = 2\) spans all four seasonal degrees of freedom (two sine/cosine pairs, dropping the identically zero \(\sin(\pi t)\) column). Automatic ARIMA error selection chooses \(\text{ARIMA}(0, 1, 3)\) errors (dof = 3), and the resulting .innov residuals pass the Ljung-Box white-noise test cleanly.

gas_80 <- aus_production |>
  filter_index("1980 Q1" ~ .) |>
  select(Gas)

fit_gas <- gas_80 |>
  model(
    K1 = ARIMA(log(Gas) ~ fourier(K = 1) + PDQ(0, 0, 0)),
    K2 = ARIMA(log(Gas) ~ fourier(K = 2) + PDQ(0, 0, 0))
  )

glance(fit_gas) |>
  select(.model, AICc, BIC) |>
  fmt_tbl(digits = 2, caption = "Dynamic harmonic regression comparison on log(Gas) (1980 Q1 onward).")
Dynamic harmonic regression comparison on log(Gas) (1980 Q1 onward).
.model AICc BIC
K1 -453.6 -432.5
K2 -452.9 -431.8
fit_gas |>
  select(K2) |>
  augment() |>
  features(.innov, ljung_box, lag = 12, dof = 3) |>
  fmt_tbl(digits = 4, caption = "Ljung-Box check (lag = 12, dof = 3) confirming white-noise innovations on log(Gas).")
Ljung-Box check (lag = 12, dof = 3) confirming white-noise innovations on log(Gas).
.model lb_stat lb_pvalue
K2 12.1 0.2079

Exercise 2: Multivariate Dynamic Regression on us_change

Fit ARIMA(Consumption ~ Income + Production + Savings) on us_change. Inspect the fitted regression and error coefficients via tidy() and check whether the innovation residuals pass a Ljung-Box test (lag = 10) using the appropriate dof.

Solution

Adding Production and Savings captures substantial contemporaneous macro variation, leaving a compact low-order ARMA error structure whose innovation residuals are indistinguishable from white noise.

fit_us_multi <- us_change |>
  model(multi = ARIMA(Consumption ~ Income + Production + Savings))

coefs <- tidy(fit_us_multi)
arma_dof <- sum(grepl("^(ar|ma|sar|sma)[0-9]+$", coefs$term))

coefs |>
  select(term, estimate, std.error, p.value) |>
  fmt_tbl(digits = 3, caption = "Dynamic regression coefficients for Consumption ~ Income + Production + Savings.")
Dynamic regression coefficients for Consumption ~ Income + Production + Savings.
term estimate std.error p.value
ar1 -0.771 0.207 0.000
ma1 0.653 0.247 0.009
Income 0.773 0.038 0.000
Production 0.074 0.015 0.000
Savings -0.055 0.003 0.000
intercept 0.219 0.030 0.000
fit_us_multi |>
  augment() |>
  features(.innov, ljung_box, lag = 10, dof = arma_dof) |>
  mutate(dof_used = arma_dof) |>
  fmt_tbl(digits = 4, caption = "Ljung-Box test on multivariate dynamic regression innovations.")
Ljung-Box test on multivariate dynamic regression innovations.
.model lb_stat lb_pvalue dof_used
multi 14.36 0.0728 2

Key Takeaways

  • Regression with ARIMA errors separates interpretable covariate slopes \(\boldsymbol{\beta}\) from autocorrelated disturbances \(\eta_t\), avoiding the bias of lagged-\(y\) OLS and the mixed dynamics of ARMAX.
  • When the error order requires differencing (\(d \ge 1\)), ARIMA() automatically differences both \(y_t\) and all predictors \(\mathbf{x}_t\).
  • Dynamic harmonic regression combines Fourier pairs (\(K \le m/2\)) with non-seasonal ARIMA errors (PDQ(0, 0, 0)), scaling smoothly to daily, weekly, and non-integer seasonal periods.
  • Always select \(K\) via AICc and verify white-noise .innov residuals using Ljung-Box with dof = p + q.
Back to top