- Fit a least-squares regression line, and interpret the slope, intercept, and residuals in context.
- Compute and interpret \(r^2\) (proportion of variation explained) and the standard error of the residuals \(s_e\).
- Distinguish outliers, high-leverage points, and influential points, and explain their effect on the fitted line.
- Explain why extrapolation is dangerous and why regression alone does not establish causation.
- Test hypotheses and construct confidence intervals for the slope with the t distribution.
- Construct and interpret a confidence interval for a mean response and a prediction interval for an individual response.
- Check the regression conditions with residual plots and normal probability plots, and use transformations when they fail.
In Chapter 4 you described relationships with scatterplots and correlation. Regression goes further: it fits a model that lets us predict a response from an explanatory variable and, with the inference tools of Chapters 9 and 10, say how precisely we know the relationship. This chapter treats simple linear regression: one quantitative explanatory variable \(x\) and one quantitative response \(y\). Regression with several explanatory variables is previewed in Chapter 15.
The linear model
The response variable \(y\) is the outcome we want to explain or predict; the explanatory variable \(x\) is the variable we use to do so. (The choice matters: regressing \(y\) on \(x\) is not the same as regressing \(x\) on \(y\).) The population model is
\[y = \beta_0 + \beta_1 x + \varepsilon,\]where \(\beta_0\) and \(\beta_1\) are unknown parameters (the population intercept and slope) and \(\varepsilon\) is a random error with mean 0 and standard deviation \(\sigma\), the same at every \(x\). The line \(\beta_0 + \beta_1 x\) gives the mean of \(y\) at each \(x\); individual observations scatter around it. From a sample we compute estimates \(b_0\) and \(b_1\) and the fitted line (regression line)
\[\hat{y} = b_0 + b_1 x.\]For each observation the residual is the vertical distance from the point to the line:
\[e_i = y_i - \hat{y}_i = \text{observed} - \text{predicted}.\]Positive residuals are points above the line (the model under-predicted), negative residuals are points below.
The least-squares line
Many lines could be drawn through a cloud of points. The least-squares regression line is the one that makes the sum of the squared residuals as small as possible:
\[\text{choose } b_0, b_1 \text{ to minimize } \sum_{i=1}^n e_i^2 = \sum_{i=1}^n (y_i - b_0 - b_1 x_i)^2.\]Squaring (rather than using absolute values) treats over- and under-prediction alike, penalizes large misses heavily, and has a clean closed-form solution.
Two facts follow. (1) The line always passes through the point of means \((\bar{x}, \bar{y})\). (2) The residuals always sum to zero, \(\sum e_i = 0\). Here \(r\) is the correlation of Chapter 4, and \(s_x, s_y\) are the sample standard deviations.
Interpreting the coefficients
- Slope \(b_1\): the predicted (average) change in \(y\) for each one-unit increase in \(x\). Always state it in context and with units, and phrase it as an association: "each additional hour of study is associated with a predicted 3.95 point higher score."
- Intercept \(b_0\): the predicted value of \(y\) when \(x = 0\). It is meaningful only if \(x = 0\) is a sensible value within the range of the data; otherwise it is just the number that positions the line.
Notice the role of \(r\) in the slope: \(b_1 = r\,s_y/s_x\). A one-standard-deviation increase in \(x\) is associated with an \(r\)-standard-deviation increase in the predicted \(y\). Because \(|r| \le 1\), the predicted \(y\) is always closer to its mean, in standard-deviation units, than \(x\) is to its own mean. This is regression to the mean, the phenomenon Sir Francis Galton observed when tall parents tended to have children who were tall but less extremely so, and which gave regression its name.
Example 14.1: Hours studied and exam score (synthetic data)
Eight students report their weekly study hours \(x\) and their exam score \(y\):
| Hours \(x\) | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
|---|---|---|---|---|---|---|---|---|
| Score \(y\) | 55 | 62 | 60 | 70 | 72 | 75 | 84 | 80 |
The means are \(\bar{x} = 5.5\) and \(\bar{y} = 69.75\). The sums of squares and products are \(S_{xx} = \sum(x - \bar{x})^2 = 42\), \(S_{xy} = \sum (x - \bar{x})(y - \bar{y}) = 166\) and \(S_{yy} = \sum (y - \bar{y})^2 = 713.5\). Hence
\[b_1 = \frac{166}{42} = 3.952, \qquad b_0 = 69.75 - 3.952(5.5) = 48.012, \qquad \hat{y} = 48.01 + 3.952\,x, \qquad r = \frac{166}{\sqrt{42 \times 713.5}} = 0.959.\]Interpretation. Each additional weekly hour of study is associated with a predicted exam score 3.95 points higher. A student who does not study at all would be predicted to score 48.0, but \(x = 0\) lies outside the observed range 2 to 9, so this intercept is an extrapolation.
Prediction and residual. For a student who studies 7 hours, \(\hat{y} = 48.012 + 3.952(7) = 75.68\). The actual score was 75, so the residual is \(e = 75 - 75.68 = -0.68\). The residuals of all eight students are \(-0.92, 2.13, -3.82, 2.23, 0.27, -0.68, 4.37, -3.58\), which sum to 0 (up to rounding).
Experiment yourself: the lab below lets you add and drag points, see the least-squares line and its squares of residuals, and compare it with a line of your own. Try to beat least squares on the sum of squared residuals. You cannot: it is the unique minimizer.
How well does the line fit?
The variation explained: \(r^2\)
Without knowing \(x\), the best prediction of \(y\) is \(\bar{y}\), and the total variation is \(\text{SST} = \sum (y_i - \bar{y})^2\). Using the regression line, the leftover unexplained variation is the sum of squared residuals \(\text{SSE} = \sum (y_i - \hat{y}_i)^2\). The difference is the variation explained by the line, \(\text{SSR} = \sum (\hat{y}_i - \bar{y})^2\). Exactly as in ANOVA (Chapter 13),
\[\text{SST} = \text{SSR} + \text{SSE}, \qquad r^2 = \frac{\text{SSR}}{\text{SST}} = 1 - \frac{\text{SSE}}{\text{SST}}.\]\(r^2\) (the "coefficient of determination", or R-squared) is the proportion of the variation in \(y\) that is explained by its linear relationship with \(x\). It equals the square of the correlation \(r\), and lies between 0 and 1. In Example 14.1, \(\text{SSE} = 57.40\) and \(\text{SST} = 713.5\), so \(r^2 = 1 - 57.40/713.5 = 0.920\): the linear relationship with study time accounts for about 92 percent of the variation in scores.
The typical size of a residual: \(s_e\)
The standard deviation of the residuals measures how far observations typically fall from the line, in the units of \(y\):
\[s_e = \sqrt{\frac{\text{SSE}}{n - 2}}.\]The divisor \(n - 2\) reflects the two parameters \((b_0, b_1)\) estimated from the data (Chapter 13's MSE is \(s_e^2\)). In Example 14.1, \(s_e = \sqrt{57.40/6} = 3.09\) points: actual scores typically differ from the fitted line by about 3 points.
A high \(r^2\) does not prove the relationship is linear, that the line is appropriate, or that \(x\) causes \(y\). A low \(r^2\) does not mean the slope is useless: a small but real slope can be very significant. Anscombe's quartet (Section 14.8) shows four utterly different data sets that share the same line and the same \(r^2\). Always look at the scatterplot and the residual plot.
Outliers, leverage, and influence
Unusual points can affect a regression in different ways, and it is useful to keep three ideas apart.
- An outlier in the regression sense is a point with a large residual: it lies far above or below the line, in the \(y\) direction.
- A point has high leverage if its \(x\) value is far from \(\bar{x}\) (an unusual value of the explanatory variable). Leverage is measured by \(h_i = \dfrac{1}{n} + \dfrac{(x_i - \bar{x})^2}{S_{xx}}\), which lies between \(1/n\) and 1; values above about \(4/n\) (twice the average of 2/n) are considered high. A point with high leverage has the potential to pull the line toward itself.
- A point is influential if removing it would change the fitted line substantially (the slope, the intercept, or the predictions). Influence combines leverage and residual size; a common measure is Cook's distance, \(D_i = \dfrac{e_i^2}{2 s_e^2}\cdot\dfrac{h_i}{(1 - h_i)^2}\).
Example 14.2: Two different unusual points (continuing Example 14.1)
A vertical outlier at the center. Add a ninth student who studied 5.5 hours (exactly the mean) and scored 90. This point has a large residual (about +20 points above the line) but its leverage is only \(1/9\): it sits at \(\bar{x}\), so it cannot tilt the line. The slope stays at 3.952; only the intercept rises (to 50.26). \(r^2\) drops from 0.92 to 0.61 because the scatter around the line has grown.
A high-leverage point. Instead add a student who studied 16 hours and scored 60. Its \(x\) is far from the others (\(h = 0.733\), far above \(4/9 = 0.44\)), and it lies well below the trend, so it drags the line toward itself: the slope collapses from 3.952 to 0.536, the intercept becomes 65.1, and \(r^2\) falls from 0.92 to 0.05. One point out of nine has wiped out an apparently very strong relationship. This point is both high-leverage and influential.
What to do. Never delete points just because they are inconvenient. Investigate: is it a data-entry error, a different kind of subject, or genuine? Report the regression with and without the unusual point and say how much the conclusions change.
Extrapolation, causation, and other cautions
- Extrapolation. Predicting for \(x\) values outside the range of the data is extrapolation. The fitted line describes the data you have; nothing guarantees the pattern continues. A line fitted to child heights from ages 3 to 10 would predict absurd heights at age 40. Interpolate; be very cautious about extrapolating.
- Association is not causation. Regression measures association. A strong linear relationship between ice-cream sales and drowning deaths does not mean one causes the other (both rise in summer, a lurking variable). Only a randomized experiment supports causal claims (Chapter 2).
- Averages hide variation. A regression of group means will show a much stronger relationship than one of the underlying individuals. The \(r^2\) for means is not comparable to the \(r^2\) for individuals.
- Regression to the mean. Extreme values on one measurement tend to be less extreme on the next. Students who did best on a first test usually score a bit lower on the second, and those who did worst score a bit higher, purely because part of any score is luck. Do not mistake this for an effect of a treatment.
- Do not reverse the roles. The line for predicting \(y\) from \(x\) is generally different from the line for predicting \(x\) from \(y\).
Inference for the slope
A sample line is a noisy estimate of the population line. Does the sample slope \(b_1\) provide convincing evidence that the population slope \(\beta_1\) is not zero (that \(x\) is truly related to the mean of \(y\))? The sampling distribution of \(b_1\) is centered at \(\beta_1\), and its standard error is estimated by
\[\text{SE}(b_1) = \frac{s_e}{\sqrt{S_{xx}}} = \frac{s_e}{s_x\sqrt{n - 1}}.\]The standard error is small when the residual scatter \(s_e\) is small, when the sample is large, and when the \(x\) values are spread widely (large \(S_{xx}\)): a wide range of \(x\) gives a more stable slope.
Conditions (Section 14.8): linear relationship, independent observations, approximately normal residuals, constant spread.
Test \(H_0\!: \beta_1 = 0\) (no linear relationship) versus \(H_a\!: \beta_1 \ne 0\):
\[t = \frac{b_1 - 0}{\text{SE}(b_1)}, \qquad \text{df} = n - 2.\]Confidence interval for \(\beta_1\): \(b_1 \pm t^*_{n-2}\,\text{SE}(b_1)\).
The same test can be phrased in terms of the correlation: \(t = r\sqrt{n-2}/\sqrt{1 - r^2}\), so testing \(\beta_1 = 0\) and testing \(\rho = 0\) are the same test. In simple regression the ANOVA F statistic of the regression equals \(t^2\).
Example 14.3: Is study time really related to score? (continuing Example 14.1)
\(\text{SE}(b_1) = 3.093/\sqrt{42} = 0.4773\). Test \(H_0\!: \beta_1 = 0\): \(t = 3.952/0.4773 = 8.28\) with \(\text{df} = 6\), giving a two-sided p-value \(0.00017\). There is very strong evidence of a linear relationship between study time and score in the population these students represent.
A 95 percent interval uses \(t^*_6 = 2.447\): \(3.952 \pm 2.447(0.4773) = (2.78,\ 5.12)\). We are 95 percent confident that each extra weekly hour is associated with a mean score between 2.8 and 5.1 points higher.
The ANOVA decomposition tells the same story: \(\text{SSR} = 656.10\), \(\text{SSE} = 57.40\), \(\text{MSE} = 57.40/6 = 9.567\), \(F = 656.10/9.567 = 68.58 = 8.28^2\), with the same p-value. (Study time is self-reported and the students were not randomly assigned, so even this strong evidence supports association, not causation.)
Example 14.4: Speed and stopping distance (classic data)
The classic cars data set (built into R) records the speed \(x\) (mph) and stopping distance \(y\) (feet) of 50 cars, from data collected in the 1920s. The least-squares line is
\[\widehat{\text{distance}} = -17.579 + 3.932\,(\text{speed}), \qquad r = 0.807,\ r^2 = 0.651,\ s_e = 15.38\text{ ft}.\]Here \(\bar{x} = 15.4\) and \(S_{xx} = 1370\), so \(\text{SE}(b_1) = 15.38/\sqrt{1370} = 0.4155\) and \(t = 3.932/0.4155 = 9.46\) with 48 degrees of freedom: \(p = 1.5 \times 10^{-12}\). A 95 percent interval for the slope is \(3.932 \pm 2.011(0.4155) = (3.10,\ 4.77)\): each additional mph of speed is associated with 3.1 to 4.8 more feet of stopping distance on average. The negative intercept (-17.6 feet at speed 0) is nonsense, a reminder that the intercept can be a pure extrapolation.
About 65 percent of the variation in stopping distance is explained by speed. (The rest reflects driver, brakes and road; stopping distance physically grows roughly with the square of speed, and Section 14.9 shows how a transformation improves the fit.)
Predicting: confidence and prediction intervals
Suppose we want to say something about \(y\) at a particular value \(x_0\). There are two different questions, and they have different answers.
- Confidence interval for the mean response: where does the average \(y\) for all individuals with \(x = x_0\) lie? The uncertainty comes from estimating the line. \[\hat{y}_0 \pm t^*_{n-2}\, s_e\sqrt{\frac{1}{n} + \frac{(x_0 - \bar{x})^2}{S_{xx}}}.\]
- Prediction interval for an individual response: where will a single new observation at \(x_0\) fall? This adds the individual's own scatter around the line. \[\hat{y}_0 \pm t^*_{n-2}\, s_e\sqrt{1 + \frac{1}{n} + \frac{(x_0 - \bar{x})^2}{S_{xx}}}.\]
The prediction interval is always wider, because of the extra "1" under the root: even if we knew the true line exactly, an individual would still scatter around it by \(\sigma\). As \(n\) grows, the confidence interval shrinks toward zero width but the prediction interval settles near \(\pm 1.96\,\sigma\). Both intervals are narrowest at \(x_0 = \bar{x}\) and widen as \(x_0\) moves away, since the term \((x_0 - \bar{x})^2\) grows: predictions near the center of the data are more reliable than predictions near its edges (and extrapolation is worse still).
Example 14.5: Intervals at 6.5 hours of study (Example 14.1)
\(\hat{y}_0 = 48.012 + 3.952(6.5) = 73.70\) and \(t^*_6 = 2.447\).
Mean response. \(\text{SE} = 3.093\sqrt{1/8 + (6.5 - 5.5)^2/42} = 1.193\); the interval is \(73.70 \pm 2.447(1.193) = (70.78,\ 76.62)\). We are 95 percent confident that the mean score of all students who study 6.5 hours is between 70.8 and 76.6.
Individual. \(\text{SE} = 3.093\sqrt{1 + 1/8 + 1/42} = 3.315\); the interval is \(73.70 \pm 2.447(3.315) = (65.59,\ 81.81)\). We are 95 percent confident that a single new student who studies 6.5 hours will score between 65.6 and 81.8. The individual interval is about 2.8 times as wide.
Checking the conditions
The inference above relies on the model \(y = \beta_0 + \beta_1 x + \varepsilon\) with errors that are independent, normal, and of constant spread. A convenient memory aid is LINE:
- Linear: the mean of \(y\) is a straight-line function of \(x\). Check the scatterplot and the residual plot.
- Independent: the observations do not influence each other (random sample or randomized experiment; be wary of time series and clustered data).
- Normal: the residuals are approximately normally distributed (check a histogram or a normal probability plot of the residuals; matters mainly for small samples and for prediction intervals).
- Equal variance: the spread of the residuals is the same at every \(x\).
The single most useful diagnostic is the residual plot: residuals \(e_i\) plotted against the fitted values \(\hat{y}_i\) (or against \(x\)). If the model is appropriate, the residual plot shows a formless horizontal band of points centered on zero.
| Pattern in the residual plot | Diagnosis | Remedy |
|---|---|---|
| A curve (U shape or arch) | The relationship is not linear | Transform \(x\) or \(y\), or add a curved term |
| A fan or funnel (spread grows with \(\hat{y}\)) | Non-constant variance | Transform \(y\) (log or square root); weighted regression |
| One or two extreme points | Outliers or influential points | Investigate; report the fit with and without them |
| Residuals heavily skewed on a normal probability plot | Non-normal errors | Transform \(y\); larger samples help |
| A trend or waves in time order | Dependent errors | Time-series methods |
In 1973 Francis Anscombe published four small data sets with (almost) identical summary statistics: \(\bar{x} = 9\), \(\bar{y} = 7.50\), \(r = 0.816\), and the same regression line \(\hat{y} = 3.00 + 0.500x\). Their graphs differ dramatically: one is an ordinary linear scatter, one is a clean curve, one is a perfect line with a single outlier, and one is a vertical stack of points with a single high-leverage point that alone determines the slope. The moral: summary numbers cannot replace plots. The diagnostic tool below reproduces all four.
Transformations
When the residual plot reveals curvature or a funnel, a transformation of \(y\) (or \(x\)) often repairs the relationship so that a straight line fits. Useful choices:
- \(\ln y\) for exponential growth (\(y = ae^{cx}\) becomes \(\ln y = \ln a + cx\), a line). Also often cures a funnel where the spread grows with the mean.
- \(\sqrt{y}\) for count-like data or a milder curve.
- \(\ln x\) and \(\ln y\) together for power laws (\(y = ax^b\) becomes \(\ln y = \ln a + b \ln x\)).
After transformation, interpret coefficients on the new scale: in \(\ln \hat{y} = b_0 + b_1 x\), each one-unit increase in \(x\) multiplies the predicted \(y\) by \(e^{b_1}\) (for small \(b_1\), about a \(100 b_1\) percent increase). Back-transform predictions with \(\hat{y} = e^{b_0 + b_1 x}\).
For the cars data, stopping distance physically grows faster than linearly with speed; regressing \(\sqrt{\text{distance}}\) on speed raises \(r^2\) from 0.651 to 0.709, and a log-log fit gives 0.733 with \(\ln \widehat{\text{distance}} = -0.730 + 1.602 \ln(\text{speed})\), close to the power of 2 predicted by physics. The diagnostic tool below lets you apply the log transformation to a curved data set and watch the residual plot become structureless.
Chapter summary
- Model: \(y = \beta_0 + \beta_1 x + \varepsilon\). Fitted line \(\hat{y} = b_0 + b_1 x\); residual \(e = y - \hat{y}\).
- Least squares minimizes \(\sum e^2\): \(b_1 = r s_y/s_x = S_{xy}/S_{xx}\), \(b_0 = \bar{y} - b_1\bar{x}\); the line passes through \((\bar{x}, \bar{y})\) and the residuals sum to 0.
- \(r^2 = \text{SSR}/\text{SST} = 1 - \text{SSE}/\text{SST}\) is the proportion of variation in \(y\) explained by the line; \(s_e = \sqrt{\text{SSE}/(n-2)}\) is the typical residual size.
- Outlier: large residual. High leverage: extreme \(x\) (\(h_i \gt 4/n\)). Influential: changes the fit a lot. High-leverage points can be very influential.
- Avoid extrapolation. Association is not causation. Watch regression to the mean.
- Slope inference: \(\text{SE}(b_1) = s_e/\sqrt{S_{xx}}\), \(t = b_1/\text{SE}(b_1)\), \(\text{df} = n - 2\); CI \(b_1 \pm t^*\text{SE}(b_1)\); \(F = t^2\).
- Mean-response CI: \(\hat{y}_0 \pm t^* s_e\sqrt{1/n + (x_0-\bar{x})^2/S_{xx}}\). Prediction interval: add 1 under the root; always wider.
- Conditions LINE, checked with the scatterplot, residual plot and normal probability plot. Transformations (log, square root) fix curvature and funnels.
Key terms
| Term | Meaning |
|---|---|
| Response and explanatory variables | The outcome being predicted and the variable used to predict it |
| Residual | Observed value minus predicted value, \(y - \hat{y}\) |
| Least-squares line | The line that minimizes the sum of squared residuals |
| \(r^2\) | Proportion of the variation in \(y\) explained by the linear relationship with \(x\) |
| \(s_e\) | Standard deviation of the residuals, with \(n - 2\) degrees of freedom |
| Leverage | How extreme a point's \(x\) value is, \(h_i = 1/n + (x_i - \bar{x})^2/S_{xx}\) |
| Influential point | A point whose removal substantially changes the fitted line |
| Extrapolation | Predicting outside the range of the observed \(x\) values |
| Confidence interval for a mean response | Interval for the average \(y\) at a given \(x_0\) |
| Prediction interval | Interval for a single new \(y\) at a given \(x_0\); wider than the CI |
| Residual plot | Residuals versus fitted values (or \(x\)); reveals violations of the conditions |