Titanic Survival App

Richard Morris
2 May 2021

Would you have survived the Titanic?

The Titanic Survival App predicts whether or not you would have survived the Titanic tragedy based on your age, sex and whether or not you think you would have travelled 1st, 2nd or 3rd class.

  • It is based on the Titanic dataset in the R Titanic package
  • A prediction of likelihood of survival is made using a logistic regression
  • These predictions are displayed in the form of a plot
  • You can input your details to find out what your chance of survival would have been

The app is deployed at: https://richard-morris.shinyapps.io/titanic/

The data

  • To create the app, we first load in the Titanic dataset and select the relevant features
  • Our data looks like this
'data.frame':   714 obs. of  4 variables:
 $ Survived: int  0 1 1 1 0 0 0 1 1 1 ...
 $ Class   : Factor w/ 3 levels "1","2","3": 3 1 3 1 3 1 3 3 2 3 ...
 $ Age     : num  22 38 26 35 35 54 2 27 14 4 ...
 $ Sex     : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 1 1 1 ...

The logistic regression

  • We regress whether or not the passenger survived on: (i) their age, (ii) their sex (male or female) and (iii) the class of their ticket (1st, 2nd or 3rd)
  • The output from the regression tells us that age, sex and class are all significant
glm(formula = Survived ~ Age + Sex + Class, family = binomial, 
    data = titanic)
               Estimate  Std. Error    z value     Pr(>|z|)
(Intercept)  3.77701265 0.401123305   9.416089 4.682044e-21
Age         -0.03698527 0.007655948  -4.830919 1.359041e-06
Sexmale     -2.52278092 0.207390924 -12.164375 4.811152e-34
Class2      -1.30979927 0.278065527  -4.710398 2.472337e-06
Class3      -2.58062532 0.281442020  -9.169296 4.761161e-20

Predicted probabilities of survival

  • We use our model to predict the probability of survival for each passenger
  • We add this information to our data for plotting purposes
Prob <- as.numeric(predict(model, type="response"))*100
titanic$Prob_Survival <- Prob
head(titanic)
  Survived Class Age    Sex Prob_Survival
1        0     3  22   male     10.526285
2        1     1  38 female     91.463372
3        1     3  26 female     55.842450
4        1     1  35 female     92.290788
5        0     3  35   male      6.780678
7        0     1  54   male     32.235446

Plot of predictions

plot of chunk unnamed-chunk-4

  • In the shiny app, you can select your age, sex and ticket class to find out your likelihood of survival
  • You can purchase a 1st class ticket with hindsight!