Regression: lesson 3 of 5
Regression
Predictions, Residuals, and Regression Loss
Use residuals and loss to understand where a regression model is wrong.
Concept
A regression model produces a prediction, written y_hat, while the dataset contains the actual target, y. Their difference is a residual:
residual = actual - predicted
Residuals show where a model is high or low for individual observations. A model also needs a summary of those misses to decide which fitted relationship is better. That summary is a metric; the quantity an algorithm seeks to reduce while fitting is often called loss.
Why It Matters
Predictions rarely match real outcomes exactly. A demand forecast can miss because of a promotion, weather, or noise in the data. Looking only at a single score can hide systematic problems. Residuals help ask whether errors are random, unusually large for one segment, or consistently biased in one direction.
A Small Example
| Actual sales | Predicted sales | Residual: actual - predicted |
|---|---|---|
| 120 | 110 | 10 |
| 90 | 105 | -15 |
| 150 | 145 | 5 |
A positive residual means the prediction was too low. A negative residual means the prediction was too high. The signs matter for diagnosis: if a demand model has mostly positive residuals for weekends, it may systematically underestimate weekend demand.
Residual, Metric, and Loss
A residual belongs to one observation. A metric summarizes prediction quality across many observations, such as mean absolute error (MAE). A loss is the mathematical quantity the fitting procedure may minimize to select model parameters. These ideas overlap but are not identical: a business team may report MAE while linear regression commonly fits coefficients using squared residuals.
MAE averages abs(actual - predicted). It treats a miss of 10 as twice as bad as a miss of 5. Squared error squares each miss first, so a miss of 10 contributes four times as much as a miss of 5. This stronger penalty makes large misses matter more during fitting.
Least-Squares Intuition
In simple linear regression, many candidate lines are possible. Each line makes different predictions, producing different residuals. Least squares chooses coefficient values that make the total squared residuals as small as possible. You do not need the closed-form calculation to understand the goal: select the line that is collectively closest to the observed targets under a squared-error objective.
Squared loss is not a universal business choice. It is useful here because it gives linear regression a clear fitting objective and strongly reacts to large errors. Module 4 returns to selecting and interpreting regression metrics for real decisions.
Code: Inspecting Residuals
results = pd.DataFrame({
"actual": y_test,
"predicted": predictions,
})
results["residual"] = results["actual"] - results["predicted"]
results["absolute_error"] = results["residual"].abs()
print(results)
This code assumes the y_test and predictions from the earlier workflow remain aligned. Inspecting the table helps turn a single MAE number into questions: which cases are far off, are errors mostly positive or negative, and do difficult cases share a pattern?
Deep Dive
Deep Dive: Residual Patterns Show How a Model Is Wrong
Metrics answer how large errors are overall. Residual diagnostics ask whether errors have structure. Plot residuals against predicted values, important features, or time when time order matters. A useful broad pattern is residuals roughly centered around zero with no obvious curve, widening, or subgroup shift. Random-looking residuals are encouraging evidence, not formal proof that a model is correct.
| Residual pattern | Possible signal | What to investigate next |
|---|---|---|
| A curve above and below zero | The model may be missing nonlinear structure. | Transform a feature, add an interaction or nonlinear term, or compare a model designed to capture the pattern. |
| A funnel that widens for larger predictions | Errors become less stable as scale grows. This is called heteroscedasticity. | Inspect target scale, large-value cases, and whether a transformation or different model structure is justified. |
| Mostly positive or negative residuals in one region | The model consistently underpredicts or overpredicts that range or subgroup. | Check missing features, feature definitions, distribution shift, and subgroup behavior. |
| A few very large isolated residuals | An unusual case, bad data, or a genuinely hard observation may be driving error. | Trace the original rows, validate the data, and compare results with business context before changing anything. |
Heteroscedasticity means residual spread is not approximately constant across the prediction or feature range. It can make high-value predictions much less reliable than low-value ones and can let RMSE be dominated by high-variance regions. It does not automatically invalidate a predictive model, but it changes what a single overall error number can safely claim.
MAE and RMSE: Similar Averages Can Hide Different Risks
MAE treats error magnitude approximately linearly. RMSE gives larger misses extra weight because errors are squared before averaging. If two models have similar MAE but one has a noticeably worse RMSE, that often suggests a smaller number of much larger mistakes.
For example, Model A has MAE = 8.1 and RMSE = 11.3; Model B has MAE = 7.8 and RMSE = 14.8. Model B is slightly better on typical error, but its larger RMSE suggests rare but more severe misses. Neither metric is universally better. Ask whether those large misses occur in costly, safety-critical, or important customer cases before choosing a model.
Decision Lab
Decision Lab: A Better Metric Is Not Automatically a Better Model
Two validation models predict customer revenue:
| Model | MAE | RMSE | Residual finding |
|---|---|---|---|
| A | 8.1 | 11.3 | Errors are mixed across customer value ranges. |
| B | 7.8 | 11.0 | Residuals are mostly positive for the largest customers. |
Model B has slightly lower aggregate metrics, but positive residuals mean actual - predicted is positive: it consistently underpredicts the largest customers. That pattern may matter more than the small average improvement if those customers drive inventory, pricing, or account-management decisions.
Before selecting B, inspect residuals by customer-value band, the number and business cost of the affected cases, feature availability for high-value customers, and whether the validation data represents the future population. A missing interaction, a nonlinear relationship, a skewed target, or recent distribution shift are hypotheses to test. Metrics summarize; diagnostics reveal structure.
Applied Assumptions: Diagnose Consequences
For linear regression to be a useful predictor, the chosen features need to represent enough of the relationship for held-out errors to be acceptable. A straight or additive form may miss curvature or interactions. Related observations over time or within one customer group can make validation look more independent than it really is. Changing error spread and influential observations can hide unequal reliability. Correlated predictors can also make individual coefficients unstable to interpret.
These concerns do not mean every imperfect assumption requires rejecting the model. They are evidence to inspect and communicate. Stronger assumptions are needed when making formal statistical-inference claims about coefficient uncertainty; predictive usefulness is primarily judged through appropriate held-out evaluation, diagnostics, and decision context.
Failure Signals
Failure Signals
Watch for a curved residual pattern, a funnel-shaped spread, residuals shifted away from zero for a subgroup, unusually large misses, or validation metrics that improve while an important segment worsens. Also compare training and validation performance: a much better training score can indicate that the model learned sample-specific detail rather than generalizable structure.
Check Your Reasoning
Check Your Reasoning
Question: A house-price model has acceptable RMSE overall. Residuals are small for inexpensive homes but spread dramatically wider as predicted price rises. What does the aggregate RMSE fail to tell you, and what would you inspect before deployment?
Reasoning: The model may have heteroscedasticity: its reliability is unequal across price ranges. Inspect high-value rows, target and feature scale, possible missing location or property-quality features, and the cost of expensive prediction mistakes. Compare transformations or another model only after identifying whether the pattern is stable on held-out, representative data.
Practice and Build
Practice and Build Connection
Reinforce residual calculations with Residuals and Model Error. For a larger applied setting, Demand Forecasting for Retail asks where forecasts are reliable and where operational caution is needed.
Failure Signals
Common Mistakes
- Reversing the residual sign without stating the convention.
- Calling one residual an overall model metric.
- Assuming the lowest training loss guarantees generalization.
- Treating a large residual as automatically bad data.
- Choosing a metric before understanding the cost of errors.
Best Practices
Check residuals on held-out data, examine both typical and large errors, and preserve their link to original rows. Describe error in target units when communicating with stakeholders. A model can have a reasonable average error while failing badly for an important customer segment.
Interview Perspective
Question: What is a residual? Answer: the actual target minus the model prediction for one observation. What the interviewer is testing: whether you can connect a prediction equation to model diagnosis. Follow-up: why might squared error emphasize large misses?
Practice Questions
- Calculate the residual when actual demand is 80 and predicted demand is 95.
- What does a consistently positive residual for a product category suggest?
- Why is MAE easier to explain than a squared-error value?
- Which contributes more squared error: a miss of 20 or two misses of 10? Explain.
- Why should residuals be inspected on held-out rows?
Quick Quiz
- What does a negative residual mean under this convention? Answer: the model predicted too high.
- Is MAE an individual residual or a summary metric? Answer: a summary metric.
- What does least squares seek to reduce? Answer: the total squared residuals.
Key Takeaway
Key Takeaways
Residuals explain individual prediction misses. Metrics summarize many errors, while loss gives fitting a direction. Linear regression uses least-squares intuition to find a line with lower total squared error.
Next Lesson
Next, extend the line from one feature to several and interpret coefficients carefully.
Finish this lesson on your terms
Mark it complete when you have worked through the material and are ready to move on.