Comparing groups and proportions
Last updated
Lesson 8.6 compared two means with a t-test. Real questions come in other shapes: three parts of the day instead of two groups, a count of GCash payments instead of an amount in pesos, or eight receipts with one huge one among them. In this lesson you'll pick the test that fits the question and the data, run ANOVA, the chi-square test and the Mann–Whitney test on a week of Tindahan receipts, and see a t-test give the wrong answer on a tiny, skewed sample.
By the end of this lesson you can
- Choose a test from the question and the kind of data
- Compare three or more means with ANOVA
- Test whether a category depends on a group with the chi-square test
- Use a rank test when groups are small and skewed
Which test?
Every test in this lesson answers the same kind of question as 8.6: could a difference this big turn up by luck, if there were really no difference? What changes is the shape of the data. Two questions decide most of it:
- What did you measure? A number for each row (pesos, kilos) is compared through its mean or its ranks. A category (GCash or cash) is compared through counts and shares.
- How many groups, and what are they like? Two groups or more; separate groups or the same things measured twice; plenty of data, or a handful of skewed values.
Walk through the picker. Then choose one of the practice questions and see if your path ends at the right test.
What did you measure for each receipt, day or person?
The rest of the lesson runs four of these tests on one week of receipts from the Batangas store, 1 to 7 March 2024 (the file from 8.1).
Three or more groups: ANOVA
Do people spend more at some times of day? The first cell cuts the hours into morning (6 to 11), midday (12 to 4 pm) and evening (5 to 9 pm). Run the cells.
The means are ₱108.7, ₱112.9 and ₱107.0. You could run three t-tests, one for each pair, but that's three tests, with the problem from 8.7. One-way ANOVA (analysis of variance) asks one question instead: is any of the means different from the others?
It compares two kinds of spread. If the groups really differ, their means will be further apart than the spread of receipts inside each group would explain:
An near 1 is what luck gives. Here , : no evidence that receipt sizes differ by time of day. A big with a small p would only say that some mean differs; to find which, compare the pairs, correcting for the number of tests as in 8.7.
Counts: the chi-square test
Payment method is a category, so there's no mean to compare. Count instead: how many GCash and cash receipts in each part of the day? The question is whether the way people pay depends on the time. Step through the idea:
The test adds up the gaps between the counts you saw, (observed), and the counts you'd expect if payment didn't depend on the time, , over every cell of the table:
is the Greek letter chi, so this is the chi-square statistic. Dividing by makes a gap of 17 count for more in a small cell than in a big one. Now run it:
pd.crosstabmakes the table of counts.stats.chi2_contingency(table)gives , 2 degrees of freedom and p = 0.03, and the table of expected counts. The sum by hand matches.- The GCash share rises through the day: 26.1% of morning receipts, 31.1% at midday, 34.5% in the evening.
- To compare just two shares,
proportions_ztestfrom statsmodels takes the counts and the totals: evening against morning, p = 0.009.
The degrees of freedom are (rows − 1) × (columns − 1) = 2 × 1 = 2. The test needs expected counts of at least about 5 in every cell; for smaller tables, use Fisher's exact test (stats.fisher_exact).
Wrong way: a t-test on eight receipts
The owners trial a suki card (a loyalty card for regular customers) and look at eight receipts from card holders and eight from everyone else. Run the cells.
The card holders' mean is ₱122.50 against ₱101. Welch's t-test says p = 0.70: no evidence of a difference. A report might say "card holders spend a bit more, but it isn't significant".
Look at the values. Seven of the eight card holders spent less than every other customer. One spent ₱495, and that single receipt pulled their mean above the others'. Drop it, and the same test says p = 0.00002. A test whose answer flips on one value isn't telling you much.
The t-test compares means, and a mean follows one big value (8.1). With large groups, the CLT makes the t-test fine even for skewed data (step 5 shows it). With eight values and one far out, it isn't.
The check: before a t-test, look at each group: a strip plot or just the sorted values. If the groups are small and one value sits far from the rest, use a test that doesn't let one value decide (step 5), and report medians.
Ranks instead of values
The Mann–Whitney U test throws away the values and keeps their order. Put all 16 receipts in a line from smallest to largest; if the card made no difference, the card holders would be scattered evenly along it. counts, over every pair of one card holder and one other customer, how often the card holder spent more. Here it's of the pairs, and all 8 come from the ₱495 receipt. To the test, ₱495 is just "the biggest"; ₱5,000 would count the same.
The third cell in step 4 runs it: p = 0.010. Without the ₱495 receipt, p = 0.0003. Both say the same thing: card holders tend to spend less. That's a different finding from the t-test's, and the honest one for this data.
Do rank tests replace the t-test? Not when there's plenty of data. Run the cells: all 1,203 receipts, GCash against cash.
With 373 and 830 receipts, both tests agree there's no evidence of a difference (p = 0.93 and p = 0.66), even though receipts are skewed. Use the t-test when groups are big enough, because its answer is about means and comes with an interval in pesos. Use a rank test, or a shuffle test (8.6), when groups are small and skewed. The rank test's version for three or more groups is Kruskal–Wallis, stats.kruskal.
Check what you learned
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.
- SciPy: scipy.stats.f_oneway: one-way ANOVA, and the assumptions it makes
- SciPy: scipy.stats.chi2_contingency: the chi-square test of independence on a table of counts
- SciPy: scipy.stats.mannwhitneyu: the Mann–Whitney U rank test
- statsmodels: proportions_ztest: a z-test for one or two proportions
- Diez, Çetinkaya-Rundel and Barr, OpenIntro Statistics, 4th ed., chapters 6 and 7: tests for proportions, chi-square, and ANOVA, with worked examples
- Mann and Whitney, On a Test of Whether one of Two Random Variables is Stochastically Larger than the Other, Annals of Mathematical Statistics 18(1), 1947: the rank test this lesson uses