Skip to content

Who survived the Titanic?

Who was more likely to survive the Titanic, and does a flexible model predict it better than a simple one?

Capstone 2~55 minapplied Titanic passengers and crew

The story

Of the 2,201 people aboard the Titanic, most did not survive. The record kept for each of them is short: class of travel (or crew), sex, whether a child or an adult, and whether they survived. A museum is preparing an exhibit and asks two questions. Which groups survived at higher rates, and how large were those gaps? And could a model built from these four columns have told you, for any one person, how likely they were to survive?

You will start with tables, because with four categorical columns a table is the analysis. Then you will fit a logistic regression to see each factor while holding the others fixed, and finally compare a baseline model with a gradient-boosted one on held-out folds, checking whether its probabilities can be trusted and what drives them.

Data: Titanic passengers and crew · R-datasets. datasets::Titanic (Dawson, R. J. M. (1995). The 'unusual episode' data revisited. Journal of Statistics Education, 3(3)), expanded to one row per person.

Stage 1 · insights · ~8 min

Read the record

Four categorical columns and 2,201 rows. Let Insights profile them and look at how lopsided each column is: how many crew, how many children, how many survived.

What share of the people aboard survived, and which column is most lopsided?

  1. Open Insights in the sidebar.
  2. Choose titanic (sample) in the picker at the top left.
  3. Open Survived from the column list to see how many people are in each group.Knowing the base rate first stops you mistaking an ordinary rate for a striking one.
  4. Look at the other three columns the same way. Which groups are small enough that their rates will be uncertain?

Stage 2 · analyze · ~9 min

Tabulate survival

With categorical columns, a two-way table and its χ² test are the first analysis. The test says whether survival and sex are related; Cramér's V and the table itself say how strongly. Then do the same for class.

How different were survival rates by sex and by class?

  1. Open Analyze and find the χ² test of independence.
  2. Choose titanic (sample), set Rows to Sex and Columns to Survived.
  3. Press Run.
  4. Read the table and Cramér's V. What share of women survived, and what share of men?
  5. Now set Rows to Class and press Run again.Standardized residuals point at the cells that make the test significant, here the classes that survived far more or less than average.

Stage 3 · analyze · ~8 min

Hold the others fixed

Class and sex are related to each other: the crew was almost all men. Logistic regression estimates each factor's association with survival while holding the others fixed, as an odds ratio.

Holding class and age fixed, how much higher were women's odds of survival?

  1. Open Logistic regression (binary outcome).
  2. Choose titanic (sample), set Outcome to Survived and Predictors to Class, Sex and Age.
  3. Press Run.
  4. Read the odds ratios. Each compares one group with the reference group, holding the other columns fixed.Odds ratios adjusted for the other columns separate the effect of sex from the fact that the crew was mostly men.

Stage 4 · model · ~19 min

A baseline first

Before anything clever, see what a model with no features achieves, then a random forest on the same folds. A model is only worth its complexity if it beats the baseline by more than the folds vary.

By how much does a model with features beat one that always predicts the majority?

  1. Open Predict in the sidebar.
  2. Press New experiment. An experiment is one prediction problem and every model you try on it.Keeping every model in one experiment makes them comparable, because they all use the same folds.
  3. Choose titanic (sample) in the dialog and press Create.
  4. Set Target to target. That is what the model will learn to predict.Features are suggested automatically, and columns that would leak the answer are left out with a reason.
  5. Press Validation at the bottom to choose how the models will be tested.
  6. Keep the suggested plan and press Learners at the bottom.Scores from cross-validation estimate how a model does on rows it has not seen.
  7. Select Baseline (no features). A model is only impressive if it beats this.
  8. Also select Random forest.
  9. Press Train. Both models run on the same folds.
  10. Read the leaderboard. By how much does the forest beat the baseline?Starting from a baseline tells you whether all the extra complexity earned anything at all.

Stage 5 · model · ~11 min

Boost, calibrate, explain

Add a gradient-boosted model to the same experiment. Then open the best model and check two things: whether its probabilities mean what they say (calibration), and which columns drive its predictions.

Are the model's probabilities calibrated, and which column matters most to its predictions?

  1. In your Titanic experiment, go back to the Learners step.
  2. Select XGBoost.
  3. Press Train.
  4. Open the top model on the leaderboard.
  5. Open Evaluation and find the calibration plot. When the model says 70%, do about 70% survive?Ranking people correctly and giving them honest probabilities are different skills, and a museum label needs the second.
  6. Open Explain. Which column moves the predictions most?
Hints
  • Start with the base rate. Everyone else's survival is compared with it.
  • The crew was almost all men, so sex and class are tangled together. Logistic regression separates them.
  • Compare every model with the baseline on the same folds, and look at calibration before quoting a probability.

model-card

A short report and a model card

Write the exhibit's findings as rates and odds ratios with intervals, then a model card for the best model: what it predicts, how well it beats the baseline, whether its probabilities are calibrated, and what it should not be used for.

  1. Who survived
  2. Holding the others fixed
  3. Model performance against the baseline
  4. Calibration
  5. Intended use and limits
Rubric
CriterionPoints
Gives survival rates by sex and class from the tables, not only test results.2
Explains that the odds ratios are adjusted for the other columns and are odds not probabilities.2
Compares the models with the baseline, with the fold-to-fold spread.2
States whether the predicted probabilities are calibrated.1
Says the model describes one historical event and that importance is not cause.1

Model answer

Show the model answer

Who survived. 711 of 2,201 people survived (32%). Women survived at 73% against 21% for men, and first-class passengers at 62% against 41%, 25% and 24% for second class, third class and crew. Both tables show strong associations; Cramér's V is larger for sex than for class.

Holding the others fixed. In the logistic regression, being female multiplies the odds of survival many times over, and lower classes have much lower odds than first class, each adjusted for the other columns. These are odds ratios: because survival was common among women, the ratio of probabilities is much smaller than the odds ratio. Read the exact ratios and intervals from your run.

Models. The baseline always predicts "No" and scores the base rate. The random forest and XGBoost both beat it by a wide margin on the same folds, and differ little from each other: with four categorical columns there are only 14 distinct kinds of person (the table has a few empty cells), so a flexible model has little extra to find.

Calibration and drivers. Check the calibration plot: with so few distinct inputs, the predicted probabilities sit close to the observed rates for each group. Sex dominates the explanation, then class.

Limits. The model describes one event in 1912. It is not a tool for anything else, and what it relies on is not a statement of cause.