Skip to course content
Free data visualization course

Data Visualization and Dashboard Storytelling

Unit 07.02: Confounders visible in a third variable

A relationship between two variables can be entirely produced by a third.

Strong overall, absent within each group

Ad spend and sales across small and large stores, where store size drives both.

The code reports the overall correlation and the within-group ones.

import numpy as np

rng = np.random.default_rng(7)
size = rng.choice([1, 2], 200)                     # small or large stores
ads = rng.normal(50, 10, 200) + size * 30          # large stores advertise more
sales = rng.normal(100, 10, 200) + size * 60       # and sell more anyway

overall = np.corrcoef(ads, sales)[0, 1]
print(f"correlation of ad spend with sales, overall: {overall:.2f}")
for group in (1, 2):
    mask = size == group
    r = np.corrcoef(ads[mask], sales[mask])[0, 1]
    label = "small stores" if group == 1 else "large stores"
    print(f"   within {label:14} {r:>5.2f}")

print("""
Strong overall, near zero within each group. Store size drives both, and a
scatter without it shows a relationship that does not exist inside either
population.

Colour the points by the third variable. If the clusters separate, the overall
line is describing the clusters, not the relationship.
""")

Overall the correlation is strong. Within each store size it is near zero. Store size drives both variables, and the overall figure is describing the difference between two clusters rather than any relationship inside them.

Colouring the points by the third variable makes this visible immediately: if the clusters separate, the overall line is describing the separation.

The mistake this prevents

The mistake is looking for confounders only when the result is inconvenient. The check is cheap - colour by any grouping variable you have - and it should run before the finding is reported, not after someone challenges it.

Takeaway

Colour the scatter by any grouping variable you have. A relationship that disappears within groups was a relationship between the groups.