Tutorials › Regression Analysis (Undergraduate) › Least Squares as a Fitting Rule

Fitting a Simple Linear Regression · Tutorial 75 of 991

Least Squares as a Fitting Rule

Define residuals and the sum of squared residuals, then see how least squares uses that total to choose a fitted line.

Beginner 9 min read

What You'll Learn

  • Define a residual as an observed response minus its fitted response.
  • Calculate the sum of squared residuals for a candidate line.
  • Explain how least squares chooses an intercept and slope.
  • Compare candidate lines without confusing a small error total with proof of a global minimum.
  • Recognize why residuals are squared and why their signs cannot simply be added.
  • Distinguish the fitting rule from assumptions needed for statistical inference.

From a Regression Line to a Fitting Rule

A simple regression line gives a way to predict a response from a predictor. But many different lines could be used for those predictions. To choose one systematically, we need a rule for measuring how well a candidate line fits the observed data. Least squares is a common rule: it chooses the intercept and slope that make the total squared prediction error as small as possible.

Suppose we have \(n\) observed pairs \((x_i,y_i)\), where \(x_i\) is the predictor value and \(y_i\) is the corresponding observed response. A candidate line has intercept \(b_0\) and slope \(b_1\). For observation \(i\), its fitted response is \(b_0+b_1x_i\). The comparison is made vertically: it measures how far the observed response is above or below the line at that observation’s predictor value.

Definition: The residual for observation \(i\) is its observed response minus its fitted response. For the candidate line with intercept \(b_0\) and slope \(b_1\), $$ e_i=y_i-(b_0+b_1x_i). $$ The sum of squared residuals for that line is $$ S(b_0,b_1)=\sum_{i=1}^{n} e_i^2 =\sum_{i=1}^{n}\left[y_i-(b_0+b_1x_i)\right]^2. $$ The least-squares line is a line whose intercept and slope make \(S(b_0,b_1)\) as small as possible among candidate straight lines.

The residual is measured in the same units as the response. The sum of squared residuals (often abbreviated SSR) is measured in squared response units. A positive residual means the observation is above the candidate line; a negative residual means it is below. Both contribute a nonnegative amount to SSR because each residual is squared.

Calculating the Error Total for a Candidate Line

To calculate SSR, first use the candidate line to find a fitted response at every observed \(x_i\). Subtract each fitted response from its observed response to get the residual, square each residual, and add the squares. The line’s intercept and slope stay fixed during this calculation; changing either one changes the fitted responses and therefore the residuals.

Worked Example: Comparing Three Candidate Lines

Consider hypothetical observations \((0,2)\), \((1,3)\), and \((2,5)\). Compare the SSR values for three candidate lines: A, \(\hat{y}=1+2x\); B, \(\hat{y}=2+1.5x\); and C, \(\hat{y}=2+x\). Here \(\hat{y}\) denotes the fitted response from a candidate line.

Line A. At \(x=0,1,2\), the fitted responses are \(1,3,5\). The residuals, observed minus fitted, are \(2-1=1\), \(3-3=0\), and \(5-5=0\). Thus,

$$ S_A=1^2+0^2+0^2=1. $$

Line B. Its fitted responses are \(2,3.5,5\). The residuals are \(2-2=0\), \(3-3.5=-0.5\), and \(5-5=0\). Thus,

$$ S_B=0^2+(-0.5)^2+0^2=0.25. $$

Line C. Its fitted responses are \(2,3,4\). The residuals are \(2-2=0\), \(3-3=0\), and \(5-4=1\). Thus,

$$ S_C=0^2+0^2+1^2=1. $$

Of these three candidates, line B has the smallest SSR, \(0.25\) response-units squared. This comparison establishes only that B beats A and C; it does not establish that B has the smallest SSR among all possible lines. The least-squares rule compares against the full set of candidate intercepts and slopes.

Why Square the Residuals?

Adding the signed residuals would allow positive and negative errors to cancel. That total could be small even when the individual observations are far from the line. Squaring prevents cancellation and makes the total respond more strongly to large residuals than to small ones. For example, a residual with magnitude \(2\) contributes \(2^2=4\), which is four times the contribution \(1^2=1\) of a residual with magnitude \(1\).

This choice has a consequence: a line with one particularly large residual can have a large SSR, even if its other residuals are small. Least squares therefore gives substantial weight to large vertical errors. It does not minimize the largest residual, nor does it minimize the sum of the residual magnitudes.

Worked Example: A Large Residual Has a Large Squared Cost

Suppose hypothetical observations are \((0,0)\), \((1,0)\), and \((2,4)\). Compare the candidate line A, \(\hat{y}=0\), with candidate line B, \(\hat{y}=-1+2x\).

For line A, the fitted responses are \(0,0,0\), so its residuals are \(0-0=0\), \(0-0=0\), and \(4-0=4\). Its sum of squared residuals is

$$ S_A=0^2+0^2+4^2=16. $$

For line B, the fitted responses at \(x=0,1,2\) are \(-1,1,3\). Its residuals are \(0-(-1)=1\), \(0-1=-1\), and \(4-3=1\). Therefore,

$$ S_B=1^2+(-1)^2+1^2=3. $$

Among these two candidates, line B has the smaller SSR. Its three residuals each have magnitude \(1\), giving a total of \(3\), while line A’s single residual of magnitude \(4\) contributes \(16\). This is the effect of squaring: several modest discrepancies can have a smaller total cost than one much larger discrepancy. The comparison is still only between the two lines shown.

The Least-Squares Rule Precisely

For the observed data, the fitting rule can be written as an optimization problem: choose real values \(b_0\) and \(b_1\) that minimize the sum of squared residuals. In this expression, the observations are fixed; only the candidate intercept and slope vary.

$$ \text{Choose }(b_0,b_1)\text{ to minimize } S(b_0,b_1)=\sum_{i=1}^{n}\left[y_i-(b_0+b_1x_i)\right]^2. $$

Each term in this sum is a square, so \(S(b_0,b_1)\geq 0\) for every candidate line. It is zero exactly when every observed response equals its fitted response, meaning all the observed points lie on that line. In data that do not lie exactly on one straight line, the least-squares rule selects the line with the smallest attainable nonnegative total; it does not require that the total be zero.

This rule defines how the line is fitted to a particular set of observations. It is not, by itself, a probability model for how the data were generated. To calculate SSR and choose the least-squares line, we need paired predictor and response values and a straight-line candidate model. We do not need to assume normally distributed errors or equal error variance merely to define and minimize this sample total. Such assumptions concern other statistical questions, not the arithmetic fitting rule.

Worked Example: Changing the Response Units

Suppose hypothetical response values are recorded in centimeters for predictor values \(x=0,1,2\): \(y=2,4,7\) cm. For the candidate line \(\hat{y}=1+2x\), with fitted responses measured in centimeters, the predictions are \(1,3,5\) cm and the residuals are \(1,1,2\) cm. Therefore,

$$ S=1^2+1^2+2^2=6\text{ cm}^2. $$

Now express the same responses in meters: \(0.02,0.04,0.07\) m. The corresponding version of the same candidate line is \(\hat{y}=0.01+0.02x\), giving predictions \(0.01,0.03,0.05\) m and residuals \(0.01,0.01,0.02\) m. Its SSR is

$$ S=(0.01)^2+(0.01)^2+(0.02)^2=0.0006\text{ m}^2. $$

Since \(1\text{ cm}=0.01\text{ m}\), each residual in meters is \(0.01\) times its value in centimeters, and each squared residual is \(0.0001\) times as large numerically. The total changes from \(6\text{ cm}^2\) to \(0.0006\text{ m}^2\), but the fitting comparison does not change: every candidate line’s response values and residuals scale by the same conversion factor. The intercept and slope must be expressed in the new response units as well.

A Common Mistake: Adding Signed Residuals

A tempting alternative is to choose a line by making the sum of its signed residuals small, or even zero. That is not the least-squares rule. Opposite-signed residuals can cancel in a sum even though both represent prediction errors.

For the observations \((0,0)\), \((1,0)\), and \((2,4)\), consider the horizontal candidate line \(\hat{y}=4/3\). Its residuals are \(-4/3,-4/3,\) and \(8/3\). Their signed sum is

$$ -\frac{4}{3}-\frac{4}{3}+\frac{8}{3}=0, $$

but their sum of squares is

$$ \left(-\frac{4}{3}\right)^2+\left(-\frac{4}{3}\right)^2+ \left(\frac{8}{3}\right)^2 =\frac{16+16+64}{9} =\frac{96}{9} \approx 10.67. $$

The zero signed total does not mean the line predicts every observation well. The correction is to calculate each residual, square it, and then add the squares. Least squares minimizes that squared total—not the signed sum and not the sum of absolute residuals.

Key takeaway: A residual is observed response minus fitted response. Least squares selects the intercept and slope that minimize the sum of the squared vertical residuals across the observed data. The criterion measures fit according to this particular rule; it does not by itself establish causation or guarantee that a straight-line model is appropriate for every purpose.

Check Your Understanding

Try these questions without looking back.

  1. For an observation with \(y_i=12\) and a fitted response of \(10\), what is its residual? What does the sign indicate?
  2. Three residuals are \(1\), \(-2\), and \(1\). Calculate their contribution to the sum of squared residuals.
  3. Why can a line with a signed residual sum of zero still have substantial prediction errors?
  4. If every response is converted from centimeters to meters, what happens to the numerical size and units of SSR?
  5. Does finding the lowest SSR among three candidate lines prove that one of them is the least-squares line? Explain.