Unit 9, lesson 61

Least Squares and R-Squared

How a least-squares line describes a response with one predictor, how to read its slope, intercept, fitted values and residuals, and what R-squared says about the residual sum of squares.

  • Video 61
  • Simple Linear Regression

Three constructed trips cover 1, 2 and 3 kilometers and take 2, 3 and 5 minutes. Which straight line best describes how duration changes with distance? Many lines pass near the three points, so the question needs a precise meaning of “best”. This lesson measures how far a line misses each point, chooses the line with the smallest sum of squared misses, reads its slope and intercept with units, and uses R-squared to say how much of the variation in the response the line accounts for within the data.

Key terms

Predictor and response

Definition Regression data consist of nn pairs (x1,y1),…,(xn,yn)(x_1, y_1), \dots, (x_n, y_n) measured on the same individuals. The response yy is the variable to be described or predicted, and the predictor xx, also called the explanatory variable, is the variable used to describe or predict it. Simple linear regression summarizes how the response changes across values of one predictor by a straight line y^=b0+b1x\hat{y} = b_0 + b_1 x.

In plain words The predictor goes on the horizontal axis and the response on the vertical axis. The names describe roles in the analysis, not causes. Calling distance the predictor does not claim that distance produces the duration. Unless the points lie exactly on a line, swapping the two roles gives a different least-squares line, not the same line read backward.

Example For the three trips, distance in kilometers is the predictor and duration in minutes is the response, so the data are the pairs (1,2)(1, 2), (2,3)(2, 3) and (3,5)(3, 5). The least-squares line found below has slope 1.5 minutes per kilometer. If distance is predicted from duration instead, the least-squares slope is 9/149/14 kilometer per minute, and solving that line for duration gives a slope of 14/9≈1.5614/9 \approx 1.56 minutes per kilometer, a different line.

Fitted value and residual

Definition For a line y^=b0+b1x\hat{y} = b_0 + b_1 x and observation ii, the fitted value y^i=b0+b1xi\hat{y}_i = b_0 + b_1 x_i is the height of the line at that observation’s predictor value. The residual of observation ii is

ei=yi−y^i,e_i = y_i - \hat{y}_i ,

the observed response minus the fitted value, in the units of the response. It is positive when the point lies above the line, negative when it lies below and zero when it lies on the line. A residual is a vertical distance taken at the fixed value xix_i, not the perpendicular distance from the point to the line.

In plain words The fitted value is what the line says at a given predictor value, and the residual is how far the actual response is from it. The hat marks a value produced by the line, not a measurement.

Example The candidate line y^=1+x\hat{y} = 1 + x gives fitted values of 2, 3 and 4 minutes at 1, 2 and 3 kilometers. The observed durations are 2, 3 and 5 minutes, so the residuals are 0, 0 and 1 minute. The third trip took one minute longer than this line says.

Residual sum of squares

Definition For a line with intercept b0b_0 and slope b1b_1, the residual sum of squares is

SSE=∑i=1nei2,\text{SSE} = \sum_{i=1}^{n} e_i^2 ,

where ei=yi−b0−b1xie_i = y_i - b_0 - b_1 x_i. It is measured in squared response units. Squaring keeps positive and negative residuals from canceling, and it makes a large miss count much more than a small one.

In plain words Square every miss and add. A smaller total means that the line passes closer to the points in this vertical, squared sense. It is a chosen criterion, and other criteria, such as the sum of absolute residuals, can pick a different line.

Example For the candidate line y^=1+x\hat{y} = 1 + x, the residuals 0, 0 and 1 give SSE=1\text{SSE} = 1 squared minute. The horizontal line at the mean duration yˉ=10/3\bar{y} = 10/3 minutes has residuals −4/3-4/3, −1/3-1/3 and 5/35/3. They add to zero, yet its SSE is 16/9+1/9+25/9=14/316/9 + 1/9 + 25/9 = 14/3 squared minutes. A residual total of zero does not by itself show a good fit.

Least-squares line

Definition For nn pairs with at least two distinct predictor values, the least-squares line is the line y^=b0+b1x\hat{y} = b_0 + b_1 x whose intercept and slope make the SSE as small as possible. It is unique, and its coefficients are

b1=SxySxx,b0=yˉ−b1xˉ,\begin{gathered} b_1 = \frac{S_{xy}}{S_{xx}}, \\[0.4em] b_0 = \bar{y} - b_1 \bar{x}, \end{gathered}

where

Sxx=∑(xi−xˉ)2,Sxy=∑(xi−xˉ)(yi−yˉ).\begin{aligned} S_{xx} &= \sum (x_i - \bar{x})^2, \\[0.4em] S_{xy} &= \sum (x_i - \bar{x})(y_i - \bar{y}). \end{aligned}

Equivalently b1=sxy/sx2b_1 = s_{xy}/s_x^2, which equals rsy/sxr s_y / s_x when sy>0s_y > 0. The line passes through the point (xˉ,yˉ)(\bar{x}, \bar{y}), and its residuals satisfy ∑ei=0\sum e_i = 0 and ∑xiei=0\sum x_i e_i = 0. These are the two equations obtained by setting the partial derivatives of the SSE with respect to b0b_0 and b1b_1 equal to zero. If all xix_i are equal, then Sxx=0S_{xx} = 0, every line through (xˉ,yˉ)(\bar{x}, \bar{y}) gives the same smallest SSE, and the slope is not determined.

In plain words The slope compares how the two variables move together with how much the predictor varies, and the intercept then places the line through the point of means. Computing the line needs no assumptions about a population. Those enter only when the line is used for inference about one.

Example For the three trips, xˉ=2\bar{x} = 2 kilometers and yˉ=10/3\bar{y} = 10/3 minutes. The centered distances are −1-1, 00 and 11, and the centered durations are −4/3-4/3, −1/3-1/3 and 5/35/3. So Sxx=2S_{xx} = 2 and Sxy=4/3+0+5/3=3S_{xy} = 4/3 + 0 + 5/3 = 3, which give

b1=32=1.5,b0=103−1.5⋅2=13.\begin{gathered} b_1 = \frac{3}{2} = 1.5, \\[0.4em] b_0 = \frac{10}{3} - 1.5 \cdot 2 = \frac{1}{3}. \end{gathered}

The fitted values at 1, 2 and 3 kilometers are 11/611/6, 10/310/3 and 29/629/6 minutes, and the residuals are 1/61/6, −1/3-1/3 and 1/61/6 minute. They add to zero, and the SSE is 1/36+1/9+1/36=1/61/36 + 1/9 + 1/36 = 1/6 squared minute, below the value 1 of the candidate line y^=1+x\hat{y} = 1 + x.

Slope and intercept

Definition In the line y^=b0+b1x\hat{y} = b_0 + b_1 x, the slope b1b_1 is the change in the fitted value when xx increases by one unit, measured in response units per predictor unit. The intercept b0b_0 is the fitted value at x=0x = 0, measured in response units. For a least-squares line both are summaries of the data. The slope compares the fitted values of individuals whose predictor values differ by one unit, and without a suitable design it should not be read as the effect of changing xx for any one individual. When x=0x = 0 lies outside the observed range of the predictor, the intercept comes from extending the line beyond the data and does not describe any observed situation. When x=0x = 0 is not a possible value, the intercept describes no real situation at all.

In plain words Read the slope with its units, as in “1.5 minutes per kilometer”, and read it as a comparison within the data. Read the intercept as the height of the line at zero, which may be a value nobody observed or could observe. Adding a constant to every response shifts the intercept by that constant and leaves the slope and the residuals unchanged. Changing the unit of the predictor rescales the slope but describes the same relationship.

Example For the trips, b1=1.5b_1 = 1.5 minutes per kilometer. Among these trips, a distance one kilometer greater goes with a fitted duration 1.5 minutes longer. That does not show that making a given trip one kilometer longer adds exactly 1.5 minutes. The intercept of 1/31/3 minute is the value of the line at 0 kilometers, outside the observed range of 1 to 3 kilometers, so it is a feature of the fitted line rather than an estimate of how long a trip of length zero takes. If every duration were 10 minutes longer, the intercept would become 31/331/3 minutes while the slope and the residuals stayed the same. With distance in meters the slope would be 0.00150.0015 minute per meter.

R-squared

Definition The total sum of squares SST=∑(yi−yˉ)2\text{SST} = \sum (y_i - \bar{y})^2 is the SSE of the horizontal line y^=yˉ\hat{y} = \bar{y}, which ignores the predictor. For a least-squares line with an intercept, fitted to data with SST>0\text{SST} > 0,

R2=1−SSESST.R^2 = 1 - \frac{\text{SSE}}{\text{SST}} .

For such a fit SST=∑(y^i−yˉ)2+SSE\text{SST} = \sum (\hat{y}_i - \bar{y})^2 + \text{SSE}, so 0≤R2≤10 \le R^2 \le 1, and R2R^2 is the fraction of the total sum of squares accounted for by the fitted values. With at least two distinct predictor values, R2=r2R^2 = r^2, the square of the Pearson correlation. When all responses are equal, SST=0\text{SST} = 0 and the ratio is undefined.

In plain words R-squared compares the line with the simplest prediction, the mean response for everyone, and gives the share of that baseline’s squared error that the line removes within these data. It is an in-sample description. A high value does not show that a straight line is the right shape, that the predictor causes the response, or that new observations will be predicted as well. With two points at different predictor values, the line passes through both, so R2=1R^2 = 1 whenever the two responses differ.

Example For the trips, SST=16/9+1/9+25/9=14/3\text{SST} = 16/9 + 1/9 + 25/9 = 14/3 squared minutes and SSE=1/6\text{SSE} = 1/6 squared minute, so

R2=1−1/614/3=2728≈0.964.\begin{aligned} R^2 &= 1 - \frac{1/6}{14/3} = \frac{27}{28} \\[0.4em] &\approx 0.964 . \end{aligned}

Within these three trips, the line removes about 96.4 percent of the squared error of the mean-only prediction. For contrast, the points (1,1)(1, 1), (2,4)(2, 4), (3,9)(3, 9) and (4,16)(4, 16) lie on the curve y=x2y = x^2. Their least-squares line y^=−5+5x\hat{y} = -5 + 5x has R2=125/129≈0.969R^2 = 125/129 \approx 0.969, yet its residuals 11, −1-1, −1-1 and 11 are positive at both ends and negative in the middle, the mark of a curve that a straight line misses.

Check yourself

  1. In another constructed data set of trips from 2 to 10 kilometers, the least-squares line is y^=4+2.5x\hat{y} = 4 + 2.5x, with xx in kilometers and yy in minutes. Interpret the slope, and find the fitted value and the residual for a 6 kilometer trip that took 21 minutes.
Show the answer

Among these trips, each additional kilometer goes with a fitted duration 2.5 minutes longer. This is a comparison within the data, not a claim about what lengthening one trip would do.

At x=6x = 6 the fitted value is 4+2.5⋅6=194 + 2.5 \cdot 6 = 19 minutes. The residual is 21−19=221 - 19 = 2 minutes. It is positive, so the point lies above the line and the trip took 2 minutes longer than the line says.

  1. Use the trip line y^=1/3+1.5x\hat{y} = 1/3 + 1.5x, fitted to distances from 1 to 3 kilometers, to predict the duration of a 4 kilometer trip. A new 4 kilometer trip took 7 minutes. Find the prediction error without refitting the line, and say what limits the trust in this prediction.
Show the answer

The prediction is 1/3+1.5⋅4=19/3≈6.331/3 + 1.5 \cdot 4 = 19/3 \approx 6.33 minutes. The prediction error, observed minus predicted, is 7−19/3=2/3≈0.677 - 19/3 = 2/3 \approx 0.67 minute, so the trip took about 0.67 minute longer than predicted.

A distance of 4 kilometers lies outside the observed range of 1 to 3 kilometers, so this prediction is an extrapolation. The three trips cannot show whether durations keep rising by about 1.5 minutes per kilometer beyond 3 kilometers, and a line fitted to only three points could change a great deal with one more trip.

  1. Fit the least-squares line to the points (1,4)(1, 4), (2,6)(2, 6), (3,5)(3, 5) and (4,9)(4, 9), and find the residuals and R2R^2. A classmate reads R2R^2 as the percentage accuracy of future predictions from this line. Is that right?
Show the answer

The means are xˉ=2.5\bar{x} = 2.5 and yˉ=6\bar{y} = 6. The centered xx values are −1.5-1.5, −0.5-0.5, 0.50.5 and 1.51.5, and the centered yy values are −2-2, 00, −1-1 and 33. So Sxx=5S_{xx} = 5 and Sxy=3+0−0.5+4.5=7S_{xy} = 3 + 0 - 0.5 + 4.5 = 7, which give b1=7/5=1.4b_1 = 7/5 = 1.4 and b0=6−1.4⋅2.5=2.5b_0 = 6 - 1.4 \cdot 2.5 = 2.5. The line is y^=2.5+1.4x\hat{y} = 2.5 + 1.4x.

The fitted values are 3.9, 5.3, 6.7 and 8.1, and the residuals are 0.1, 0.7, −1.7-1.7 and 0.9, which add to zero. Their squares 0.01, 0.49, 2.89 and 0.81 add to SSE=4.2\text{SSE} = 4.2, and the squared centered responses 4, 0, 1 and 9 add to SST=14\text{SST} = 14, so

R2=1−4.214=0.7.R^2 = 1 - \frac{4.2}{14} = 0.7 .

The classmate is wrong. The value 0.7 says that within these four points the line removes 70 percent of the squared error of predicting every response by the mean 6. It is not a percentage of correct predictions, and because it is computed from the same points used to fit the line, it says nothing certain about how well the line will predict new observations. With only four points, a fairly high R2R^2 can also arise by chance.

How this lesson connects

It also uses equation of a straight line.