Time Series • Introduction to Autoregressive Models and Forecasting
Time Series / P4.08 GLS for Time Series Data

P4.08 GLS for Time Series Data

Practical 4 Introduction to Autoregressive Models and Forecasting

Fit OLS, inspect residual autocorrelation, then fit GLS with an AR(1)-like error covariance. Compare coefficients and explain what GLS is trying to do.

Practical / Solution

P4.08 GLS and Regression for Time Series

Problem Statement

Fit OLS, inspect residual autocorrelation, then fit GLS with an AR(1)-like error covariance. Compare coefficients and explain what GLS is trying to do.

Learning Outcomes

  • Detect leftover AR(1)-like errors after a trend fit.
  • Fit GLS as a teaching contrast to OLS.
  • State remaining limitations.

Theory

Generalised least squares allows a non-scalar error covariance. For time series, a common teaching case is AR(1) errors: the residual at t depends on the residual at t−1. GLS (or feasible GLS) aims for efficient estimates and more honest SEs under that covariance. It still assumes the mean model and the error model are roughly right. It is not a multivariate VAR, and it is not automatic proof that autocorrelation is gone.

Dataset / Data Source

Constructed 40 observations: linear trend plus AR(1) errors. Teaching data, not a public download.

Kaggle-style Workflow

  • Inspect the series and time plot
  • Fit OLS trend
  • Check residual ACF
  • Build a simple GLS / AR-error regression
  • Compare coefficients
  • Interpret whether inference changed

Analysis / Program

import numpy as np import pandas as pd import matplotlib.pyplot as plt import statsmodels.api as sm from statsmodels.graphics.tsaplots import plot_acf rng = np.random.default_rng(6) n = 40 t = np.arange(n) e = np.zeros(n) u = rng.normal(size=n) for i in range(1, n): e[i] = 0.6 * e[i - 1] + u[i] y = 5 + 0.3 * t + e X = sm.add_constant(t) ols = sm.OLS(y, X).fit() print("OLS slope:", round(ols.params[1], 3), "SE:", round(ols.bse[1], 3)) plot_acf(ols.resid, title="OLS residual ACF") plt.tight_layout() plt.show() # Feasible GLS with AR(1) correlation structure gls = sm.GLSAR(y, X, rho=1).iterative_fit(maxiter=5) print(gls.summary().tables[1])

Expected Output

OLS slope and SE, a residual ACF that should show lag-1 dependence for this construction, then a GLSAR coefficient table. GLS SEs often differ from OLS SEs. Do not memorise a single 'correct' slope.

Result / Interpretation

OLS described the trend but treated errors as independent. GLS adjusts for AR(1)-like dependence. Students should still plot GLS residuals. Regression with time-series errors is a modelling strategy, not a claim that OLS was 'wrong' as a trend sketch.

Note

GLS can account for correlated errors. Ordinary regression does not automatically solve autocorrelation.