Skip to content
Tutorials Center
My learning

Topics

Lesson 26 of 36 ยท Panel and Time Series

ARIMA Models, Unit Roots and Forecasting

Test for unit roots with dfuller, fit AR and ARIMA models with the arima command, read the ACF and PACF, and forecast with predict. Runnable Python stand-ins.

Most economic and business series wander: they trend, drift and do not return to a fixed mean. Regressing one wandering series on another produces "spurious" relationships with high R-squared and tiny p-values that mean nothing. The remedy is to check whether a series is stationary, difference it if not, and then model what remains with an ARIMA process. This lesson covers the Stata tools for each step.

Stata cannot run here. All Stata commands are read-only, and we print no Stata output. The runnable blocks are Python stand-ins: a hand-written Dickey-Fuller regression and an AR(1) fit with forecasts.

Stationarity and unit roots

A series is (weakly) stationary if its mean, variance and autocorrelations do not depend on time. A random walk y_t = y_(t-1) + e_t has a unit root: shocks never fade, and the variance grows without bound. Its first difference, D.y, is stationary. Such a series is called integrated of order one, I(1).

The augmented Dickey-Fuller test regresses D.y on the lagged level and, optionally, lagged differences:

STATA
tsset month
dfuller sales, lags(4) trend regress
dfuller D.sales, lags(4)
pperron sales, lags(4)
dfgls sales

The null hypothesis of dfuller is that the series has a unit root. The test statistic is compared with Dickey-Fuller critical values (not the normal ones). If the statistic is more negative than the critical value, or the MacKinnon p-value is small, you reject the unit root. Options matter: drift or the default constant for a series fluctuating around a nonzero mean, trend when it may fluctuate around a linear trend. dfgls is a more powerful variant, and kpss (user-written) reverses the null to stationarity. Because unit-root tests have low power, use several and the plot.

Stand-in: Dickey-Fuller by hand

The simplified test below regresses the first difference on the lagged level with a constant and compares the t statistic with the 5% critical value of about -2.86. It does not use extra lags, so it is a teaching version of dfuller.

Python Dickey-Fuller test (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The walk fails to reject, while the AR(1) series and the differenced walk reject clearly. That is the usual pattern: difference the first, keep the others in levels.

ARIMA(p, d, q)

An ARIMA model has three parts: p autoregressive terms, d differences and q moving-average terms. Pick p and q from the correlograms of the stationary series:

  • AR(p): the partial autocorrelation cuts off after lag p; the ACF decays.
  • MA(q): the autocorrelation cuts off after lag q; the PACF decays.
  • ARMA: both decay gradually; compare candidates with information criteria.
STATA
ac D.sales
pac D.sales
arima sales, arima(1,1,1)
arima D.sales, ar(1) ma(1)         // same model, written on differences
arima sales advertising, arima(2,0,0)   // with a regressor (ARMAX)
estat ic
estat aroots

arima estimates by maximum likelihood. Check the inverse roots with estat aroots: all should lie inside the unit circle. Examine the residuals; they should look like white noise:

STATA
predict res, residuals
wntestq res, lags(20)

The Ljung-Box-type test wntestq has the null of white noise, so you want a large p-value here. estat ic reports AIC and BIC; lower is better, and BIC favours smaller models.

For seasonal data add seasonal terms, arima sales, arima(1,1,1) sarima(0,1,1,12). For volatility clustering in returns, see arch and garch; for several interacting series, var and vec (vector autoregression and cointegration), including varsoc, vargranger and irf.

Forecasting

STATA
tsappend, add(12)                  // add 12 empty future periods
predict fc, y dynamic(tm(2024m1))   // dynamic forecast from Jan 2024
predict fc_se, mse
tsline sales fc

dynamic() uses forecasts rather than actual values for lags from the chosen date onwards, which is how real forecasts must work. In-sample one-step predictions (no dynamic) look better than any true forecast, so be careful when reporting accuracy. Hold out the last observations and compare errors.

Python AR(1) fit and multi-step forecast (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice how the forecast glides toward the long-run mean c / (1 - phi): stationary models forecast mean reversion, so far-ahead forecasts are almost constant. A forecast should always come with an interval (predict, mse and a normal approximation) that widens as the horizon grows.

Spurious regression warning

If both y and x are I(1) and not cointegrated, regress their differences instead (regress D.y D.x) or model the long-run relation with an error-correction model. If they are cointegrated, the residual from the levels regression is stationary, which you can check with dfuller on the residuals (using the appropriate Engle-Granger critical values) or with the Johansen test, vecrank.

Python Spurious regression demo (Python stand-in)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Never trust a levels regression between trending series without checking for unit roots. See time-series basics for the operators used above.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is the null hypothesis of dfuller?

    Dickey-Fuller tests the null of a unit root; rejecting suggests stationarity.
  2. 2. In the ARIMA(p,d,q) notation, d is

    d is the number of times the series is differenced to achieve stationarity.
  3. 3. A PACF that cuts off after lag 2 with a decaying ACF suggests

    An AR(p) process has a PACF that cuts off after lag p.
  4. 4. Why use predict with dynamic()?

    Dynamic forecasts mimic real out-of-sample use, when future actual values are unknown.

Summary

  • Test stationarity with dfuller, pperron, dfgls; difference I(1) series.
  • Choose arima(p,d,q) using ac, pac and estat ic; check residuals with wntestq.
  • Forecast with tsappend and predict, dynamic(); regressions between unit-root series can be spurious.
  • Python blocks are stand-ins, not Stata output.

Next: Do-files and log files