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.
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
| Symbol | Meaning |
|---|---|
| x_j | An included predictor available at forecast time. |
| b_j | Conditional fitted outcome change per predictor unit. |
| p | Number of predictors; enough information is needed to estimate them reliably. |
| MAE | Mean 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
- The fitted training intercept is R100.65.
- The fitted coefficients are approximately R1.8344 per parcel and R3.7062 per kilometre, conditional on the other input.
- For held-out (22,3), prediction =100.65+1.834375×22+3.70625×3≈R152.125.
- The second test prediction is approximately R166.912; mean absolute error over the two held-out rows is R1.394.
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 notebookApply it at work
- Choose the prediction decision, target unit, candidate inputs and a realistic time- or group-based holdout.
- Inspect relationships, missingness and correlated predictors; encode and scale using training information only.
- Fit a defensible baseline, inspect residuals and coefficient stability, and compare alternatives using validation data.
- 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.
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
Sources and further reading
Concepts are linked to primary teaching or technical sources. The business examples are illustrative applications, not reported client results.