Predict survival on the Titanic


In [ ]:

About:

The sinking of the RMS Titanic is one of the most infamous shipwrecks in history. On April 15, 1912, during her maiden voyage, the Titanic sank after colliding with an iceberg, killing 1502 out of 2224 passengers and crew. This sensational tragedy shocked the international community and led to better safety regulations for ships.

One of the reasons that the shipwreck led to such loss of life was that there were not enough lifeboats for the passengers and crew. Although there was some element of luck involved in surviving the sinking, some groups of people were more likely to survive than others, such as women, children, and the upper-class.

In this challenge, we ask you to complete the analysis of what sorts of people were likely to survive. In particular, we ask you to apply the tools of machine learning to predict which passengers survived the tragedy.


In [ ]:


In [ ]:


In [72]:
# #Load the data into a dataframe
titanic_train = read.csv("train.csv", header=T, na.strings=c("","NA"))

In [73]:
# #Structure of the data
str(titanic_train)


'data.frame':	891 obs. of  12 variables:
 $ PassengerId: int  1 2 3 4 5 6 7 8 9 10 ...
 $ Survived   : int  0 1 1 1 0 0 0 0 1 1 ...
 $ Pclass     : int  3 1 3 1 3 3 1 3 3 2 ...
 $ Name       : Factor w/ 891 levels "Abbing, Mr. Anthony",..: 109 191 358 277 16 559 520 629 417 581 ...
 $ Sex        : Factor w/ 2 levels "female","male": 2 1 1 1 2 2 2 2 1 1 ...
 $ Age        : num  22 38 26 35 35 NA 54 2 27 14 ...
 $ SibSp      : int  1 1 0 1 0 0 0 3 0 1 ...
 $ Parch      : int  0 0 0 0 0 0 0 1 2 0 ...
 $ Ticket     : Factor w/ 681 levels "110152","110413",..: 524 597 670 50 473 276 86 396 345 133 ...
 $ Fare       : num  7.25 71.28 7.92 53.1 8.05 ...
 $ Cabin      : Factor w/ 147 levels "A10","A14","A16",..: NA 82 NA 56 NA NA 130 NA NA NA ...
 $ Embarked   : Factor w/ 3 levels "C","Q","S": 3 1 3 3 3 2 3 3 3 1 ...

In [74]:
head(titanic_train)


Out[74]:
PassengerIdSurvivedPclassNameSexAgeSibSpParchTicketFareCabinEmbarked
1103Braund, Mr. Owen Harrismale2210A/5 211717.25NAS
2211Cumings, Mrs. John Bradley (Florence Briggs Thayer)female3810PC 1759971.2833C85C
3313Heikkinen, Miss. Lainafemale2600STON/O2. 31012827.925NAS
4411Futrelle, Mrs. Jacques Heath (Lily May Peel)female351011380353.1C123S
5503Allen, Mr. William Henrymale35003734508.05NAS
6603Moran, Mr. JamesmaleNA003308778.4583NAQ

In [75]:
summary(titanic_train)


Out[75]:
  PassengerId       Survived          Pclass     
 Min.   :  1.0   Min.   :0.0000   Min.   :1.000  
 1st Qu.:223.5   1st Qu.:0.0000   1st Qu.:2.000  
 Median :446.0   Median :0.0000   Median :3.000  
 Mean   :446.0   Mean   :0.3838   Mean   :2.309  
 3rd Qu.:668.5   3rd Qu.:1.0000   3rd Qu.:3.000  
 Max.   :891.0   Max.   :1.0000   Max.   :3.000  
                                                 
                                    Name         Sex           Age       
 Abbing, Mr. Anthony                  :  1   female:314   Min.   : 0.42  
 Abbott, Mr. Rossmore Edward          :  1   male  :577   1st Qu.:20.12  
 Abbott, Mrs. Stanton (Rosa Hunt)     :  1                Median :28.00  
 Abelson, Mr. Samuel                  :  1                Mean   :29.70  
 Abelson, Mrs. Samuel (Hannah Wizosky):  1                3rd Qu.:38.00  
 Adahl, Mr. Mauritz Nils Martin       :  1                Max.   :80.00  
 (Other)                              :885                NA's   :177    
     SibSp           Parch             Ticket         Fare       
 Min.   :0.000   Min.   :0.0000   1601    :  7   Min.   :  0.00  
 1st Qu.:0.000   1st Qu.:0.0000   347082  :  7   1st Qu.:  7.91  
 Median :0.000   Median :0.0000   CA. 2343:  7   Median : 14.45  
 Mean   :0.523   Mean   :0.3816   3101295 :  6   Mean   : 32.20  
 3rd Qu.:1.000   3rd Qu.:0.0000   347088  :  6   3rd Qu.: 31.00  
 Max.   :8.000   Max.   :6.0000   CA 2144 :  6   Max.   :512.33  
                                  (Other) :852                   
         Cabin     Embarked  
 B96 B98    :  4   C   :168  
 C23 C25 C27:  4   Q   : 77  
 G6         :  4   S   :644  
 C22 C26    :  3   NA's:  2  
 D          :  3             
 (Other)    :186             
 NA's       :687             

In [ ]:


In [ ]:


In [ ]:


In [76]:
# #The dependent variable is "Survived" - 0 or 1 AND all the other variables are the independent variables

In [77]:
titanic_train_cleaned = titanic_train[, !(colnames(titanic_train) %in% c("Name","Ticket", "Cabin"))]

In [78]:
head(titanic_train_cleaned)


Out[78]:
PassengerIdSurvivedPclassSexAgeSibSpParchFareEmbarked
1103male22107.25S
2211female381071.2833C
3313female26007.925S
4411female351053.1S
5503male35008.05S
6603maleNA008.4583Q

In [ ]:


In [ ]:


In [79]:
# #Comparing the number of Men and Women who survived
table(titanic_train_cleaned$Survived, titanic_train_cleaned$Sex)


Out[79]:
   
    female male
  0     81  468
  1    233  109

In [80]:
# #Percentage of Women who survived
233*100/(233+81)


Out[80]:
74.203821656051

In [81]:
# #Percentage of Men who survived
109*100/(109+468)


Out[81]:
18.8908145580589

In [ ]:


In [ ]:


In [ ]:


In [82]:
# titanic_train_cleaned$Sex = as.factor(titanic_train_cleaned$Sex)
# titanic_train_cleaned$Embarked = as.factor(titanic_train_cleaned$Embarked)

In [83]:
#titanic_train_cleaned$Age = as.integer(titanic_train_cleaned$Age)

In [84]:
#titanic_train_cleaned$Fare = as.integer(titanic_train_cleaned$Fare)

In [ ]:


In [ ]:


In [85]:
model_1 = glm(Survived ~ ., data = titanic_train_cleaned, family = "binomial")

In [86]:
summary(model_1)


Out[86]:
Call:
glm(formula = Survived ~ ., family = "binomial", data = titanic_train_cleaned)

Deviance Residuals: 
    Min       1Q   Median       3Q      Max  
-2.7048  -0.6462  -0.3746   0.6171   2.4619  

Coefficients:
              Estimate Std. Error z value Pr(>|z|)    
(Intercept)  5.4741881  0.6570132   8.332  < 2e-16 ***
PassengerId  0.0003554  0.0003835   0.927  0.35410    
Pclass      -1.1958026  0.1649215  -7.251 4.15e-13 ***
Sexmale     -2.6502485  0.2232348 -11.872  < 2e-16 ***
Age         -0.0433744  0.0082343  -5.268 1.38e-07 ***
SibSp       -0.3544431  0.1296580  -2.734  0.00626 ** 
Parch       -0.0684682  0.1244979  -0.550  0.58235    
Fare         0.0014747  0.0025584   0.576  0.56433    
EmbarkedQ   -0.8148818  0.5982237  -1.362  0.17314    
EmbarkedS   -0.3979287  0.2712076  -1.467  0.14231    
---
Signif. codes:  0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1

(Dispersion parameter for binomial family taken to be 1)

    Null deviance: 960.90  on 711  degrees of freedom
Residual deviance: 631.48  on 702  degrees of freedom
  (179 observations deleted due to missingness)
AIC: 651.48

Number of Fisher Scoring iterations: 5

In [ ]: