50% OFF STATA BUNDLE!!
In time series analysis, one of the first questions we need to ask is: is my series stationary?
Why does this matter? Because most forecasting models—like ARIMA, VAR, and many econometric techniques—require the data to be stationary. A stationary series is one whose mean, variance, and autocorrelation do not change over time. In practice, this means the series doesn’t have a long-term trend or changing volatility.
If your data is non-stationary, you risk spurious regressions and unreliable forecasts. That’s why, before applying advanced models, we must test and transform the series until it meets the assumption of stationarity.
In this tutorial, we’ll continue working with our quarterly dataset on oil prices and activity index. This time, our goal is to:
Visualize the series with autocorrelation plots and decomposition.
Perform formal statistical tests (ADF and KPSS).
Apply log transformations and differencing if necessary.
Verify whether the transformed series is stationary.
By the end, you’ll not only know how to test for stationarity in R, but also why these steps are essential.
We’ll use the tseries package, which provides functions for stationarity tests:
💡 Tip: Remember that you only need to install a package once. But you must load it (library()) every time you start a new R session.
Autocorrelation tells us how much the series is related to its past values. A slowly decaying ACF often indicates non-stationarity.
ACF: if correlations persist over many lags, the series is likely non-stationary.
PACF: helps us see direct relationships at each lag.
Decomposition separates the series into trend, seasonality, and random components. This helps us visually identify if a clear trend exists.
The trend component shows long-term movement.
The seasonal component repeats in regular cycles.
The random component is what’s left after removing trend and seasonality.
Augmented Dickey-Fuller (ADF) Test
Null hypothesis (H0): The series has a unit root (not stationary).
If the p-value < 0.05, we reject H0 and conclude the series is stationary.
KPSS Test
Null hypothesis (H0): The series is stationary.
If the p-value < 0.05, we reject H0 and conclude the series is not stationary.
👉 Using both ADF and KPSS together is a good practice, since they complement each other with opposite null hypotheses.
If the series is not stationary, we can transform it:
Logarithms stabilize variance (useful if the series grows exponentially).
Differencing removes trends and makes the series mean-reverting.
Interpretation:
diff(log_oil) gives the growth rate in proportion (e.g., 0.05 = 5%).
Multiplying by 100 converts it into a percentage growth rate (5 instead of 0.05).
The series now has no trend. It consistently wiggles around its mean, which is what we are looking for in a stationary series.
Look at the the real economic activity index and verify whether it looks stationary or has a clear upwards or downwards trend.