Field note · January 17, 2025
The overall average hides 2 different numbers
Real group means for one column across 2 segments, and what the pooled average conceals.
sex splits clinical-trial into 2 groups. Here is what bmi looks like inside each.
M— mean 27.32, median 27.50 (402 rows)F— mean 26.97, median 26.90 (498 rows)
Top to bottom that is 27.32 against 26.97, a spread of 1.3%. The pooled average is 27.13.
select sex,
count(*) as rows,
round(avg(bmi)::numeric, 2) as mean,
percentile_cont(0.5) within group (order by bmi) as median
from clinical_trial
group by 1
order by mean desc;The groups are close enough that the pooled average is a fair summary. That is worth confirming rather than assuming: the check costs one query and the failure mode is invisible.
Notice the mean and median columns disagree only slightly here. Always compute both in the group-by. The comparison between them per segment is free and tells you whether you are looking at a level difference or a tail difference.
This is the setup for Simpson's paradox — the case where every segment moves one way and the total moves the other.