GARETH GRANTTECHNOLOGY · DATA · INNOVATION
Explore

29 / Predictive analytics

Multiple linear regression

Multiple regression extends a straight-line prediction rule to several predictors. It can account for observable differences in job mix and produce a transparent planning baseline, but adding variables does not automatically remove bias, create causality or improve future prediction.

AdvancedFitted example + Python

The business question

How do several available business factors jointly relate to a numerical outcome?

  • Estimate delivery cost using order volume, route distance and other known operating inputs.
  • Compare average performance while adjusting for measured product or service mix.
  • Develop inspectable workload, cost or demand models before evaluating more complex alternatives.

How the method works

Ordinary least squares estimates a set of coefficients that jointly minimise squared residuals. A coefficient describes the fitted outcome change for one predictor unit while holding the other included predictors fixed. Categorical predictors require suitable indicator coding; nonlinear patterns can be represented with explicitly chosen transformations or interaction terms when justified.

The holding-other-factors-fixed interpretation is conditional on the model. If volume and distance nearly always rise together, the data may contain little information to separate their coefficients. This multicollinearity can make individual estimates unstable even when combined predictions look reasonable. More data in the relevant combinations, simpler models or regularisation can help.

Predictive use requires features available at the decision time, preprocessing estimated only on training data and evaluation on genuinely new work. Classical coefficient tests and intervals need suitable residual and design assumptions; the short example focuses on arithmetic and held-out prediction instead. An adjusted observational association can still be confounded by unrecorded driver skill, congestion, product mix or scheduling decisions.

The calculation

predicted y = b_0+b_1x_1+…+b_px_p; estimate b by minimising Σ(y_i−predicted y_i)²
Read the symbols
SymbolMeaning
x_jAn included predictor available at forecast time.
b_jConditional fitted outcome change per predictor unit.
pNumber of predictors; enough information is needed to estimate them reliably.
MAEMean absolute held-out error, in the outcome’s original units.

A worked business example

ILLUSTRATIVE SCENARIO

Constructed delivery-cost teaching data use parcel count and route kilometres as inputs.

Inputs

  • Eight parcel/km pairs: (10,2), (12,3), (14,1), (16,4), (18,2), (20,5), (22,3), (24,6).
  • Costs are generated as R100 + R2×parcels + R3×km plus small stated errors.
  • Fit only the first six rows and hold the final two rows aside.

Work through the numbers

  1. The fitted training intercept is R100.65.
  2. The fitted coefficients are approximately R1.8344 per parcel and R3.7062 per kilometre, conditional on the other input.
  3. For held-out (22,3), prediction =100.65+1.834375×22+3.70625×3≈R152.125.
  4. The second test prediction is approximately R166.912; mean absolute error over the two held-out rows is R1.394.
The fitted model gives an easily inspected delivery-cost rule and a small test error on this constructed example.

The data were intentionally generated from a near-linear rule. Eight rows and two test cases are not enough to establish real-world accuracy or coefficient stability. The validation structure is the lesson, not a claim that delivery costs are this predictable.

Run and inspect the fitted example

This method needs a richer model-fitting workflow. The complete runnable Python example below shows the fit and relevant checks; use the downloadable notebook to change the data and rerun it.

Download the worked notebook

Apply it at work

  1. Choose the prediction decision, target unit, candidate inputs and a realistic time- or group-based holdout.
  2. Inspect relationships, missingness and correlated predictors; encode and scale using training information only.
  3. Fit a defensible baseline, inspect residuals and coefficient stability, and compare alternatives using validation data.
  4. Report held-out errors and uncertainty, then monitor drift and retrain only through a documented process.

Check these assumptions

  • The functional form adequately represents the conditional mean over the intended prediction range.
  • Included predictors are observed reliably and available before the outcome is known.
  • For classical inference, residual independence, variance and distribution conditions need appropriate checks or robust adjustments.
  • For causal interpretation, the design must address confounding and selection; including several columns alone does not do so.

Common mistakes and how to avoid them

  • Selecting predictors using the final test set leaks information and makes the reported performance optimistic.
  • Strongly correlated inputs can create unstable coefficients that should not be read as independent physical mechanisms.
  • An apparently adjusted result can remain biased by omitted variables, data selection or measurement error.

How this can help the business

  • A cost model can support transparent quote checks and workload budgeting when validated on representative future work.
  • Measured job-mix adjustment can improve diagnostics while identifying where an experiment is needed to support causal action.
A decision rule to adapt

Adopt the model only if relevant held-out error, stability and operating constraints are acceptable relative to a simple baseline; interpret coefficients conditionally and within the observed range.

When to use another approach

Use regularised regression for unstable high-dimensional inputs, nonlinear models for complex patterns, or causal designs when the question concerns an intervention.

Reproduce the worked example in Python

The code is self-contained for this example. Dependencies and run instructions are included with the downloadable notebook.

Show executable code and recorded output
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error
x = np.array([[10,2],[12,3],[14,1],[16,4],[18,2],[20,5],[22,3],[24,6]])
noise = np.array([1,-1,0,2,-2,1,0,-1])
cost = 100 + 2*x[:,0] + 3*x[:,1] + noise
model = LinearRegression().fit(x[:6], cost[:6])
predictions = model.predict(x[6:])
print("Intercept (R):", round(model.intercept_, 4))
print("Parcel and km coefficients:", np.round(model.coef_, 4).tolist())
print("Test predictions (R):", np.round(predictions, 3).tolist())
print("Test MAE (R):", round(mean_absolute_error(cost[6:], predictions), 3))

Recorded output

Intercept (R): 100.65
Parcel and km coefficients: [1.8344, 3.7062]
Test predictions (R): [152.125, 166.912]
Test MAE (R): 1.394
Download all worked examples

Sources and further reading

Concepts are linked to primary teaching or technical sources. The business examples are illustrative applications, not reported client results.