liveBook · Manning

Coefficient of determination

As explained in Statistics Every Programmer Needs

The coefficient of determination, commonly denoted R-squared, is a regression metric measuring the proportion of dependent-variable variance explained by a fitted model.

The coefficient of determination, R-squared (R²), measures how much of the observed variation in a numeric response a fitted linear regression explains. In simple regression, it is the square of the correlation between the predictor and response. A higher value indicates better fit to the observed data, but R² alone does not establish that a model is reliable. section 4.1.2

For the fitted regression, separate variation in the response into:

  • SST, total variation around the response mean, before accounting for predictors. section 4.2.3
  • SSR, variation accounted for by fitted values; and SSE, variation left in the residuals. section 4.2.3
  • Compute R² = SSR / SST, or equivalently R² = 1 − SSE / SST when SST = SSR + SSE. section 1.4 section 4.2.3

Interpreting and checking a fitted model

A regression predicting Stage 2 split times from Stage 1 split times for 20 runners gives R² ≈ 0.575: Stage 1 times account for about 57.5% of the observed variation in Stage 2 times. Given the fitted model and data, the calculation can be checked in Python: import numpy as np y = mds['stage2'] SST = np.sum((y - np.mean(y)) ** 2) SSR = np.sum((lm.predict(x) - np.mean(y)) ** 2) print(SSR / SST) # 0.5749303748763321 The numerator measures variation in fitted values around the response mean; the denominator measures variation in observed responses around that mean. section 4.2.3

Table 4.2 The sum of squares summary for the fitted linear regression. The stage1 and stage2 columns represent, respectively, the observed values from the two mds variables. SST equals the sum of squares total, or the total variability in the dependent variable without the consideration of any predictors. The yhat column contains the predictions for the response variable from the fitted regression. SSE equals the sum of squares error, or the portion of the SST that can’t be explained by the regression; and SSR equals the sum of squares regression, or the percentage of the variance in the dependent variable explained by the model.
x
stage1
y
stage2
( y − ȳ) 2
SST
y
yhat
( y − ŷ) 2
SSE
( ŷ − ȳ) 2
SSR
152
191
3,654.20
195.78
22.88
3098.73
162
184
4,549.50
208.87
618.36
1813.33
165
193
3,416.40
212.79
391.71
1494.46
163
191
3,654.20
210.18
367.68
1,703.62
166
193
3,416.40
214.10
445.21
1,395.02
187
292
1,644.30
241.57
2,542.75
97.53
175
235
270.60
225.87
83.27
654.10
202
226
647.70
261.20
1,238.96
95.04
179
214
1,402.50
231.11
292.68
413.80
222
274
508.50
287.36
178.62
1,289.87
184
300
2,357.10
237.65
3,887.60
190.46
193
242
89.30
249.42
55.12
4.10
255
279
759.00
330.54
2,656.24
6,255.02
202
288
1,335.90
261.20
718.30
95.04
221
269
308.00
286.06
290.92
1,197.61
207
329
6,014.00
267.74
3,752.75
265.37
227
268
273.90
293.91
671.14
1,802.54
239
315
4,038.60
309.61
29.10
3,382.11
164
201
2,545.20
211.48
109.90
1,597.33
226
345
8,751.60
292.60
2,745.97
1,693.16
49,636.95
21,099.16
28,538.24

The observation-by-observation table places total, explained, and residual squared differences side by side, making the R² calculation inspectable. section 4.2.3

Use R² alongside other evidence. Adding predictors can increase it even when they contribute little and make the model more complex; adjusted R² penalizes unnecessary predictors. Check residuals, model assumptions, and other fit and significance measures before relying on the result. A low R² calls for examining what the model fails to explain, not automatically adding variables. section 1.4.1 section 4.2.3 section 4.3

How Statistics Every Programmer Needs covers it

How deep it goes
Derives R² from explained and total sums of squares, works through a Python calculation, and cautions against judging fit from one statistic.
Examples
Uses runners’ Stage 1 and Stage 2 split times, a fitted regression line, and NumPy sums of squares yielding R² ≈ 0.575.
What it assumes
Builds on predictor and response variables and a fitted linear regression, while explaining the sums of squares used to calculate R².
What you can do after
Evaluate/understand it by calculating R² from fitted values and observed responses, interpreting explained variation, and checking whether other diagnostics support the fit.
How it connects
Links linear regression, correlation, residuals, adjusted R-squared, overfitting, and regression diagnostics.

Also explained in

Compare how 7 books explain coefficient of determination →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Statistics Every Programmer Needs. The section links point to the parts of the book it draws on.