Least squares regression and correlation coefficient

Regression finds the straight line that best fits paired data. The correlation coefficient r measures how closely the points follow a line, from -1 to 1.

Write 2x or 2*x to multiply, ^ for powers, and ; between simultaneous equations. You can also type factor, expand, derivative of, integrate … from a to b, limit … as x->0 or 15% of 80.

Try an example

Before you start

This page is easier if you are comfortable with:

What this topic covers

The line of best fit minimises the squared vertical distances (linear regression line, line of best fit). The strength of the relationship is measured by the product moment correlation coefficient, which is always between −1 and 1.

Each part of this topic has its own page with the formula, worked examples and a solver: Linear regression line, Product moment correlation coefficient, Line of best fit.

Closely related topics: Averages and spread, Histograms and box plots, Straight lines and coordinates, Probability distributions.

What is it used for?

Correlation measures how closely two variables follow a straight line, and regression gives the equation of that line so you can make predictions.

How to solve it

  1. Work out the means of x and y.
  2. Sxy = Σ(x - x̄)(y - ȳ) and Sxx = Σ(x - x̄)².
  3. Gradient b = Sxy ÷ Sxx. Intercept a = ȳ - b x̄.
  4. r = Sxy ÷ √(Sxx × Syy).
  5. Only predict inside the range of the data (interpolation).

Worked examples

The examples marked with a button were worked out by the solver on this page. You can try each one yourself.

Example 1: scatter x: 1, 2, 3, 4, 5 y: 2, 4, 5, 4, 5

Worked out by the solver on this page. Press the button to try it yourself.

Scatter graph and regression Answer checked
Answer
y=2.2+0.6xy = 2.2 + 0.6x
Scatter graphmean012345623456Scatter graphxyGraph with x from -0.38 to 6.38 and y from 1.466 to 6.094. Shows data (5); point mean at (3, 4).
  1. Means
  2. Sums of squares and products
  3. Gradient and intercept
    The line passes through the mean point .
  4. Product moment correlation coefficient
    This is a moderate positive correlation.
  5. Check: the residuals cancel out and the slope matches r × (spread of y ÷ spread of x)

Example 2: line of best fit (1,3), (2,5), (3,7), (4,9)

Worked out by the solver on this page. Press the button to try it yourself.

Scatter graph and regression Answer checked
Answer
y=1+2xy = 1 + 2x
Scatter graphmean00.511.522.533.544.55246810Scatter graphxyGraph with x from -0.035 to 5.035 and y from 0.93 to 11.07. Shows data (4); point mean at (2.5, 6).
  1. Means
  2. Sums of squares and products
  3. Gradient and intercept
    The line passes through the mean point .
  4. Product moment correlation coefficient
    This is a strong positive correlation.
  5. Check: the residuals cancel out and the slope matches r × (spread of y ÷ spread of x)

Example 3: correlation x: 1,2,3,4 y: 8,6,4,1

Worked out by the solver on this page. Press the button to try it yourself.

Scatter graph and regression Answer checked
Answer
y=10.5−2.3xy = 10.5 - 2.3x
Scatter graphmean00.511.522.533.544.550246810Scatter graphxyGraph with x from -0.035 to 5.035 and y from -1.0805 to 10.5805. Shows data (4); point mean at (2.5, 4.75).
  1. Means
  2. Sums of squares and products
  3. Gradient and intercept
    The line passes through the mean point .
  4. Product moment correlation coefficient
    This is a strong negative correlation.
  5. Check: the residuals cancel out and the slope matches r × (spread of y ÷ spread of x)

Special cases

A few situations come up again and again. Knowing them saves time and helps you spot answers that cannot be right.

Positive correlation

r near +1

y rises as x rises.

No correlation

r near 0

No linear pattern.

Gradient

Sxy/Sxx = 30/25 = 1.2

Slope of the regression line.

Intercept

ȳ − b x̄

The line passes through the means.

Word problems

Plot the data, calculate r for the strength, and use y = a + bx with b = Sxy/Sxx to predict.

Gradient

Sxy = 30, Sxx = 25: find b.

  1. b30/25
  2. Check: 1.2 ✓1.2

Intercept

x̄ = 3, ȳ = 4, b = 1.2: find a.

  1. a = ȳ − b x̄4 − 3.6
  2. Check: the line passes through (3, 4) ✓0.4

Correlation Higher

Sxy = 30, Sxx = 25, Syy = 40: find r.

  1. r30/√1000
  2. Check: strong positive ✓0.9487

Prediction Higher

Predict y at x = 5 with y = 0.4 + 1.2x.

  1. Substitute0.4 + 6
  2. Check: 6.4 ✓6.4

Common mistakes

Correlation means causation

x causes y ✗

only association ✓

A third factor may drive both.

Extrapolating far beyond the data

predict at x = 100 ✗

be careful outside the range ✓

Unreliable.

r outside [−1, 1]

r = 1.2 ✗

check the arithmetic ✓

The coefficient is bounded.

Swapping x and y

regress x on y ✗

y on x for predicting y ✓

The lines differ.

Exam tips

  1. r between −1 and 1.
  2. b = Sxy/Sxx.
  3. Line through (x̄, ȳ).
  4. Interpolate, do not extrapolate.
  5. Correlation ≠ causation.

Practice questions

Try each one on paper, then open the answer. Use the solver above to see the full working.

1. b: Sxy = 18, Sxx = 12 Foundation

1.5

2. a: ȳ = 10, x̄ = 4, b = 2 Foundation

2

3. y at x = 6 for y = 2 + 3x Foundation

20

4. r: Sxy = 12, Sxx = 9, Syy = 16 Higher

1

5. Mean of y: 2, 4, 5, 4, 5 Higher

4

6. y at x = 10 for y = 1 + 0.5x Higher

6

Practice generator

Unlimited fresh questions with instant marking. Choose a level, work it out on paper, then type your answer.

Solve: …

Key terms

Correlation
Linear association.
Regression line
y = a + bx.
Gradient
Change in y per unit x.
Residual
Observed minus predicted.
Interpolation
Prediction inside the data range.
Extrapolation
Prediction outside the range.

Frequently asked questions

What does the correlation coefficient measure?

The strength and direction of a linear relationship.

How do I find the regression line?

b = Sxy/Sxx and a = ȳ − b x̄.

Does correlation prove cause?

No.

Which page covers the regression line?

linear regression line.

Which page covers r?

product moment correlation coefficient.

Which page covers a line of best fit?

line of best fit.

More in Statistics and the formula sheets.