The Line of Best Fit: Ordinary Least Squares
Ordinary least squares picks the line that makes the squared gaps between data and line as small as possible. Here is how to compute it by hand.
Key takeaways
- A regression line has the form ŷ = b₀ + b₁x, where b₀ is the intercept and b₁ is the slope.
- A residual is the actual value minus the line's prediction for that point.
- OLS chooses the line that makes the sum of squared residuals as small as possible.
- The slope equals the sum of (x − x̄)(y − ȳ) divided by the sum of (x − x̄)², and the line always passes through the point of averages.
- OLS describes the data you have. Using it far outside that range is risky.
Five friends in a statistics class compare notes. They each studied a different number of hours for a quiz: 1, 2, 3, 4 and 5. Their scores were 55, 60, 70, 75 and 90. You scatter the five dots on a graph and ask a simple question: if a sixth friend studies 3.5 hours, what score should we guess?
Your eye can draw a line through the dots. But your eye and a friend's eye would draw slightly different lines, and in real research there may be 10,000 dots. We need a rule that gives everyone the same line. The rule that nearly all of economics uses is called ordinary least squares, or OLS. By the end of this lesson you will compute it with nothing but a pencil.
This lesson picks up from Correlation Is Not Causation. The line we draw here describes a pattern. Whether the pattern is a cause is a separate question.
The line and its parts
A straight line can be written as:
ŷ = b₀ + b₁ × x Here x is the variable we use to predict (hours studied). The ŷ, read "y-hat," is the line's prediction for the outcome (the score). The intercept b₀ is where the line crosses the vertical axis, meaning the prediction when x is 0. The slope b₁ is how much the prediction rises when x rises by 1.
The small hats and b's matter. We write b for numbers estimated from data. Later you will see β (beta) for the true, unknown value that the b is trying to guess.
Residuals: how wrong is the line at each dot?
No line passes through every dot. For each observation, the gap between the real value and the line's prediction is called the residual:
residual = actual y − predicted ŷ A positive residual means the dot sits above the line. A negative one means it sits below. A good line has small residuals overall. But how do we add them up? If we just summed them, big positives and big negatives would cancel, and a terrible line could look perfect.
Why squares?
OLS squares each residual before adding. Squaring does two useful things. It makes every gap positive, so no canceling. And it punishes big misses more than small ones: a miss of 4 counts 16, while a miss of 2 counts only 4. One huge miss is worse than two small ones, which usually matches how we feel about errors.
OLS finds the one line that makes the sum of squared residuals as small as possible. "Ordinary" means this is the plain, standard version. "Least squares" names the thing being minimized. There are other ways to measure fit, such as adding absolute values, but squaring leads to clean formulas, and under good conditions it gives very reliable estimates, as The Classical Assumptions explains.
The recipe
You do not need to search for the best line by trial and error. Calculus (and, if you like, algebra) shows that the minimizing line has these exact formulas:
slope b₁ = Σ(x − x̄)(y − ȳ) ÷ Σ(x − x̄)²
intercept b₀ = ȳ − b₁ × x̄ The symbol Σ means "add up over all observations," and x̄ ("x-bar") means the average of x. The top of the slope fraction measures how x and y move together. The bottom measures how spread out x is. So the slope is "co-movement per unit of spread."
The second formula says the line always passes through the point (x̄, ȳ), the point of averages. That is a handy check.
Worked example: the study-hours line
Our data again:
Hours (x): 1 2 3 4 5
Score (y): 55 60 70 75 90 Step 1: averages. x̄ = (1+2+3+4+5) ÷ 5 = 3. ȳ = (55+60+70+75+90) ÷ 5 = 350 ÷ 5 = 70.
Step 2: gaps and products.
x y x−3 y−70 (x−3)(y−70) (x−3)²
1 55 −2 −15 30 4
2 60 −1 −10 10 1
3 70 0 0 0 0
4 75 1 5 5 1
5 90 2 20 40 4
Sum: 85 10 Step 3: slope b₁ = 85 ÷ 10 = 8.5. Each extra hour of study goes with 8.5 more points.
Step 4: intercept b₀ = 70 − 8.5 × 3 = 70 − 25.5 = 44.5.
The fitted line is ŷ = 44.5 + 8.5x. For our sixth friend who studied 3.5 hours: 44.5 + 8.5 × 3.5 = 44.5 + 29.75 = 74.25. We would guess about 74.
The line passes through the point of averages (3, 70).
Checking the residuals
Now compute each prediction and its residual.
x actual predicted (44.5 + 8.5x) residual
1 55 53.0 +2.0
2 60 61.5 −1.5
3 70 70.0 0.0
4 75 78.5 −3.5
5 90 87.0 +3.0
Sum of residuals: 2.0 − 1.5 + 0 − 3.5 + 3.0 = 0 Two things to notice. First, the residuals sum to exactly zero. This is always true for OLS when the line has an intercept. Second, the squared residuals add to 4 + 2.25 + 0 + 12.25 + 9 = 27.5. No other straight line through these five dots does better than 27.5. Try b₁ = 9 with the line still through the means: the intercept becomes 70 − 27 = 43, predictions 52, 61, 70, 79, 88, residuals +3, −1, 0, −4, +2, squares 9 + 1 + 0 + 16 + 4 = 30. Higher, so worse.
Example with made-up numbers.
Why the formula makes sense
The slope formula is not magic. Look at the top of the fraction again: Σ(x − x̄)(y − ȳ). Take one friend, the one who studied 5 hours. She studied 2 hours more than average and scored 20 points above average. Both gaps are positive, so their product, 40, is positive. That tells the formula "high x goes with high y." Now picture a friend who studied a lot but scored below average. His product would be negative, and it would pull the slope down.
If most products are positive, the slope is positive. If most are negative, the slope is negative. If positives and negatives roughly cancel, the slope lands near zero, which says the line is nearly flat and x tells us little about y.
The bottom of the fraction, Σ(x − x̄)², is there to put the answer in the right units. If every friend had studied almost exactly the same number of hours, there would be very little spread in x, and a tiny difference in scores could swing the slope wildly. Dividing by the spread keeps that honest. It also hints at something we will use later: you learn more about a slope when x varies a lot. A study of five people who all studied 3 hours could never reveal what an extra hour does.
The same recipe with different units
OLS does not care what the numbers mean. Suppose a small town records the size of five homes in hundreds of square feet and the sale price in thousands of dollars. The recipe is identical: find the averages, multiply the gaps, divide the sums. If the slope comes out to 6, then each extra 100 square feet goes with about $6 thousand more in price. If you changed the size to square feet instead of hundreds of square feet, the slope would become 0.06 per square foot, a 100-fold smaller number but exactly the same relationship.
This is why you must always ask what units a slope is in before judging whether it is big or small. A slope of 0.06 sounds tiny until you learn it is thousands of dollars per single square foot.
What OLS does not promise
OLS always produces a line. It will happily fit a line through nonsense. The formulas do not check whether a straight line is a sensible shape, whether x causes y, or whether the pattern would hold in new data. Those are separate questions, and the next several lessons take them up.
Also, the line describes the typical relationship in your sample. It is a summary, the way an average is a summary. The line is not a law the friends must obey.
A common mistake: predicting far outside the data
Our friends studied 1 to 5 hours. Suppose someone asks about 20 hours. The formula gives 44.5 + 8.5 × 20 = 44.5 + 170 = 214.5. A score of 214 on a 100-point quiz is absurd. The line fit well in the range we observed, but it was never tested out there. Real relationships bend and flatten.
The safe habit is to predict only inside, or close to, the range of x values you actually saw. Going beyond it is called extrapolation, and it deserves extra suspicion. A related tip: if you read a study that says "each extra hour adds 8.5 points," ask over what range of hours that was measured.
A short history, and why it stuck
The idea of fitting a line by minimizing squared errors goes back more than two hundred years. Astronomers wanted a way to combine slightly different measurements of the same planet or comet into one best path, and least squares gave them one. Economists adopted it because it is simple, it is fast to compute, and it has a clean mathematical story: under the right conditions, no other method of the same kind does better.
Today a computer does the arithmetic in a blink, even for millions of rows. But the computer is only doing what you just did by hand, five dots at a time. When you see a regression result in a paper or a news report, picture those little gaps and squares behind it. That picture will help you spot when something is off.
Where this goes next
You now know how the line is chosen. The next question is how to read it: what does the slope mean in plain words, what does the intercept mean, and how well does the line explain the data? That is the job of Interpreting Slopes, Intercepts and R-Squared. After that, we will ask how confident we can be in a slope calculated from only five friends.
Words to know
Ordinary least squares (OLS) the method that picks the line with the smallest sum of squared residuals.
Slope how much the predicted outcome changes when the predictor rises by one unit.
Intercept the predicted outcome when the predictor equals zero.
Residual the actual value minus the value the line predicts.
Fitted value the line's prediction for an observation, written ŷ.
Sum of squared residuals the total of all squared residuals, the quantity OLS makes as small as possible.
Extrapolation using a fitted line to predict outside the range of the data it was built on.
Check yourself
- What does OLS minimize?
- Why do we square the residuals instead of just adding them?
- Using the data x = 1, 2, 3 and y = 2, 4, 9, compute the OLS slope and intercept.
- In the study-hours line, what does the slope of 8.5 mean in a sentence?
- A friend studied 4 hours and scored 75. What is the fitted value and the residual?
- Explain why predicting a score for 20 hours of study is a bad idea.
Show the answers
- The sum of the squared residuals, meaning the squared gaps between actual values and the line.
- Plain residuals above and below the line cancel out; squaring makes them all positive and punishes big misses more.
- x̄ = 2, ȳ = 5; products (−1)(−3) + 0 + (1)(4) = 3 + 0 + 4 = 7; Σ(x−2)² = 1 + 0 + 1 = 2; slope 7 ÷ 2 = 3.5; intercept 5 − 3.5 × 2 = −2.
- In this sample, each extra hour of study goes with about 8.5 more points on the quiz.
- Fitted value 44.5 + 8.5 × 4 = 78.5, and the residual is 75 − 78.5 = −3.5.
- It is far outside the 1 to 5 hours we observed, and the line would predict 214.5 on a 100-point quiz, which is impossible.