Multiple Linear Regression
Topic — The previous page fitted one feature and found something unresolved:
agehad a real effect of that its marginal correlation of completely hid, and only became visible oncesize's contribution was removed. Doing that removal automatically, for every feature at once, is what multiple regression is. This page works out how, and then asks which measure can be trusted to tell you whether an added feature helped.
The model, and the meaning of one coefficient
Each feature gets its own coefficient:
With one feature this is a line, with two a plane, with more a hyperplane — impossible to picture and no different algebraically.
The definition of is the part that carries all the weight:
Holding every other feature in the model constant, one additional unit of is associated with a increase in .
Both qualifications are load-bearing, and the first one is not a caveat but a description of what the arithmetic actually does. "Holding every other feature constant" means is fitted on the variation in that the other features cannot explain — exactly the residual-then-refit operation the previous page did by hand. "Is associated with" is not "causes"; regression measures association, and nothing about fitting a hyperplane establishes direction.
Solving for two coefficients by hand
The two-feature case has a closed form small enough to compute on paper, and doing so shows where the "holding constant" behaviour comes from mechanically.
Working in deviations from the means — , and so on — the least-squares coefficients satisfy two simultaneous equations, the normal equations:
and measure how much each feature varies. measures how much they vary together — and it is the only term that couples the two equations. Set and they decouple completely: and , each identical to its simple-regression value. Everything interesting about multiple regression lives in .
The Collinearity & Determinant Solver
Experiment with the interactive solver below. Modify the coupling term (covariance between features) and the individual variances to watch how the simultaneous normal equations couple and decouple, how the determinant shrinks, and how the coefficients behave.
Interactive Normal Equations Solver
The Coupled Normal Equations:
det = S₁₁S₂₂ − S₁₂²ŷ = 3.43 + 1.05x₁ + 1.38x₂That determinant is the whole story of collinearity
det = S₁₁S₂₂ − S₁₂² sits in the denominator of both coefficients. is the coupling term, so as the two features become more alike, grows toward and the determinant heads to zero — dividing by something arbitrarily small. This is the algebraic source of every instability later on this page, and at exactly the determinant is zero and there is no unique solution at all, which is the next page's subject.
The same structure generalises: with features you get simultaneous equations, which is unwieldy by hand and elegant in matrix form — hence the next page.
What "holding all else constant" does to a coefficient
A coefficient's value depends on what else is in the model. Two cases on the house-price data, where the truth is :
2. WHAT 'HOLDING ALL ELSE CONSTANT' DOES TO A COEFFICIENT
size's DIRECT effect is 3000 in both models.
but rooms = 2 + 0.05·size, so in the second model size ALSO acts
through rooms: 3000 + 5000×0.05 = 3250 is its total marginal effect.
pair corr size alone size joint shift
size, age 0.0614 3043.86 3062.84 +18.98
size, rooms 0.8244 3218.22 2921.04 -297.18
With a near-orthogonal partner, the coefficient barely moves. size and age correlate at , and size's coefficient shifts by — under 1% — between the solo and joint fits. , so the equations nearly decouple and each feature is estimated as though the other were not there.
With a correlated partner, it moves substantially and for a specific reason. rooms is built as , correlating with size at . Now size influences price through two channels: directly at , and through rooms at . Its total marginal effect is therefore , and its direct effect is .
Total versus Direct Effect Sandbox
Toggle the model partner below to watch how size's coefficient transitions from measuring the total effect (when fitted alone) to measuring only the direct effect (when fitted jointly with rooms).
Total versus Direct Effect Sandbox
Toggle the model to see how holding `rooms` constant isolates the direct physical effect of home size.
A coefficient is not a property of a feature
It is a property of a feature in a particular model. Add or drop a correlated column and it changes — to here. Reporting "size is worth 3000 per unit" without stating what else was in the model is not a rounding-level imprecision; it is an incomplete statement.
Marginal correlation is a poor screen
The obvious way to choose features is to correlate them with the target. On this data that ranking is:
age, with a real effect of per year, ranks below door_colour, which is pure noise. Any threshold that keeps door_colour keeps age, and any threshold that drops age drops nothing else of value — the screen simply carries no information about these two.
Now fit them in combination:
| Features | Train | CV | Adjusted | |
|---|---|---|---|---|
| size | 1 | 0.9700 | 0.9661 | 0.9698 |
| size, age | 2 | 0.9800 | 0.9773 | 0.9797 |
| size, age, door_colour | 3 | 0.9800 | 0.9766 | 0.9795 |
| size, age + 20 noise | 22 | 0.9838 | 0.9732 | 0.9802 |
Three readings, in increasing order of importance:
age genuinely helps. CV rises when it joins size, despite that marginal correlation of . The joint fit finds the effect the screen missed: once size is accounted for, age correlates with what remains at .
door_colour is harmless in small numbers. Train unchanged at , CV down a hair to . One irrelevant column costs almost nothing.
Twenty noise columns raised train to while CV fell to . Both numbers moved, in opposite directions, from the same change.
Training never decreases when you add a feature
Not "rarely" — never, as a mathematical fact. The model can always set a new coefficient to zero and do exactly as well as before, so the best achievable fit is at least as good as it was. Twenty columns of noise bought of training and cost of measured out-of-sample performance.
So training cannot be used to decide whether a feature belongs.
The standard answer to that problem is Adjusted , which penalises the model for the number of features it uses. Look at its column above: for size, age, and 0.9802 for the same model plus twenty columns of pure noise. It ranks the noise model higher.
Does Adjusted R² actually catch the noise?
That single comparison could be luck, so here is the sweep: noise columns from 0 to 100 on rows, every row averaged over 30 independent regenerations of both the data and the noise.
Adjusted R² vs. CV R² Noise Sweeper
Adjust the slider below to add between 0 and 100 columns of pure Gaussian noise to the baseline size, age model. Watch how Training , Adjusted , and Cross-Validated behave in real-time.
Interactive Complexity Sweeper
Add pure noise columns to watch Adjusted R² fail to penalize overfitting while CV R² catches it.
Why the penalty fails
Adjusted is:
and the mechanism is a race between two quantities that happen to grow at similar rates. As rises, the penalty factor grows — at it is , a sevenfold inflation of the unexplained variance. But is shrinking at almost exactly the rate that offsets it, because spurious columns inflate train : from to , so goes , a factor of 7.5.
Multiply the two and you get roughly what you started with. The penalty is real, correctly implemented, and almost exactly cancelled by the overfitting it is meant to expose.
Adjusted R² is not an overfitting guard
At it failed to flag 100 columns of pure noise. It is a mild correction for the fact that cannot decrease — useful for comparing two models that differ by a feature or two, and no substitute for measuring out-of-sample performance.
The number that worked here is cross-validated , which fell monotonically and by . When train and CV diverge, the training number is the one lying — and Adjusted is close enough to a training number to lie with it.
This is also why the honest form of the earlier claim matters: Adjusted and cross-validation are often presented as two interchangeable remedies for feature-count inflation. On this data they are not remotely interchangeable. Only one of them worked.
When coefficients stop meaning anything
The determinant warning from the hand-worked fit, measured. Two features, each with a true coefficient of exactly , fitted 40 times at increasing correlation between them:
| Mean | Mean | Sum | SD of | |
|---|---|---|---|---|
0.000 | 2.9927 | 3.0017 | 5.9944 | 0.0646 |
0.500 | 2.9918 | 3.0019 | 5.9937 | 0.0718 |
0.900 | 2.9893 | 3.0038 | 5.9931 | 0.1426 |
0.990 | 2.9810 | 3.0118 | 5.9928 | 0.4500 |
0.999 | 2.9554 | 3.0373 | 5.9928 | 1.4312 |
Read the columns carefully, because the result is subtle:
- The means stay correct. and at the extreme — still centred near . Collinearity does not bias the coefficients.
- The sum stays exact. in the last three rows, against a true total of , and it barely moves across the whole table.
- The spread explodes. Standard deviation of the first coefficient goes — 22× larger — and most of that happens between and .
The interpretation is precise and worth stating carefully: when two features carry nearly the same information, the data determines their combined effect perfectly well and says almost nothing about how to divide it between them. Any individual fit picks a split more or less arbitrarily, and the next sample picks a different one.
This is multicollinearity. Its consequences:
| Affected | How |
|---|---|
| Predictions | Barely — the sum is what drives them, and the sum is stable |
| Individual coefficients | Severely — unstable, and can even flip sign |
| Interpretation | Fatally — "size adds 3000" is not a supportable claim |
| Standard errors | Inflated, so significance tests fail to reject |
What ridge does about it
Ridge regression adds a penalty on the size of the coefficients, which resolves the ambiguity by preferring the split that keeps them small. At :
| Model | Mean | Mean | SD of | Sum | Test |
|---|---|---|---|---|---|
| LinearRegression | 3.2657 | 2.7314 | 1.8311 | 5.9971 | 0.9710 |
| Ridge(alpha=1.0) | 3.0206 | 2.9563 | 0.2378 | 5.9769 | 0.9713 |
The stability gain is dramatic. Coefficient spread drops , an 87% reduction, and the split becomes even: the gap between the two means narrows from to .
The prediction gain is nil. Test goes — an improvement of 0.0003, which is nothing.
That pairing is the lesson, and it confirms the thesis of the table above from the opposite direction: since collinearity was never damaging the predictions, fixing collinearity cannot improve them. What ridge buys is coefficients you can report and reproduce. Whether that is worth a small bias depends entirely on whether anyone is going to read the coefficients.
Regularisation gets its own treatment later
Ridge appears here only as the direct answer to collinearity. The penalty term, choosing its strength, and the different behaviour of lasso — which drives coefficients to exactly zero rather than merely shrinking them — belong with generalisation and model control.
2. Implementation Lab
Lab Exercise: Multiple Linear Regression
An interactive, fully-functional Google Colab / Jupyter Notebook is available to experiment with this concept live in your browser.
How to run the lab:
- Click the "Open In Colab" badge above to launch the interactive notebook.
- Click "Run all" or execute cells individually using
Shift + Enter.
Things to try inside the script:
- Experiment 1 (Determinant Crash): In section 1, change
x2to exactly2 * x1. Watchdet = 0and OLS crash with a singular matrix division-by-zero, while Ridge handles it safely. - Experiment 2 (Spurious k/n ratios): In section 4, raise from
120to2000while keeping the noise counts. Find the ratio where Adjusted finally starts falling. - Experiment 3 (Ridge sweep): In section 6, sweep
alphaover[0.01, 0.1, 1, 10, 100]to find the perfect trade-off between coefficient stability and test score.
3. Common Mistakes
- 1. Absolute Coefficients
- 2. Solo Correlation Screening
- 3. Trusting Adjusted R²
- 4. Collinearity biases fits
- The Misconception: "A coefficient is an absolute property of a feature."
- Wrong Thinking: Believing
sizeis always worth 3000 per unit, regardless of what other columns (likerooms) are included in the fit. - The Right Principle: A coefficient is a property of a feature in a particular model context. Adding or dropping correlated columns immediately shifts other weights (e.g., once
roomsjoined) as credit is shared.
- The Misconception: "I should drop features that have near-zero correlation with the target."
- Wrong Thinking: Removing
agebecause its solo correlation with house price is . - The Right Principle: Dominant features can completely mask secondary signals. Once dominant features (like
size) are controlled for, hidden correlations become extremely clear (e.g.,agepartial correlation was , significantly boosting CV ).
- The Misconception: "Adjusted R² is a robust guard against overfitting."
- Wrong Thinking: Adding 100 noise columns because Adjusted read (higher than the clean 2-feature baseline of ).
- The Right Principle: Adjusted is not an overfitting guard. Spurious columns inflate training so rapidly that the inflation nearly cancels out the feature-count penalty. Always trust out-of-sample Cross-Validation scoring.
- The Misconception: "Multicollinearity introduces a bias into our regression coefficients."
- Wrong Thinking: Believing collinearity makes the averages of fitted coefficients drift away from the true values.
- The Right Principle: Collinearity does not bias coefficients—their means stay exactly correct. Instead, it makes coefficient variance explode, creating massive instability where individual weights swing wildly.
4. Summary
Core Theory
- Normal Equations: Simultaneous equations satisfied by OLS weights. Coupled by the covariance term .
- Decoupling Rule: If , the equations decouple, and multiple regression collapses into independent simple regressions.
- Collinearity Root: The determinant sits in the denominator of OLS coefficients. As correlation climbs, determinant heads to zero, blowing up coefficient sizes.
- Total vs. Direct Effect: Solo fits capture total effects across all channels; joint fits isolate only the direct effect of each feature.
Engineering Realities
- R² Never Decreases: Training is mathematically guaranteed to increase or stay constant as columns are added, making it useless for complexity evaluation.
- Adjusted R² Flatness: Adjusted fails completely under heavy noise columns because overfitting and degrees-of-freedom penalties cancel out.
- Generalization general: Out-of-sample Cross-Validated is the only metric that monotonically captures generalization collapse.
- Ridge regularisation: Ridge adds an L2 size penalty to coefficients, stabilizing their spread (variance) under extreme collinearity without biasing the means.
Key Takeaways
- Sum Invariance: Under multicollinearity, while individual coefficients are unstable and change wildly, their combined sum remains perfectly stable and predictions remain robust.
- Interpretation vs. Prediction: Collinearity completely destroys model interpretation ("how much size adds"), but has almost no effect on prediction performance.
- Ridge trade-off: Ridge regression does not improve accuracy (test stays flat); it buys reportable, stable, and reproducible coefficients.
Next in this section: The Matrix Formulation — the same solution for any number of features in one algebraic step, and what happens when correlation reaches exactly 1.0.
5. Active Recall
Attempt each question first, then click to reveal detailed answers, mathematical derivations, and sample explanations.
Interactive Checkpoint Quiz
Interactive Checkpoint: The Collinearity Conundrum
Under severe multicollinearity (e.g. $r = 0.999$), what happens to our model's predictions and its individual coefficients?
Review Flashcards
1. [THEORY] What does mean, and what happens to the simultaneous normal equations?
- The Covariance Rule: measures the covariance (shared variation) between features and .
- If , the features are completely orthogonal (uncorrelated).
- Substituting into the simultaneous normal equations:
- The equations decouple completely. Multiple linear regression collapses into two completely independent simple linear regressions, with both slopes equal to their solo values.
2. [ANALYZE] How does the determinant head to zero, and why does this explode coefficient variance?
- The Math: Solve the normal equations using Cramer's rule. The determinant sits in the denominator of both coefficients and :
- As features become more collinear, their covariance increases, heading towards the maximum limit (where ).
- This forces the denominator (determinant) closer and closer to . Dividing by an arbitrarily tiny number makes the calculated coefficient value extremely sensitive to minor fluctuations in target measurements (), causing coefficient variance to explode.
3. [THEORY] Why is it mathematically impossible for training to decrease when you add a feature?
- The Optimization Rule: OLS operates by finding coefficients that minimize the Sum of Squared Errors (SSE):
- If you add a new feature , the optimizer has a new parameter to tune.
- In the worst-case scenario where the new feature is completely useless noise, the optimizer can simply assign , achieving the exact same SSE (and thus the exact same ) as before.
- Because the search space of the optimization has expanded, the minimum SSE can only stay the same or decrease, meaning training can never decrease.
4. [ANALYZE] Why does Adjusted completely fail as an overfitting guard against multiple noise columns?
- The Formula: .
- Adding noise columns triggers a literal race between two opposing terms:
- As increases, the penalty ratio grows (at , it inflates unexplained variance by 7.0×).
- However, because of overfitting, the training unexplained variance shrinks from (a 7.5× reduction).
- The Result: The two factors nearly cancel each other out, making Adjusted remain completely flat and completely blind to the fact that the model is fitting pure noise.
5. [THEORY] How does Ridge regularisation resolve coefficient instability without improving prediction accuracy?
- Coefficient Stability: Ridge regression adds an L2-norm penalty () to the OLS cost function. When features are collinear, infinite splits of the coefficients yield the same prediction. OLS picks a split randomly, but Ridge enforces stability by picking the split that keeps both coefficients small and evenly distributed, cutting standard deviation by over 85%.
- No Prediction Gain: Because OLS was already finding a stable combined sum, its predictions were already robust. Restricting the coefficients via Ridge does not change this sum—it merely changes the individual allocation, leaving prediction score virtually unchanged ( gain).