In this article,I have predicted the fate of the passengers aboard the RMS Titanic, which famously sank in the Atlantic ocean during its maiden voyage from the UK to New York City after colliding with an iceberg.The two Titanic dataset (train and testw) have been downloaded from Kaggle. The test set is smaller, with only 418 passengers' fate to predict, and only 11 variables since the "Survived" column is missing.The challenge is to predict it with most accuracy.
The Training set, complete with the outcome (or target variable-Survived) for a group of passengers as well as a collection of other parameters such as their age, gender,sex, etc. This is the dataset on which we train our predictive model.We see that there are 891 observations (rows) in the training set with 12 variables each.
The Test set is smaller, with only 418 passengers' fate to predict, and only 11 variables since the "Survived" column is missing.The challenge is to predict the now unknown target variable based on the other passenger attributes that are provided for both datasets.
Import data
train <- read.csv("~/titanic.train.csv")
test <- read.csv("~/titanic.test.csv")
str(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/ 148 levels "","A10","A14",..: 1 83 1 57 1 1 131 1 1 1 ...
## $ Embarked : Factor w/ 4 levels "","C","Q","S": 4 2 4 4 4 3 4 4 4 2 ...
We first analyse the basic summary and see that, 342 passengers survived,in the Training Set, while 549 died.
table(train$Survived)
##
## 0 1
## 549 342
prop.table(table(train$Survived))
##
## 0 1
## 0.6161616 0.3838384
With the proportion function,we understand that 38% have survived the disaster. observing that most of the passengers aboard died,we make an initial,coarse assumption that all passsengers in the test dataset died.Okay, so we add our "everyone dies" prediction to the test set dataframe.Since there was no "Survived" column in the dataframe, we create one with.
test$Survived <- rep(0,418)
Titanic was known widely for saving 'Women and Children ' first. So, we look into the train dataframe's 'Sex' and 'Age' variables for any evident patterns .
prop.table(table(train$Sex,train$Survived),1)
##
## 0 1
## female 0.2579618 0.7420382
## male 0.8110919 0.1889081
We use the proportion table and see majority of females surviving the disaster while very low percentage of the males lived. So, we proceed to assume all females survived.
test$Survived[test$Sex == 'female'] <- 1
Moving our attention to the 'Age' variable, through the summary function we get the clear status of passenger's Age.We see that we're missing 177 passengers' age data and proceed with presuming them to be the mean Age.
summary(train$Age)
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 0.42 20.12 28.00 29.70 38.00 80.00 177
train$Age[is.na(train$Age) ] <- 29
Our previous tables were on categorical variables, ie. they only had a few values. Now we have a continuous variable (Age) which makes drawing proportion tables almost useless, as there may only be one or two passengers for each age! So, we create a new variable, "Child", to indicate whether the passenger is below the age of 18 :
train$Child <- 0
train$Child[train$Age < 18] <- 1
Aggregate Function
Using the aggregate function we subset the whole database into different combinations of Age and Sex variables. Applying the function defined below, we obtain the proportions.
aggregate(Survived ~ Child + Sex, data= train,FUN = function(x) {sum(x)/ length(x)} )
## Child Sex Survived
## 1 0 female 0.7528958
## 2 1 female 0.6909091
## 3 0 male 0.1657033
## 4 1 male 0.3965517
Well, it still appears that if a passenger is female most survive, and if they were male most don't, regardless of whether they were a child or not. So we haven't got anything to change our predictions on,here. So, we explore two potential variables; the class they were traversing in ; fare for the trip.
While the class variable is limited to a manageable 3 values, the fare is again a continuous variable and so we reduce it in order to tabulate easily . We break the fares into less than $10, between $10 and $20, $20 to $30 and more than $30 and store it separately [Fare2]
train$Fare2 <- '30+'
train$Fare2[train$Fare < 30 & train$Fare >= 20] <- '20-30'
train$Fare2[train$Fare < 20 & train$Fare >= 10] <- '10-20'
train$Fare2[train$Fare < 10] <- '<10'
Now we run a longer aggregate function to see if there's anything interesting here:
aggregate(Survived ~Fare2 + Pclass + Sex ,data = train,FUN = function(x) {sum(x)/ length(x)} )
## Fare2 Pclass Sex Survived
## 1 20-30 1 female 0.8333333
## 2 30+ 1 female 0.9772727
## 3 10-20 2 female 0.9142857
## 4 20-30 2 female 0.9000000
## 5 30+ 2 female 1.0000000
## 6 <10 3 female 0.5937500
## 7 10-20 3 female 0.5813953
## 8 20-30 3 female 0.3333333
## 9 30+ 3 female 0.1250000
## 10 <10 1 male 0.0000000
## 11 20-30 1 male 0.4000000
## 12 30+ 1 male 0.3837209
## 13 <10 2 male 0.0000000
## 14 10-20 2 male 0.1587302
## 15 20-30 2 male 0.1600000
## 16 30+ 2 male 0.2142857
## 17 <10 3 male 0.1115385
## 18 10-20 3 male 0.2368421
## 19 20-30 3 male 0.1250000
## 20 30+ 3 male 0.2400000
Prediction of Survival of test dataframe
Female in pclass3 with fare paid greater than 20$ have low survival chances.
While the majority of males, regardless of class or fare still don't do so well.
We notice that most of the class 3 women who paid more than 10$ for their ticket actually also miss out on a lifeboat.
Also, males travelling in class1 paying more than 20$ ,comparatively, have more chances of survival.
We make a new prediction based on the new insights.
test$Survived <- 0
test$Survived[test$Sex == 'female'] <- 1
test$Survived[test$Sex == 'female' & test$Pclass == 3 & test$Fare >= 10] <- 0
sum(test$Survived)
## [1] 122
test$Survived[test$Sex == 'male' & test$Pclass == 1 & test$Fare >= 20] <- 1
We have displayed the compact structure of thetest dataframe for predicted results.
str(test)
## 'data.frame': 418 obs. of 12 variables:
## $ PassengerId: int 892 893 894 895 896 897 898 899 900 901 ...
## $ Pclass : int 3 3 2 3 3 3 3 2 3 3 ...
## $ Name : Factor w/ 418 levels "Abbott, Master. Eugene Joseph",..: 210 409 273 414 182 370 85 58 5 104 ...
## $ Sex : Factor w/ 2 levels "female","male": 2 1 2 2 1 2 1 2 1 2 ...
## $ Age : num 34.5 47 62 27 22 14 30 26 18 21 ...
## $ SibSp : int 0 1 0 0 1 0 0 1 0 2 ...
## $ Parch : int 0 0 0 0 1 0 0 1 0 0 ...
## $ Ticket : Factor w/ 363 levels "110469","110489",..: 153 222 74 148 139 262 159 85 101 270 ...
## $ Fare : num 7.83 7 9.69 8.66 12.29 ...
## $ Cabin : Factor w/ 77 levels "","A11","A18",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Embarked : Factor w/ 3 levels "C","Q","S": 2 3 2 3 3 3 2 3 1 3 ...
## $ Survived : num 0 1 0 0 0 0 1 0 1 0 ...
Decision Tree
Moving on, we take a quick review of the possible variables we could look at. Last time we used aggregate and proportion tables to compare gender, age, class and fare. But we never investigated SibSp, Parch or Embarked. The remaining variables of passenger name, ticket number and cabin number are all unique identifiers for now; they don't give any new subsets that would be interesting for a decision tree. So we build a tree off everything else :
require(rpart)
## Loading required package: rpart
fit <- rpart(Survived ~ Pclass + Sex + Age + SibSp + Parch + Fare + Embarked,
data=train,
method="class")
plot(fit)
text(fit)

library('rattle')
## Rattle: A free graphical interface for data mining with R.
## Version 4.1.0 Copyright (c) 2006-2015 Togaware Pty Ltd.
## Type 'rattle()' to shake, rattle, and roll your data.
library('rpart.plot')
library('RColorBrewer')
fancyRpartPlot(fit)

Okay, now the decisions that have been found to go a lot deeper than what we saw last time when we looked for them manually. Decisions have been found for the SipSp variable, as well as the port of embarkation one that we didn't even look at. And on the male side, the kids younger than 6 years old have a better chance of survival, even if there weren't too many aboard. That resonates with the famous naval law we mentioned earlier.
Now,we make a prediction of passengers' suvival in test dataframe from this tree.We point the function to the model's fit object, which contains all of the decisions we see above, and tell it to work its magic on the test dataframe. The rpart package automatically caps the depth that the tree grows by using a metric called complexity which stops the resulting model from overfitting the data.
Prediction <- predict(fit, test, type = "class")
Prediction
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
## 0 0 0 0 1 0 1 0 1 0 0 0 1 0 1 1 0 0
## 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
## 1 0 0 0 1 0 1 0 1 0 0 0 0 0 1 0 0 0
## 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
## 1 1 0 0 0 0 0 1 1 0 0 0 1 1 0 0 1 1
## 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
## 0 0 0 0 0 1 0 0 0 1 0 1 1 0 0 1 1 0
## 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
## 1 0 1 0 0 1 0 1 1 0 0 0 0 0 1 1 1 1
## 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
## 1 0 1 0 0 0 1 0 1 0 1 0 0 0 1 0 0 0
## 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
## 0 0 0 1 1 1 1 0 0 1 0 1 1 0 1 0 0 1
## 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
## 0 1 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0 0
## 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
## 0 0 0 0 0 0 1 0 0 1 0 0 1 1 0 1 1 0
## 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
## 1 0 0 1 0 0 1 1 0 0 0 0 0 1 1 0 1 1
## 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
## 0 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 1
## 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216
## 0 1 1 1 0 1 0 0 1 0 1 0 0 0 0 1 0 0
## 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234
## 1 0 1 0 1 0 1 0 1 1 0 1 0 0 0 1 0 0
## 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
## 0 0 0 0 1 1 1 1 0 0 0 0 1 0 1 1 1 0
## 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
## 0 0 0 0 0 0 1 0 0 0 1 1 0 0 0 0 1 0
## 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
## 0 0 1 1 0 1 0 0 0 0 1 1 1 1 1 0 0 0
## 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
## 0 0 0 1 0 0 0 0 1 0 0 0 0 0 0 0 1 1
## 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
## 0 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0
## 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
## 1 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 0 0
## 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
## 0 1 0 1 0 0 0 1 1 0 0 0 1 0 1 0 0 1
## 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378
## 0 1 1 0 1 0 0 0 1 0 0 1 0 0 1 1 1 0
## 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396
## 0 0 0 0 1 1 0 1 0 0 0 0 0 1 0 0 0 1
## 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414
## 0 1 0 0 1 0 1 0 0 0 0 0 1 1 1 1 1 0
## 415 416 417 418
## 1 0 0 0
## Levels: 0 1
Accuracy Check
prop.table(table(train$Survived))
##
## 0 1
## 0.6161616 0.3838384
prop.table(table(test$Survived))
##
## 0 1
## 0.576555 0.423445
We see that 42% passengers ,from the test dataset,should be surviving which is pretty close to the 38% survival rate in the train dataset. Thus, we have predicted the survival chances with accuracy using decision tree model .