Skip to main content

Note 07: Unit Roots & Formal Stationarity Testing (ADF & KPSS)

Module 2: Stationarity & Regression

Learning Objectives

  • Distinguish deterministic trends (trend-stationary) from stochastic trends (difference-stationary unit roots).
  • Formulate the Augmented Dickey-Fuller (ADF) and KPSS tests, including their opposite null hypotheses and deterministic term specifications.
  • Avoid specification mismatches between feasts::unitroot_kpss() and tseries::adf.test() by aligning urca::ur.df() and urca::ur.kpss() explicitly.

2. ADF and KPSS Tests: Specification Alignment Matters

Formal testing pairs two complementary procedures with reversed null hypotheses:

Augmented Dickey-Fuller (ADF) Test

Subtracting \(y_{t-1}\) from an \(\text{AR}(p)\) model yields the ADF regression:

\[ \Delta y_t = \alpha + \beta t + \gamma y_{t-1} + \sum_{i=1}^{k} \delta_i \Delta y_{t-i} + \varepsilon_t, \]

where \(\gamma = \phi - 1\) and the \(k\) lagged differences \(\Delta y_{t-i}\) absorb residual serial correlation so \(\varepsilon_t\) approximates white noise. We test:

\[ H_0: \gamma = 0 \;\;(\text{unit root, non-stationary}) \qquad \text{vs.} \qquad H_1: \gamma < 0 \;\;(\text{stationary}). \]

Under \(H_0\), the \(t\)-ratio \(\tau = \hat{\gamma}/\text{SE}(\hat{\gamma})\) does not follow a Student-\(t\) distribution; it follows a left-skewed Dickey-Fuller distribution whose critical values depend strictly on which deterministic terms appear in the regression:

  • type = "none": no intercept or trend (\(\alpha = \beta = 0\), tests zero-mean stationarity).
  • type = "drift": intercept only (\(\beta = 0\), tests level stationarity).
  • type = "trend": intercept and linear trend (tests trend stationarity).

KPSS Test

The Kwiatkowski-Phillips-Schmidt-Shin (KPSS) test reverses the burden of proof by decomposing \(y_t = \xi t + r_t + \varepsilon_t\) where \(r_t = r_{t-1} + u_t\) is a random walk with variance \(\sigma_u^2\):

\[ H_0: \sigma_u^2 = 0 \;\;(\text{stationary}) \qquad \text{vs.} \qquad H_1: \sigma_u^2 > 0 \;\;(\text{unit root}). \]

The specification type = "mu" (\(\xi = 0\)) tests level stationarity, while type = "tau" tests trend stationarity.

Critical Default-Mismatch Trap

A widespread mistake in R scripts is pairing tseries::adf.test() with feasts::unitroot_kpss() using default arguments:

  • tseries::adf.test() always includes a linear trend (type = "trend").
  • feasts::unitroot_kpss() wraps urca::ur.kpss() with default type = "mu" (level stationarity, no linear trend!).

For a genuinely trend-stationary series, adf.test() rejects \(H_0\) (detecting trend stationarity) while default unitroot_kpss() also rejects \(H_0\) (because a trending mean is not level-stationary!). Always match deterministic terms explicitly using urca::ur.df() paired with urca::ur.kpss():

  • Level stationarity check: ur.df(..., type = "drift") paired with ur.kpss(..., type = "mu").
  • Trend stationarity check: ur.df(..., type = "trend") paired with ur.kpss(..., type = "tau").
ADF (H0: unit root) KPSS (H0: stationary) Confirmatory Conclusion
Reject \(H_0\) Fail to reject \(H_0\) Strong evidence of stationarity (level or trend, matching spec)
Fail to reject \(H_0\) Reject \(H_0\) Strong evidence of a unit root (differencing required)
Fail to reject \(H_0\) Fail to reject \(H_0\) Insufficient power / inconclusive (short sample or near-unit root)
Reject \(H_0\) Reject \(H_0\) Conflict: check fractional integration, structural break, or spec mismatch

3. Worked Testing Suite Across Four Benchmarks

Let us construct a clean helper wrapping aligned urca::ur.df() and urca::ur.kpss() plus feasts::unitroot_ndiffs(), and evaluate four series: (1) simulated stationary AR(1) (\(\phi = 0.7\)), (2) simulated random walk with drift (\(\delta = 0.15\)), (3) Alphabet trading-day log(Close) and log returns (data/google.csv), and (4) annual Nile River flow (datasets::Nile).

set.seed(493)
n <- 250
sim_ar1 <- as.numeric(arima.sim(model = list(ar = 0.7), n = n))
sim_rwd <- cumsum(0.15 + rnorm(n))

goog <- readr::read_csv("data/google.csv", show_col_types = FALSE) |>
  arrange(Date) |>
  mutate(day = row_number(), log_close = log(Close), log_ret = log_close - lag(log_close))

nile_x <- as.numeric(datasets::Nile)

run_ur_pair <- function(x, label, df_type = "drift", kpss_type = "mu") {
  x <- as.numeric(na.omit(x))
  adf_obj  <- ur.df(x, type = df_type, selectlags = "AIC")
  kpss_obj <- ur.kpss(x, type = kpss_type)
  adf_stat <- unname(adf_obj@teststat[1])
  adf_5pct <- unname(adf_obj@cval[1, "5pct"])
  kp_stat  <- unname(kpss_obj@teststat)
  kp_5pct  <- unname(kpss_obj@cval[1, "5pct"])
  tibble(
    Series = label,
    Spec = paste0(df_type, " / ", kpss_type),
    `ADF Stat` = adf_stat,
    `ADF 5% CV` = adf_5pct,
    `ADF Reject` = adf_stat < adf_5pct,
    `KPSS Stat` = kp_stat,
    `KPSS 5% CV` = kp_5pct,
    `KPSS Reject` = kp_stat > kp_5pct,
    `ndiffs` = unname(unitroot_ndiffs(x))
  )
}

bind_rows(
  run_ur_pair(sim_ar1, "Sim AR(1) (φ = 0.7)", "drift", "mu"),
  run_ur_pair(sim_rwd, "Sim RW + Drift", "trend", "tau"),
  run_ur_pair(goog$log_close, "GOOG log(Close)", "trend", "tau"),
  run_ur_pair(goog$log_ret, "GOOG Log Returns", "drift", "mu"),
  run_ur_pair(nile_x, "Nile Annual Flow", "drift", "mu")
) |> fmt_tbl(digits = 3, caption = "Aligned ADF (ur.df) and KPSS (ur.kpss) unit-root diagnostics at the 5% significance level.")
Aligned ADF (ur.df) and KPSS (ur.kpss) unit-root diagnostics at the 5% significance level.
Series Spec ADF Stat ADF 5% CV ADF Reject KPSS Stat KPSS 5% CV KPSS Reject ndiffs
Sim AR(1) (φ = 0.7) drift / mu -6.612 -2.88 TRUE 0.210 0.463 FALSE 0
Sim RW + Drift trend / tau -1.879 -3.43 FALSE 0.461 0.146 TRUE 1
GOOG log(Close) trend / tau -1.822 -3.41 FALSE 2.470 0.146 TRUE 1
GOOG Log Returns drift / mu -26.154 -2.86 TRUE 0.111 0.463 FALSE 0
Nile Annual Flow drift / mu -4.049 -2.89 TRUE 0.965 0.463 TRUE 1

Look closely at the Nile River row: both ADF (drift) and KPSS (mu) reject their respective null hypotheses! Moreover, tseries::adf.test(nile_x) (which adds an unnecessary linear trend) fails to reject (\(p \approx 0.064\)), obscuring the signal further. Plotting Nile alongside Alphabet log returns immediately explains the conflict: the Nile experienced an abrupt mean drop near 1898 (the Aswan Low Dam structural break, studied in Module 6), which violates level stationarity without being a pure unit root.

p_nile <- as_tsibble(datasets::Nile) |>
  autoplot(value, colour = ts_pal["navy"], linewidth = 0.7) +
  geom_vline(xintercept = 1898, linetype = "dashed", colour = ts_pal["crimson"]) +
  labs(
    title = "Annual Nile River Flow",
    subtitle = "Structural shift near 1898 triggers ADF/KPSS conflict",
    x = "Year",
    y = TeX(r"(Flow ($10^8\,m^3$))", bold = TRUE)
  )

p_ret <- goog |>
  drop_na(log_ret) |>
  ggplot(aes(x = Date, y = log_ret)) +
  geom_line(colour = ts_pal["teal"], linewidth = 0.45) +
  geom_hline(yintercept = 0, linetype = "dashed", colour = ts_pal["slate"]) +
  labs(
    title = "Alphabet (GOOG) Daily Log Returns",
    subtitle = "ADF rejects unit root; KPSS retains stationarity",
    x = "Date",
    y = TeX(r"(Log Return ($\nabla \ln(P_t)$))", bold = TRUE)
  )

(p_nile | p_ret) +
  plot_layout(guides = "collect") &
  theme(legend.position = "bottom")
Figure 1: Annual Nile River flow showing an abrupt structural mean shift around 1898 (left) alongside stationary Alphabet daily log returns (right).

Practice Exercises

Exercise 1: Confirmatory Quadrant Check on U.S. GDP

Using global_economy for the United States, apply aligned ADF and KPSS tests (under both "drift"/"mu" and "trend"/"tau") and unitroot_ndiffs() to log(GDP) and its first difference difference(log(GDP)). Interpret the confirmatory table.

Solution

For log(GDP), ADF fails to reject the unit root while KPSS strongly rejects both level and trend stationarity, confirming a stochastic trend (ndiffs = 1 or 2 under KPSS level checks). After first differencing, annual log GDP growth passes level-stationarity checks cleanly under aligned "drift"/"mu" tests.

us_gdp <- global_economy |>
  filter(Country == "United States") |>
  drop_na(GDP) |>
  mutate(log_gdp = log(GDP), dlog_gdp = difference(log_gdp))

bind_rows(
  run_ur_pair(us_gdp$log_gdp, "log(US GDP)", "drift", "mu"),
  run_ur_pair(us_gdp$log_gdp, "log(US GDP)", "trend", "tau"),
  run_ur_pair(us_gdp$dlog_gdp, "Δ log(US GDP)", "drift", "mu")
) |> fmt_tbl(digits = 3, caption = "Confirmatory ADF and KPSS results for U.S. log GDP and annual log growth.")
Confirmatory ADF and KPSS results for U.S. log GDP and annual log growth.
Series Spec ADF Stat ADF 5% CV ADF Reject KPSS Stat KPSS 5% CV KPSS Reject ndiffs
log(US GDP) drift / mu -3.573 -2.89 TRUE 1.520 0.463 TRUE 2
log(US GDP) trend / tau 0.219 -3.45 FALSE 0.379 0.146 TRUE 2
Δ log(US GDP) drift / mu -2.267 -2.89 FALSE 0.963 0.463 TRUE 1

Exercise 2: Seasonal and Regular Unit-Root Checks on AirPassengers

Apply unitroot_nsdiffs() (with .period = 12), unitroot_ndiffs(), and aligned ADF/KPSS tests to log(AirPassengers), its seasonal difference \(\nabla_{12} \ln y_t\), and its doubly differenced form \(\nabla \nabla_{12} \ln y_t\).

Solution

unitroot_nsdiffs() flags 1 seasonal difference on log(AirPassengers). Once seasonally differenced, unitroot_ndiffs() reports 0, and adding a regular first difference yields clear agreement between ADF (rejecting unit root) and KPSS (failing to reject level stationarity).

log_ap <- log(as.numeric(AirPassengers))
sdiff_ap <- diff(log_ap, lag = 12)
both_ap  <- diff(sdiff_ap, lag = 1)

tibble(
  Check = c("nsdiffs on log(AP)", "ndiffs on ∇_12 log(AP)", "ndiffs on ∇∇_12 log(AP)"),
  Order = c(
    unname(unitroot_nsdiffs(log_ap, .period = 12)),
    unname(unitroot_ndiffs(sdiff_ap)),
    unname(unitroot_ndiffs(both_ap))
  )
) |> fmt_tbl(caption = "Automated seasonal and regular differencing orders for log(AirPassengers).")
Automated seasonal and regular differencing orders for log(AirPassengers).
Check Order
nsdiffs on log(AP) 1
ndiffs on ∇_12 log(AP) 0
ndiffs on ∇∇_12 log(AP) 0
bind_rows(
  run_ur_pair(log_ap, "log(AP)", "trend", "tau"),
  run_ur_pair(sdiff_ap, "∇_12 log(AP)", "drift", "mu"),
  run_ur_pair(both_ap, "∇∇_12 log(AP)", "drift", "mu")
) |> fmt_tbl(digits = 3, caption = "Aligned ADF and KPSS checks across differencing stages of log(AirPassengers).")
Aligned ADF and KPSS checks across differencing stages of log(AirPassengers).
Series Spec ADF Stat ADF 5% CV ADF Reject KPSS Stat KPSS 5% CV KPSS Reject ndiffs
log(AP) trend / tau -6.995 -3.43 TRUE 0.113 0.146 FALSE 1
∇_12 log(AP) drift / mu -3.342 -2.88 TRUE 0.368 0.463 FALSE 0
∇∇_12 log(AP) drift / mu -9.304 -2.88 TRUE 0.084 0.463 FALSE 0

Key Takeaways

  • Trend-stationary series revert to a deterministic line after shocks, whereas unit-root series accumulate shocks permanently and require differencing.
  • ADF tests \(H_0\): unit root against \(H_1\): stationarity; KPSS reverses the hypotheses (\(H_0\): stationary against \(H_1\): unit root).
  • Always align deterministic specifications (ur.df(..., type = "drift") with ur.kpss(..., type = "mu"), or "trend" with "tau") rather than mixing default tseries::adf.test() and feasts::unitroot_kpss().
  • When ADF and KPSS both reject, inspect the time plot for structural breaks or regime shifts (as in datasets::Nile).
Back to top