Statistics & Probability

Line of Best Fit: Linear Regression and Correlation Explained

By Math Solving Space · · 2 min read

On this page
  1. The formulas
  2. Worked example 1
  3. Worked example 2: a negative trend
  4. Reading r and r²
  5. Predicting with the line
  6. Common mistakes
  7. Practice questions
  8. Frequently asked questions

A line of best fit is the straight line that follows a scatter of points as closely as possible. The least-squares line makes the total of the squared vertical gaps (residuals) as small as it can be. The correlation coefficient rr, between −1-1 and 11, says how tightly the points hug a straight line.

The formulas

m=n∑xy−∑x∑yn∑x2−(∑x)2c=yˉ−mxˉm = \frac{n\sum xy - \sum x \sum y}{n\sum x^2 - (\sum x)^2} \qquad c = \bar{y} - m\bar{x}

The line always passes through the mean point (xˉ,yˉ)(\bar{x}, \bar{y}).

Worked example 1

xx 1 2 3 4 5 6
yy 2.1 3.9 6.2 7.8 10.1 12.2

Working through the sums gives m=2.02m = 2.02 and c=−0.02c = -0.02:

y=2.02x−0.02y = 2.02x - 0.02

Each step of 1 in xx adds about 2 to yy, and rr is very close to 1.

Worked example 2: a negative trend

For x=10,20,30,40,50x = 10, 20, 30, 40, 50 and y=95,80,70,52,40y = 95, 80, 70, 52, 40, the line is y=−1.38x+108.8y = -1.38x + 108.8. The negative slope means yy falls as xx rises.

Reading r and r²

rr Relationship
close to 11 strong positive
close to 00 little or no linear relationship
close to −1-1 strong negative

r2r^2 is the fraction of the variation in yy that the line explains: r2=0.95r^2 = 0.95 means 95%.

Predicting with the line

Substitute an xx-value: with y=2.02x−0.02y = 2.02x - 0.02, at x=7x = 7 we predict y≈14.12y \approx 14.12. Predicting far outside your data range (extrapolation) is risky.

Common mistakes

  • Swapping xx and yy: the line for predicting yy from xx is different.
  • Trusting a line when rr is near 0.
  • Extrapolating far beyond the data.

Practice questions

  1. If r=−0.9r = -0.9, what kind of relationship is it?
  2. If r=0.6r = 0.6, what is r2r^2?
  3. Using y=2.02x−0.02y = 2.02x - 0.02, predict yy at x=10x = 10.

Answers: 1) strong negative 2) 0.36 3) 20.18

Paste your own data into the linear regression calculator to get the line, rr, and a scatter plot with residuals. For single-variable summaries, see mean, median and mode.

Frequently asked questions

Why square the residuals?

Squaring makes all gaps positive and penalises big misses more, and it gives a neat formula for the best line.

What is a residual?

The vertical distance between a data point and the line: actual y minus predicted y.

Can a curve fit better than a line?

Yes. If the scatter bends, a curved model may fit better than a straight line.

Open the calculator →

#statistics#regression#data