Linear Regression

Enter data points and see the best-fit line, residuals, and correlation statistics.

Loading interactive simulation...

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 not x = 0 is anywhere near the data.
ŷ
a fitted value: what the line predicts at a given x. The hat keeps it distinct from an observed y.
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 but signed, so it carries the direction too.

Where the formula comes from

  1. Write down the quantity to be minimised: S = Σ(y − a − b·x)², summed over every point.
  2. S is 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.
  3. 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 = 1 to x = 5; ask for a prediction at x = 6 and it returns ŷ(6) = 7.05 without a murmur that it is now extrapolating past every observation it was built from. A high is no protection either — it measures how well a line fits, never whether a line was the right thing to fit.

why one point can hijack the whole line 🖖

Least squares minimizes the sum of squared residuals, so a point far from the trend is penalized quadratically — it pulls the line toward itself far more than an ordinary point would. Cook's distance measures exactly that: how much the fitted line would shift if one point were removed. A high R² can hide this too — a single influential point can inflate correlation even when the rest of the cloud is nearly uncorrelated. Drag the flagged point outward and watch the slope chase it.

what the best-fit line really does 🖖

The tool draws the single straight line that sits closest to all your dots at once. "Closest" means the smallest total of the squared vertical gaps between each point and the line — those gaps are the residuals shown as little sticks. R² then reports how much of the up-and-down variation in y the line accounts for: R² = 0.8 means the line explains 80% of the spread, leaving 20% unexplained.

swap the axes and the line moves 🖖

Fitting y from x minimizes vertical gaps, but fitting x from y minimizes horizontal gaps — two different lines through the same cloud, even though both pass through the mean point (x̄, ȳ). Multiply their two slopes together and you get exactly r². So the familiar r is the geometric mean of the two regression slopes, and the two lines coincide only when the fit is perfect (r = 1).

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.

  1. Press Spearman ρ=1: the points 1, 2.5, 5, 9, 16, 28 rise at every single step, and the preset name gives away that ρ = 1. Predict Pearson's r for the same points.

    Show answer
    0.9382, with R² = 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² = 0 and the formula collapses to 1 − 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.
  2. 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.
  3. 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 only R² = 0.0137 and 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

  1. 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.

    1. The means come first, because both coefficients are built from deviations around them.

    2. 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.

    3. 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.

    4. 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.

    5. 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)

Example problems