Unit 8 / 11

Time Series Analysis: Stationarity, ARIMA and Forecasting

Gains:

  • Ability to understand the concepts of trend, seasonality, stationarity and unit root and use artificial intelligence with accurate data for time series modeling and forecasting.
  • Ability to interpret ARIMA/seasonal models and forecast uncertainty (confidence interval, residual analysis) and avoid spurious regression
  • Being able to distinguish that artificial intelligence prediction is based on past patterns and that expert judgment comes to the fore in periods of structural break and crisis.

Much of the data that is the bread and butter of economists and financial analysts is time series: values of the same variable measured at regular intervals over time (monthly inflation, daily stock price, quarterly GDP). Time series differ fundamentally from ordinary cross-sectional data because the observations are not independent: today's value depends on yesterday. This dependence is both an opportunity (we can predict the future) and a danger (ordinary regression easily misleads here). In this unit, we'll see how artificial intelligence (AI) is accelerating time series modeling and forecasting, but why the most dangerous trap—spurious regression—requires attention.

Basic concepts

A time series generally contains three components: trend (long-term upward/ downward trend), seasonality (regular pattern that repeats throughout the year, e.g. increased tourism in summer), and cycle/noise (residual volatility). Before modeling, the most critical feature of the series is stationarity.

A stationary series is a series whose statistical properties (mean, variance) remain constant over time. A non-stationary series contains a trend, its variance grows, or it has a unit root. A unit root is a structure in which the series permanently "remembers" shocks and does not return to its mean; Such a series behaves like a random walk. Why is it important? Because regression between two non-stationary series can produce high R-squared and significant coefficients even if there is no relationship in reality — this is called spurious regression. Only the common trend makes the two series appear to be "related".

Caution: Two rising series (e.g. one country's population and another country's ice cream sales) can be highly correlated; as they both increase over time. This is not a relationship, but the illusion of common trend. Be sure to check stationarity before starting regression in the time series.

Step-by-step time series workflow

1. Visualize. Plot the series: is there a trend, is there seasonality, does the variance change over time, is there a structural break (sudden level change)?

2. Test stationarity. ADF test (Augmented Dickey-Fuller) and KPSS test test unit root/stationarity. The two complement each other: in ADF the null hypothesis is "there is a unit root", in KPSS it is "stationary". It is safer to use both together.

3. Stagnate. If the series is non-stationary, it is usually differenced (the difference of successive observations; this removes the trend). Differentiating once makes a "first order integrated" (I(1)) series stationary. Log transformation also helps if the variance is growing.

4. Build a model. The most common framework is ARIMA (AutoRegressive Integrated Moving Average): It combines the components AR (dependence of the series on its own past values), I (degree of differencing), MA (effect of past errors). If there is seasonality, SARIMA is used. AI generates code for automatic selection tools such as auto.arima; But don't blindly accept automatic selection.

5. Check for leftovers. The residuals of a good model should be white noise: no pattern, no autocorrelation. The Ljung-Box test tests this. If there is still a pattern in the residuals, the model is incomplete.

6. Predict and show uncertainty. The forecast is not a single line; always given with a confidence/estimate interval. The range widens as the horizon lengthens — this is a sign of honesty, not flaw.

Cointegration: real relationship with non-stationary series

Two nonstationary series do not always give spurious relationships. If there is a real long-run equilibrium between them (they move together), this is called cointegration and can be modeled with the error correction model (ECM). So the solution is not "difference and throw away the information"; is to test cointegration first. This distinction distinguishes spurious regression from true long-term correlation and is a theoretical consideration that AI cannot decide on its own.

comparison chart

Status

symptom

right approach

stationary series

Constant mean/variance

Direct ARCHING

With trend (unit root)

Continuous rise, ADF is meaningless

Difference, then model

Two non-stationary series

High R² may be fake

First, cointegration test

seasonal

Annual repeating pattern

SARIMA / seasonal difference

structural break

Sudden level/slope change

Breakage test, expert judgment

Four copyable prompts

1. Stationarity diagnosis:

Your role: time series assistant. I want R code, I don't share the data. 'series' is a monthly time series (ts object). Task: (1) draw the series and autocorrelation (ACF/PACF) graphs, (2) apply the ADF and KPSS tests, (3) explain how to interpret the results together. I will make the decision to take the difference; You produce a diagnosis.

2. Model building (supervised):

My series appears stationary at first difference. Propose a model with auto.arima but: (1) explain the selected (p, d, q) orders and WHY they were chosen, (2) add code to test whether the residuals are white noise with Ljung-Box, (3) remind me not to blindly accept the automatic selection.

3. Fake regression warning:

I want to set up regression between my two time series (X, Y) but they are both detrended. Write workflow code that checks the risk of spurious regression: first test the stationarity of both, then apply a cointegration test (Engle-Granger or Johansen). Before I run lm() directly, stop me and explain why it's dangerous.

4. Prediction and uncertainty:

Write a code that produces a 12-month forward forecast with the ARIMA model I created; Plot the estimate with its 80% and 95% confidence intervals. Add a note below the chart that the prediction is based on past patterns and may become invalid in the event of a structural break/crisis.

Weak prompt / Strong prompt

Weak prompt:

Find the relationship of these two series with regression and predict the next year.

This claim is an invitation to the false regression trap: if regression is set up without checking for stationarity, a high R-squared but meaningless "relationship" and an unwarranted estimate emerge.

Powerful prompt:

Your role: careful time series assistant. Proceed step by step: (1) test the stationarity of both series and report, (2) if they are not stationary, DO NOT do direct regression, test cointegration first, (3) if you produce a forecast, be sure to add a confidence interval and write a structural break warning. Leave the decision to me at every step; automatic progress.

Difference: the strong demand requires stationarity and cointegration before regression, presenting the estimate with uncertainty.

three mini cases

Case 1 — Spurious regression. An analyst found R²=0.91, p<0.001 between two rising series (turnover of one sector and energy consumption of another country) and wrote "strong relationship". When his consultant asked for the stationarity test, it was seen that both of them had unit roots, and when their differences were taken, the relationship (r = 0.04) completely disappeared. The high R-squared was merely an illusion of common trend. Lesson: time series regression is unreliable without testing for stationarity.

Case 2 — Blind trust in the automatic model. A student used the model chosen by auto.arima without examining it; he no longer noticed significant seasonality remaining in his analysis. The model systematically underestimated the summer months. The Ljung-Box test showed autocorrelation in the residuals. Correct way: was to add the seasonal component (SARIMA). Lesson: automatic selection is the beginning, not the final word; Residues must be checked.

Case 3 — Forecast collapse at structural break. One model predicted 2020 demand with 2019 data; the confidence interval was narrow, the estimate was confident. The big shock (pandemic) of 2020 completely blew the prediction because the model only knew the past pattern. Lesson: time series forecasting assumes that the past will be similar to the future; In times of crisis/breakdown, expert judgment comes to the fore.

Common mistakes

  • Establishing regression without checking for stationarity. Spurious regression produces a high R-squared but insignificant relationship; Apply ADF/KPSS first.
  • Differentiating and skipping cointegration. If there is a real long-term relationship, blindly taking the difference throws away information; Test cointegration first.
  • Using the automatic model without checking it. auto.arima is a good start but unreliable without checking the residuals (Ljung-Box).
  • Presenting the estimate as a single line. Estimation without a confidence interval creates false precision; Always show uncertainty.
  • Ignoring the structural break. Crisis/regime change disrupts the past pattern; The model cannot know this, expert judgment is required.
Tip: Before writing a time series finding, look again at the raw graph of the series. A clear trend or break that you see with your own eyes is the strongest warning that no test should make you forget.

In summary

In time series analysis, observations are not independent; This both gives predictive power and creates pitfalls such as spurious regression. Test stationarity (ADF/KPSS) before modelling; test cointegration rather than directly establishing regression between non-stationary series; Check the residuals of the ARIMA/SARIMA model until there is white noise; Always present the estimate with a confidence interval. AI generates the code for all these steps, but the expert controls the automatic model selection, cointegration decision, and structural break interpretation. Prediction is based on the past; In times of crisis, human judgment has the final say.

Application task

Choose a time series (monthly or quarterly). Before AI, request and run stationarity diagnostics (graph, ACF/PACF, ADF, KPSS) and classify the series as stationary/non-stationary. Differentiate if necessary, set up an ARIMA/SARIMA model and check the residuals with the Ljung-Box. Produce a 6-12 period forward forecast and plot it with a confidence interval. Finally, consider in a paragraph how your prediction might be wrong if there were a structural break in your data.

checklist

  • [ ] I plotted the series and visually examined the trends, seasonality and breaks.
  • [ ] I tested stationarity with ADF and KPSS; I got the required difference.
  • [ ] I avoided direct regression and tested cointegration between non-stationary series.
  • [ ] I checked the model residuals (Ljung-Box) and confirmed that it was white noise.
  • [ ] I presented the estimate with confidence interval; I did not give it as a single line.
  • [ ] I noted the limits of prediction in case of structural break/crisis.