Lesson
The theory — Linear Regression
The least-squares line is the single straight line that makes the total of the squared vertical gaps between the points and the line as small as possible. Squared for two reasons: so that gaps above and below cannot cancel out, and so that one large gap counts for more than several small ones.
What each symbol means
b- the slope — how much y changes per unit of x.
a- the intercept — the fitted value of y at
x = 0, whether or notx = 0is anywhere near the data. ŷ- a fitted value: what the line predicts at a given x. The hat keeps it distinct from an observed y.
R²- the share of the variation in y that the line accounts for, between 0 and 1.
r- Pearson’s r, the correlation — the same information as
R²but signed, so it carries the direction too.
Where the formula comes from
- Write down the quantity to be minimised:
S = Σ(y − a − b·x)², summed over every point. Sis a smooth function of a and b, so at its minimum both partial derivatives are zero. Setting them to zero gives two linear equations in a and b — the normal equations.- Solving those for the slope gives
b = (n·Σxy − Σx·Σy) / (n·Σx² − (Σx)²)— the formula printed above with the five sums filled in:(5·69.7 − 15·19.8) / (5·55 − 15²) = 51.5 / 50 = 1.03.
- Assumes
- That a straight line is the right shape to begin with, and that all the error lies in y: it is the vertical gaps that get minimised, so x is treated as known exactly. Every point also carries equal weight, however far from the rest it sits.
- Breaks when
- The line has no idea where your data stopped. The default points run from
x = 1tox = 5; ask for a prediction atx = 6and it returnsŷ(6) = 7.05without a murmur that it is now extrapolating past every observation it was built from. A highR²is no protection either — it measures how well a line fits, never whether a line was the right thing to fit.
Practice
Check yourself
Predict the answer first, then use the controls above to find out. Reveal only after you have committed to a guess — that is what makes it practice.
-
Press Spearman ρ=1: the points
1, 2.5, 5, 9, 16, 28rise at every single step, and the preset name gives away thatρ = 1. Predict Pearson's r for the same points.Show answer
0.9382, withR² = 0.8803— high, and nowhere near the 1 that ρ reports. The ρ working shows why it is exactly 1: the ranks of x and y match position for position, soΣd² = 0and the formula collapses to1 − 0. Pearson is not measuring order, it is measuring straightness, and these points curve. Perfectly ordered and perfectly linear are different properties, and this is the dataset that pulls them apart. -
Same preset. The warning underneath flags point
(6, 28)as high-leverage, Cook's D ≈1.7931. Is that point an error?Show answer
No — it is as sound as every other point here. Cook's D measures how far the fitted line would move if a point were dropped, not whether the point is right, and this one moves the line a long way for a reason that has nothing to do with data quality: the trend curves, the fit is straight, and the point furthest out along that curve is the one a straight line struggles with most. Leverage describes a position, never a mistake. -
Press No correlation — a scatter with no visible trend at all. Does the page decline to fit a line?
Show answer
It never declines. Out comesŷ = 4 + 0.1429·x, printed with exactly the confidence of any other fit, and onlyR² = 0.0137and the note reading weak correlation suggest otherwise. Least squares always returns the best line available; it has no concept of “there is no relationship here”, only of “the smallest total of squared gaps”. Whether a line belonged at all is your judgement, and nothing in the output will make it for you.
Problem solved in full
-
Deriving the slope and intercept for y = 0.87 + 1.03x 5 steps
Five points, and a best-fit line of y = 0.87 + 1.03x with R² = 0.996. Derive the slope and intercept from scratch, and then prove a property of every least-squares line that the panel never mentions.
-
The means come first, because both coefficients are built from deviations around them.
-
Least squares minimises the sum of squared vertical residuals. Differentiating that sum and setting it to zero gives the normal equations, whose solution is a ratio of two sums: how x and y vary together, over how x varies alone.
-
The slope is 1.03. The intercept then follows without any further minimisation, because the second normal equation is simply the requirement that the residuals sum to zero.
-
The correlation uses the same two sums plus the variation in y. Squaring it gives R² = 0.996, the share of y's variation the line accounts for.
-
Now the property. The intercept was defined as ȳ − b·x̄, so substituting x̄ back into the line returns ȳ exactly.
Answer
The tool prints a slope of 1.03, an intercept of 0.87, R² = 0.996 and r = 0.998. The structural fact is the last step: every least-squares line passes through the mean of the data, no matter what the points are. That is not a property of this dataset, it is a consequence of the intercept formula, and it is why a regression line pivots about the centroid when you drag a point rather than sliding bodily. It is also the quickest sanity check on a fitted line: put in the mean x, and if you do not get the mean y, the fit is wrong. Try the outlier preset: it swaps in a fresh dataset, five points sitting exactly on y = 2x plus a sixth at (6, 30), and that sixth point drags the slope from 2 to 4.57 and the intercept to −6 while the line still runs exactly through the new mean, (3.5, 10).
-
Learning path
Fitting a line to data
References (1)
- Who invented least squares, and the long argument about it — Legendre published first, Gauss claimed priority: S. M. Stigler, “Gauss and the Invention of Least Squares.” The Annals of Statistics 9, 1981.