2.2.8 · D3Linear & Logistic Regression

Worked examples — R-squared and adjusted R-squared

2,602 words12 min readBack to topic

Before we start, let us re-anchor the four quantities every example below leans on, in plain words, so nothing uses a symbol you have not seen.

Recall The four quantities, in words (click to reveal)

What is ? ::: The plain average of the true values — the number you'd guess if you knew nothing else. What is ? ::: Add up, for every point, how far the true value sits from that average, squared. It measures how spread-out the data is to begin with. What is ? ::: Add up how far each true value sits from the model's guess, squared. It measures the error that is left over after the model tried. What is ? ::: — the fraction of the original spread the model managed to erase.

We square each gap so that a miss of and a miss of both count as badness, never cancelling. The units of every are "(units of ) squared", so when we divide one by another the units cancel and is a pure number — that is why it reads like a percentage.


Where the adjusted "rent" comes from

Before Ex 5, one gap from the parent note deserves filling: why is the penalty factor exactly ?


The scenario matrix

Every question about lives in one of these cells. Each example below is tagged with the cell it fills.

Cell What makes it special Expected behaviour Example
A. Perfect fit Ex 1
B. Typical good fit small Ex 2
C. Model = mean Ex 3
D. Worse than mean Ex 4
E. Adjusted penalty helps useless feature added but Ex 5
F. Degenerate d.o.f. collapses Ex 6
G. Real-world word problem pick a model reasoning + numbers Ex 7
H. Exam twist from correlation (simple reg.) Ex 8
Z. Degenerate target undefined callout above

Ex 1 — Cell A: the perfect fit

Forecast: guess the answer before reading on. (It is the largest value can take.)

  1. Mean. . Why this step? The mean is the baseline every compares against — we always need it first.
  2. Total spread. . Why this step? This is the amount of variation the model is being asked to explain.
  3. Leftover error. . Why this step? Predictions equal the truth, so every gap is zero.
  4. Assemble. .

Verify: is the ceiling — it can only happen when , exactly our case. ✓

Figure — R-squared and adjusted R-squared
Figure 1 — Ex 1: mint dots sit exactly on the lavender "predicted = actual" diagonal, so there are no vertical error gaps to add up. Observe that zero vertical distance is what forces and .


Ex 2 — Cell B: the typical good fit

Forecast: the errors are small but non-zero, so guess a number close to 1 but below it.

  1. Mean. . Why this step? The mean is our do-nothing baseline; measures how far past it the model gets.
  2. Total spread. . Why this step? Records the raw variation before the model helps.
  3. Leftover error. . Why this step? The squared misses that survive the model.
  4. Assemble. .

Verify: , and since our errors are tiny compared to the spread, a near-1 value is exactly what we forecast. ✓

Figure — R-squared and adjusted R-squared
Figure 2 — Ex 2: coral error bars (truth → prediction) are short next to the lavender bars (truth → mean). Because the coral total (26) is tiny against the lavender total (500), the model erased almost all the spread → .


Ex 3 — Cell C: model is no better than the mean

Forecast: if the model literally is the mean, how much extra variation did it explain? Guess the floor.

  1. Mean. . Why this step? We need the baseline first — and here the model happens to equal it, which is the whole point.
  2. Total spread. .
  3. Leftover error. . Why this step? Because everywhere, this sum is identical to .
  4. Assemble. .

Verify: . A model that always guesses the mean is the definition of "explains nothing beyond the baseline". ✓

Figure — R-squared and adjusted R-squared
Figure 3 — Ex 3: the flat lavender line IS the mean, so the model's error bars land exactly on the mean's error bars. Identical bars ⇒ .

This is the boundary between error-based metrics and the baseline: any real model must beat this to be worth keeping.


Ex 4 — Cell D: worse than the mean → negative R-squared

Forecast: the predictions are wildly off. Can go below its "floor" of 0? Guess yes or no.

  1. Mean. . Why this step? We still need the baseline to compare against — even a terrible model is judged relative to the mean.
  2. Total spread. .
  3. Leftover error. . Why this step? The model's guesses are far worse than the mean, so its leftover error explodes.
  4. Assemble. .

Verify: , so , so . Negative is not a bug — it is the metric shouting "you'd have done better predicting the average". This is exactly Common Mistake 3 from the parent note. ✓

Figure — R-squared and adjusted R-squared
Figure 4 — Ex 4: the coral prediction line sits far above the mint data cloud, while the lavender mean line hugs it. The coral error gaps dwarf the spread around the mean — that overshoot is exactly what pushes below zero.


Ex 5 — Cell E: adjusted R-squared catches a useless feature

Forecast: rose. Guess whether the adjusted score rose or fell.

Recall the penalty form (derived in the "rent" section above): .

  1. Before (). . Why this step? We divide the leftover-fraction by the degrees of freedom to charge rent for each parameter.
  2. Compute: , so .
  3. After (). . Why this step? One more feature means the denominator shrank from 7 to 6 — the rent went up.
  4. Compute: , so .

Verify: went (up), but went (down). The adjusted metric correctly flagged the noise feature — this is why we prefer it during Feature Selection. ✓


Ex 6 — Cell F: degenerate degrees of freedom

Forecast: almost as many features as data points. Guess: does stay near 0.99 or crash?

  1. Degrees of freedom left. . Why this step? This tiny number is the alarm — only 1 "free" data point remains after fitting.
  2. Apply. . Why this step? Dividing by 1 means the penalty factor 49 is applied at full force.
  3. Compute: .

Verify: the impressive collapses to . As , the denominator and the penalty balloons — a mathematical signature of Overfitting. If exactly, the denominator hits 0 and is undefined (division by zero). ✓


Ex 7 — Cell G: real-world word problem

Forecast: the predictions look close. Guess an above or below 0.9.

  1. Mean. dollars. Why this step? Baseline: "just predict the typical day's revenue".
  2. Total spread. deviations from are . (dollars²). Why this step? Measures how much revenue naturally bounces around.
  3. Leftover error. (dollars²). Why this step? The revenue the model still misses.
  4. Assemble. .

Verify: units: dollars²/dollars² cancel → pure number, good. means the model erases about 95.5% of the revenue variation. Above 0.9, as forecast. The owner should still confirm on new days via Cross-Validation before trusting it — training alone never proves generalisation. ✓

Figure — R-squared and adjusted R-squared
Figure 5 — Ex 7: daily revenue (mint) vs model (coral) across six days; the dashed lavender line is the mean baseline. The coral curve tracks the ups and downs closely while the flat baseline misses them — that tracking is the visual meaning of .


Ex 8 — Cell H: exam twist, R-squared from correlation

Forecast: guess the sign of before reading.

  1. Recall the identity. For simple linear regression, (stated in the parent note). Why this step? This shortcut only holds with one feature — it is the exam's trap to test whether you know that.
  2. Square it. . Why this step? Squaring destroys the sign — a negative correlation still explains variance.
  3. Address the misconception. tells you the direction of the relationship; tells you the strength of the fit. A strong negative trend fits just as well as a strong positive one.

Verify: , contradicting the student. A negative never makes negative because we square it. (The shortcut fails for multiple regression — do not carry it over.) ✓

Figure — R-squared and adjusted R-squared
Figure 6 — Ex 8: a downward-sloping cloud has (negative direction) yet a tight fit. Squaring folds the negative sign away, so — direction and strength are different questions.


Recall Self-test (click to reveal)

If , what is ? ::: Exactly 0 — the model matched the mean and nothing more. Can ordinary least squares with an intercept give a negative on training data? ::: No — never. With an intercept the fitted line always beats or ties the mean on the training set, so training is guaranteed. Negative needs a dropped intercept, a bad manual model, or held-out test data. Adding a noise feature: does what? does what? ::: stays same or rises; typically falls. What happens to when all are identical? ::: , so the formula divides by zero and is undefined. Simple regression with : ::: .

See also: Bias-Variance Tradeoff, Model Selection, Linear Regression, 2.2.08 R-squared and adjusted R-squared (Hinglish).