Aller au contenu

Leçon 12 · Two Variables

Comparing Numbers Across Categories

Compare a numeric variable between categories with grouped summaries, side-by-side plots and effect sizes, while watching group sizes and overlap.

"Is delivery slower in the north region?" "Do pro users spend longer in the app?" These questions pair a category with a number. The basic method is to split the data by category, summarize each part and compare. The skill lies in comparing fairly.

Grouped summaries

For each group, report at least the count, the median or mean, and a measure of spread.

Python Summaries per group

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

East has only two values. Its mean and median look like the worst, but two observations cannot support that claim. A table without the n column would have hidden the problem.

The same with pandas

groupby plus agg creates the whole table.

Python groupby with named aggregations

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

count counts non-missing values. size counts all rows. If they differ, that group has missing values, which is a finding of its own. More in groupby.

Compare whole distributions

Averages compare one point. Plots compare much more. Good choices are side-by-side box plots, strip plots that show every point (for small data), and overlaid histograms with shared bins.

Python Box plots with the points on top

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Putting n in the axis labels keeps group size in view. The small random sideways shift, called jitter, reduces the overlap between points.

How big is the difference?

State the difference in real units first: "the north mean is about 8 minutes longer". Then relate it to the spread. A standardized mean difference such as Cohen's d divides the difference in means by a pooled standard deviation. Also useful is the probability of superiority: if you pick one value from each group at random, how often is the first larger (counting ties as half)?

Python Effect size and overlap

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

There is no universal rule for what counts as a large effect. A tiny effect on millions of orders may matter; a large one in a rare case may not. Judge it against the decision. A statistical test addresses a separate question about uncertainty; see multiple testing and effect sizes.

Fair comparisons

Before you explain a gap, ask whether the groups are comparable.

Is the comparison fair?

Same period? Same weeks and seasons
Same definition? Measured the same way
Same mix? Similar kinds of cases
Enough data? No tiny groups
1 / 2

If north gets more long-distance orders, the regional gap may be a distance gap. Split by distance band and compare again. The lesson on confounders explores this idea.

Many groups

With dozens of groups, the most extreme ones are often among the smallest, because small samples vary more. Sort by the summary, but show n, and consider setting a minimum size for ranking. Report how many groups you left out.

Do not announce "store 14 is our best store" because it tops a ranking built from five orders. Next week it may be at the bottom.

Reading overlap

Two groups can differ in their averages and still overlap heavily. If the probability of superiority is close to 0.5, choosing the group tells you little about any single case. If it is close to 1 or 0, the groups are well separated. This matters when someone wants to act on individuals: a regional average cannot tell you that a particular northern delivery will be late.

Also compare more than the center. Two regions can have the same median while one has far more very slow deliveries. If the business cares about late orders, compare the share above an agreed limit, such as 45 minutes, or compare a high percentile. State the limit and why it matters before you look at the result.

A short comparison checklist

  1. How many cases does each group have, and how many are missing?
  2. Which direction is the difference, and how large is it in real units?
  3. Do the spreads or tails differ too?
  4. Could a different mix of cases explain it?
  5. Does the gap persist in another time period?

Try it yourself

Add a "late" column to the first example that counts deliveries over 35 minutes in each region, as a count and as a share. Decide whether this view changes your impression of the north-south difference.

When a mean and a median disagree

Sometimes one group has the higher mean but the lower median. This can happen when most of its cases are quick and a few are very slow. Do not pick whichever number supports your story. Plot both distributions and describe them: "Region A is usually faster, but it has more extreme delays." Then ask which matters for the decision. Total time spent depends on the mean, while the experience of a typical customer is closer to the median.

State the comparison

Write the groups in a consistent order and say what you subtracted from what. "The difference is -8 minutes" is confusing on its own. "South's mean is about 8 minutes shorter than North's" cannot be read the wrong way. Add both group sizes and the period, and use the same statistic for both groups.

Keep small groups visible

Do not drop a small group without saying so. List it with its count and mark it as too small to compare. A reader then knows that it exists and can ask for more data. Merging it into a neighbor is reasonable only when the two truly belong together.

Try it yourself

Add ten more ordinary values to the east group in the first example and see how its mean and median change. Notice how strongly the first two values shaped your earlier impression.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. Why include n in a grouped summary?

    Without n, readers cannot judge how reliable each number is.
  2. 2. What is the difference between count and size in a pandas groupby?

    A gap between them signals missing values in that group.
  3. 3. What is the best first way to state a difference between groups?

    Real units connect the result to decisions; standardized measures add context.
  4. 4. North orders travel farther than south orders. How can you check whether distance explains the delay gap?

    Comparing within bands holds distance roughly constant.

Summary

  • Summarize each group with count, center and spread.
  • Plot distributions, not only averages, and show group sizes.
  • Report differences in real units, then consider effect size and overlap.
  • Check periods, definitions and case mix before explaining a gap.
  • Extreme rankings often come from small groups.

Next: Comparing Two Categorical Variables

Votre progression est enregistrée dans ce navigateur. Aucun compte nécessaire.