liveBook · Manning

Goodness-of-fit measure

As explained in Statistics Every Programmer Needs

Goodness of fit is the degree to which a statistical model's predictions agree with observed data.

A goodness-of-fit measure indicates how closely a fitted model accounts for observed data. It helps evaluate a regression model, but no single number establishes that its predictions or conclusions are reliable. section 4.0 section 4.1.2

For linear regression, the line of best fit minimizes the sum of squared residuals: the squared differences between observed and predicted responses. A smaller sum means the fitted predictions are closer to the observations. R-squared (R²) expresses a different view of fit: the proportion of response variance explained by the predictors. Higher R² indicates greater explanatory fit; it does not, by itself, establish model quality. section 4.1.2 section 4.3

Interpreting a running-time regression

A regression predicting Stage 2 split times from Stage 1 split times has R² = 0.575: Stage 1 accounts for about 57.5% of the observed variation in Stage 2 times. Its explained sum of squares is 28,537.79, versus a total sum of squares of 49,636.95; dividing the former by the latter gives approximately 0.575. In Python, the calculation is `R2 = SSR / SST`. A scatter plot with observed times and the fitted line helps show what that number summarizes. 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

Compare each observed response with its prediction and mean to see how total, unexplained, and explained sums of squares contribute to R². section 4.2.3

Choose and validate measures for the model at hand:

  • For linear regression, compare R² with adjusted R² when adding predictors: R² can rise even for unhelpful variables, whereas adjusted R² penalizes unnecessary predictors. Also inspect residuals, model significance, coefficient p-values, and assumptions such as linearity, independence, homoscedasticity, and residual normality. section 4.1.2 section 4.3
  • For time-series models, compare fits to the same data using reported goodness-of-fit metrics. Log likelihood reflects how well model parameters explain observations; AIC, BIC, and HQIC bring model complexity into the comparison. Inspect residual plots and correlation plots as further checks. section 7.3.10
  • For logistic regression, a statsmodels summary reports goodness-of-fit statistics alongside coefficients and p-values. Its log-likelihood-ratio p-value tests whether the predictors collectively distinguish the classes; it is not a substitute for examining individual predictors. section 5.2.2

How Statistics Every Programmer Needs covers it

How deep it goes
Derives linear-regression R² from sums of squares and interprets model outputs, while emphasizing diagnostics beyond a single statistic.
Examples
Uses marathon stage split times, a sum-of-squares table, a fitted-line plot, and time-series residual plots.
What it assumes
Builds on basic probability and distributions, while introducing regression assumptions and goodness-of-fit measures before Python implementation.
What you can do after
Apply it via NumPy and statsmodels to interpret fitted-model output and check whether a fit statistic warrants confidence.
How it connects
Connects residuals, R-squared, adjusted R-squared, overfitting, model assumptions, and information criteria to model evaluation.

Also explained in

Compare how 2 books explain goodness-of-fit measure →

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.