Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Correlation is not causation

After this lesson: I can measure a relationship and not confuse it with a cause.

60 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

What does correlation tell you?

Correlation is a number from -1 to 1 that measures how closely two measurements rise and fall together. It is easy to find, and easy to misread.

When two things move together, it is tempting to say one causes the other. This lesson shows how to measure the link and why that is not enough.

By the end of this lesson

Note

I can measure a relationship and not confuse it with a cause.

Key words

WordWhat it meansExample
Scatter plota chart with one dot for each pair of values
Correlation coefficienta number from -1 to 1 for how closely a straight line fitsr = 0.77
Positiveone value rises as the other rises
Confoundera hidden third thing that drives both values
Causationone thing really makes the other happen

The cells students fill in are left blank in this preview.

The coefficient gives direction and strength

On a scatter plot each day is one dot, with temperature across and hires up. If the dots climb from left to right, the link is positive. If they fall, it is negative.

The correlation coefficient, written r, puts that in one number. Its sign is the direction. Its size is the strength.

Worked example: how to read r

Value of rWhat the dots look likeExample
Near 1A tight line rising to the rightHires and temperature
Near -1A tight line falling to the rightHires and rain
Near 0A cloud with no straight-line trendHires and the last digit of the date

The cells students fill in are left blank in this preview.

Find r for temperature and rain

Command
docker exec jupyter python3 -c "import pandas as pd; h = pd.read_csv('hires.csv'); print(round(h['temp_c'].corr(h['hires']), 2), round(h['rain_mm'].corr(h['hires']), 2))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

You find r from gaps that move together

For each pair, find the temperature gap and the hires gap from their means. Multiply the two gaps. Add up the products. This is large and positive when high goes with high.

Then divide by the square root of the two sums of squared gaps. That scales the answer so it always lies between -1 and 1.

Worked example: r for five days

TempHiresTemp gapHires gapGaps multipliedTemp gap squaredHires gap squared
2150-8-86688647396
6190-4-46184162116
1025001400196
142604249616576
18330894752648836
Total172016019120
r1720 divided by the square root of 160 x 191200.983

The cells students fill in are left blank in this preview.

Work out r in Python

Code: python
# Temperature in degrees and hires on five days.
temps = [2, 6, 10, 14, 18]
hires = [150, 190, 250, 260, 330]

mean_t = sum(temps) / len(temps)
mean_h = sum(hires) / len(hires)

together = 0
t_squares = 0
h_squares = 0
for n in range(len(temps)):
    t_gap = temps[n] - mean_t
    h_gap = hires[n] - mean_h
    together = together + t_gap * h_gap
    t_squares = t_squares + t_gap * t_gap
    h_squares = h_squares + h_gap * h_gap

r = together / (t_squares * h_squares) ** 0.5
print("correlation:", round(r, 3))
# Reverse the hires list: [330, 260, 250, 190, 150].
# What happens to the sign of r?

Correlation only sees straight lines

The coefficient measures how well a straight line fits. A strong curved pattern can give an r near zero.

Hires rise through spring and fall again after summer. A straight line through that hill is nearly flat, so the day of the year looks almost unrelated to hires. Always look at the scatter plot as well.

Find r for the day of the year

Command
docker exec jupyter python3 -c "import pandas as pd; h = pd.read_csv('hires.csv'); print(round(h.index.to_series().corr(h['hires']), 2))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A strong link does not show a cause

Two things can move together for three reasons. One causes the other. Both are driven by a hidden third thing, a confounder. Or it is chance.

Bike hires and sunscreen sales both rise in a hot week. Sunscreen does not cause hires. The weather drives both. Only a controlled experiment, or strong knowledge of how things work, can show causation.

A common mistake

Warning

Many people think a high r proves that one thing causes the other. It does not. It shows only that they move together. Ask what else could drive both, and whether the link could be chance.

Going further

In your own notebook, draw the scatter plot of temp_c against hires, then find the correlation of temp_c with rain_mm.

It is close to zero. Say what that tells you about whether warm days are drier, and why a scatter plot still helps.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.