Residual diagnostics
Quick answer
Residual diagnostics examine the errors of a fitted regression model rather than only one aggregate score. Plot residuals against fitted values, feature values or time to look for curvature, changing error spread, isolated large errors and dependence. Use training diagnostics for model development and a separate held-out set for the final predictive comparison. All code examples run locally in Python 3.13.5; library examples use scikit-learn 1.8.0 or NumPy 2.3.5. ByteMind package execution is unverified.
Signed residuals and MAE
y=[3,7,5]; p=[1,9,5]e=[a-b for a,b in zip(y,p)]print(e, sum(e))print(round(sum(abs(v) for v in e)/len(e),6))Expected output:
[2, -2, 0] 01.333333Residuals cancel in the signed sum, but absolute error is nonzero.
How it works
A positive residual means the prediction is too low; negative means too high. With an intercept, training OLS residuals sum to approximately zero in floating-point arithmetic, but test residuals need not. A curved residual pattern may indicate a missing nonlinear term; a fan shape may indicate nonconstant variance (heteroscedasticity); sequential runs of errors can indicate dependence. These are diagnostic clues, not proof. A high-leverage observation has unusual predictor values; an influential observation meaningfully changes the fit when removed. A large residual alone is not the same as high leverage. Compare test MAE or RMSE to a baseline fitted only on training data. For classical coefficient inference, assumptions depend on the model and inferential method: conditional mean specification and exogeneity matter for unbiased OLS under the usual model, and conventional variance formulas rely on suitable error covariance assumptions. Normal errors can support exact small-sample classical tests under additional assumptions, but normality is not required merely to compute least squares. Robust standard errors, dependence-aware inference and causal designs are separate topics. Do not delete points automatically; investigate provenance and document decisions.
Compare held-out residuals with a baseline
from sklearn.linear_model import LinearRegressionfrom sklearn.dummy import DummyRegressorfrom sklearn.metrics import mean_absolute_errorX=[[0],[1],[2],[3]]; y=[1,4,5,8]Xt=[[4],[5]]; yt=[10,12]for m in [LinearRegression(),DummyRegressor(strategy='mean')]: m.fit(X,y); p=m.predict(Xt) print([round(float(a-b),2) for a,b in zip(yt,p)],round(mean_absolute_error(yt,p),2))Expected output:
[0.0, -0.2] 0.1[5.5, 7.5] 6.5Both predictors use identical held-out targets. The synthetic linear rule favors the regression model; two points cannot establish real-world reliability.
Common mistakes
Do not use a zero mean training residual to claim a perfect fit, nor remove observations solely to improve R². A test set is not a tuning set. Residual plots on tiny samples cannot verify independence or normality.
Practice and next steps
For residuals [-2,0,2], calculate mean residual and MAE. Explain why the mean alone is misleading. In the milestone compare the model and training-mean baseline on the same held-out rows. Complete the chapter knowledge check and coding exercise. Next: Regularized and nonlinear regression.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Residual diagnostics — knowledge check
Apply residual diagnostics to small regression fixtures.
A residual of -4 means what?
A fan-shaped residual plot suggests what?
What does near-zero training residual sum show for OLS with intercept?
What baseline is appropriate for held-out MAE comparison?
Is Gaussian error required to calculate OLS coefficients?