Statistics & Probability · Grades 10, 11

The Least-Squares Regression Line

Quick answer

The least-squares regression line is the line through a scatter plot that makes the sum of the squared residuals as small as possible. Its slope is b = Σ(x − x̄)(y − ȳ)/Σ(x − x̄)², equivalently r·s_y/s_x, and it always passes through the point of means (x̄, ȳ). The slope is the predicted change in y for each unit of x. R², the square of the correlation, is the fraction of the variation in y the line explains. Predictions are trustworthy only inside the range of the data.

What you'll learn

  • Compute the slope and intercept of the least-squares line
  • Explain what 'least squares' minimizes
  • Interpret the slope, the intercept and R² in context
  • Recognize the danger of extrapolation

The line that misses least

Five students record hours studied, xx, and test score, yy:

xx (hours)1122334455
yy (score)57576060686876767979

Many lines pass near these points. A residual measures how far a line misses one point, vertically: y−y^y - \hat{y}. The least-squares regression line is the one line that makes the sum of the squared residuals as small as possible.

Its slope and intercept have formulas:

b=∑(x−xˉ)(y−yˉ)∑(x−xˉ)2a=yˉ−b xˉb = \frac{\sum (x - \bar{x})(y - \bar{y})}{\sum (x - \bar{x})^2} \qquad a = \bar{y} - b\,\bar{x}

For the data, xˉ=3\bar{x} = 3 and yˉ=68\bar{y} = 68. The deviations of xx are −2,−1,0,1,2-2, -1, 0, 1, 2 and of yy are −11,−8,0,8,11-11, -8, 0, 8, 11. So ∑(x−xˉ)(y−yˉ)=22+8+0+8+22=60\sum (x - \bar{x})(y - \bar{y}) = 22 + 8 + 0 + 8 + 22 = 60 and ∑(x−xˉ)2=10\sum (x - \bar{x})^2 = 10. The slope is b=6b = 6 and the intercept is a=68−6×3=50a = 68 - 6 \times 3 = 50:

y^=50+6x\hat{y} = 50 + 6x
Test scores and the least-squares line A scatter plot of five points: (1, 57), (2, 60), (3, 68), (4, 76) and (5, 79). The line y = 50 + 6x runs through the middle of them, passing through the point of means (3, 68). The points sit 1, 2, 0, 2 and 1 units off the line. 123456405060708090xy
  • ŷ = 50 + 6x
Test scores and the least-squares line

Why squares, and why the line passes through the means

Squaring makes every residual count as positive, so misses above and below cannot cancel. It also punishes large misses more than small ones, so the line will not ignore a point to fit the others perfectly.

Minimizing a sum of squares is a calculus problem with two unknowns, aa and bb. Setting the partial derivative with respect to aa to zero gives ∑(y−a−bx)=0\sum (y - a - bx) = 0: the residuals of the best line add to zero, which rearranges to yˉ=a+bxˉ\bar{y} = a + b\bar{x}. So the line always passes through (xˉ,yˉ)(\bar{x}, \bar{y}). Here the residuals are 1,−2,0,2,−11, -2, 0, 2, -1, which do add to 00. Setting the other derivative to zero gives the formula for bb.

Slope from the correlation

The slope can also be written with summary statistics:

b=r sysxb = r\,\frac{s_y}{s_x}

For the data, r≈0.9864r \approx 0.9864, sy≈9.618s_y \approx 9.618 and sx≈1.581s_x \approx 1.581, so b≈0.9864×6.083≈6.00b \approx 0.9864 \times 6.083 \approx 6.00. In words: a change of one standard deviation in xx predicts a change of rr standard deviations in yy.

How good is the fit?

R2R^2, the square of the correlation, is the fraction of the variation in yy that the line explains. The total variation is ∑(y−yˉ)2=370\sum (y - \bar{y})^2 = 370. What the line leaves unexplained is the sum of squared residuals, 1+4+0+4+1=101 + 4 + 0 + 4 + 1 = 10. So

R2=1−10370≈0.973R^2 = 1 - \frac{10}{370} \approx 0.973

About 97%97\% of the variation in scores is explained by hours studied.

Worked examples

Common mistakes

Practice problems

  1. Find the least-squares line for x=2,4,6,8x = 2, 4, 6, 8 and y=3,7,8,14y = 3, 7, 8, 14.

    Answer

    y^=−0.5+1.7x\hat{y} = -0.5 + 1.7x

    Full solution

    xˉ=5\bar{x} = 5, yˉ=8\bar{y} = 8. The products of deviations add to 15+1+0+18=3415 + 1 + 0 + 18 = 34, and ∑(x−xˉ)2=20\sum (x - \bar{x})^2 = 20, so b=1.7b = 1.7 and a=8−1.7×5=−0.5a = 8 - 1.7 \times 5 = -0.5.

  2. Find the least-squares line for x=0,1,2,3,4x = 0, 1, 2, 3, 4 and y=10,8,7,5,0y = 10, 8, 7, 5, 0.

    Answer

    y^=10.6−2.3x\hat{y} = 10.6 - 2.3x

    Full solution

    xˉ=2\bar{x} = 2, yˉ=6\bar{y} = 6. The products of deviations add to −8−2+0+(−1)−12=−23-8 - 2 + 0 + (-1) - 12 = -23, and ∑(x−xˉ)2=10\sum (x - \bar{x})^2 = 10, so b=−2.3b = -2.3 and a=6+4.6=10.6a = 6 + 4.6 = 10.6.

  3. Given r=0.8r = 0.8, sx=2s_x = 2 and sy=5s_y = 5, find the slope.

    Answer

    22

    Full solution

    b=0.8×52=2b = 0.8 \times \tfrac{5}{2} = 2.

  4. A line has slope 44 and passes through the point of means (10,50)(10, 50). Find its intercept.

    Answer

    1010

    Full solution

    a=50−4×10=10a = 50 - 4 \times 10 = 10.

  5. Find the residuals for the line y^=50+6x\hat{y} = 50 + 6x and the five students’ data, and check that they add to zero.

    Answer

    1,−2,0,2,−11, -2, 0, 2, -1; sum 00

    Full solution

    Predictions are 56,62,68,74,8056, 62, 68, 74, 80. Subtracting from 57,60,68,76,7957, 60, 68, 76, 79 gives the residuals.

  6. A regression of house price on size has R2=0.64R^2 = 0.64. What is rr if the relationship is positive, and what does R2R^2 mean?

    Answer

    r=0.8r = 0.8; size explains 64%64\% of the variation in price.

    Full solution

    r=0.64=0.8r = \sqrt{0.64} = 0.8, positive because larger houses cost more. The other 36%36\% of the variation comes from other factors.

  7. The line y^=50+6x\hat{y} = 50 + 6x predicts a score of 122122 for 1212 hours. Why should this prediction not be trusted?

    Answer

    It is an extrapolation far outside the data, and it gives an impossible score.

    Full solution

    The data cover 11 to 55 hours. Scores must level off at the maximum, so the straight-line pattern cannot continue to 1212 hours.

  8. A student finds the line for predicting hours from score by flipping b=6b = 6 to 16\tfrac{1}{6}. Why is this wrong?

    Answer

    Predicting xx from yy minimizes horizontal misses, a different line; its slope is r sxsy≈0.162r\,\tfrac{s_x}{s_y} \approx 0.162, not 16\tfrac{1}{6}.

    Full solution

    0.9864×1.5819.618≈0.1620.9864 \times \tfrac{1.581}{9.618} \approx 0.162. The two lines agree only when the points lie exactly on a line, r=±1r = \pm 1.

Frequently asked questions

What is the least-squares regression line?

The line that minimizes the sum of the squared residuals, the vertical distances from the data points to the line. It is written ŷ = a + bx.

What is the formula for the slope?

b = Σ(x − x̄)(y − ȳ) / Σ(x − x̄)², which equals r · s_y / s_x, where r is the correlation and s_x and s_y are the standard deviations.

How do you find the intercept?

The line passes through the point of means, so a = ȳ − b·x̄.

What does R² mean?

The fraction of the variation in y that the line explains. An R² of 0.97 means 97 percent of the variation in y is accounted for by its linear relationship with x.

Why is extrapolation risky?

The line describes the data's pattern only over the range of x that was observed. Outside that range the relationship may bend, level off or break down.

What to learn next

Standards alignment

This lesson covers the following Common Core State Standards for Mathematics.

  • CCSS.MATH.CONTENT.HSS.ID.B.6Interpreting Categorical and Quantitative DataRepresent data on two quantitative variables on a scatter plot, and describe how the variables are related.
  • CCSS.MATH.CONTENT.HSS.ID.B.6aInterpreting Categorical and Quantitative DataFit a function to the data; use functions fitted to data to solve problems in the context of the data.
  • CCSS.MATH.CONTENT.HSS.ID.C.7Interpreting Categorical and Quantitative DataInterpret the slope (rate of change) and the intercept (constant term) of a linear model in the context of the data.
  • CCSS.MATH.CONTENT.HSS.ID.C.8Interpreting Categorical and Quantitative DataCompute (using technology) and interpret the correlation coefficient of a linear fit.