The least-squares line $\hat{y} = b_0 + b_1 x$ minimizes the residual sum of squares. Its slope is $b_1 = S_{xy}/S_{xx} = r\,s_y/s_x$ and its intercept $b_0 = \bar{y} - b_1\bar{x}$, so it passes through the point of means, and with one input $R^2 = 1 - \mathrm{RSS}/\mathrm{TSS} = r^2$. For progression on BMI in 442 diabetes patients the line is $-117.77 + 10.233 \times \text{BMI}$, with $R^2 = 0.344$ and a 95% slope interval of 8.91 to 11.56 ($p \approx 3.5 \times 10^{-42}$). At a BMI of 30 it predicts 189.2: the mean progression of such patients lies in 181.7 to 196.8, and one new patient's in 66.1 to 312.3. The line itself needs no assumptions. Its standard errors assume uncorrelated errors with a common variance, and its $t$ intervals also assume normal errors.
Introduction to Regression described the relationship between body mass index (BMI) and disease progression in a diabetes study of 442 patients (Efron, Hastie, Johnstone and Tibshirani, 2004) as positive, roughly straight and moderate in strength. Simple linear regression turns that description into a formula: one input $x$, one output $y$, and a straight line
$$\hat{y} = b_0 + b_1 x,$$
where $b_0$ is the intercept, the predicted output when $x = 0$, and $b_1$ is the slope, the difference in predicted output between two cases whose inputs differ by one unit. Infinitely many lines could be drawn through the scatter plot. This article derives which one is best under the standard criterion, works the calculation by hand on five patients, then applies it to all 442 and asks how much the line explains and how precisely its slope is known.
The least-squares criterion
A line makes a prediction $\hat{y}_i = b_0 + b_1 x_i$ for each observation, and misses by the residual $e_i = y_i - \hat{y}_i$. A good line has small residuals, but there are many ways to combine $n$ residuals into one measure of size. Adding them does not work, because positive and negative residuals cancel, and many poor lines have residuals summing to zero. The standard choice adds their squares, the residual sum of squares
$$\mathrm{RSS}(b_0, b_1) = \sum_{i=1}^{n} e_i^2 = \sum_{i=1}^{n} \bigl(y_i - b_0 - b_1 x_i\bigr)^2,$$
and the least-squares line is the pair $(b_0, b_1)$ that makes RSS as small as possible (James, Witten, Hastie and Tibshirani, 2021, §3.1.1, where RSS is eq. 3.3). The same quantity is sometimes called the sum of squared errors, SSE.
Squaring has three consequences. It makes the problem easy to solve exactly, because RSS is a smooth bowl-shaped function of the two coefficients with a single lowest point, found by calculus below. It is the right criterion when the errors follow a normal distribution, in the sense that least squares then gives the maximum likelihood estimates (Gelman, Hill and Vehtari, 2020, §8.1). And it weights large residuals heavily: a residual of 6 contributes 36, four times as much as a residual of 3. The least-squares line therefore works hard to avoid large misses, which makes it sensitive to outliers, as the third of Anscombe's datasets shows. Minimizing the sum of absolute residuals instead gives a line that is more robust to outliers but has no closed-form solution; Loss Functions for Regression compares the two criteria.
The method itself is old. Legendre published it in 1805 and Gauss in 1809, and Gauss claimed to have used it earlier. That priority dispute is, in Stigler's (1981) words, the most famous in the history of statistics; Stigler argues, "though not conclusively", that Gauss probably had the method before Legendre but did not succeed in communicating it.
Deriving the line
The minimum of RSS is where both partial derivatives are zero. Differentiating with respect to the intercept,
$$\frac{\partial\,\mathrm{RSS}}{\partial b_0} = -2 \sum_{i=1}^{n} \bigl(y_i - b_0 - b_1 x_i\bigr) = 0,$$
and with respect to the slope,
$$\frac{\partial\,\mathrm{RSS}}{\partial b_1} = -2 \sum_{i=1}^{n} x_i \bigl(y_i - b_0 - b_1 x_i\bigr) = 0.$$
Dropping the factor $-2$, these are two linear equations in the two unknowns, called the normal equations:
$$\sum_{i} e_i = 0, \qquad \sum_{i} x_i\, e_i = 0.$$
The first says the residuals sum to zero. Writing it out, $\sum y_i = n b_0 + b_1 \sum x_i$, and dividing by $n$ gives
$$\bar{y} = b_0 + b_1 \bar{x}, \qquad\text{so}\qquad b_0 = \bar{y} - b_1 \bar{x},$$
where $\bar{x}$ and $\bar{y}$ are the sample means. The least-squares line always passes through the point of means $(\bar{x}, \bar{y})$. Substituting this $b_0$ into the second equation turns each residual into $e_i = (y_i - \bar{y}) - b_1 (x_i - \bar{x})$. Because the residuals sum to zero, $\sum x_i e_i = \sum (x_i - \bar{x}) e_i$, and the second equation becomes
$$\sum_{i} (x_i - \bar{x})\bigl[(y_i - \bar{y}) - b_1 (x_i - \bar{x})\bigr] = 0.$$
Solving for $b_1$:
$$b_1 = \frac{\sum_{i}(x_i - \bar{x})(y_i - \bar{y})}{\sum_{i}(x_i - \bar{x})^2} = \frac{S_{xy}}{S_{xx}},$$
where $S_{xx}$ is the sum of squared deviations of $x$ from its mean and $S_{xy}$ is the sum of products of the deviations of $x$ and $y$. This is equation 3.4 of James et al. (2021). The second derivatives are $\partial^2 \mathrm{RSS}/\partial b_0^2 = 2n$, $\partial^2 \mathrm{RSS}/\partial b_1^2 = 2\sum x_i^2$ and $\partial^2 \mathrm{RSS}/\partial b_0\,\partial b_1 = 2\sum x_i$. The matrix they form has a positive diagonal and determinant $4n\sum x_i^2 - 4(\sum x_i)^2 = 4n S_{xx}$, which is positive whenever the $x_i$ are not all equal. A symmetric 2-by-2 matrix with a positive diagonal and a positive determinant is positive definite, so RSS is a strictly convex quadratic and this stationary point is its unique minimum. If all the $x_i$ were equal, $S_{xx}$ would be zero and no slope could be estimated, which makes sense: data at a single input value say nothing about how the output changes with the input.
The two normal equations have a geometric reading worth remembering. The residuals are uncorrelated with the input: whatever the line has not explained shows no straight-line relationship with $x$. If it did, tilting the line further would reduce RSS.
Five patients by hand
The first five patients in the dataset have these BMI values and progression measures:
| Patient | BMI $x$ | Progression $y$ | $x - \bar{x}$ | $y - \bar{y}$ | $(x - \bar{x})(y - \bar{y})$ | $(x - \bar{x})^2$ |
|---|---|---|---|---|---|---|
| 1 | 32.1 | 151 | 5.6 | 9.4 | 52.64 | 31.36 |
| 2 | 21.6 | 75 | −4.9 | −66.6 | 326.34 | 24.01 |
| 3 | 30.5 | 141 | 4.0 | −0.6 | −2.40 | 16.00 |
| 4 | 25.3 | 206 | −1.2 | 64.4 | −77.28 | 1.44 |
| 5 | 23.0 | 135 | −3.5 | −6.6 | 23.10 | 12.25 |
| Sum | 132.5 | 708 | 0 | 0 | 322.40 | 85.06 |
The means are $\bar{x} = 132.5/5 = 26.50$ and $\bar{y} = 708/5 = 141.60$. The slope and intercept are
$$b_1 = \frac{322.40}{85.06} = 3.7903, \qquad b_0 = 141.60 - 3.7903 \times 26.50 = 41.158.$$
The fitted values and residuals:
| Patient | Fitted $\hat{y}$ | Residual $e$ | $e^2$ |
|---|---|---|---|
| 1 | 162.83 | −11.83 | 139.8 |
| 2 | 123.03 | −48.03 | 2,306.7 |
| 3 | 156.76 | −15.76 | 248.4 |
| 4 | 137.05 | 68.95 | 4,753.9 |
| 5 | 128.33 | 6.67 | 44.4 |
The residuals sum to zero and so does the sum of each residual times its BMI, to rounding, as the normal equations require. RSS is 7,493.22. Values are rounded in the tables; the squares are computed from unrounded residuals.
Five patients make a noisy sample. Patient 4 has a near-average BMI but the highest progression, and the line, which must keep that large residual in check, tilts less than it would without that patient. The slope of 3.79 from five patients is far from the 10.23 found from all 442 below. Estimates from small samples vary. Worked problem 1 measures how much: the five-patient slope has a standard error of 5.42, and its 95% interval runs from $-13.46$ to $21.04$.
All 442 patients
The same formulas applied to the full study give $\bar{x} = 26.376$, $\bar{y} = 152.133$, and
$$\widehat{\text{progression}} = -117.773 + 10.2331 \times \text{BMI}.$$
The slope says that patients whose BMI differs by one unit differ in predicted progression by 10.23 units. The careful reading is a comparison between patients, not a statement about what would happen to a patient whose BMI changed: the data are observational, and patients with higher BMI may differ in other ways too (Gelman, Hill and Vehtari, 2020, §10.2). Multiple Linear Regression shows that the BMI slope falls once blood pressure and a serum measurement are taken into account.
The intercept of $-117.77$ is the predicted progression at a BMI of zero, which is physically impossible and far below the smallest BMI in the data, 18.0. The intercept is needed to place the line, but here it has no meaning of its own: interpreting it is an extrapolation, in the sense of Introduction to Regression. Centering the input, regressing on $x - \bar{x}$ instead of $x$, would make the intercept equal to $\bar{y}$, the predicted value for a patient of average BMI, without changing the slope.
The bowl in the figure is the least-squares criterion seen directly. Lines through the means with slopes of 8 or 12 have RSS of 1,762,510 and 1,746,455, both above the minimum of 1,719,582.
Correlation and the slope
The correlation coefficient measures the direction and strength of a straight-line relationship without regard to units. With sample standard deviations $s_x = \sqrt{S_{xx}/(n-1)}$ and $s_y = \sqrt{S_{yy}/(n-1)}$, where $S_{yy} = \sum (y_i - \bar{y})^2$,
$$r = \frac{1}{n - 1} \sum_{i=1}^{n} \left(\frac{x_i - \bar{x}}{s_x}\right)\left(\frac{y_i - \bar{y}}{s_y}\right) = \frac{S_{xy}}{\sqrt{S_{xx}\, S_{yy}}}.$$
Each term is the product of the two variables' z-scores, their distances from the mean in units of standard deviation. Points in the upper-right and lower-left quadrants around the means contribute positive products, points in the other two quadrants negative ones, and $r$ is their average, which by the Cauchy-Schwarz inequality lies between $-1$ and $+1$. It equals $\pm 1$ only when all points lie exactly on a line. A value near zero means no straight-line relationship, which is not the same as no relationship: points on a symmetric parabola can have $r = 0$ exactly. For $x = -1, 0, 1$ and $y = x^2$, the deviations of $x$ are $-1, 0, 1$ and those of $y$ are $1/3, -2/3, 1/3$, so $S_{xy} = -1/3 + 0 + 1/3 = 0$. Correlation: Pearson, Spearman and Kendall proves these bounds and adds two coefficients built on ranks.
Comparing the formula for $r$ with $b_1 = S_{xy}/S_{xx}$ shows that
$$b_1 = r \,\frac{s_y}{s_x}.$$
The slope is the correlation rescaled from standard-deviation units into the units of the data. For the diabetes data, $r = 0.5865$, $s_x = 4.4181$ and $s_y = 77.0930$, and $0.5865 \times 77.0930 / 4.4181 = 10.233$, the slope found above. Expectation, Variance and Covariance proves the same formula for two random variables, as the best linear predictor of one from the other (its Theorem 8).
Regression to the mean
Rewriting the line with $b_0 = \bar{y} - b_1 \bar{x}$ and $b_1 = r\, s_y/s_x$ gives
$$\frac{\hat{y} - \bar{y}}{s_y} = r\,\frac{x - \bar{x}}{s_x}.$$
In standardized units, the prediction is $r$ times the input. A patient one standard deviation above the mean BMI (a BMI of 30.79) is predicted to be $r = 0.586$ standard deviations above the mean progression, a prediction of 197.3, not a full standard deviation. Because $|r| < 1$ unless the fit is perfect, predictions are always closer to the mean, in standardized units, than the inputs they come from. This is Galton's regression toward mediocrity, described in Introduction to Regression. It requires no causal mechanism, only imperfect correlation. The same reasoning explains why a patient selected for an extreme first measurement will tend to be less extreme on the second, even with no treatment at all.
The asymmetry also means the regression of $y$ on $x$ is a different line from the regression of $x$ on $y$. Predicting BMI from progression gives $\widehat{\text{BMI}} = 21.263 + 0.03361 \times \text{progression}$; drawn in the same axes, with progression vertical, that line has slope $1/0.03361 = 29.75$, much steeper than 10.23. Each line minimizes squared errors in its own output. Drawn in the same axes, the first has slope $r\,s_y/s_x$ and the second $s_y/(r\,s_x)$. For $r \ne 0$ these are equal exactly when $r^2 = 1$, and both lines pass through the point of means, so the lines coincide only when $r = \pm 1$. When $r = 0$, the regression of $x$ on $y$ is the vertical line $x = \bar{x}$, perpendicular to the horizontal regression of $y$ on $x$. Worked problem 3 shows that the product of the two fitted slopes, $10.2331 \times 0.03361$, is $r^2 = 0.3439$.
How much the line explains
The total sum of squares, $\mathrm{TSS} = \sum (y_i - \bar{y})^2$, is the RSS of the horizontal line at the mean, the model that ignores the input. It splits into two parts. Writing $y_i - \bar{y} = (\hat{y}_i - \bar{y}) + e_i$ and squaring,
$$\sum_i (y_i - \bar{y})^2 = \sum_i (\hat{y}_i - \bar{y})^2 + \sum_i e_i^2 + 2 \sum_i (\hat{y}_i - \bar{y})\, e_i.$$
The cross term is zero: $\hat{y}_i - \bar{y} = b_1 (x_i - \bar{x})$, and $\sum (x_i - \bar{x}) e_i = 0$ by the normal equations. So
$$\mathrm{TSS} = \mathrm{ESS} + \mathrm{RSS},$$
where $\mathrm{ESS} = \sum (\hat{y}_i - \bar{y})^2$ is the explained sum of squares, the variation of the fitted values around the mean. The coefficient of determination is the explained share,
$$R^2 = \frac{\mathrm{ESS}}{\mathrm{TSS}} = 1 - \frac{\mathrm{RSS}}{\mathrm{TSS}}$$
(James et al., 2021, eq. 3.17). For the diabetes data, TSS is 2,621,009, ESS is 901,427 and RSS is 1,719,582, which add up as they should, and $R^2 = 0.3439$: BMI accounts for about 34% of the variation in progression.
For simple linear regression, $R^2$ is the square of the correlation. Substituting $\hat{y}_i - \bar{y} = b_1(x_i - \bar{x})$ gives $\mathrm{ESS} = b_1^2 S_{xx} = S_{xy}^2 / S_{xx}$, so
$$R^2 = \frac{S_{xy}^2}{S_{xx}\, S_{yy}} = r^2.$$
Here $0.5865^2 = 0.3439$, matching. The identity holds only with one input; with several, $R^2$ is the squared correlation between $y$ and the fitted values $\hat{y}$ (James et al., 2021, §3.2.2), which The Geometry of Least Squares proves.
Two cautions apply. First, $R^2$ lies between 0 and 1 for a least-squares fit with an intercept, evaluated on the data it was fitted to. Evaluated on new data, $1 - \mathrm{RSS}/\mathrm{TSS}$ can be negative when a model predicts worse than the new data's mean, and Multiple Linear Regression shows an example. Second, a low $R^2$ does not mean the slope is unimportant or poorly estimated. BMI explains a third of the variation in progression, and yet, as the next section shows, its slope is known quite precisely. What $R^2$ measures is how well individual outcomes can be predicted, and individuals vary for many reasons besides BMI.
How precisely the slope is known
The five-patient slope of 3.79 and the full-study slope of 10.23 differ because each sample gives a different estimate. To say how far an estimate might be from the truth, regression needs a model of how the data arose. The standard one is
$$Y = \beta_0 + \beta_1 X + \varepsilon,$$
with true but unknown coefficients $\beta_0$ and $\beta_1$, and errors $\varepsilon$ that have mean zero, a common variance $\sigma^2$, and are uncorrelated with each other (James et al., 2021, §3.1.2). Treat the inputs as fixed. The slope is a weighted sum of the outcomes, $b_1 = \sum_i w_i y_i$ with $w_i = (x_i - \bar{x})/S_{xx}$, because $\sum_i (x_i - \bar{x})\,\bar{y} = 0$. The weights satisfy $\sum_i w_i = 0$ and $\sum_i w_i x_i = 1$, so $E[b_1] = \sum_i w_i(\beta_0 + \beta_1 x_i) = \beta_1$: the slope is unbiased. Uncorrelated errors with a common variance give $\mathrm{Var}(b_1) = \sigma^2 \sum_i w_i^2 = \sigma^2 S_{xx}/S_{xx}^2 = \sigma^2/S_{xx}$. Theorem 1 of The Gauss–Markov Theorem proves both moments for the intercept too, in matrix form and for any number of inputs. The standard errors are
$$\mathrm{SE}(b_1) = \frac{\sigma}{\sqrt{S_{xx}}}, \qquad \mathrm{SE}(b_0) = \sigma \sqrt{\frac{1}{n} + \frac{\bar{x}^2}{S_{xx}}}$$
(James et al., 2021, eq. 3.8). No other unbiased estimator that is linear in the outcomes has a smaller variance, by the Gauss–Markov theorem (Theorem 2 of the same article). The slope is known more precisely when the errors are small, when there are more observations, and when the inputs are spread out: a line through points spread over a wide range is harder to tilt than one through a narrow cluster.
The error standard deviation $\sigma$ is estimated by the residual standard error, $\mathrm{RSE} = \sqrt{\mathrm{RSS}/(n-2)}$. The divisor is $n - 2$ because two coefficients were estimated: under the model the expected RSS is exactly $(n - 2)\sigma^2$, so $\mathrm{RSS}/(n - 2)$ is an unbiased estimate of $\sigma^2$ (Theorem 1 of the Gauss–Markov article, with two coefficients). For the diabetes data, RSE is 62.52 on 440 degrees of freedom: a typical patient's progression is about 63 units from the line. Then
$$\mathrm{SE}(b_1) = \frac{62.52}{\sqrt{S_{xx}}} = 0.6738.$$
If the errors are also normally distributed, $(b_1 - \beta_1)/\mathrm{SE}(b_1)$ has a $t$ distribution with $n - 2$ degrees of freedom (The Normal Linear Model proves it, for any number of inputs), and a 95% confidence interval for the slope is $b_1 \pm t_{0.975,\,n-2}\,\mathrm{SE}(b_1)$. With $t_{0.975,\,440} = 1.9654$, the interval is $[8.909, 11.557]$. To test whether BMI is related to progression at all, the null hypothesis $\beta_1 = 0$ gives $t = 10.2331/0.6738 = 15.19$, and a $p$-value of about $3.5 \times 10^{-42}$ (eq. 3.14 of James et al., 2021). The same calculations appear for comparing retrieval systems in Comparing Systems with Significance Tests. The evidence that the slope is positive is overwhelming, even though $R^2$ is only 0.34.
Predicting the mean and predicting a patient
The line can answer two different questions about patients with a BMI of 30. The predicted value is $-117.773 + 10.2331 \times 30 = 189.2$ either way, but the uncertainty differs.
- The average progression of all such patients. Its uncertainty comes only from not knowing the true line. The standard error is $\mathrm{RSE}\sqrt{1/n + (x_0 - \bar{x})^2/S_{xx}}$, and the 95% confidence interval is $[181.7, 196.8]$.
- The progression of one new patient. Now the patient's own error $\varepsilon$ adds to the uncertainty about the line, and the standard error is $\mathrm{RSE}\sqrt{1 + 1/n + (x_0 - \bar{x})^2/S_{xx}}$. The 95% prediction interval is $[66.1, 312.3]$.
Theorem 3 of The Normal Linear Model proves both intervals for normal errors. The quantity $1/n + (x_0 - \bar{x})^2/S_{xx}$ under both square roots is the leverage of $x_0$, which Linear Regression Assumptions puts to work.
At a BMI of 30 the prediction interval is 246.2 units wide and the confidence interval only 15.1 units, because individual patients scatter far more than the line is uncertain. As a rough check of the model, 426 of the 442 patients (96.4%) lie within their own 95% prediction intervals. Both intervals widen as $x_0$ moves away from $\bar{x}$, and both depend on assumptions about the errors that Linear Regression Assumptions examines. The least-squares line itself requires none of them: it is the line with the smallest RSS whatever the errors are like. The assumptions matter for the standard errors, intervals and tests.
When a straight line is appropriate
A practical order of work:
- Plot the data. If the points follow a curve, a straight line will be wrong in a systematic way, whatever the value of $r$. Multiple Linear Regression shows how to fit curves with the same machinery.
- Compute $r$ and fit the line. A correlation near zero with a straight form means the input says little about the output; the line is valid but not useful for prediction.
- Plot the residuals against the fitted values. They should scatter evenly around zero with no curve or funnel.
- Report the slope with its interval, and $R^2$, and say over what range of inputs the line was fitted.
Worked problems
The problems extend the article on its own data. Every number is recomputed, with assertions, in the review-pass block of tools/experiments/data-science/simple-linear-regression.py.
Problem 1 (how much five patients say). For the five-patient line, compute the standard error of the slope and its 95% interval, and find which patient the slope depends on most.
Solution. The five-patient RSS is 7,493.22 on $5 - 2 = 3$ degrees of freedom, so $\mathrm{RSE} = \sqrt{7{,}493.22/3} = 49.98$ and $\mathrm{SE}(b_1) = 49.98/\sqrt{85.06} = 5.419$. With $t_{0.975,\,3} = 3.182$, the interval is $3.79 \pm 3.182 \times 5.419$, from $-13.46$ to $21.04$. It contains the full-study slope, 10.23, and it also contains zero: $t = 0.70$, $p = 0.53$. Five patients cannot even settle the sign of the relationship. To find the patient that matters most, refit without each patient in turn. The slopes are 5.60, $-1.55$, 5.00, 5.03 and 4.21 without patients 1 to 5. Removing patient 4, the large positive residual in the figure, steepens the line from 3.79 to 5.03, as the figure's caption says. Removing patient 2 reverses the sign. Patient 2 combines a large residual ($-48.03$) with high leverage, $1/5 + 4.9^2/85.06 = 0.482$; only patient 1, at 0.569, has more, and its residual is small. Influence needs both. Leverage, Influence and Residual Diagnostics measures it with Cook's distance and computes every deletion without refitting.
Problem 2 (the parabola in the RSS figure). Show that, among lines through the point of means, RSS is a parabola in the slope with curvature $S_{xx}$, and check the values quoted for slopes 8 and 12.
Solution. A line through the means predicts $\bar{y} + b(x_i - \bar{x})$. Its RSS is $\sum_i [(y_i - \bar{y}) - b(x_i - \bar{x})]^2 = S_{yy} - 2bS_{xy} + b^2 S_{xx}$. Completing the square with $b_1 = S_{xy}/S_{xx}$ gives
$$\mathrm{RSS}(b) = \bigl(S_{yy} - S_{xy}^2/S_{xx}\bigr) + S_{xx}(b - b_1)^2 = \mathrm{RSS}_{\min} + S_{xx}(b - b_1)^2.$$
Restricting to lines through the means loses nothing, because for any fixed slope the first normal equation still makes $\bar{y} - b\bar{x}$ the best intercept. With $S_{xx} = 8{,}608.2$, a slope of 8 adds $8{,}608.2 \times 2.2331^2 = 42{,}928$ to the minimum 1,719,582, giving 1,762,510, and a slope of 12 adds 26,873, giving 1,746,455. At $b = 0$ the formula returns $\mathrm{RSS}_{\min} + S_{xx}b_1^2 = \mathrm{RSS} + \mathrm{ESS} = \mathrm{TSS}$, the horizontal line at the mean, as the caption says. A larger $S_{xx}$ makes the bowl steeper, which is the same fact as the slope's smaller standard error, $\sigma/\sqrt{S_{xx}}$.
Problem 3 (the two regression lines). Show that the slope of $y$ on $x$ times the slope of $x$ on $y$ equals $r^2$.
Solution. The two slopes are $S_{xy}/S_{xx}$ and $S_{xy}/S_{yy}$, and their product is $S_{xy}^2/(S_{xx}S_{yy}) = r^2$. For the diabetes data, $10.2331 \times 0.03361 = 0.3439 = 0.5865^2$. The product is below 1 unless the fit is perfect, which is the algebraic form of the statement that each line is flatter than the SD line in its own direction of prediction.
Problem 4 (regression to the mean in the data). Take the tenth of the patients with the highest BMI. How far above average are they in BMI, in predicted progression and in observed progression, in standard-deviation units?
Solution. The top tenth (BMI at or above the 90th percentile) is 45 patients with mean BMI 34.87, which is 1.922 standard deviations above the mean. The line predicts their mean progression to be 239.0, which is $0.5865 \times 1.922 = 1.127$ standard deviations above the mean, by the standardized form of the line. (The mean of fitted values is the fitted value at the mean input, because the line is linear.) Their observed mean progression is 237.4, 1.107 standard deviations above. Patients selected for extreme BMI are about half as extreme in progression, and the line anticipated almost exactly how much.
Open questions
- How much does measurement error in BMI flatten the slope? BMI measured once carries error. Classical measurement error in an input, error independent of the true value and of the outcome's error, shrinks a least-squares slope toward zero by the reliability ratio, the share of the measured input's variance that is true variance (Carroll, Ruppert, Stefanski and Crainiceanu, 2006, ch. 3). Repeated BMI measurements on the same patients, which these data do not include, would give the reliability needed to correct the slope.
- Does the $t$ interval cover at $n = 5$ in this population? Treating the 442 patients as a population with slope 10.23, drawing many samples of five under 30 seeds, and recording how often the 95% interval of Problem 1 contains 10.23 (median and interquartile range over seeds) would show whether the interval is trustworthy when the residuals are not normal and the sample is tiny.
- Is one prediction interval right for every BMI? The residual spread is narrower for low fitted values (Linear Regression Assumptions). Coverage of the prediction interval within each third of BMI, and with a model in which the spread depends on the fitted value, would show whether the overall 96.4% hides over-coverage at one end and under-coverage at the other.
Where it leads
This article fitted the line and stated what its standard errors and intervals mean when the errors behave. Linear Regression Assumptions states that model's assumptions, checks each one on these patients, and measures by simulation how far the intervals go wrong when one fails. The Statistics collection proves the theory in general: The Geometry of Least Squares for the line as a projection, The Gauss–Markov Theorem for its optimality, and The Normal Linear Model for the $t$ intervals.
In Python
scikit-learn fits the line; scipy.stats.linregress also returns $r$, the standard error of the slope and its $p$-value. (Checked against scikit-learn 1.5.2 and SciPy 1.17.1, which produced the numbers here, and the scikit-learn 1.9.1 documentation, November 2026.)
from scipy import stats
from sklearn.datasets import load_diabetes
from sklearn.linear_model import LinearRegression
data = load_diabetes(scaled=False)
bmi, y = data.data[:, 2], data.target
model = LinearRegression().fit(bmi.reshape(-1, 1), y)
print(model.intercept_, model.coef_[0]) # -117.77, 10.233
print(model.score(bmi.reshape(-1, 1), y)) # R^2 = 0.3439
fit = stats.linregress(bmi, y)
print(fit.rvalue, fit.stderr, fit.pvalue) # 0.5865, 0.6738, about 3.5e-42
model.score returns $R^2$ on whatever data it is given, which is the training data here. The numbers in this article, including the intervals, are computed by tools/experiments/data-science/simple-linear-regression.py in the site's repository.
References
- Carroll, R. J., Ruppert, D., Stefanski, L. A., and Crainiceanu, C. M. (2006). Measurement Error in Nonlinear Models: A Modern Perspective (2nd ed.). Chapman and Hall/CRC. record
- Efron, B., Hastie, T., Johnstone, I., and Tibshirani, R. (2004). Least angle regression. The Annals of Statistics, 32(2), 407–499. record · 21 pages
- Gelman, A., Hill, J., and Vehtari, A. (2020). Regression and Other Stories. Cambridge University Press. record · 7 pages
- James, G., Witten, D., Hastie, T., and Tibshirani, R. (2021). An Introduction to Statistical Learning with Applications in R (2nd ed.). Springer. record · 39 pages
- Stigler, S. M. (1981). Gauss and the invention of least squares. The Annals of Statistics, 9(3), 465–474. record
Reviewed: verified, 23 Nov 2026 · citations checked, 23 Nov 2026 · editorial review, 23 Nov 2026 · 7 revisions