Fitting a Line

ECON 250 · Chapter 18 · move the sliders and watch what happens

Three steps. First a line on its own, with nothing to fit. Then the same line dropped onto the Camry data from lecture, where you try to find the best one by hand. Then the formula that finds it for you — and why covariance and variance are what turn up in it.

1. A line, before any data

A straight line needs two numbers. The intercept says where it crosses the vertical axis; the slope says how far it climbs for a one-unit step to the right. School writes it y = mx + b; statistics writes the same line y = b₀ + b₁x, putting the intercept first.

y = 10.00 + 2.00x

10.0
2.0

The dashed triangle is one step right and 2.00 up. That ratio is the slope.

Things to try. Set the slope to zero — what is the line now saying about x? Make it negative, and watch the triangle flip. Change only the intercept: does the steepness change at all?

2. Now put data under it

The hundred Camrys from lecture. x is the odometer reading in thousands of miles, y is the auction price in thousands of dollars. No line passes through all hundred points, so every line misses. The thin red gaps are the residuals: what the line missed at each car.

We square them and add. Squaring stops a miss above cancelling a miss below, and punishes one large miss more than several small ones. That total is SSE. Hunt for the smallest you can find.

ŷ = 17.25 − 0.0669x

19.00
-0.1200
SSE = — best so far: —

The first eight cars, at the line you have set:

odometerpriceŷresidualresidual²

… and 92 more rows, all of them counted in the SSE above.

Things to try. Get SSE below 11 by hand — how close to 10.4447 can you get before it is guesswork? Find a line whose residuals sum to roughly zero but whose SSE is still large; zero-sum is not the same as best. Then press the button and notice the line passes through the point of means, the grey cross. It always does.

3. Why covariance and variance turn up

You do not have to hunt. Fix the intercept at its best value for whatever slope you pick — which forces the line through the point of means — and SSE becomes a function of the slope alone. That function is a parabola, and a parabola has exactly one lowest point.

Setting its derivative to zero gives the slope at the bottom, and what falls out is a ratio you already know from Chapter 4:

b₁ = sxy ÷ s²x = −2.9063 ÷ 43.4686 = −0.0669

the covariance of x and y, divided by the variance of x

So the two summaries from Chapter 4 were not idle. Covariance carries the direction and the joint movement — negative here, because higher mileage goes with lower price. Dividing by the variance of x rescales it into dollars per thousand miles. That division is what turns a number nobody can interpret into a slope anybody can.

-0.030

At b₁ = −0.030, SSE = —.

Things to try. Walk the slider to the bottom of the curve — what value does it stop at? The curve is steep at the sides and flat at the bottom: what does that flatness say about how precisely the data pins the slope down?


The hundred observations reproduce the summary statistics used in lecture. Least squares on them gives b₁ = −0.0669, b₀ = 17.250, SSE = 10.4447 and R² = 0.648 — the figures Stata reports for regress y x.