-
Notifications
You must be signed in to change notification settings - Fork 2
/
titanic.html
651 lines (598 loc) · 64.6 KB
/
titanic.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
<!DOCTYPE html>
<html >
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Machine Learning with R</title>
<meta name="description" content="This book is about using R for machine learning purposes.">
<meta name="generator" content="bookdown 0.5.4 and GitBook 2.6.7">
<meta property="og:title" content="Machine Learning with R" />
<meta property="og:type" content="book" />
<meta property="og:description" content="This book is about using R for machine learning purposes." />
<meta name="github-repo" content="fderyckel/machinelearningwithr" />
<meta name="twitter:card" content="summary" />
<meta name="twitter:title" content="Machine Learning with R" />
<meta name="twitter:description" content="This book is about using R for machine learning purposes." />
<meta name="author" content="François de Ryckel">
<meta name="date" content="2017-12-05">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="apple-mobile-web-app-capable" content="yes">
<meta name="apple-mobile-web-app-status-bar-style" content="black">
<link rel="prev" href="model-evaluation.html">
<link rel="next" href="mushroom.html">
<script src="libs/jquery-2.2.3/jquery.min.js"></script>
<link href="libs/gitbook-2.6.7/css/style.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-bookdown.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-highlight.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-search.css" rel="stylesheet" />
<link href="libs/gitbook-2.6.7/css/plugin-fontsettings.css" rel="stylesheet" />
<style type="text/css">
div.sourceCode { overflow-x: auto; }
table.sourceCode, tr.sourceCode, td.lineNumbers, td.sourceCode {
margin: 0; padding: 0; vertical-align: baseline; border: none; }
table.sourceCode { width: 100%; line-height: 100%; }
td.lineNumbers { text-align: right; padding-right: 4px; padding-left: 4px; color: #aaaaaa; border-right: 1px solid #aaaaaa; }
td.sourceCode { padding-left: 5px; }
code > span.kw { color: #007020; font-weight: bold; } /* Keyword */
code > span.dt { color: #902000; } /* DataType */
code > span.dv { color: #40a070; } /* DecVal */
code > span.bn { color: #40a070; } /* BaseN */
code > span.fl { color: #40a070; } /* Float */
code > span.ch { color: #4070a0; } /* Char */
code > span.st { color: #4070a0; } /* String */
code > span.co { color: #60a0b0; font-style: italic; } /* Comment */
code > span.ot { color: #007020; } /* Other */
code > span.al { color: #ff0000; font-weight: bold; } /* Alert */
code > span.fu { color: #06287e; } /* Function */
code > span.er { color: #ff0000; font-weight: bold; } /* Error */
code > span.wa { color: #60a0b0; font-weight: bold; font-style: italic; } /* Warning */
code > span.cn { color: #880000; } /* Constant */
code > span.sc { color: #4070a0; } /* SpecialChar */
code > span.vs { color: #4070a0; } /* VerbatimString */
code > span.ss { color: #bb6688; } /* SpecialString */
code > span.im { } /* Import */
code > span.va { color: #19177c; } /* Variable */
code > span.cf { color: #007020; font-weight: bold; } /* ControlFlow */
code > span.op { color: #666666; } /* Operator */
code > span.bu { } /* BuiltIn */
code > span.ex { } /* Extension */
code > span.pp { color: #bc7a00; } /* Preprocessor */
code > span.at { color: #7d9029; } /* Attribute */
code > span.do { color: #ba2121; font-style: italic; } /* Documentation */
code > span.an { color: #60a0b0; font-weight: bold; font-style: italic; } /* Annotation */
code > span.cv { color: #60a0b0; font-weight: bold; font-style: italic; } /* CommentVar */
code > span.in { color: #60a0b0; font-weight: bold; font-style: italic; } /* Information */
</style>
<link rel="stylesheet" href="style.css" type="text/css" />
</head>
<body>
<div class="book without-animation with-summary font-size-2 font-family-1" data-basepath=".">
<div class="book-summary">
<nav role="navigation">
<ul class="summary">
<li><strong><a href="./">Machine Learning with R</a></strong></li>
<li class="divider"></li>
<li class="chapter" data-level="1" data-path="index.html"><a href="index.html"><i class="fa fa-check"></i><b>1</b> Prerequisites</a><ul>
<li class="chapter" data-level="1.1" data-path="index.html"><a href="index.html#pre-requisite-and-conventions"><i class="fa fa-check"></i><b>1.1</b> Pre-requisite and conventions</a></li>
<li class="chapter" data-level="1.2" data-path="index.html"><a href="index.html#organization"><i class="fa fa-check"></i><b>1.2</b> Organization</a></li>
</ul></li>
<li class="chapter" data-level="2" data-path="testinference.html"><a href="testinference.html"><i class="fa fa-check"></i><b>2</b> Tests and inferences</a><ul>
<li class="chapter" data-level="2.1" data-path="testinference.html"><a href="testinference.html#normality"><i class="fa fa-check"></i><b>2.1</b> Assumption of normality</a><ul>
<li class="chapter" data-level="2.1.1" data-path="testinference.html"><a href="testinference.html#visual-check-of-normality"><i class="fa fa-check"></i><b>2.1.1</b> Visual check of normality</a></li>
<li class="chapter" data-level="2.1.2" data-path="testinference.html"><a href="testinference.html#normality-tests"><i class="fa fa-check"></i><b>2.1.2</b> Normality tests</a></li>
</ul></li>
<li class="chapter" data-level="2.2" data-path="testinference.html"><a href="testinference.html#ttest"><i class="fa fa-check"></i><b>2.2</b> T-tests</a></li>
<li class="chapter" data-level="2.3" data-path="testinference.html"><a href="testinference.html#anova---analyse-of-variance."><i class="fa fa-check"></i><b>2.3</b> ANOVA - Analyse of variance.</a></li>
</ul></li>
<li class="chapter" data-level="3" data-path="mlr.html"><a href="mlr.html"><i class="fa fa-check"></i><b>3</b> Single & Multiple Linear Regression</a><ul>
<li class="chapter" data-level="3.1" data-path="mlr.html"><a href="mlr.html#single-variable-regression"><i class="fa fa-check"></i><b>3.1</b> Single variable regression</a></li>
<li class="chapter" data-level="3.2" data-path="mlr.html"><a href="mlr.html#multi-variables-regression"><i class="fa fa-check"></i><b>3.2</b> Multi-variables regression</a><ul>
<li class="chapter" data-level="3.2.1" data-path="mlr.html"><a href="mlr.html#predicting-wine-price-again"><i class="fa fa-check"></i><b>3.2.1</b> Predicting wine price (again!)</a></li>
</ul></li>
<li class="chapter" data-level="3.3" data-path="mlr.html"><a href="mlr.html#model-diagnostic-and-evaluation"><i class="fa fa-check"></i><b>3.3</b> Model diagnostic and evaluation</a></li>
<li class="chapter" data-level="3.4" data-path="mlr.html"><a href="mlr.html#final-example---boston-dataset---with-backward-elimination"><i class="fa fa-check"></i><b>3.4</b> Final example - Boston dataset - with backward elimination</a><ul>
<li class="chapter" data-level="3.4.1" data-path="mlr.html"><a href="mlr.html#model-diagmostic"><i class="fa fa-check"></i><b>3.4.1</b> Model diagmostic</a></li>
</ul></li>
<li class="chapter" data-level="3.5" data-path="mlr.html"><a href="mlr.html#references"><i class="fa fa-check"></i><b>3.5</b> References</a></li>
</ul></li>
<li class="chapter" data-level="4" data-path="logistic.html"><a href="logistic.html"><i class="fa fa-check"></i><b>4</b> Logistic Regression</a><ul>
<li class="chapter" data-level="4.1" data-path="logistic.html"><a href="logistic.html#introduction"><i class="fa fa-check"></i><b>4.1</b> Introduction</a></li>
<li class="chapter" data-level="4.2" data-path="logistic.html"><a href="logistic.html#the-logistic-equation."><i class="fa fa-check"></i><b>4.2</b> The logistic equation.</a></li>
<li class="chapter" data-level="4.3" data-path="logistic.html"><a href="logistic.html#performance-of-logistic-regression-model"><i class="fa fa-check"></i><b>4.3</b> Performance of Logistic Regression Model</a></li>
<li class="chapter" data-level="4.4" data-path="logistic.html"><a href="logistic.html#setting-up"><i class="fa fa-check"></i><b>4.4</b> Setting up</a></li>
<li class="chapter" data-level="4.5" data-path="logistic.html"><a href="logistic.html#example-1---graduate-admission"><i class="fa fa-check"></i><b>4.5</b> Example 1 - Graduate Admission</a></li>
<li class="chapter" data-level="4.6" data-path="logistic.html"><a href="logistic.html#example-2---diabetes"><i class="fa fa-check"></i><b>4.6</b> Example 2 - Diabetes</a><ul>
<li class="chapter" data-level="4.6.1" data-path="logistic.html"><a href="logistic.html#accounting-for-missing-values"><i class="fa fa-check"></i><b>4.6.1</b> Accounting for missing values</a></li>
<li class="chapter" data-level="4.6.2" data-path="logistic.html"><a href="logistic.html#imputting-missing-values"><i class="fa fa-check"></i><b>4.6.2</b> Imputting Missing Values</a></li>
<li class="chapter" data-level="4.6.3" data-path="logistic.html"><a href="logistic.html#roc-and-auc"><i class="fa fa-check"></i><b>4.6.3</b> ROC and AUC</a></li>
</ul></li>
<li class="chapter" data-level="4.7" data-path="logistic.html"><a href="logistic.html#references-1"><i class="fa fa-check"></i><b>4.7</b> References</a></li>
</ul></li>
<li class="chapter" data-level="5" data-path="softmax-and-multinomial-regressions.html"><a href="softmax-and-multinomial-regressions.html"><i class="fa fa-check"></i><b>5</b> Softmax and multinomial regressions</a><ul>
<li class="chapter" data-level="5.1" data-path="softmax-and-multinomial-regressions.html"><a href="softmax-and-multinomial-regressions.html#multinomial-logistic-regression"><i class="fa fa-check"></i><b>5.1</b> Multinomial Logistic Regression</a></li>
<li class="chapter" data-level="5.2" data-path="softmax-and-multinomial-regressions.html"><a href="softmax-and-multinomial-regressions.html#references-2"><i class="fa fa-check"></i><b>5.2</b> References</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="gradient-descent.html"><a href="gradient-descent.html"><i class="fa fa-check"></i><b>6</b> Gradient Descent</a><ul>
<li class="chapter" data-level="6.1" data-path="gradient-descent.html"><a href="gradient-descent.html#example-on-functions"><i class="fa fa-check"></i><b>6.1</b> Example on functions</a></li>
<li class="chapter" data-level="6.2" data-path="gradient-descent.html"><a href="gradient-descent.html#example-on-regressions"><i class="fa fa-check"></i><b>6.2</b> Example on regressions</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="knnchapter.html"><a href="knnchapter.html"><i class="fa fa-check"></i><b>7</b> KNN - K Nearest Neighbour</a><ul>
<li class="chapter" data-level="7.1" data-path="knnchapter.html"><a href="knnchapter.html#example-1.-prostate-cancer-dataset"><i class="fa fa-check"></i><b>7.1</b> Example 1. Prostate Cancer dataset</a></li>
<li class="chapter" data-level="7.2" data-path="knnchapter.html"><a href="knnchapter.html#example-2.-wine-dataset"><i class="fa fa-check"></i><b>7.2</b> Example 2. Wine dataset</a><ul>
<li class="chapter" data-level="7.2.1" data-path="knnchapter.html"><a href="knnchapter.html#understand-the-data"><i class="fa fa-check"></i><b>7.2.1</b> Understand the data</a></li>
</ul></li>
<li class="chapter" data-level="7.3" data-path="knnchapter.html"><a href="knnchapter.html#references-3"><i class="fa fa-check"></i><b>7.3</b> References</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="principal-component-analysis.html"><a href="principal-component-analysis.html"><i class="fa fa-check"></i><b>8</b> Principal Component Analysis</a><ul>
<li class="chapter" data-level="8.1" data-path="principal-component-analysis.html"><a href="principal-component-analysis.html#pca-on-an-easy-example."><i class="fa fa-check"></i><b>8.1</b> PCA on an easy example.</a></li>
<li class="chapter" data-level="8.2" data-path="principal-component-analysis.html"><a href="principal-component-analysis.html#references."><i class="fa fa-check"></i><b>8.2</b> References.</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html"><i class="fa fa-check"></i><b>9</b> Trees, Random forests and Classification</a><ul>
<li class="chapter" data-level="9.1" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#introduction-1"><i class="fa fa-check"></i><b>9.1</b> Introduction</a></li>
<li class="chapter" data-level="9.2" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#first-example."><i class="fa fa-check"></i><b>9.2</b> First example.</a></li>
<li class="chapter" data-level="9.3" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#second-example."><i class="fa fa-check"></i><b>9.3</b> Second Example.</a></li>
<li class="chapter" data-level="9.4" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#how-does-a-tree-decide-where-to-split"><i class="fa fa-check"></i><b>9.4</b> How does a tree decide where to split?</a></li>
<li class="chapter" data-level="9.5" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#third-example."><i class="fa fa-check"></i><b>9.5</b> Third example.</a></li>
<li class="chapter" data-level="9.6" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#references-4"><i class="fa fa-check"></i><b>9.6</b> References</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="model-evaluation.html"><a href="model-evaluation.html"><i class="fa fa-check"></i><b>10</b> Model Evaluation</a><ul>
<li class="chapter" data-level="10.1" data-path="model-evaluation.html"><a href="model-evaluation.html#biais-variance-tradeoff"><i class="fa fa-check"></i><b>10.1</b> Biais variance tradeoff</a></li>
<li class="chapter" data-level="10.2" data-path="model-evaluation.html"><a href="model-evaluation.html#bagging"><i class="fa fa-check"></i><b>10.2</b> Bagging</a></li>
<li class="chapter" data-level="10.3" data-path="model-evaluation.html"><a href="model-evaluation.html#crossvalidation"><i class="fa fa-check"></i><b>10.3</b> Cross Validation</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="titanic.html"><a href="titanic.html"><i class="fa fa-check"></i><b>11</b> Case Study - Predicting Survivalship on the Titanic</a><ul>
<li class="chapter" data-level="11.1" data-path="titanic.html"><a href="titanic.html#import-the-data."><i class="fa fa-check"></i><b>11.1</b> Import the data.</a></li>
<li class="chapter" data-level="11.2" data-path="titanic.html"><a href="titanic.html#tidy-the-data"><i class="fa fa-check"></i><b>11.2</b> Tidy the data</a></li>
<li class="chapter" data-level="11.3" data-path="titanic.html"><a href="titanic.html#understand-the-data-1"><i class="fa fa-check"></i><b>11.3</b> Understand the data</a><ul>
<li class="chapter" data-level="11.3.1" data-path="titanic.html"><a href="titanic.html#a.-transform-the-data"><i class="fa fa-check"></i><b>11.3.1</b> A. Transform the data</a></li>
<li class="chapter" data-level="11.3.2" data-path="titanic.html"><a href="titanic.html#a.-vizualize-with-families."><i class="fa fa-check"></i><b>11.3.2</b> A. Vizualize with families.</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="titanic.html"><a href="titanic.html#a.-visualize-with-cabins."><i class="fa fa-check"></i><b>11.4</b> A. Visualize with cabins.</a></li>
<li class="chapter" data-level="11.5" data-path="titanic.html"><a href="titanic.html#b.-transform-dealing-with-missing-data."><i class="fa fa-check"></i><b>11.5</b> B. Transform Dealing with missing data.</a><ul>
<li class="chapter" data-level="11.5.1" data-path="titanic.html"><a href="titanic.html#overview."><i class="fa fa-check"></i><b>11.5.1</b> Overview.</a></li>
<li class="chapter" data-level="11.5.2" data-path="titanic.html"><a href="titanic.html#c.-transform-more-feature-engineering-with-the-ages-and-others."><i class="fa fa-check"></i><b>11.5.2</b> C. Transform More feature engineering with the ages and others.</a></li>
</ul></li>
<li class="chapter" data-level="11.6" data-path="titanic.html"><a href="titanic.html#references.-1"><i class="fa fa-check"></i><b>11.6</b> References.</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="mushroom.html"><a href="mushroom.html"><i class="fa fa-check"></i><b>12</b> Case Study - Mushrooms Classification</a><ul>
<li class="chapter" data-level="12.1" data-path="mushroom.html"><a href="mushroom.html#import-the-data"><i class="fa fa-check"></i><b>12.1</b> Import the data</a></li>
<li class="chapter" data-level="12.2" data-path="mushroom.html"><a href="mushroom.html#tidy-the-data-1"><i class="fa fa-check"></i><b>12.2</b> Tidy the data</a></li>
<li class="chapter" data-level="12.3" data-path="mushroom.html"><a href="mushroom.html#understand-the-data-2"><i class="fa fa-check"></i><b>12.3</b> Understand the data</a><ul>
<li class="chapter" data-level="12.3.1" data-path="mushroom.html"><a href="mushroom.html#transform-the-data"><i class="fa fa-check"></i><b>12.3.1</b> Transform the data</a></li>
<li class="chapter" data-level="12.3.2" data-path="mushroom.html"><a href="mushroom.html#visualize-the-data"><i class="fa fa-check"></i><b>12.3.2</b> Visualize the data</a></li>
<li class="chapter" data-level="12.3.3" data-path="mushroom.html"><a href="mushroom.html#modeling"><i class="fa fa-check"></i><b>12.3.3</b> Modeling</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="mushroom.html"><a href="mushroom.html#communication"><i class="fa fa-check"></i><b>12.4</b> Communication</a></li>
</ul></li>
<li class="chapter" data-level="13" data-path="breastcancer.html"><a href="breastcancer.html"><i class="fa fa-check"></i><b>13</b> Case Study - Wisconsin Breast Cancer</a><ul>
<li class="chapter" data-level="13.1" data-path="breastcancer.html"><a href="breastcancer.html#import-the-data-1"><i class="fa fa-check"></i><b>13.1</b> Import the data</a></li>
<li class="chapter" data-level="13.2" data-path="breastcancer.html"><a href="breastcancer.html#tidy-the-data-2"><i class="fa fa-check"></i><b>13.2</b> Tidy the data</a></li>
<li class="chapter" data-level="13.3" data-path="breastcancer.html"><a href="breastcancer.html#understand-the-data-3"><i class="fa fa-check"></i><b>13.3</b> Understand the data</a><ul>
<li class="chapter" data-level="13.3.1" data-path="breastcancer.html"><a href="breastcancer.html#transform-the-data-1"><i class="fa fa-check"></i><b>13.3.1</b> Transform the data</a></li>
<li class="chapter" data-level="13.3.2" data-path="breastcancer.html"><a href="breastcancer.html#pre-process-the-data"><i class="fa fa-check"></i><b>13.3.2</b> Pre-process the data</a></li>
<li class="chapter" data-level="13.3.3" data-path="breastcancer.html"><a href="breastcancer.html#model-the-data-1"><i class="fa fa-check"></i><b>13.3.3</b> Model the data</a></li>
</ul></li>
<li class="chapter" data-level="13.4" data-path="breastcancer.html"><a href="breastcancer.html#references-5"><i class="fa fa-check"></i><b>13.4</b> References</a></li>
</ul></li>
</ul>
</nav>
</div>
<div class="book-body">
<div class="body-inner">
<div class="book-header" role="navigation">
<h1>
<i class="fa fa-circle-o-notch fa-spin"></i><a href="./">Machine Learning with R</a>
</h1>
</div>
<div class="page-wrapper" tabindex="-1" role="main">
<div class="page-inner">
<section class="normal" id="section-">
<div id="titanic" class="section level1">
<h1><span class="header-section-number">Chapter 11</span> Case Study - Predicting Survivalship on the Titanic</h1>
<p>This chapter demonstrates another example of classification with machine learning. Kaggle made this exercise quite popular.</p>
<p>In this study, the training and test sets have already been defined, so we</p>
<div id="import-the-data." class="section level2">
<h2><span class="header-section-number">11.1</span> Import the data.</h2>
<p>We have put our data into our google drive <a href="https://drive.google.com/open?id=0ByHtvgo2NGDMV2VBWklMNFpVaVE">here</a> and <a href="https://drive.google.com/open?id=0ByHtvgo2NGDMaFByZWRxVEJSeDg">here</a>. You can find them on Kaggle if need be.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tidyverse)
train_set <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"dataset/Kaggle_Titanic_train.csv"</span>)
test_set <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"dataset/Kaggle_Titanic_test.csv"</span>)
## Let's bind both set of data for our exploratory analysis.
df2 <-<span class="st"> </span><span class="kw">bind_rows</span>(train_set, test_set)
## Let's have a first glimpse to our data
<span class="kw">glimpse</span>(df2)</code></pre></div>
<pre><code>## Observations: 1,309
## Variables: 12
## $ PassengerId <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,...
## $ Survived <int> 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 1, 0,...
## $ Pclass <int> 3, 1, 3, 1, 3, 3, 1, 3, 3, 2, 3, 1, 3, 3, 3, 2, 3,...
## $ Name <chr> "Braund, Mr. Owen Harris", "Cumings, Mrs. John Bra...
## $ Sex <chr> "male", "female", "female", "female", "male", "mal...
## $ Age <dbl> 22, 38, 26, 35, 35, NA, 54, 2, 27, 14, 4, 58, 20, ...
## $ SibSp <int> 1, 1, 0, 1, 0, 0, 0, 3, 0, 1, 1, 0, 0, 1, 0, 0, 4,...
## $ Parch <int> 0, 0, 0, 0, 0, 0, 0, 1, 2, 0, 1, 0, 0, 5, 0, 0, 1,...
## $ Ticket <chr> "A/5 21171", "PC 17599", "STON/O2. 3101282", "1138...
## $ Fare <dbl> 7.2500, 71.2833, 7.9250, 53.1000, 8.0500, 8.4583, ...
## $ Cabin <chr> NA, "C85", NA, "C123", NA, NA, "E46", NA, NA, NA, ...
## $ Embarked <chr> "S", "C", "S", "S", "S", "Q", "S", "S", "S", "C", ...</code></pre>
</div>
<div id="tidy-the-data" class="section level2">
<h2><span class="header-section-number">11.2</span> Tidy the data</h2>
<p>One can already see that we should put <code>Survived</code>, <code>Sex</code> and <code>Embarked</code> as factor.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>Survived <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>Survived)
df2<span class="op">$</span>Sex <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>Sex)
df2<span class="op">$</span>Embarked <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>Embarked)</code></pre></div>
</div>
<div id="understand-the-data-1" class="section level2">
<h2><span class="header-section-number">11.3</span> Understand the data</h2>
<p>This step consists in massaging our variables to see if we can construct new ones or create additional meaning from what we have. This step require some additional knowledge related to the data and getting familiar with the topics at hand.</p>
<div id="a.-transform-the-data" class="section level3">
<h3><span class="header-section-number">11.3.1</span> A. Transform the data</h3>
<p>The great thing about this data set is all the features engineering one can do to increase the predictibilty power of our model.</p>
<div id="dealing-with-names." class="section level4">
<h4><span class="header-section-number">11.3.1.1</span> Dealing with names.</h4>
<p>One of the thing one can notice is the title associated with the name. The full names on their own might have little predictibility power, but the <em>title</em> in the name might have some value and can be used as an additional variables.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">glimpse</span>(df2<span class="op">$</span>Name) </code></pre></div>
<pre><code>## chr [1:1309] "Braund, Mr. Owen Harris" ...</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## gsub is never fun to use. But we need to strip the cell up to the comma,
## then everything after the point of the title.
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">'(.*,)|(</span><span class="ch">\\</span><span class="st">..*)'</span>, <span class="st">""</span>, df2<span class="op">$</span>Name)
<span class="kw">table</span>(df2<span class="op">$</span>Sex,df2<span class="op">$</span>title)</code></pre></div>
<pre><code>##
## Capt Col Don Dona Dr Jonkheer Lady Major Master Miss
## female 0 0 0 1 1 0 1 0 0 260
## male 1 4 1 0 7 1 0 2 61 0
##
## Mlle Mme Mr Mrs Ms Rev Sir the Countess
## female 2 1 0 197 2 0 0 1
## male 0 0 757 0 0 8 1 0</code></pre>
<p>Some titles are just translations from other languages. Let’s regroup those. Some other titles aren’t occuring often and would not justify to have a category on their own. We have regroup some titles under common category. There is some arbitraire in here.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Mlle"</span>, <span class="st">"Miss"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Mme"</span>, <span class="st">"Mrs"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Ms"</span>, <span class="st">"Miss"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Jonkheer"</span>, <span class="st">"Mr"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Capt|Col|Major"</span>, <span class="st">"Army"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Don|Dona|Lady|Sir|the Countess"</span>, <span class="st">"Nobility"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"Dr|Rev"</span>, <span class="st">"Others"</span>, df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>title)
df2<span class="op">$</span>title <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>title,
<span class="kw">levels</span>(df2<span class="op">$</span>title)[<span class="kw">c</span>(<span class="dv">5</span>, <span class="dv">3</span>, <span class="dv">2</span>, <span class="dv">4</span>, <span class="dv">7</span>, <span class="dv">1</span>, <span class="dv">6</span>)] )
<span class="kw">table</span>(df2<span class="op">$</span>Sex, df2<span class="op">$</span>title)</code></pre></div>
<pre><code>##
## Mrs Miss Master Mr Others Army Nobility
## female 198 264 0 0 1 0 3
## male 0 0 61 758 15 7 2</code></pre>
<p>It would be also interesting in fact to check the proportion of survivors for each type of title.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">round</span>(<span class="kw">prop.table</span>(<span class="kw">table</span>(df2<span class="op">$</span>Survived, df2<span class="op">$</span>title), <span class="dv">2</span>), <span class="dv">2</span>)</code></pre></div>
<pre><code>##
## Mrs Miss Master Mr Others Army Nobility
## 0 0.21 0.30 0.42 0.84 0.77 0.60 0.25
## 1 0.79 0.70 0.57 0.16 0.23 0.40 0.75</code></pre>
<p>We can notice that <code>Mrs</code> are more likely to survive than <code>Miss</code>. As expected, our <code>Mr</code> have a very low likelyhood of success. Our <code>Noble</code> title managed mostly to survive.</p>
<p>Our next step is to create a <code>Last_Name</code> variable. This could be helpful as the ways family have escaped the boat might hold some pattens.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">## To get the last name we strip everything after the first comma.
df2<span class="op">$</span>last_name <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">",.*"</span>, <span class="st">""</span>, df2<span class="op">$</span>Name)
## We can now put this as factor and check how many families.
df2<span class="op">$</span>last_name <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>last_name)</code></pre></div>
<p>So we have 875 different families on board of the Titanic. Of course, there might have different families with the same last name. If that’s the case, we won’t know.</p>
</div>
</div>
<div id="a.-vizualize-with-families." class="section level3">
<h3><span class="header-section-number">11.3.2</span> A. Vizualize with families.</h3>
<p>We could add a variable about the family size.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>family_size <-<span class="st"> </span>df2<span class="op">$</span>SibSp <span class="op">+</span><span class="st"> </span>df2<span class="op">$</span>Parch <span class="op">+</span><span class="st"> </span><span class="dv">1</span></code></pre></div>
<p>If we plot that to check survivalship in function of family size, one can notice interesting patterns.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span>df2[<span class="dv">1</span><span class="op">:</span><span class="dv">891</span>,]
<span class="kw">ggplot</span>(x, <span class="kw">aes</span>(<span class="dt">x =</span> family_size, <span class="dt">fill =</span> <span class="kw">factor</span>(Survived))) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_bar</span>(<span class="dt">stat =</span> <span class="st">'count'</span>, <span class="dt">position =</span> <span class="st">"dodge"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_x_continuous</span>(<span class="dt">breaks =</span> <span class="kw">c</span>(<span class="dv">1</span><span class="op">:</span><span class="dv">11</span>)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Family Size"</span>, <span class="dt">fill =</span> <span class="st">"Survived"</span>,
<span class="dt">title =</span> <span class="st">"Survivalship by Family Size"</span>) <span class="op">+</span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="kw">c</span>(<span class="fl">0.9</span>, <span class="fl">0.8</span>), <span class="dt">panel.background =</span> <span class="ot">NULL</span>)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survical_family_pic1-1.png" width="672" /></p>
<p>Obviously, we only have the survivalship for the train set of data, as we have to guess the test set of data. So from what we have, there is a clear advantage in being a family of 2, 3 or 4. We could collapse the variable <code>Family_Size</code> into 3 levels.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>family_size_type[df2<span class="op">$</span>family_size <span class="op">==</span><span class="st"> </span><span class="dv">1</span>] <-<span class="st"> "Singleton"</span>
df2<span class="op">$</span>family_size_type[df2<span class="op">$</span>family_size <span class="op"><=</span><span class="st"> </span><span class="dv">4</span> <span class="op">&</span><span class="st"> </span>df2<span class="op">$</span>family_size <span class="op">></span><span class="st"> </span><span class="dv">1</span>] <-<span class="st"> "Small"</span>
df2<span class="op">$</span>family_size_type[df2<span class="op">$</span>family_size <span class="op">></span><span class="st"> </span><span class="dv">4</span>] <-<span class="st"> "Large"</span>
df2<span class="op">$</span>family_size_type <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>family_size_type, <span class="dt">levels =</span> <span class="kw">c</span>(<span class="st">"Singleton"</span>, <span class="st">"Small"</span>, <span class="st">"Large"</span>))</code></pre></div>
<p>We can see how many people in each category, then we plot the proportion of survivers in each category.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df3 <-<span class="st"> </span>df2[<span class="dv">1</span><span class="op">:</span><span class="dv">891</span>,]
<span class="kw">table</span>(df3<span class="op">$</span>Survived, df3<span class="op">$</span>family_size_type)</code></pre></div>
<pre><code>##
## Singleton Small Large
## 0 374 123 52
## 1 163 169 10</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df3 <-<span class="st"> </span><span class="kw">as_tibble</span>(df3)
<span class="kw">library</span>(ggmosaic)
<span class="kw">ggplot</span>(<span class="dt">data =</span> df3) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_mosaic</span>(<span class="kw">aes</span>(<span class="dt">weight =</span> <span class="dv">1</span>, <span class="dt">x =</span> <span class="kw">product</span>(family_size_type),
<span class="dt">fill =</span> <span class="kw">factor</span>(Survived), <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Family Size"</span>, <span class="dt">y =</span> <span class="st">"Proportion"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="ot">NULL</span>)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survival_family_pic2-1.png" width="672" /></p>
<p>Clearly, there is an advantage in being in a family of size 2, 3 or 4; while there is a disadventage in being part of of a bigger family.</p>
<p>We can try to digg in a bit further with our new family size and titles. For people who are part of a <em>Small</em> family size, which <em>title</em> are more likely to surived?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df4 <-<span class="st"> </span>df3 <span class="op">%>%</span><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">filter</span>(family_size_type <span class="op">==</span><span class="st"> "Small"</span>)
<span class="kw">table</span>(df4<span class="op">$</span>Survived, df4<span class="op">$</span>title)</code></pre></div>
<pre><code>##
## Mrs Miss Master Mr Others Army Nobility
## 0 17 13 0 89 3 1 0
## 1 78 46 22 20 1 0 2</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> df4) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_mosaic</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">product</span>(title), <span class="dt">fill =</span> Survived)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Survivorship for Small Families in function of their title"</span>,
<span class="dt">y =</span> <span class="st">"Proportion"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="ot">NULL</span>, <span class="dt">axis.text.x =</span> <span class="kw">element_text</span>(<span class="dt">angle=</span><span class="dv">90</span>, <span class="dt">vjust=</span><span class="dv">1</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survical_title_pic1-1.png" width="672" /></p>
<p>All masters in small families have survived. Miss & Mrs in small family size have also lots of chane of survival.</p>
<p>Similarly, for people who embarked alone (<em>Singleton</em>), which <em>title</em> are more likely to surived?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df4 <-<span class="st"> </span>df3 <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(family_size_type <span class="op">==</span><span class="st"> "Singleton"</span>)
<span class="kw">table</span>(df4<span class="op">$</span>Survived, df4<span class="op">$</span>title)</code></pre></div>
<pre><code>##
## Mrs Miss Master Mr Others Army Nobility
## 0 2 25 0 337 7 2 1
## 1 19 78 0 61 2 2 1</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> df4) <span class="op">+</span><span class="st"> </span><span class="kw">geom_mosaic</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">product</span>(title), <span class="dt">fill =</span> Survived)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Survivorship for people who boarded alone in function of their title"</span>,
<span class="dt">y =</span> <span class="st">"Proportion"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="ot">NULL</span>, <span class="dt">axis.text.x =</span> <span class="kw">element_text</span>(<span class="dt">angle=</span><span class="dv">90</span>, <span class="dt">vjust=</span><span class="dv">1</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survival_title_pic2-1.png" width="672" /></p>
<p>It might not comes as clear, but we could do the same for title and gender. Vertically the stacks are ordered as <code>Singleton</code> then <code>Small</code> then <code>Large</code>.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> df3) <span class="op">+</span><span class="st"> </span><span class="kw">geom_mosaic</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">product</span>(family_size_type, title), <span class="dt">fill =</span> Survived)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Survivorship in function of family type and title summary"</span>,
<span class="dt">y =</span> <span class="st">"Proportion"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="ot">NULL</span>, <span class="dt">axis.text.x =</span> <span class="kw">element_text</span>(<span class="dt">angle=</span><span class="dv">90</span>, <span class="dt">vjust=</span><span class="dv">1</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survival_title_pic3-1.png" width="672" /></p>
</div>
</div>
<div id="a.-visualize-with-cabins." class="section level2">
<h2><span class="header-section-number">11.4</span> A. Visualize with cabins.</h2>
<p>Although there are many missing data there, we can use the cabin number given to passengers. The first letter of the cabin number correspond to the deck on the boat. So let’s strip that deck location from the cabin number.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df3<span class="op">$</span>deck <-<span class="st"> </span><span class="kw">gsub</span>(<span class="st">"([A-Z]+).*"</span>, <span class="st">"</span><span class="ch">\\</span><span class="st">1"</span>, df3<span class="op">$</span>Cabin)
df4 <-<span class="st"> </span>df3 <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(<span class="op">!</span><span class="kw">is.na</span>(deck))
<span class="kw">table</span>(df3<span class="op">$</span>Survived, df3<span class="op">$</span>deck)</code></pre></div>
<pre><code>##
## A B C D E F G T
## 0 8 12 24 8 8 5 2 1
## 1 7 35 35 25 24 8 2 0</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(<span class="dt">data =</span> df4) <span class="op">+</span><span class="st"> </span><span class="kw">geom_mosaic</span>(<span class="kw">aes</span>(<span class="dt">x =</span> <span class="kw">product</span>(deck), <span class="dt">fill =</span> Survived)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Survivorship in function of Deck Location"</span>, <span class="dt">y =</span> <span class="st">"Proportion"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">panel.background =</span> <span class="ot">NULL</span>, <span class="dt">axis.text.x =</span> <span class="kw">element_text</span>(<span class="dt">angle=</span><span class="dv">90</span>, <span class="dt">vjust=</span><span class="dv">1</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_survival_class_pic1-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">detach</span>(<span class="st">"package:ggmosaic"</span>, <span class="dt">unload=</span><span class="ot">TRUE</span>)</code></pre></div>
<p>There is a bit of an anomaly here as it almost as if most people survived. Now let’s keep in mind, that this is only for people which we have their cabin data.</p>
<p>Let’s have a look at how the <code>Passenger Class</code> are distributed on the decks. As we are also finishing this first round of feature engineering, let’s just mention also how the Passenger Class is affecting survivalship.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">table</span>(df3<span class="op">$</span>Pclass, df3<span class="op">$</span>deck)</code></pre></div>
<pre><code>##
## A B C D E F G T
## 1 15 47 59 29 25 0 0 1
## 2 0 0 0 4 4 8 0 0
## 3 0 0 0 0 3 5 4 0</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">round</span>(<span class="kw">prop.table</span>(<span class="kw">table</span>(df3<span class="op">$</span>Survived, df3<span class="op">$</span>Pclass), <span class="dv">2</span>), <span class="dv">2</span>)</code></pre></div>
<pre><code>##
## 1 2 3
## 0 0.37 0.53 0.76
## 1 0.63 0.47 0.24</code></pre>
<p>More first class people have survived than other classes.</p>
</div>
<div id="b.-transform-dealing-with-missing-data." class="section level2">
<h2><span class="header-section-number">11.5</span> B. Transform Dealing with missing data.</h2>
<div id="overview." class="section level3">
<h3><span class="header-section-number">11.5.1</span> Overview.</h3>
<p>I found this very cool package called <code>visdat</code> based on <code>ggplot2</code> that help us visualize easily missing data.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">visdat<span class="op">::</span><span class="kw">vis_dat</span>(df2)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_missingdata_pic1-1.png" width="672" /> Straight away one can see that the variables <code>cabin</code> and and <code>Age</code> have quite a lot of missing data.<br />
For more accuracy one could check</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">fun1 <-<span class="st"> </span><span class="cf">function</span>(x){<span class="kw">sum</span>(<span class="kw">is.na</span>(x))}
<span class="kw">map_dbl</span>(df2, fun1)</code></pre></div>
<pre><code>## PassengerId Survived Pclass Name
## 0 418 0 0
## Sex Age SibSp Parch
## 0 263 0 0
## Ticket Fare Cabin Embarked
## 0 1 1014 2
## title last_name family_size family_size_type
## 0 0 0 0</code></pre>
<p>So we can see some missing data in <code>Fare</code> and in <code>Embarked</code> as well.<br />
Let’s deal with these last 2 variables first.</p>
<div id="basic-replacement." class="section level4">
<h4><span class="header-section-number">11.5.1.1</span> Basic Replacement.</h4>
<p>We first start with the dessert and the variables that have few missing data. For those, one can take the median of similar data.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span><span class="kw">which</span>(<span class="kw">is.na</span>(df2<span class="op">$</span>Embarked))
<span class="kw">glimpse</span>(df2[y, ])</code></pre></div>
<pre><code>## Observations: 2
## Variables: 16
## $ PassengerId <int> 62, 830
## $ Survived <fctr> 1, 1
## $ Pclass <int> 1, 1
## $ Name <chr> "Icard, Miss. Amelie", "Stone, Mrs. George Ne...
## $ Sex <fctr> female, female
## $ Age <dbl> 38, 62
## $ SibSp <int> 0, 0
## $ Parch <int> 0, 0
## $ Ticket <chr> "113572", "113572"
## $ Fare <dbl> 80, 80
## $ Cabin <chr> "B28", "B28"
## $ Embarked <fctr> NA, NA
## $ title <fctr> Miss, Mrs
## $ last_name <fctr> Icard, Stone
## $ family_size <dbl> 1, 1
## $ family_size_type <fctr> Singleton, Singleton</code></pre>
<p>So the 2 passengers that have no data on the origin of their embarqument are 2 ladies that boarded alone and that shared the same room in first class and that paid $80.</p>
<p>Let’s see who might have paid $80 for a fare.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span>df2 <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(<span class="op">!</span><span class="kw">is.na</span>(Embarked))
<span class="kw">ggplot</span>(y, <span class="kw">aes</span>(<span class="dt">x =</span> Embarked, <span class="dt">y =</span> Fare, <span class="dt">fill =</span> <span class="kw">factor</span>(Pclass))) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_boxplot</span>() <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_y_continuous</span>(<span class="dt">labels =</span> scales<span class="op">::</span>dollar, <span class="dt">limits =</span> <span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">250</span>)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">fill =</span> <span class="st">"Passenger </span><span class="ch">\n</span><span class="st"> Class"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_hline</span>(<span class="kw">aes</span>(<span class="dt">yintercept =</span> <span class="dv">80</span>), <span class="dt">color =</span> <span class="st">"red"</span>, <span class="dt">linetype =</span> <span class="st">"dashed"</span>, <span class="dt">lwd =</span> <span class="dv">1</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="kw">c</span>(<span class="fl">0.9</span>, <span class="fl">0.8</span>), <span class="dt">panel.background =</span> <span class="ot">NULL</span>)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_missingdata_pic2-1.png" width="672" /></p>
<p>Following this graph, the 2 passengers without origin of embarcation are most likely from “C”. That said, one can argue that the 2 ladies should have embarked from “S” as this is where most people embarked as shown in this table.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">table</span>(df2<span class="op">$</span>Embarked)</code></pre></div>
<pre><code>##
## C Q S
## 270 123 914</code></pre>
<p>That said, if we filter our data for the demographics of these 2 ladies, the likelhood of coming from “S” decreased quite a bit.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">x <-<span class="st"> </span>df2 <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(Sex <span class="op">==</span><span class="st"> "female"</span>, Pclass <span class="op">==</span><span class="st"> </span><span class="dv">1</span>, family_size <span class="op">==</span><span class="st"> </span><span class="dv">1</span>)
<span class="kw">table</span>(x<span class="op">$</span>Embarked)</code></pre></div>
<pre><code>##
## C Q S
## 30 0 20</code></pre>
<p>So if we go with median price and with the demographics of the ladies, it would be more likely that they come from “C”. So let’s input that.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>Embarked[<span class="kw">c</span>(<span class="dv">62</span>, <span class="dv">830</span>)] <-<span class="st"> "C"</span></code></pre></div>
<p>Now onto that missing <code>Fare</code> data</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span><span class="kw">which</span>(<span class="kw">is.na</span>(df2<span class="op">$</span>Fare))
<span class="kw">glimpse</span>(df2[y, ])</code></pre></div>
<pre><code>## Observations: 1
## Variables: 16
## $ PassengerId <int> 1044
## $ Survived <fctr> NA
## $ Pclass <int> 3
## $ Name <chr> "Storey, Mr. Thomas"
## $ Sex <fctr> male
## $ Age <dbl> 60.5
## $ SibSp <int> 0
## $ Parch <int> 0
## $ Ticket <chr> "3701"
## $ Fare <dbl> NA
## $ Cabin <chr> NA
## $ Embarked <fctr> S
## $ title <fctr> Mr
## $ last_name <fctr> Storey
## $ family_size <dbl> 1
## $ family_size_type <fctr> Singleton</code></pre>
<p>That passenger is a male that boarded in Southampton in third class. So let’s take the median price for similar passagers.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span>df2 <span class="op">%>%</span><span class="st"> </span><span class="kw">filter</span>(Embarked <span class="op">==</span><span class="st"> "S"</span> <span class="op">&</span><span class="st"> </span>Pclass <span class="op">==</span><span class="st"> "3"</span> <span class="op">&</span><span class="st"> </span>Sex <span class="op">==</span><span class="st"> "male"</span> <span class="op">&</span><span class="st"> </span>
<span class="st"> </span>family_size <span class="op">==</span><span class="st"> </span><span class="dv">1</span> <span class="op">&</span><span class="st"> </span>Age <span class="op">></span><span class="st"> </span><span class="dv">40</span>)
<span class="kw">median</span>(y<span class="op">$</span>Fare, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</code></pre></div>
<pre><code>## [1] 7.8521</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>Fare[<span class="dv">1044</span>] <-<span class="st"> </span><span class="kw">median</span>(y<span class="op">$</span>Fare, <span class="dt">na.rm =</span> <span class="ot">TRUE</span>)</code></pre></div>
</div>
<div id="predictive-modeling-replacement." class="section level4">
<h4><span class="header-section-number">11.5.1.2</span> Predictive modeling replacement.</h4>
<p>First, we’ll focus on the <code>Age</code> variable.<br />
There are several methods to input missing data. We’ll try 2 different ones in here.<br />
But before we can go forward, we have to factorise some variables.<br />
Let’s do the same with <code>Sibsp</code> and <code>Parch</code></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>Pclass <-<span class="st"> </span><span class="kw">factor</span>(df2<span class="op">$</span>Pclass)</code></pre></div>
<p>The first method we’ll be using is with the <code>missForest</code> package.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span>df2 <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(Pclass, Sex, Fare, Embarked, title, family_size, SibSp, Parch, Age)
y <-<span class="st"> </span><span class="kw">data.frame</span>(y)
<span class="kw">library</span>(missForest)
z1 <-<span class="st"> </span><span class="kw">missForest</span>(y, <span class="dt">maxiter =</span> <span class="dv">50</span>, <span class="dt">ntree =</span> <span class="dv">500</span>)
z1 <-<span class="st"> </span>z1[[<span class="dv">1</span>]]
<span class="co"># To view the new ages</span>
<span class="co"># View(z1[[1]])</span>
<span class="kw">detach</span>(<span class="st">"package:missForest"</span>, <span class="dt">unload=</span><span class="ot">TRUE</span>)</code></pre></div>
<p>The process is fairly rapid on my computer (around 10~15 seconds)</p>
<p>Our second method takes slightly more time.<br />
This time we are using the <code>mice</code> package.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">y <-<span class="st"> </span>df2 <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(Pclass, Sex, Fare, Embarked, title, family_size, SibSp, Parch, Age)
y<span class="op">$</span>Pclass <-<span class="st"> </span><span class="kw">factor</span>(y<span class="op">$</span>Pclass)
y<span class="op">$</span>family_size <-<span class="st"> </span><span class="kw">factor</span>(y<span class="op">$</span>family_size)
y <-<span class="st"> </span><span class="kw">data.frame</span>(y)
<span class="kw">library</span>(mice)
mice_mod <-<span class="st"> </span><span class="kw">mice</span>(y, <span class="dt">method =</span> <span class="st">'rf'</span>)
z2 <-<span class="st"> </span><span class="kw">complete</span>(mice_mod)
<span class="co"># To view the new ages</span>
<span class="co">#View(z2[[1]])</span>
<span class="kw">detach</span>(<span class="st">"package:mice"</span>, <span class="dt">unload=</span><span class="ot">TRUE</span>)</code></pre></div>
<p>let’s compare both type of imputations.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">p1 <-<span class="st"> </span><span class="kw">ggplot</span>(df2, <span class="kw">aes</span>(<span class="dt">x =</span> df2<span class="op">$</span>Age)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="kw">aes</span>(<span class="dt">y =</span> ..density.., <span class="dt">fill =</span> ..count..),<span class="dt">binwidth =</span> <span class="dv">5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Age"</span>, <span class="dt">y =</span> <span class="st">"Frequency"</span>, <span class="dt">fil =</span> <span class="st">"Survived"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"none"</span>)
p1</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/titanic_missingdata_pic3-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">p2 <-<span class="st"> </span><span class="kw">ggplot</span>(z1, <span class="kw">aes</span>(<span class="dt">x =</span> z1<span class="op">$</span>Age)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="kw">aes</span>(<span class="dt">y =</span> ..density.., <span class="dt">fill =</span> ..count..),<span class="dt">binwidth =</span> <span class="dv">5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Age"</span>, <span class="dt">y =</span> <span class="st">"Frequency"</span>, <span class="dt">fil =</span> <span class="st">"Survived"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"none"</span>)
p3 <-<span class="st"> </span><span class="kw">ggplot</span>(z2, <span class="kw">aes</span>(<span class="dt">x =</span> z2<span class="op">$</span>Age)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_histogram</span>(<span class="kw">aes</span>(<span class="dt">y =</span> ..density.., <span class="dt">fill =</span> ..count..),<span class="dt">binwidth =</span> <span class="dv">5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">labs</span>(<span class="dt">x =</span> <span class="st">"Age"</span>, <span class="dt">y =</span> <span class="st">"Frequency"</span>, <span class="dt">fil =</span> <span class="st">"Survived"</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">theme</span>(<span class="dt">legend.position =</span> <span class="st">"none"</span>)
<span class="kw">multiplot</span>(p1, p2, p3, <span class="dt">cols =</span> <span class="dv">3</span>)</code></pre></div>
<p>It does seem like our second method for imputation follow better our first graph. So let’s use that one and input our predicted age into our main dataframe.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># df2$Age <- z2$Age</span></code></pre></div>
</div>
</div>
<div id="c.-transform-more-feature-engineering-with-the-ages-and-others." class="section level3">
<h3><span class="header-section-number">11.5.2</span> C. Transform More feature engineering with the ages and others.</h3>
<p>Now that we have filled the <code>NA</code> for the age variable. we can massage a bit more that variable.<br />
We can create 3 more variables: Infant from 0 to 5 years old. Child from 5 to 15 years old. Mothers if it is a woman with the variable <code>Parch</code> which is greater than one.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>infant <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">if_else</span>(df2<span class="op">$</span>Age <span class="op"><=</span><span class="st"> </span><span class="dv">5</span>, <span class="dv">1</span>, <span class="dv">0</span>))
df2<span class="op">$</span>child <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">if_else</span>((df2<span class="op">$</span>Age <span class="op">></span><span class="st"> </span><span class="dv">5</span> <span class="op">&</span><span class="st"> </span>df2<span class="op">$</span>Age <span class="op"><</span><span class="st"> </span><span class="dv">15</span>), <span class="dv">1</span>, <span class="dv">0</span>))</code></pre></div>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">df2<span class="op">$</span>mother <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">if_else</span>((df2<span class="op">$</span>Sex <span class="op">==</span><span class="st"> "female"</span> <span class="op">&</span><span class="st"> </span>df2<span class="op">$</span>Parch <span class="op">!=</span><span class="st"> </span><span class="dv">0</span>), <span class="dv">1</span>, <span class="dv">0</span>))
df2<span class="op">$</span>single <-<span class="st"> </span><span class="kw">factor</span>(<span class="kw">if_else</span>((df2<span class="op">$</span>SibSp <span class="op">+</span><span class="st"> </span>df2<span class="op">$</span>Parch <span class="op">+</span><span class="st"> </span><span class="dv">1</span> <span class="op">==</span><span class="st"> </span><span class="dv">1</span>), <span class="dv">1</span>, <span class="dv">0</span>))</code></pre></div>
</div>
</div>
<div id="references.-1" class="section level2">
<h2><span class="header-section-number">11.6</span> References.</h2>
<ul>
<li>Exploring the titanic dataset from Megan Risdal. <a href="https://www.kaggle.com/mrisdal/titanic/exploring-survival-on-the-titanic">here</a><br />
</li>
<li>The <code>visdat</code> package. <a href="https://github.com/njtierney/visdat">here</a><br />
</li>
<li>The <code>ggmosaic</code> package. <a href="https://github.com/haleyjeppson/ggmosaic">here</a></li>
</ul>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="model-evaluation.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="mushroom.html" class="navigation navigation-next " aria-label="Next page"><i class="fa fa-angle-right"></i></a>
</div>
</div>
<script src="libs/gitbook-2.6.7/js/app.min.js"></script>
<script src="libs/gitbook-2.6.7/js/lunr.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-search.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-sharing.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-fontsettings.js"></script>
<script src="libs/gitbook-2.6.7/js/plugin-bookdown.js"></script>
<script src="libs/gitbook-2.6.7/js/jquery.highlight.js"></script>
<script>
gitbook.require(["gitbook"], function(gitbook) {
gitbook.start({
"sharing": {
"github": false,
"facebook": true,
"twitter": true,
"google": false,
"weibo": false,
"instapper": false,
"vk": false,
"all": ["facebook", "google", "twitter", "weibo", "instapaper"]
},
"fontsettings": {
"theme": "white",
"family": "sans",
"size": 2
},
"edit": {
"link": "https://github.com/fderyckel/machinelearningwithr/edit/master/18-titanic.Rmd",
"text": "Suggest edit to this page"
},
"download": null,
"toc": {
"collapse": "section"
}
});
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://cdn.bootcss.com/mathjax/2.7.1/MathJax.js?config=TeX-MML-AM_CHTML";
if (location.protocol !== "file:" && /^https?:/.test(script.src))
script.src = script.src.replace(/^https?:/, '');
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>