Sub topic 14
Reading results carefully: missing data, confounding, and small groups
This chapter assumes you can load the Titanic train.csv into a DataFrame called df, and that you have already used isna, groupby, and pd.cut. Here you learn to check whether a result means what it appears to mean.
Missing data can change a result
Earlier you found that Age has missing values and Cabin has many more. Any statistic on Age silently skips those rows. Before you trust it, check whether the missing rows differ from the rest.
import pandas as pd
df = pd.read_csv("train.csv")
df["AgeMissing"] = df["Age"].isna()
print(df.groupby("AgeMissing")["Survived"].agg(["count", "mean"]))
print(df.groupby("AgeMissing")["Pclass"].value_counts(normalize=True).round(2))If passengers with a missing age survive at a different rate or sit in different classes, the age-based results describe only part of the data. That part may not represent everyone. Rows with a missing Age also get a missing AgeGroup, so value_counts(dropna=False) is the way to see how many are left out.
Confounding
A confounder is a third variable that is linked to both the factor you study and the outcome. It can create or hide a pattern. Fare and survival look related, but fare also tracks passenger class. Group by both to separate them.
df["Paid"] = pd.cut(df["Fare"], bins=[-1, 10, 30, 600],
labels=["low", "mid", "high"])
print(df.groupby("Paid", observed=True)["Survived"].agg(["count", "mean"]))
print(df.groupby(["Pclass", "Paid"], observed=True)["Survived"].agg(["count", "mean"]))If the fare effect shrinks inside each class, then class explains part of the first table. Grouping lets you hold one variable roughly fixed while you look at another. It does not remove every confounder, because the data may not record the others.
Small groups
A mean from a handful of rows is unstable. One passenger changes a group of four by 25 percentage points. Always print count next to mean, as in the tables above, and treat rates from groups of fewer than about 30 rows as hints only.
table = df.groupby(["Pclass", "Sex", "Embarked"])["Survived"].agg(["count", "mean"])
print(table[table["count"] < 30])Splitting by more columns makes every cell smaller. If you cut the data into many cells, expect some extreme rates by chance alone.
A checklist for each result
- How many rows does this number rest on?
- How many rows were dropped because of missing values?
- Could another column, such as class or sex, explain the pattern?
- Is the difference large compared with what small groups produce by chance?
Exercise
A table shows a survival rate of 100% for female passengers in one class and port combination. What do you check before reporting it, and why?
Show answer
Check the count column. If the group has only a few passengers, the rate is unstable and should be reported as a hint, not a finding. Also check whether the passengers in that cell differ in other ways, such as fare or age, and whether missing values removed some of them.
Exercise
Passengers with a missing Cabin value survived less often than those with a known Cabin. Does this show that having a cabin helped survival? Give one alternative explanation.
Show answer
No. It shows an association only. Cabin is mostly recorded for first-class passengers, so passenger class may confound the result. Group by Pclass and compare again to test this.