Statistics for analysis · lesson 11 of 13
Parts in this lesson
Asmf.ols("y ~ x + confounder", data=df)8.10: compare like with like by adding the confounder
Bdf.groupby("group")[...].mean()4.1: look inside each group before trusting the total

Correlation, causation and Simpson's paradox

Last updated

Rainy days sell less. Does rain cause it? Maybe; or maybe something that comes with rain does. In this lesson you'll meet confounders, the hidden third variables behind many correlations; watch a trend reverse when the data are split into groups (Simpson's paradox), first in a stormy month at the Rosario store and then in a famous case from the University of California, Berkeley; draw simple causal diagrams; and see why experiments can answer questions that observing can't.

By the end of this lesson you can

  • Explain what a confounder is, and adjust for one in a regression
  • Recognise Simpson's paradox, and split data by the group that causes it
  • Draw a simple causal diagram for a question
  • Say what a randomised experiment adds

Confounders

In 8.6 and 8.10, rainy days sold fewer kilos at the Batangas store. There are several ways that could happen:

  • Rain causes it: people stay home when it rains.
  • Something else causes both. Rain comes with storms; in a storm with a typhoon signal up, the store closes early. The closing, not the rain, cuts sales.
  • Chance: a few unlucky days (8.6 dealt with this one).

A variable that affects both the thing you're changing and the thing you're measuring is a confounder. It makes the two move together even if neither affects the other. Ice cream sales and drownings rise together; hot weather drives both. Towns with more fire trucks have more fires; bigger towns have more of both.

A correlation (8.9), a difference between groups (8.6) or a regression slope (8.10) can't tell these apart on its own. It shows that two things go together, not why.

Wrong way: “rain costs us sales”

July 2024 at the Rosario store had three typhoon signals. The owners want to know how much stock to cut on rainy days. Step through the month:

Rain and sales at the Rosario store, July 2024. July 2024 at the Rosario store: each dot is a day, with its rain and the kilos sold.020406080020406080rain (mm)kilos sold
1 of 4 July 2024 at the Rosario store: each dot is a day, with its rain and the kilos sold.

Now the same 31 days in Python. Run the cells.

july.ipynb
  • With rain alone, each extra millimetre comes with 0.53 kg less (95% interval −0.67 to −0.38). Clear, and it fits what everyone expects. A report would say: "Rain costs us half a kilo per millimetre."
  • The second cell splits the days: on the 8 signal days it rained 64.1 mm on average and the store sold 27.4 kg; on ordinary days, 10.7 mm and 62.7 kg.
  • With signal in the model (8.10), the signal costs 52.59 kg, and rain's coefficient turns positive: +0.32 kg per mm (0.14 to 0.50). On a day of the same kind, more rain went with slightly more sold.

When a trend in the whole data reverses inside every group, it's called Simpson's paradox. Here the closures caused the drop, and they happened on the wettest days. Cutting stock on every rainy day would have been the wrong decision.

The check: before saying X causes Y, list what else could affect both, and look at the data group by group, or add those variables to the model. If the answer changes, the first one was confounded.

Simpson's paradox at Berkeley

In autumn 1973 the graduate school of the University of California, Berkeley admitted 44% of the men who applied and 35% of the women. It looked like bias against women, and the university asked statisticians to check. Here are the six largest departments, as published by Peter Bickel and colleagues in 1975. Run the cells.

berkeley.ipynb
  • All six together: men 44.5%, women 30.4%.
  • Department by department: women had the higher rate in four of the six (in A, 82.4% against 62.1%), and slightly lower rates in the other two.

How can women do better in most departments and worse overall? Look at where they applied. Most men applied to A and B, which admitted most applicants. Most women applied to C to F, which admitted a third or fewer of everyone. The department was a confounder: it affected both who applied (men or women) and the chance of getting in.

The authors' conclusion was that the departments admitted women at least as readily as men, and that the overall gap came from which departments women applied to. That raised a different question, about why the departments women chose were the hardest to get into, but the overall numbers on their own had pointed at the wrong one.

Causal diagrams, briefly

A causal diagram draws each variable as a box and each "affects" as an arrow. It makes your assumptions visible, and tells you what to adjust for. Three shapes come up again and again.

A confounder: one thing affects both. Adjust for it (split by it, or put it in the model), as in step 2.

typhoon signalrainkilos sold
A typhoon signal affects both the rain and whether the store closes early; closing cuts sales.

A mediator: the cause works through it. A price promotion brings more customers, and more customers buy more. Don't adjust for customers if you want the promotion's whole effect: that would remove the very path it works by.

promotioncustomerskilos sold
A promotion brings more customers, who buy more kilos.

A collider: two things both affect a third. Selecting on the third makes the two look related when they aren't. If you study only stores that made it into a "best stores" list (for location or for service), good location and good service will look like opposites among them, because a store with neither didn't make the list.

The diagram is a statement of what you believe, not something the data prove. Its value is that others can see it, argue with it, and see which comparisons it implies.

What experiments add

Adjusting works only for confounders you know about and have measured. There's always a chance of one you didn't think of.

A randomised experiment gets around that. If a coin decides which stores get the promotion, nothing about the stores (size, town, weather, owner) can affect who gets it. Every arrow into the treatment is cut, so any confounder, known or not, is balanced between the groups on average:

store sizecoin flippromotionkilos sold
When a coin decides who gets the promotion, store size can't affect who gets it, only the sales.

That's why medicine relies on randomised trials, and why online businesses run A/B tests. Lesson 8.12 is about designing and analysing one. When an experiment isn't possible (you can't randomise typhoons), the careful version is what you did in step 2: name the confounders, adjust for them, and say what you couldn't adjust for.

Check what you learned

Barangays with more sari-sari stores have more cases of dengue. What’s the most likely explanation?
Overall, a new teaching method has worse test scores. In every school that used it, it has better scores. What should you suspect?
Why does randomly choosing which stores get a promotion help?

Sources

Every example on this page was run with Python 3.14, and its output and error messages are copied from it. These pages cover the same ground in more depth.

All sources for Simple Data Analysis