Time Series • Introduction to Autoregressive Models and Forecasting
Time Series / P4.01 Linear Regression with Time

P4.01 Linear Regression with Time

Practical 4 Introduction to Autoregressive Models and Forecasting

Fit a simple linear regression of monthly sales on a time index. Explain the slope as an average change per month, and state why this is not the same as an ARIMA model.

Practical / Solution

P4.01 Linear Regression with Time

Problem Statement

Fit a simple linear regression of monthly sales on a time index. Explain the slope as an average change per month, and state why this is not the same as an ARIMA model.

Learning Outcomes

  • Treat time as a predictor in OLS.
  • Interpret intercept and slope in original units.
  • State the independence assumption carefully.

Hint

Create t = 0, 1, 2, ... then regress sales on t. Plot fitted line against the series.

Theory

A trend regression says Y_t = β0 + β1 t + ε_t. Ordinary least squares finds β0 and β1 by minimising the sum of squared residuals. The slope is the average change in Y per unit time if the linear form is adequate. OLS assumes independent errors. Time series errors are often correlated, so OLS can still describe a trend but inference may be misleading. Regression does not automatically remove autocorrelation.

Dataset / Data Source

Constructed 24 monthly sales figures with a gentle rise and modest noise. Teaching data.

Analysis / Program

# Import libraries import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm # Load a small monthly series idx = pd.date_range("2024-01-01", periods=24, freq="MS") t = np.arange(24) sales = 80 + 1.5 * t + np.array([ 2, -1, 3, 0, -2, 4, 1, -3, 2, 0, -1, 3, 1, -2, 2, 0, 1, -1, 3, -2, 0, 2, -1, 1 ]) y = pd.Series(sales, index=idx, name="sales") # Inspect print(y.head()) print("Shape:", y.shape) # Prepare datetime-aligned time index as a predictor X = sm.add_constant(t) # Fit OLS model = sm.OLS(y.values, X).fit() print(model.summary()) # Visualize actual vs fitted fitted = pd.Series(model.fittedvalues, index=idx) y.plot(marker="o", label="sales") fitted.plot(label="OLS trend", style="--") plt.title("Sales with linear time trend") plt.legend() plt.tight_layout() plt.show()

Expected Output

A 24-row series preview, a regression summary with intercept near 80 and slope near 1.5, and a plot of sales with a straight fitted trend. Exact coefficients will be close to the construction values but not identical.

Result / Interpretation

The slope estimates the average monthly increase under a straight-line trend. The line summarises direction. It does not model seasonal wiggles or lagged dependence. Residuals should still be checked in later practicals.

Note

A time trend regression describes average change with t. It is not an ARIMA model and does not by itself fix autocorrelated errors.