Practice / Probability for ML

Simple linear regression: the least-squares line, residuals and R²

Ten problems on fitting a straight line by least squares: the slope and intercept from sums of squares, residuals that sum to zero, the split of the total sum of squares and R² as the squared correlation, the slope from a correlation and two standard deviations, the standard error of the slope with a t test and confidence interval, what changes with the units, one point of high leverage, the normal equations from calculus, intervals for a mean response and for a new observation, and a negative R² on test data, with worked solutions and the mistakes of inverting the slope to predict x, dividing by n instead of n − 2, reading r as the fraction of variance explained, extrapolating far past the data and trusting training R² as a test score.

Before you start

Simple linear regression fits the line y^=b0+b1x\hat y = b_0 + b_1 x that makes the squared vertical distances from the data to the line as small as possible. It is the one-feature case of the least-squares problems elsewhere on this site, but in statistics it comes with its own vocabulary: the slope as a ratio of sums of squares, the residuals, R2R^2 as the share of the variation in yy that the line explains, and a standard error that says how far the slope could move with new data. These ten problems compute all of them by hand on small data sets, connect the slope to the correlation, and end with the number every machine-learning library reports as a score, R2R^2 on held-out data, which can be negative.

  • Data and sums of squares. Pairs (xi,yi)(x_i, y_i) for i=1,…,ni = 1, \dots, n, with means xˉ\bar x and yˉ\bar y. Sxx=∑(xi−xˉ)2S_{xx} = \sum (x_i - \bar x)^2, Sxy=∑(xi−xˉ)(yi−yˉ)S_{xy} = \sum (x_i - \bar x)(y_i - \bar y) and Syy=∑(yi−yˉ)2S_{yy} = \sum (y_i - \bar y)^2.
  • The least-squares line. b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=yˉ−b1xˉb_0 = \bar y - b_1 \bar x. The line passes through (xˉ,yˉ)(\bar x, \bar y). With the correlation rr and the standard deviations sxs_x, sys_y, the slope is also b1=r sy/sxb_1 = r\,s_y/s_x.
  • Residuals. ei=yi−y^ie_i = y_i - \hat y_i, with y^i=b0+b1xi\hat y_i = b_0 + b_1 x_i. For a line with an intercept, ∑ei=0\sum e_i = 0 and ∑xiei=0\sum x_i e_i = 0.
  • Sums of squares. SST=Syy\text{SST} = S_{yy}, SSE=∑ei2\text{SSE} = \sum e_i^2 and SSR=∑(y^i−yˉ)2=b12Sxx\text{SSR} = \sum (\hat y_i - \bar y)^2 = b_1^2 S_{xx}, with SST=SSR+SSE\text{SST} = \text{SSR} + \text{SSE}.
  • R2R^2. R2=SSR/SST=1−SSE/SSTR^2 = \text{SSR}/\text{SST} = 1 - \text{SSE}/\text{SST}, and for simple regression R2=r2R^2 = r^2. On data the line was not fitted to, R2=1−SSE/SSTR^2 = 1 - \text{SSE}/\text{SST} can be negative.
  • Inference. s=SSE/(n−2)s = \sqrt{\text{SSE}/(n - 2)} estimates the noise standard deviation; two degrees of freedom go to b0b_0 and b1b_1. SE⁡(b1)=s/Sxx\operatorname{SE}(b_1) = s/\sqrt{S_{xx}}, and t=b1/SE⁡(b1)t = b_1/\operatorname{SE}(b_1) has a tt distribution with n−2n - 2 degrees of freedom when the true slope is 00. Two-sided 5%5\% cutoffs: t3=3.182t_3 = 3.182, t4=2.776t_4 = 2.776, t8=2.306t_8 = 2.306.
  • Intervals at a new x0x_0. For the mean response, SE⁡=s1/n+(x0−xˉ)2/Sxx\operatorname{SE} = s\sqrt{1/n + (x_0 - \bar x)^2/S_{xx}}. For a single new observation, add 11 under the root: s1+1/n+(x0−xˉ)2/Sxxs\sqrt{1 + 1/n + (x_0 - \bar x)^2/S_{xx}}.

Builds on: Variance, covariance and correlation, Hypothesis tests and p-values: z and t tests, power and multiple comparisons

Problems

  1. ·

    Five students report hours studied and test scores:

    x (hours)12345y (score)5255616468\begin{array}{c|ccccc} x \text{ (hours)} & 1 & 2 & 3 & 4 & 5 \\ \hline y \text{ (score)} & 52 & 55 & 61 & 64 & 68 \end{array}

    Find the least-squares line.

  2. ·

    For the line of Problem 1, find the five residuals. Check that ∑ei=0\sum e_i = 0 and ∑xiei=0\sum x_i e_i = 0, and predict the score for 66 hours.

  3. ··

    For the data of Problem 1, find SST\text{SST}, SSR\text{SSR} and SSE\text{SSE}, then R2R^2. Check that R2=r2R^2 = r^2.

  4. ··

    Adult heights have mean 170170 cm and sd 1010 cm, weights mean 7070 kg and sd 1212 kg, with correlation r=0.5r = 0.5. Find the line predicting weight from height and the prediction at 180180 cm. Then find the line predicting height from weight, and the predicted height for 8282 kg.

  5. ··

    For the data of Problem 1, find ss, the standard error of b1b_1, the tt statistic for H0 ⁣:β1=0H_0\!: \beta_1 = 0 and a 95%95\% confidence interval for the slope.

  6. ··

    In Problem 1, the hours are re-recorded in minutes. What are the new slope, intercept and R2R^2? What are the slope and intercept if instead the hours are centered, x′=x−3x' = x - 3?

  7. ···

    Add a sixth student to the data of Problem 1, with 1010 hours and a score of 6060. Find the new line and R2R^2, and explain what the one point did.

  8. ···

    Minimize SSE(b0,b1)=∑(yi−b0−b1xi)2\text{SSE}(b_0, b_1) = \sum (y_i - b_0 - b_1 x_i)^2 with calculus. Derive the two normal equations, and show they give b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=yˉ−b1xˉb_0 = \bar y - b_1 \bar x.

  9. ···

    For the data of Problem 1 at x0=6x_0 = 6 hours, find a 95%95\% confidence interval for the mean score and a 95%95\% prediction interval for one new student's score.

  10. ···

    The line of Problem 1 is tested on two new students, with (6,66)(6, 66) and (7,70)(7, 70). Find R2=1−SSE/SSTR^2 = 1 - \text{SSE}/\text{SST} on these two points, where SST\text{SST} uses their own mean.

Worked solutions

Problem 1

Five students report hours studied and test scores:

x (hours)12345y (score)5255616468\begin{array}{c|ccccc} x \text{ (hours)} & 1 & 2 & 3 & 4 & 5 \\ \hline y \text{ (score)} & 52 & 55 & 61 & 64 & 68 \end{array}

Find the least-squares line.

  1. xˉ=3\bar x = 3, yˉ=300/5=60\bar y = 300/5 = 60The two means.
  2. x−xˉ=−2,−1,0,1,2x - \bar x = -2, -1, 0, 1, 2; y−yˉ=−8,−5,1,4,8y - \bar y = -8, -5, 1, 4, 8Center both variables.
  3. Sxy=16+5+0+4+16=41S_{xy} = 16 + 5 + 0 + 4 + 16 = 41; Sxx=10S_{xx} = 10Products of the centered values, and squares of the centered xx.
  4. b1=41/10=4.1b_1 = 41/10 = 4.1; b0=60−4.1⋅3=47.7b_0 = 60 - 4.1 \cdot 3 = 47.7Slope, then the intercept from the point (xˉ,yˉ)(\bar x, \bar y).
  5. y^=47.7+4.1x\hat y = 47.7 + 4.1xEach extra hour goes with 4.14.1 more points, on average, in these data.

Problem 2

For the line of Problem 1, find the five residuals. Check that ∑ei=0\sum e_i = 0 and ∑xiei=0\sum x_i e_i = 0, and predict the score for 66 hours.

  1. y^=51.8,55.9,60,64.1,68.2\hat y = 51.8, 55.9, 60, 64.1, 68.247.7+4.1x47.7 + 4.1x at x=1,…,5x = 1, \dots, 5.
  2. e=0.2,−0.9,1,−0.1,−0.2e = 0.2, -0.9, 1, -0.1, -0.2Observed minus fitted.
  3. ∑ei=0.2−0.9+1−0.1−0.2=0\sum e_i = 0.2 - 0.9 + 1 - 0.1 - 0.2 = 0The intercept makes the residuals balance.
  4. ∑xiei=0.2−1.8+3−0.4−1=0\sum x_i e_i = 0.2 - 1.8 + 3 - 0.4 - 1 = 0The slope makes the residuals uncorrelated with xx.
  5. Residuals 0.2,−0.9,1,−0.1,−0.20.2, -0.9, 1, -0.1, -0.2, summing to 00 with ∑xiei=0\sum x_i e_i = 0; at 66 hours, y^=72.3\hat y = 72.3The two zero sums are the normal equations (Problem 8), so they hold for every least-squares line with an intercept.

Problem 3

For the data of Problem 1, find SST\text{SST}, SSR\text{SSR} and SSE\text{SSE}, then R2R^2. Check that R2=r2R^2 = r^2.

  1. SST=64+25+1+16+64=170\text{SST} = 64 + 25 + 1 + 16 + 64 = 170The squared centered scores from Problem 1.
  2. SSE=0.04+0.81+1+0.01+0.04=1.9\text{SSE} = 0.04 + 0.81 + 1 + 0.01 + 0.04 = 1.9The squared residuals from Problem 2.
  3. SSR=b12Sxx=16.81⋅10=168.1\text{SSR} = b_1^2 S_{xx} = 16.81 \cdot 10 = 168.1And 168.1+1.9=170168.1 + 1.9 = 170.
  4. R2=168.1/170≈0.9888R^2 = 168.1/170 \approx 0.9888The share of the variation in yy explained by the line.
  5. SST=170\text{SST} = 170, SSR=168.1\text{SSR} = 168.1, SSE=1.9\text{SSE} = 1.9, R2≈0.989R^2 \approx 0.989; r=41/10⋅170≈0.9944r = 41/\sqrt{10 \cdot 170} \approx 0.9944 and r2≈0.989r^2 \approx 0.989r2=Sxy2/(SxxSyy)=b12Sxx/Syy=SSR/SSTr^2 = S_{xy}^2/(S_{xx}S_{yy}) = b_1^2 S_{xx}/S_{yy} = \text{SSR}/\text{SST}.

Problem 4

Adult heights have mean 170170 cm and sd 1010 cm, weights mean 7070 kg and sd 1212 kg, with correlation r=0.5r = 0.5. Find the line predicting weight from height and the prediction at 180180 cm. Then find the line predicting height from weight, and the predicted height for 8282 kg.

  1. b1=r sy/sx=0.5⋅12/10=0.6b_1 = r\,s_y/s_x = 0.5 \cdot 12/10 = 0.6 kg per cmThe slope from the correlation.
  2. b0=70−0.6⋅170=−32b_0 = 70 - 0.6 \cdot 170 = -32The line passes through the means.
  3. At 180180 cm: −32+0.6⋅180=76-32 + 0.6 \cdot 180 = 76 kgOne sd above the mean in height predicts only 0.50.5 sd above in weight: regression toward the mean.
  4. Height on weight: slope r sx/sy=0.5⋅10/12≈0.417r\,s_x/s_y = 0.5 \cdot 10/12 \approx 0.417 cm per kgSwap the roles of xx and yy; the correlation is the same.
  5. Weight =−32+0.6⋅height= -32 + 0.6 \cdot \text{height}, 7676 kg at 180180 cm; height =170+0.417(weight−70)= 170 + 0.417(\text{weight} - 70), 175175 cm at 8282 kg8282 kg is one sd above the mean, so the prediction is half an sd, 55 cm, above. Inverting the first line gives a slope of 1/0.6≈1.671/0.6 \approx 1.67 instead (Mistake 1).

Problem 5

For the data of Problem 1, find ss, the standard error of b1b_1, the tt statistic for H0 ⁣:β1=0H_0\!: \beta_1 = 0 and a 95%95\% confidence interval for the slope.

  1. s2=SSE/(n−2)=1.9/3≈0.6333s^2 = \text{SSE}/(n - 2) = 1.9/3 \approx 0.6333Two degrees of freedom are used by b0b_0 and b1b_1.
  2. s≈0.7958s \approx 0.7958The typical size of a residual.
  3. SE⁡(b1)=0.7958/10≈0.2517\operatorname{SE}(b_1) = 0.7958/\sqrt{10} \approx 0.2517s/Sxxs/\sqrt{S_{xx}}.
  4. t=4.1/0.2517≈16.29t = 4.1/0.2517 \approx 16.29, with 33 degrees of freedomFar past the cutoff 3.1823.182; two-sided p≈0.0005p \approx 0.0005.
  5. s≈0.796s \approx 0.796, SE⁡(b1)≈0.252\operatorname{SE}(b_1) \approx 0.252, t≈16.3t \approx 16.3; 95%95\% interval 4.1±3.182⋅0.2517≈(3.30,4.90)4.1 \pm 3.182 \cdot 0.2517 \approx (3.30, 4.90)With only 55 points the tt cutoff is 3.1823.182, not 1.961.96. Dividing by nn instead of n−2n - 2 gives an interval that is too narrow (Mistake 2).

Problem 6

In Problem 1, the hours are re-recorded in minutes. What are the new slope, intercept and R2R^2? What are the slope and intercept if instead the hours are centered, x′=x−3x' = x - 3?

  1. xmin=60xx_{\text{min}} = 60x, so SxyS_{x y} and SxxS_{xx} scale by 6060 and 36003600Every centered xx is multiplied by 6060.
  2. b1=4.1/60≈0.0683b_1 = 4.1/60 \approx 0.0683 points per minuteThe slope is per unit of xx, so it shrinks by the factor 6060.
  3. b0=60−0.0683⋅180=47.7b_0 = 60 - 0.0683 \cdot 180 = 47.7; R2R^2 unchangedThe fitted values are the same, so the residuals and R2R^2 are too.
  4. Centered: slope 4.14.1, intercept yˉ=60\bar y = 60With xˉ′=0\bar x' = 0, b0=yˉ−b1⋅0b_0 = \bar y - b_1 \cdot 0.
  5. Minutes: b1≈0.0683b_1 \approx 0.0683, b0=47.7b_0 = 47.7, R2≈0.989R^2 \approx 0.989; centered: b1=4.1b_1 = 4.1, b0=60b_0 = 60A slope's size depends on the units, so it is not by itself a measure of how strong the relationship is. R2R^2 and rr do not depend on units.

Problem 7

Add a sixth student to the data of Problem 1, with 1010 hours and a score of 6060. Find the new line and R2R^2, and explain what the one point did.

  1. xˉ=25/6≈4.167\bar x = 25/6 \approx 4.167, yˉ=360/6=60\bar y = 360/6 = 60The mean score does not move, because the new score is 6060.
  2. Sxy=∑xi(yi−60)=41S_{xy} = \sum x_i(y_i - 60) = 41The new point has y−yˉ=0y - \bar y = 0, so it adds nothing; and ∑(yi−yˉ)=0\sum(y_i - \bar y) = 0 lets xˉ\bar x drop out.
  3. Sxx=155−6xˉ2≈50.83S_{xx} = 155 - 6\bar x^2 \approx 50.83∑xi2=55+100=155\sum x_i^2 = 55 + 100 = 155; the new point is far from the others in xx.
  4. b1=41/50.83≈0.807b_1 = 41/50.83 \approx 0.807; b0=60−0.807⋅4.167≈56.64b_0 = 60 - 0.807 \cdot 4.167 \approx 56.64Slope, then intercept.
  5. y^≈56.64+0.807x\hat y \approx 56.64 + 0.807x, R2≈0.195R^2 \approx 0.195, down from 0.9890.989SSR=0.8072⋅50.83≈33.07\text{SSR} = 0.807^2 \cdot 50.83 \approx 33.07 of SST=170\text{SST} = 170. One point with an extreme xx, a point of high leverage, cut the slope by a factor of 55. Plot the data before trusting a line.

Problem 8

Minimize SSE(b0,b1)=∑(yi−b0−b1xi)2\text{SSE}(b_0, b_1) = \sum (y_i - b_0 - b_1 x_i)^2 with calculus. Derive the two normal equations, and show they give b1=Sxy/Sxxb_1 = S_{xy}/S_{xx} and b0=yˉ−b1xˉb_0 = \bar y - b_1 \bar x.

  1. ∂ SSE/∂b0=−2∑(yi−b0−b1xi)=0\partial\,\text{SSE}/\partial b_0 = -2\sum (y_i - b_0 - b_1 x_i) = 0That is, ∑ei=0\sum e_i = 0.
  2. ∂ SSE/∂b1=−2∑xi(yi−b0−b1xi)=0\partial\,\text{SSE}/\partial b_1 = -2\sum x_i(y_i - b_0 - b_1 x_i) = 0That is, ∑xiei=0\sum x_i e_i = 0.
  3. First equation over nn: yˉ−b0−b1xˉ=0\bar y - b_0 - b_1\bar x = 0So b0=yˉ−b1xˉb_0 = \bar y - b_1\bar x, and the line goes through (xˉ,yˉ)(\bar x, \bar y).
  4. Substitute: ∑xi((yi−yˉ)−b1(xi−xˉ))=0\sum x_i\big((y_i - \bar y) - b_1(x_i - \bar x)\big) = 0yi−b0−b1xi=(yi−yˉ)−b1(xi−xˉ)y_i - b_0 - b_1x_i = (y_i - \bar y) - b_1(x_i - \bar x).
  5. b1=∑xi(yi−yˉ)∑xi(xi−xˉ)=SxySxxb_1 = \dfrac{\sum x_i(y_i - \bar y)}{\sum x_i(x_i - \bar x)} = \dfrac{S_{xy}}{S_{xx}}, b0=yˉ−b1xˉb_0 = \bar y - b_1\bar xReplacing xix_i by xi−xˉx_i - \bar x in front changes neither sum, because ∑(yi−yˉ)=0\sum(y_i - \bar y) = 0 and ∑(xi−xˉ)=0\sum(x_i - \bar x) = 0. The Hessian has determinant 4nSxx>04nS_{xx} > 0, so this is a minimum.

Problem 9

For the data of Problem 1 at x0=6x_0 = 6 hours, find a 95%95\% confidence interval for the mean score and a 95%95\% prediction interval for one new student's score.

  1. y^0=72.3\hat y_0 = 72.3 and (x0−xˉ)2/Sxx=9/10=0.9(x_0 - \bar x)^2/S_{xx} = 9/10 = 0.9Problem 2, and the distance of x0x_0 from the center of the data.
  2. Mean: SE⁡=0.79580.2+0.9≈0.8347\operatorname{SE} = 0.7958\sqrt{0.2 + 0.9} \approx 0.83471/n=0.21/n = 0.2.
  3. New student: SE⁡=0.79581+0.2+0.9≈1.1533\operatorname{SE} = 0.7958\sqrt{1 + 0.2 + 0.9} \approx 1.1533One more s2s^2 for the student's own scatter around the line.
  4. Margins 3.182⋅0.8347≈2.663.182 \cdot 0.8347 \approx 2.66 and 3.182⋅1.1533≈3.673.182 \cdot 1.1533 \approx 3.67The tt cutoff with 33 degrees of freedom.
  5. Mean score: 72.3±2.66≈(69.6,75.0)72.3 \pm 2.66 \approx (69.6, 75.0); one new student: 72.3±3.67≈(68.6,76.0)72.3 \pm 3.67 \approx (68.6, 76.0)The prediction interval is always wider. More data shrinks the first toward 00, but the second never shrinks below about ±1.96σ\pm 1.96\sigma.

Problem 10

The line of Problem 1 is tested on two new students, with (6,66)(6, 66) and (7,70)(7, 70). Find R2=1−SSE/SSTR^2 = 1 - \text{SSE}/\text{SST} on these two points, where SST\text{SST} uses their own mean.

  1. Predictions 72.372.3 and 76.476.447.7+4.1⋅647.7 + 4.1 \cdot 6 and 47.7+4.1⋅747.7 + 4.1 \cdot 7.
  2. Errors −6.3-6.3 and −6.4-6.4, so SSE=39.69+40.96=80.65\text{SSE} = 39.69 + 40.96 = 80.65Both students score well below the line.
  3. Test mean 6868, so SST=4+4=8\text{SST} = 4 + 4 = 8The spread of the test scores around their own mean.
  4. R2=1−80.65/8R^2 = 1 - 80.65/8SSE\text{SSE} is ten times SST\text{SST}.
  5. R2≈−9.08R^2 \approx -9.08A negative R2R^2 means the line predicts worse than the constant 6868, the test set's own mean. On training data with an intercept R2≥0R^2 \ge 0 is guaranteed; on new data it is not (Mistake 5).

Where this goes wrong

1. Inverting the slope to predict x from y

Problem 4 has a line for weight from height, and solving it for height looks like the line for height from weight.

  1. Weight =−32+0.6⋅height= -32 + 0.6 \cdot \text{height}Right so far.
  2. “Solve for height: height =(weight+32)/0.6= (\text{weight} + 32)/0.6.”That line minimizes the vertical distances to weight, not the distances in height. Least squares in the other direction is a different problem, with slope r sx/syr\,s_x/s_y.
  3. At 8282 kg: height =114/0.6=190= 114/0.6 = 190 cmThe right prediction is 175175 cm. The inverted slope 1/0.6≈1.671/0.6 \approx 1.67 cm per kg is four times 0.4170.417, since the two slopes multiply to r2=0.25r^2 = 0.25, not to 11.

2. Dividing SSE by n instead of n − 2

Problem 5 needs an estimate of the noise variance, and an average of squared residuals looks like dividing by nn.

  1. SSE=1.9\text{SSE} = 1.9, n=5n = 5Right so far.
  2. “s2=1.9/5=0.38s^2 = 1.9/5 = 0.38.”The residuals are forced to satisfy two equations, ∑ei=0\sum e_i = 0 and ∑xiei=0\sum x_i e_i = 0, so only n−2n - 2 of them are free, and SSE/n\text{SSE}/n underestimates σ2\sigma^2 on average.
  3. SE⁡(b1)≈0.195\operatorname{SE}(b_1) \approx 0.195, interval ≈(3.48,4.72)\approx (3.48, 4.72)With n−2=3n - 2 = 3: SE⁡(b1)≈0.252\operatorname{SE}(b_1) \approx 0.252 and interval (3.30,4.90)(3.30, 4.90). The wrong interval is about 23%23\% too narrow, and it covers the true slope only about 91%91\% of the time instead of 95%95\%.

3. Reading r as the fraction of variance explained

The correlation of Problem 4 is 0.50.5, and a half sounds like a share.

  1. r=0.5r = 0.5 between height and weightRight so far.
  2. “Height explains 50%50\% of the variation in weight.”The fraction explained is R2=r2R^2 = r^2, not rr.
  3. 50%50\% of the variance in weight is explainedIt is r2=25%r^2 = 25\%. A correlation of 0.50.5 leaves three quarters of the variance unexplained, and even r=0.7r = 0.7 explains less than half.

4. Extrapolating far past the data

The line of Problem 1 has a clean R2≈0.989R^2 \approx 0.989, so it seems safe to use anywhere.

  1. y^=47.7+4.1x\hat y = 47.7 + 4.1x, fitted for 1≤x≤51 \le x \le 5Right so far.
  2. “A student who studies 4040 hours scores 47.7+4.1⋅4047.7 + 4.1 \cdot 40.”The data say nothing about the relationship outside 11 to 55 hours, and the prediction interval of Problem 9 already widens with (x0−xˉ)2(x_0 - \bar x)^2.
  3. y^=211.7\hat y = 211.7 at 4040 hoursOn a 100100-point test. A high R2R^2 says the line fits the range it was fitted on; it says nothing about where the line stops being a line.

5. Trusting training R² as a test score

The line of Problem 1 has R2≈0.989R^2 \approx 0.989 on its own data, and that number is reported as how good the model is.

  1. R2≈0.989R^2 \approx 0.989 on the five training pointsRight so far.
  2. “So the model will explain about 99%99\% of the variation in new scores.”Training R2R^2 measures the fit to the points that chose the line. With few points and a flexible model it is optimistic, and it says nothing about new data from a different group.
  3. Expected test R2≈0.99R^2 \approx 0.99On the two test students of Problem 10, R2≈−9.08R^2 \approx -9.08. Report R2R^2 on held-out data, and remember it is not bounded below by 00.

Print this set: simple-linear-regression-and-r-squared.pdf (problems, answers, and worked solutions on separate pages).