2.2.8 · D4Linear & Logistic Regression

Exercises — R-squared and adjusted R-squared

2,877 words13 min readBack to topic

Before we start, let me re-anchor the three quantities so nothing below uses a symbol you haven't seen defined on this page.

Figure — R-squared and adjusted R-squared

Look at the figure: the red vertical stubs are the residuals that build ; the grey stubs to the flat line build . Every exercise below is really about the size of those red stubs — the smaller they are compared to the grey stubs, the closer climbs to .

Figure — R-squared and adjusted R-squared

Level 1 — Recognition

L1.1 — Read the formula

State, in one sentence each, what , , and measure. Then: if a model predicts the mean for every point, what is ?

Recall Solution
  • = total squared distance between actual points and the model's predictions (leftover error).
  • = total squared distance between actual points and their average (the whole spread).
  • = the fraction of that spread the model removed.

If the model predicts everywhere, then , so . Then A useless model scores exactly — this is the baseline the whole idea is built on. In the first figure, this is the case where every red stub matches its grey stub exactly.

L1.2 — Plug and chug

Given and , compute .

Recall Solution

The model explains of the spread.

L1.3 — Direction of the penalty

Without computing anything, decide: as you add more useless features (with fixed and unchanged), does go up or down? Say why using the formula .

Recall Solution

Increasing shrinks the denominator , so the fraction grows (fewer free numbers left — see the second figure's right side). That makes larger, and we subtract it, so goes down. Useless features are punished even when stays flat.


Level 2 — Application

L2.1 — Full from raw data

Actuals , predictions . Compute , , , and .

Recall Solution

Residuals (these are exactly the red stubs in the first figure):

L2.2 — Adjusted

Using from L2.1, with and features, compute .

Recall Solution

The two features cost us . Note the free numbers left: , so the fee is already sizeable.

L2.3 — Back-solve for

A model reports on data with . What is ?

Recall Solution


Level 3 — Analysis

L3.1 — Does the new feature earn its keep?

Model A: , . You add one feature, giving Model B: , . Both use . Compute for each and decide which model to keep.

Recall Solution

Model A: Model B: , so the new feature earned its place — keep Model B. The rise from to was big enough to beat the extra-parameter fee.

L3.2 — The overfitting cliff

, and a model achieves using features. Compute . What does it reveal?

Recall Solution

A dazzling collapses to . With only residual degree of freedom, the model is memorising noise, not learning signal — the adjusted score has stripped away almost all of the apparent fit. This is textbook Overfitting — the adjusted score refuses to be fooled.

L3.3 — Negative

A no-intercept model (see the "What intercept means" box above) on data with produces predictions with . Compute and interpret.

Recall Solution

is negative — the model is worse than just predicting the mean (which would score ). Because the intercept was forced to , the line cannot match the data's average level, so (error from predictions) exceeds (error from the flat mean line). Not a bug; a verdict.


Level 4 — Synthesis

L4.1 — Feature-selection decision table

You have points and three nested models. Compute each and recommend one.

Model
M1 0.70 2
M2 0.75 5
M3 0.76 12
Recall Solution

M1: M2: M3:

Ranking: M2 () > M3 () > M1 (). M3 has the highest raw but its 7 extra features over M2 buy only more explained variance — not worth the degrees of freedom. Recommend M2. This is exactly what Feature Selection and Model Selection aim to formalise.

L4.2 — Derive the penalty gap

Show algebraically that , starting from . Then verify with , , (matches L3.1's Model A).

Recall Solution

Start: Write : Substitute: Since : Check (): — matches L3.1's exactly. The subtracted term is literally the "gap" between and .


Level 5 — Mastery

L5.1 — End-to-end from data to decision

Raw data, two competing fits on the same points. . Model A (1 feature) predicts — a perfect line. Model B (3 features) predicts .

(a) Compute and for both. (b) Which model wins, and what deeper lesson does the pair teach?

Recall Solution

Mean:

Model A — residuals all :

Model B — residuals :

(b) Winner: Model A on every metric (, ). The lesson: Model B used 3 features to do a worse job than Model A's 1 feature. More parameters bought nothing — in fact they slightly increased error. makes the waste explicit ( vs ). Simpler wins when it fits; this is the whole spirit of Model Selection and guards against Overfitting. (See Linear Regression for why a straight generating rule like is captured exactly by one feature, and Mean Squared Error for the per-point view of these same residuals.)

L5.2 — Sensitivity: how many features break the model?

With and a genuine that stays fixed, find the largest integer for which is still positive. (Assume adding features keeps at — the pessimistic "useless features" case.)

Recall Solution

Positivity requires , i.e. , i.e. . Since is an integer and we need (positive residual df), the largest is Check : ✔ Check : ✘ So even a strong turns worthless once you cram in useless features on points — the degrees of freedom run out.