The earlier chapters compared groups. Many questions in medicine are about a quantity that changes with another quantity. Blood pressure changes with age, and a baby’s weight changes with a hormone level in the mother.
11.1 The problem
Late in pregnancy, an obstetrician wants to know if the fetus is too small. A baby with growth restriction can need closer monitoring or an early delivery. You cannot put a fetus on a scale. You need a measurement from the mother that follows the growth of the fetus.
In the 1960s, one such measurement was estriol, a hormone in the mother’s urine. In pregnancy, most estriol comes from a chain of three organs: the fetal adrenal gland, the fetal liver and the placenta. A larger and healthier fetus and placenta make more estriol. Low estriol in the mother was a warning sign.
Greene and Touchstone measured urinary estriol in pregnant women and recorded the birthweight of each baby. They published the study in 1963 as “Urinary estriol as an index of placental function. A study of 279 cases” (American Journal of Obstetrics and Gynecology 85: 1–9). We use the 31 pregnancies that Bernard Rosner gives in Fundamentals of Biostatistics (Cengage). Estriol is in mg per 24 hours. Birthweight is in units of 100 g, so 25 means 2.5 kg, the usual cut-off for low birthweight.
Can a urine test before delivery tell us how heavy the baby will be?
Figure 11.1: Maternal urinary estriol and birthweight in 31 pregnancies (Greene and Touchstone, via Rosner). The dashed line marks 2.5 kg, the cut-off for low birthweight.
Mothers with more estriol tend to have heavier babies. The cloud is wide, so the relation is far from exact. The correlation is \(r\) = 0.61. A correlation shows that the two variables move together. The number of grams of birthweight for each extra mg of estriol comes from a line.
11.2 Intuitive solution
You can draw many lines through the cloud. Each line makes a prediction \(\hat y\) for each baby. The residual is the difference between the real birthweight and the prediction, \(e = y - \hat y\). A good line makes the residuals small.
Some residuals are positive and some are negative. If we add them, they cancel. So we square each residual and add the squares. This total is the residual sum of squares, RSS. A smaller RSS means a better line.
We try five slopes. Each line goes through the point of means, \((\bar x, \bar y)\) = (17.2, 32). The formal section shows that the best line always goes through this point.
Figure 11.2: Five candidate lines through the point of means (top), and the residual sum of squares (RSS) for slopes from -0.2 to 1.4 (bottom). The red segments are the residuals, and the title of each panel gives its RSS. The minimum (red point) is the least-squares slope.
A flat line (slope 0) ignores estriol and predicts the mean birthweight for every baby. Its RSS is 674. A slope of 0.6 brings the RSS down to 423. Steeper lines make the RSS larger again. The curve in the bottom panel is a parabola. Its lowest point is at a slope of 0.608, with RSS = 423.4.
This search gives the answer to the obstetrician’s question. Each extra mg/24 h of estriol goes with 61 g more birthweight. We found the line by trial, without a formula. The formal solution finds the same line in one step.
We square the residuals instead of taking their absolute size. Squares give large misses more weight than small ones. Squares also make the RSS a smooth curve, so calculus can find its minimum. If the errors follow a normal distribution, least squares also gives the most probable line, as the formal solution shows.
11.3 Formal solution
11.3.1 The model
The simple linear regression model is
\[
Y_i = \beta_0 + \beta_1 x_i + \varepsilon_i, \qquad i = 1, \dots, n.
\]
\(Y_i\) is the response, here the birthweight of baby \(i\). \(x_i\) is the predictor, here the estriol of the mother. \(\beta_0\) is the intercept, the mean of \(Y\) when \(x = 0\). \(\beta_1\) is the slope, the change in the mean of \(Y\) for each unit increase in \(x\). \(\varepsilon_i\) is the error. It holds everything else that the model does not explain, for example other causes of birthweight, measurement error and chance.
The model describes the mean of \(Y\) at each value of \(x\), \(E[Y \mid x] = \beta_0 + \beta_1 x\). The two variables do not have equal roles. We treat \(x\) as fixed and known, and we model the distribution of \(Y\) at each \(x\).
The intercept has a meaning only if \(x = 0\) is inside the range of the data. No mother in our data has zero estriol. If you want a useful intercept, centre the predictor: use \(x - \bar x\). Then \(\beta_0\) is the mean birthweight at the average estriol.
The word “regression” comes from Francis Galton, who fitted lines to the heights of parents and children (Chapter 1).
11.3.2 The assumptions: LINE
Letter
Assumption
Meaning
L
Linearity
The mean of \(Y\) is a straight line in \(x\), so \(E[\varepsilon_i] = 0\)
I
Independence
The errors of different subjects are independent
N
Normality
\(\varepsilon_i \sim N(0, \sigma^2)\)
E
Equal variance
\(\operatorname{Var}(\varepsilon_i) = \sigma^2\) at every \(x\)
Different results need different assumptions. The estimates of \(\beta_0\) and \(\beta_1\) are unbiased if L is true. The formulas for their standard errors need L, I and E. Exact p-values and confidence intervals need all four, or a large sample. Normality applies to the errors, and the model makes no assumption about the distribution of \(x\) or of \(Y\) as a whole. Chapter 13 shows how to check each assumption.
11.3.3 Least squares and the normal equations
Least squares chooses the \(b_0\) and \(b_1\) that make the RSS smallest:
The deviations from a mean add to zero: \(\sum_i (x_i - \bar x) = 0\) and \(\sum_i (y_i - \bar y) = 0\). So we can replace the factor \(x_i\) by \(x_i - \bar x\) on both sides, and the sums do not change:
The last form of the slope uses \(r = S_{xy}/\sqrt{S_{xx}S_{yy}}\) and \(s_y/s_x = \sqrt{S_{yy}/S_{xx}}\). The slope is the correlation, converted into units of \(y\) per unit of \(x\).
We must also check that this point is the only minimum. The second derivatives of the RSS are \(2n\) for \(b_0\), \(2\sum_i x_i^2\) for \(b_1\) and \(2\sum_i x_i\) for the mixed term. The determinant of this \(2 \times 2\) matrix is \(4n\sum x_i^2 - 4(\sum x_i)^2 = 4nS_{xx}\).
This determinant is positive when the \(x_i\) are not all equal, and the diagonal is positive. So the RSS is a bowl, and the single point where both slopes are zero is its lowest point.
The slope 0.6082 agrees with the grid search above. The grid had steps of 0.001, so it found 0.608.
11.3.4 The matrix form
With more than one predictor, we write the model in matrix form, \(\mathbf y = \mathbf X \boldsymbol\beta + \boldsymbol\varepsilon\). \(\mathbf X\) is an \(n \times p\) matrix. Its first column holds 1s for the intercept, and each other column holds one predictor. For a coefficient vector \(\mathbf b\), the RSS is
\[
\text{RSS}(\mathbf b) = (\mathbf y - \mathbf X\mathbf b)^\top(\mathbf y - \mathbf X\mathbf b)
= \mathbf y^\top\mathbf y - 2\,\mathbf b^\top\mathbf X^\top\mathbf y + \mathbf b^\top\mathbf X^\top\mathbf X\,\mathbf b .
\]
The middle term combines \(\mathbf y^\top\mathbf X\mathbf b\) and \(\mathbf b^\top\mathbf X^\top\mathbf y\). These are equal, because each is a single number and a number equals its own transpose. The gradient with respect to \(\mathbf b\) is
\[
\frac{\partial\,\text{RSS}}{\partial \mathbf b} = -2\,\mathbf X^\top\mathbf y + 2\,\mathbf X^\top\mathbf X\,\mathbf b .
\]
Set the gradient to zero. This gives the normal equations in matrix form, \(\mathbf X^\top\mathbf X\,\hat{\boldsymbol\beta} = \mathbf X^\top\mathbf y\). If the columns of \(\mathbf X\) are linearly independent, \(\mathbf X^\top\mathbf X\) has an inverse, and
\[
\hat{\boldsymbol\beta} = (\mathbf X^\top\mathbf X)^{-1}\mathbf X^\top\mathbf y .
\]
The matrix of second derivatives is \(2\mathbf X^\top\mathbf X\). For any non-zero vector \(\mathbf v\), \(\mathbf v^\top\mathbf X^\top\mathbf X\,\mathbf v = \lVert\mathbf X\mathbf v\rVert^2 > 0\). So this solution is the unique minimum. With \(p = 2\) and the columns \(\mathbf 1\) and \(\mathbf x\), the matrix equations are the two normal equations above.
The normal equations also have a geometric meaning. They say \(\mathbf X^\top(\mathbf y - \mathbf X\hat{\boldsymbol\beta}) = \mathbf 0\): the vector of residuals is at right angles to every column of \(\mathbf X\). The fitted values \(\hat{\mathbf y} = \mathbf X\hat{\boldsymbol\beta}\) are the projection of \(\mathbf y\) onto the space that the columns of \(\mathbf X\) span. R solves the equations, and we check that the residuals are at right angles to both columns:
t(X) %*% (y - X %*% beta_hat) # both entries are zero, up to rounding
[,1]
[1,] 1.030287e-13
[2,] 1.023182e-12
Chapter 14 uses the same equations with more columns.
11.3.5 Least squares is maximum likelihood
Adrien-Marie Legendre published least squares in 1805. Gauss gave it a basis in probability in 1809 (Chapter 1). Assume the LINE model. Then each \(y_i\) has a normal distribution with mean \(\beta_0 + \beta_1 x_i\) and variance \(\sigma^2\). The log-likelihood of the data is
The parameters \(\beta_0\) and \(\beta_1\) occur only in the sum, and the sum is the RSS. The sign in front of the sum is negative. So the values that make \(\ell\) largest are the values that make the RSS smallest. Under normal errors, the least-squares line is the maximum-likelihood line.
To estimate \(\sigma^2\), set the derivative of \(\ell\) with respect to \(\sigma^2\) to zero:
This estimate is too small on average, because the fitted line is closer to the data than the true line. We can calculate by how much. Let \(\mathbf H = \mathbf X(\mathbf X^\top\mathbf X)^{-1}\mathbf X^\top\), the matrix that projects \(\mathbf y\) onto the fitted values. The residuals are \(\mathbf e = (\mathbf I - \mathbf H)\mathbf y = (\mathbf I - \mathbf H)\boldsymbol\varepsilon\), because \((\mathbf I - \mathbf H)\mathbf X = \mathbf 0\). The matrix \(\mathbf I - \mathbf H\) is symmetric and equals its own square. So
The last step uses \(\operatorname{tr}\mathbf H = \operatorname{tr}\big((\mathbf X^\top\mathbf X)^{-1}\mathbf X^\top\mathbf X\big) = \operatorname{tr}\mathbf I_p = p\). The fit uses up one degree of freedom for each coefficient. With \(p = 2\), the unbiased estimate is \(s^2 = \text{RSS}/(n-2)\). R calls \(s\) the residual standard error.
If the errors have a different distribution, the best loss is different. Errors with a Laplace distribution lead to the sum of absolute residuals, which fits a median. Counts and yes/no outcomes lead to generalised linear models. For the mean of a continuous response, least squares is the standard.
11.3.6 Fitting the model with lm()
In R, lm() fits the model. The formula birthweight ~ estriol means “birthweight as a linear function of estriol, with an intercept”.
fit <-lm(birthweight ~ estriol, data = estriol_df)coef(fit)
(Intercept) estriol
21.5234286 0.6081905
The intercept is 21.52 and the slope is 0.608. Each extra mg/24 h of estriol goes with 61 g more birthweight, on average. The intercept is the predicted birthweight at zero estriol. This value is outside the data, so it has no clinical meaning.
predict() gives the fitted value for a new mother. A mother with 10 mg/24 h of estriol has a predicted baby of 27.6 units, or 2.76 kg.
Figure 11.3: The least-squares line for the estriol data (red). Each grey segment is a residual. The cross marks the point of means, which is always on the line.
The slope is an estimate from 31 pregnancies. A different set of 31 mothers gives a different line. Chapter 12 asks how much the line can change, and if the slope is larger than chance alone can make.
11.4 Which distance to minimise
Least squares measures each residual vertically, in units of \(y\). This choice follows from the model: all the error is in \(Y\), and \(x\) is known. Other distances give other lines.
If we regress estriol on birthweight, we measure the residuals horizontally. If we measure the perpendicular distance to the line, we get the first principal component (orthogonal regression). All three lines go through \((\bar x, \bar y)\), but their slopes differ.
Figure 11.4: Three lines through the estriol data. They differ only in the distance that they make small: vertical (regression of birthweight on estriol), perpendicular (first principal component) and horizontal (regression of estriol on birthweight).
The vertical slope is 0.61 and the horizontal one, drawn in the same plane, is 1.64. The ratio of the two slopes is \(r^2\) = 0.372. The two regression lines are the same only if \(|r| = 1\).
This asymmetry is Galton’s regression to the mean. The regression of \(y\) on \(x\) is always flatter than the orthogonal line, because it predicts each \(y\) closer to \(\bar y\). For prediction of birthweight from estriol, the vertical line is correct. It answers the obstetrician’s question, and the other two lines answer different questions.
11.5 Summary
Linear regression models the mean of \(Y\) as a straight line in \(x\): \(E[Y \mid x] = \beta_0 + \beta_1 x\).
Least squares chooses the line with the smallest sum of squared vertical residuals. The slope is \(S_{xy}/S_{xx}\), and the line goes through \((\bar x, \bar y)\).
Least squares is also maximum likelihood when the errors are independent, normal and have equal variance.
The R function lm(y ~ x) fits the line. For the estriol data, each extra mg/24 h goes with 61 g more birthweight.