Statistics for analysis · lesson 8 of 13
Parts in this lesson
Astats.f_oneway(a, b, c)one-way ANOVA: do any of several means differ?
Bstats.chi2_contingency(table)chi-square test on a table of counts
Cproportions_ztest(counts, totals)two proportions, from statsmodels
Dstats.mannwhitneyu(a, b)a test on ranks, for small or skewed groups

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.

Which test?answer each question
Practise on a question (optional)

What did you measure for each receipt, day or person?

0 of 6 questions matched

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.

receipts.ipynb

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:

F=variation between the group meansvariation within the groups

An F near 1 is what luck gives. Here F=0.72, p=0.49: no evidence that receipt sizes differ by time of day. A big F 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 chi-square test, step by step. What we saw: GCash receipts in each part of the day. The evening has the most, but it also has the most receipts of any kind (496).94 of 360108 of 347171 of 496morningmiddayevening050100150200GCash receipts
1 of 4 What we saw: GCash receipts in each part of the day. The evening has the most, but it also has the most receipts of any kind (496).

The test adds up the gaps between the counts you saw, O (observed), and the counts you'd expect if payment didn't depend on the time, E, over every cell of the table:

χ2=∑(O−E)2E

χ is the Greek letter chi, so this is the chi-square statistic. Dividing by E makes a gap of 17 count for more in a small cell than in a big one. Now run it:

receipts.ipynb
  • pd.crosstab makes the table of counts. stats.chi2_contingency(table) gives χ2=6.8, 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_ztest from 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.

suki_card.ipynb

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. U counts, over every pair of one card holder and one other customer, how often the card holder spent more. Here it's U=8 of the 8×8=64 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.

receipts.ipynb

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

You want to know whether customers in the five towns differ in how often they pay by GCash. Which test?
Predict, then work it out: a cell has 20 observed and 25 expected. What does it add to χ²?
ANOVA on four stores gives p = 0.001. What does that tell you?

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