Testing whether a series is stationary with KPSS

Today let's understand what it means for a time series to be stationary, and how to test for it, working through one real series from start to finish.

AirPassengers is a dataset built into R: the monthly count of international airline passengers, in thousands, from January 1949 to December 1960, 144 months in all. It was first published by Box and Jenkins, and time series courses have reached for it ever since.

Here is the whole series, plotted month by month.

The line climbs across the whole twelve years, and the summer bump on top of it gets taller every year. Both of those, the climb and the growing bump, are exactly what this lesson is going to test for.

What "stationary" means: three properties that stay constant

A series is stationary when three things about it hold still as time passes. Its mean stays the same, its variance stays the same, and its autocorrelation structure, meaning how strongly a value relates to its own recent past, stays the same too. None of the three is allowed to drift as you move from the start of the series to the end.

That sounds abstract, so let's put a number on the first two for AirPassengers as a whole, before checking whether they actually hold.

RInteractive R
# Build the AirPassengers tsibble and compute its overall mean and standard deviation library(tsibble) library(dplyr) air <- as_tsibble(AirPassengers) |> rename(passengers = value, month = index) mean(air$passengers) #> [1] 280.2986 sd(air$passengers) #> [1] 119.9663

  

Across the whole series, the mean is about 280 and the standard deviation is about 120, both in thousands of passengers a month, the same units the data itself is recorded in. Those two numbers, 280.30 and 119.97, are the baseline. If AirPassengers really is stationary, any stretch of it you pull out on its own should land close to those same two numbers. Let's check that next.

Splitting the series in half to see the mean and variance move

A single mean and standard deviation for the whole series cannot tell you whether they are drifting. To check that, split AirPassengers into its first half and its second half, and compute the same two numbers on each piece separately.

RInteractive R
# Split AirPassengers into its first half and second half, and compare their mean and standard deviation air_first_half <- air |> filter_index("1949 01" ~ "1954 12") air_second_half <- air |> filter_index("1955 01" ~ "1960 12") mean(air_first_half$passengers) #> [1] 182.9028 sd(air_first_half$passengers) #> [1] 47.70424 mean(air_second_half$passengers) #> [1] 377.6944 sd(air_second_half$passengers) #> [1] 86.43921

  

filter_index() is tsibble's way of slicing a series by date range: here, "1949 01" through "1954 12" for the first six years, and "1955 01" through "1960 12" for the last six.

The first half averages about 183 with a standard deviation of about 48. The second half averages about 378 with a standard deviation of about 86, more than double both numbers. Compare that to the whole-series baseline from the last step, 280 and 120: neither half matches it, and the two halves do not match each other either.

Here is the same split plotted, with the first half and second half shaded differently.

The plot makes it obvious: the second half does not just sit higher, its peaks and troughs are further apart too. That is a mean that has moved and a variance that has grown, exactly the two numbers you just computed.

Quick check: reading what stays constant

Right. Neither property held still: the mean nearly doubled and the standard deviation nearly doubled too, so both the mean and the variance fail to stay constant across the split.
Both properties matter here. The mean moved from about 183 to about 378, and the standard deviation moved from about 48 to about 86: it takes checking both to say the series fails stationarity, not the mean on its own.

Why a trend and a growing season break stationarity

You just saw the mean and the standard deviation move between the two halves. It is worth understanding why, because both come from the same two things you can already see in the plot: a trend, and a seasonal swing that keeps growing.

A trend is a mean that keeps moving in one direction as time passes, by construction. AirPassengers climbs almost every year, so its local average in 1960 sits well above its local average in 1949. A series cannot hold its mean constant while it is trending upward.

The seasonal swing, the gap between a year's quietest month and its busiest month, is doing something else. Look at how that gap changes across the twelve years.

RInteractive R
# Compute each calendar year's seasonal swing (max minus min) and compare early years against late years air_years <- air |> mutate(year = as.integer(format(month, "%Y"))) early <- air_years |> filter(year %in% c(1949, 1950, 1951)) |> as_tibble() |> group_by(year) |> summarise(swing = max(passengers) - min(passengers)) late <- air_years |> filter(year %in% c(1958, 1959, 1960)) |> as_tibble() |> group_by(year) |> summarise(swing = max(passengers) - min(passengers)) early #> # A tibble: 3 × 2 #> year swing #> <int> <dbl> #> 1 1949 44 #> 2 1950 56 #> 3 1951 54 late #> # A tibble: 3 × 2 #> year swing #> <int> <dbl> #> 1 1958 195 #> 2 1959 217 #> 3 1960 232

  

In 1949 the gap between the quiet month and the busy month was 44. By 1960 that same gap had grown to 232, more than five times as wide. A season that widens as the series' level rises is called multiplicative seasonality, and it is exactly why the variance you computed two steps back grew along with the mean: a bigger seasonal swing spreads the values further from their local average.

The autocorrelation structure shifts for the same reason. Early on, one month's value relates to the next month's value across a gap of about 44. Later, that same relationship plays out across a gap five times as wide. The relationship between a value and its own recent past is not the relationship it used to be, so the third stationarity property fails right along with the other two.

The KPSS test: reading the statistic against the critical value

So far you have seen non-stationarity by eye and by hand. KPSS turns that into a single test you can run on any series.

KPSS stands for Kwiatkowski-Phillips-Schmidt-Shin, the four statisticians who published it in 1992. Its null hypothesis, the assumption the test starts from and looks for evidence against, is that the series IS stationary. That is the opposite of most statistical tests you may have met before, where the null hypothesis is usually the boring, nothing-going-on option. Here, stationary is the boring option, and the test is looking for evidence that the series is not boring after all.

Run it on AirPassengers with urca::ur.kpss().

RInteractive R
# Run the KPSS test on the whole series and read the statistic against its critical values library(urca) kpss_air <- ur.kpss(air$passengers, type = "mu") summary(kpss_air) #> #> ####################### #> # KPSS Unit Root Test # #> ####################### #> #> Test is of type: mu with 4 lags. #> #> Value of test-statistic is: 2.7395 #> #> Critical value for a significance level of: #> 10pct 5pct 2.5pct 1pct #> critical values 0.347 0.463 0.574 0.739

  

type = "mu" tells ur.kpss() to test against a constant mean, the plain stationarity you have been checking so far, rather than against a trend.

The output gives you two things to compare: the test statistic, 2.7395, and a row of critical values, one for each significance level. Read them the way you would read any critical-value table: if the statistic sits above the critical value for a given level, you reject the null hypothesis at that level.

AirPassengers' statistic, 2.7395, sits above every critical value in that row, even the strictest one, 0.739 at the 1 percent level. So KPSS rejects stationarity here about as firmly as it can. That matches everything you already found by hand: the mean moved, the variance moved, and now a formal test agrees.

What a non-stationary error process does to a confidence interval

You have now confirmed AirPassengers is not stationary. Before moving on to fixing that, it is worth seeing why any of this matters beyond a test passing or failing.

Ordinary regression, and the confidence intervals built around it, assume the errors are independent of each other, meaning one observation's error carries no information about the next one's. A trending, non-stationary series is one common way that assumption breaks: the level itself carries over from one time step to the next, so nearby errors end up correlated too, a property called autocorrelation.

The widget below runs that as a separate experiment. It fits many regressions on data built with a chosen amount of autocorrelation in the errors, and it measures two things every time: how often the model's 95 percent interval actually contains the true value, called coverage, and how good the fit looks, R-squared. It uses a fresh simulated regression, not AirPassengers itself, but the same problem applies to any non-stationary series with real autocorrelation left in it.

Drag the dial from independent toward severe. Coverage, the share of intervals that actually contain the truth, collapses well below the nominal 95 percent as the errors become more strongly autocorrelated. Now watch R-squared while you do it: it does not get worse, and it may even rise, because a smooth, slowly drifting error series happens to flatter the fit at the same moment it is destroying the interval.

That is the real cost of skipping a stationarity check. A model can look just as good, or even better, by its fit statistic, while the interval built around it has quietly become worthless.

The shortcut: unitroot_kpss() and unitroot_ndiffs()

Running ur.kpss() and reading its critical-value table by hand works, but the feasts package wraps the same idea into a one-line function, and adds a second one that tells you what to do about a non-stationary series.

First, a quick definition, since the second function's answer is a count of how many times to do this. Differencing means subtracting each value from the one right before it, which removes a trend: a series that keeps climbing by roughly the same amount each step turns, after differencing, into a series of those step sizes instead, which no longer climbs.

unitroot_kpss() gives you the same statistic and a p-value in one call.

RInteractive R
# Run the KPSS test and count the differences AirPassengers needs, using feasts library(feasts) air |> features(passengers, unitroot_kpss) #> # A tibble: 1 × 2 #> kpss_stat kpss_pvalue #> <dbl> <dbl> #> 1 2.74 0.01 air |> features(passengers, unitroot_ndiffs) #> # A tibble: 1 × 1 #> ndiffs #> <int> #> 1 1

  

kpss_stat is 2.74, the same number ur.kpss() gave you a couple of steps back, just rounded. kpss_pvalue of 0.01 is feasts' way of saying the statistic fell at or beyond the most extreme value in the KPSS table, the 1 percent row: the same rejection you already read off the table by hand.

unitroot_ndiffs answers the practical question that follows a rejection: how many times do you need to difference this series before a model can treat it as stationary? For AirPassengers, the answer is 1. One round of ordinary differencing, each value minus the one before it, is what this series needs before you fit an ARIMA model on it.

Quick check: putting the statistic, the table and the count together

Exactly. The statistic beating every critical value rejects stationarity, and the ndiffs count of 1 is the practical next step it hands you: difference once before modeling.
The ndiffs count of 1 is a count of differences, not a p-value: the KPSS p-value was 0.01, a separate number. And the widget showed coverage collapsing while R-squared held steady or rose, the opposite of what this option claims. The chain that matters: statistic above the 1 percent critical value rejects stationarity, and ndiffs = 1 says to difference once.

Your turn: test a slice of the series yourself

air_first_half, the first six years of AirPassengers, is still sitting in your session from a few steps back. Run the same two feasts checks on it: unitroot_kpss to test whether it is stationary, and unitroot_ndiffs to see how many times it needs differencing. Then compare that ndiffs count to the full series' count from two steps ago.

Right: air_first_half's KPSS statistic is 1.62, smaller than the full series' 2.74 but still above the 1 percent critical value of 0.739, so it still rejects stationarity. And its ndiffs count is 1, the same count the full series needed: even six years of AirPassengers alone still carries the trend that one round of differencing removes.Call features() on air_first_half twice, once testing passengers with unitroot_kpss and once with unitroot_ndiffs: air_first_half |> features(passengers, unitroot_kpss) and air_first_half |> features(passengers, unitroot_ndiffs).
Show answer
# Test air_first_half for stationarity and count how many differences it needs
air_first_half |> features(passengers, unitroot_kpss)
#> # A tibble: 1 × 2
#>   kpss_stat kpss_pvalue
#>       <dbl>       <dbl>
#> 1      1.62        0.01

air_first_half |> features(passengers, unitroot_ndiffs)
#> # A tibble: 1 × 1
#>   ndiffs
#>    <int>
#> 1      1

References

  • Forecasting: Principles and Practice (3rd ed.) - Hyndman and Athanasopoulos, the free online textbook. Section 9.1 covers stationarity and differencing, the source for the KPSS and ndiffs workflow taught here.
  • Kwiatkowski, Phillips, Schmidt and Shin (1992), "Testing the null hypothesis of stationarity against the alternative of a unit root", Journal of Econometrics 54(1-3), 159-178 - the original KPSS paper and the source of the critical value table.
  • feasts package reference - documentation for unitroot_kpss() and unitroot_ndiffs().
  • urca package reference - documentation for ur.kpss().
  • R's AirPassengers dataset documentation (datasets package) - originally published in Box and Jenkins, "Time Series Analysis: Forecasting and Control" (1976).

What you can do now

You now have the whole chain, start to finish. Define stationarity as three properties holding still: mean, variance and autocorrelation structure. See it break on a real series, AirPassengers' climbing mean and widening season. Test it formally with KPSS, reading a statistic against a critical-value table, or the same read in one line with unitroot_kpss(). And count exactly how many times to difference a series before modeling it, with unitroot_ndiffs().

Differencing itself, subtracting each value from the one right before it, is what actually removes that trend and hands a model a series whose mean, variance and autocorrelation structure hold still enough to work with.