-
Notifications
You must be signed in to change notification settings - Fork 2
/
case-study-mushrooms-classification.html
804 lines (758 loc) · 74.4 KB
/
case-study-mushrooms-classification.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
<!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-11-19">
<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="case-study-predicting-survivalship-on-the-titanic.html">
<link rel="next" href="case-study-wisconsin-breast-cancer.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>
</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> 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><ul>
<li class="chapter" data-level="3.1.1" data-path="mlr.html"><a href="mlr.html#first-example.-predicting-wine-price"><i class="fa fa-check"></i><b>3.1.1</b> First example. Predicting wine price</a></li>
</ul></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#first-example.-predicting-wine-price-1"><i class="fa fa-check"></i><b>3.2.1</b> First example. Predicting wine price</a></li>
</ul></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"><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-1"><i class="fa fa-check"></i><b>5.2</b> References</a></li>
</ul></li>
<li class="chapter" data-level="6" data-path="knnchapter.html"><a href="knnchapter.html"><i class="fa fa-check"></i><b>6</b> KNN - K Nearest Neighbour</a><ul>
<li class="chapter" data-level="6.1" data-path="knnchapter.html"><a href="knnchapter.html#example-1.-prostate-cancer-dataset"><i class="fa fa-check"></i><b>6.1</b> Example 1. Prostate Cancer dataset</a></li>
<li class="chapter" data-level="6.2" data-path="knnchapter.html"><a href="knnchapter.html#example-2.-wine-dataset"><i class="fa fa-check"></i><b>6.2</b> Example 2. Wine dataset</a><ul>
<li class="chapter" data-level="6.2.1" data-path="knnchapter.html"><a href="knnchapter.html#understand-the-data"><i class="fa fa-check"></i><b>6.2.1</b> Understand the data</a></li>
</ul></li>
<li class="chapter" data-level="6.3" data-path="knnchapter.html"><a href="knnchapter.html#references-2"><i class="fa fa-check"></i><b>6.3</b> References</a></li>
</ul></li>
<li class="chapter" data-level="7" data-path="principal-component-analysis.html"><a href="principal-component-analysis.html"><i class="fa fa-check"></i><b>7</b> Principal Component Analysis</a><ul>
<li class="chapter" data-level="7.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>7.1</b> PCA on an easy example.</a></li>
<li class="chapter" data-level="7.2" data-path="principal-component-analysis.html"><a href="principal-component-analysis.html#references."><i class="fa fa-check"></i><b>7.2</b> References.</a></li>
</ul></li>
<li class="chapter" data-level="8" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html"><i class="fa fa-check"></i><b>8</b> Trees, Random forests and Classification</a><ul>
<li class="chapter" data-level="8.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>8.1</b> Introduction</a></li>
<li class="chapter" data-level="8.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>8.2</b> First example.</a></li>
<li class="chapter" data-level="8.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>8.3</b> Second Example.</a></li>
<li class="chapter" data-level="8.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>8.4</b> How does a tree decide where to split?</a></li>
<li class="chapter" data-level="8.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>8.5</b> Third example.</a></li>
<li class="chapter" data-level="8.6" data-path="trees-random-forests-and-classification.html"><a href="trees-random-forests-and-classification.html#references-3"><i class="fa fa-check"></i><b>8.6</b> References</a></li>
</ul></li>
<li class="chapter" data-level="9" data-path="model-evaluation.html"><a href="model-evaluation.html"><i class="fa fa-check"></i><b>9</b> Model Evaluation</a><ul>
<li class="chapter" data-level="9.1" data-path="model-evaluation.html"><a href="model-evaluation.html#biais-variance-tradeoff"><i class="fa fa-check"></i><b>9.1</b> Biais variance tradeoff</a></li>
<li class="chapter" data-level="9.2" data-path="model-evaluation.html"><a href="model-evaluation.html#bagging"><i class="fa fa-check"></i><b>9.2</b> Bagging</a></li>
<li class="chapter" data-level="9.3" data-path="model-evaluation.html"><a href="model-evaluation.html#crossvalidation"><i class="fa fa-check"></i><b>9.3</b> Cross Validation</a></li>
</ul></li>
<li class="chapter" data-level="10" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html"><i class="fa fa-check"></i><b>10</b> Case Study - Predicting Survivalship on the Titanic</a><ul>
<li class="chapter" data-level="10.1" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#import-the-data."><i class="fa fa-check"></i><b>10.1</b> Import the data.</a></li>
<li class="chapter" data-level="10.2" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#tidy-the-data"><i class="fa fa-check"></i><b>10.2</b> Tidy the data</a></li>
<li class="chapter" data-level="10.3" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#understand-the-data-1"><i class="fa fa-check"></i><b>10.3</b> Understand the data</a><ul>
<li class="chapter" data-level="10.3.1" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#a.-transform-the-data"><i class="fa fa-check"></i><b>10.3.1</b> A. Transform the data</a></li>
<li class="chapter" data-level="10.3.2" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#a.-vizualize-with-families."><i class="fa fa-check"></i><b>10.3.2</b> A. Vizualize with families.</a></li>
</ul></li>
<li class="chapter" data-level="10.4" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#a.-visualize-with-cabins."><i class="fa fa-check"></i><b>10.4</b> A. Visualize with cabins.</a></li>
<li class="chapter" data-level="10.5" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#b.-transform-dealing-with-missing-data."><i class="fa fa-check"></i><b>10.5</b> B. Transform Dealing with missing data.</a><ul>
<li class="chapter" data-level="10.5.1" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#overview."><i class="fa fa-check"></i><b>10.5.1</b> Overview.</a></li>
<li class="chapter" data-level="10.5.2" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#c.-transform-more-feature-engineering-with-the-ages-and-others."><i class="fa fa-check"></i><b>10.5.2</b> C. Transform More feature engineering with the ages and others.</a></li>
</ul></li>
<li class="chapter" data-level="10.6" data-path="case-study-predicting-survivalship-on-the-titanic.html"><a href="case-study-predicting-survivalship-on-the-titanic.html#references.-1"><i class="fa fa-check"></i><b>10.6</b> References.</a></li>
</ul></li>
<li class="chapter" data-level="11" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html"><i class="fa fa-check"></i><b>11</b> Case Study - Mushrooms Classification</a><ul>
<li class="chapter" data-level="11.1" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.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="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#tidy-the-data-1"><i class="fa fa-check"></i><b>11.2</b> Tidy the data</a></li>
<li class="chapter" data-level="11.3" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#understand-the-data-2"><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="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#transform-the-data"><i class="fa fa-check"></i><b>11.3.1</b> Transform the data</a></li>
<li class="chapter" data-level="11.3.2" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#visualize-the-data"><i class="fa fa-check"></i><b>11.3.2</b> Visualize the data</a></li>
<li class="chapter" data-level="11.3.3" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#modeling"><i class="fa fa-check"></i><b>11.3.3</b> Modeling</a></li>
</ul></li>
<li class="chapter" data-level="11.4" data-path="case-study-mushrooms-classification.html"><a href="case-study-mushrooms-classification.html#communication"><i class="fa fa-check"></i><b>11.4</b> Communication</a></li>
</ul></li>
<li class="chapter" data-level="12" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html"><i class="fa fa-check"></i><b>12</b> Case Study - Wisconsin Breast Cancer</a><ul>
<li class="chapter" data-level="12.1" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#import-the-data-1"><i class="fa fa-check"></i><b>12.1</b> Import the data</a></li>
<li class="chapter" data-level="12.2" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#tidy-the-data-2"><i class="fa fa-check"></i><b>12.2</b> Tidy the data</a></li>
<li class="chapter" data-level="12.3" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#understand-the-data-3"><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="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#transform-the-data-1"><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="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#pre-process-the-data"><i class="fa fa-check"></i><b>12.3.2</b> Pre-process the data</a></li>
<li class="chapter" data-level="12.3.3" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#model-the-data-1"><i class="fa fa-check"></i><b>12.3.3</b> Model the data</a></li>
</ul></li>
<li class="chapter" data-level="12.4" data-path="case-study-wisconsin-breast-cancer.html"><a href="case-study-wisconsin-breast-cancer.html#references-4"><i class="fa fa-check"></i><b>12.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="case-study---mushrooms-classification" class="section level1">
<h1><span class="header-section-number">Chapter 11</span> Case Study - Mushrooms Classification</h1>
<p>This example demonstrates how to classify muhsrooms as edible or not. It also answer the question: what are the main characteristics of an edible mushroom?</p>
<p><a href="https://stoltzmaniac.com/random-forest-classification-of-mushrooms/">This blog post</a> gave us first the idea and we followed most of it. We also noticed that Kaggle has put online the same data set and classification exercise. We have taken inspiration from some posts <a href="https://www.kaggle.com/abhishekheads/d/uciml/mushroom-classification/walk-through-of-different-classification-models">here</a> and <a href="https://www.kaggle.com/jhuno137/d/uciml/mushroom-classification/classification-tree-using-rpart-100-accuracy">here</a></p>
<p>The data set is available on the <a href="http://archive.ics.uci.edu/ml/datasets/Mushroom">Machine Learning Repository</a> of the UC Irvine website.</p>
<div id="import-the-data" class="section level2">
<h2><span class="header-section-number">11.1</span> Import the data</h2>
<p>The data set is given to us in a rough form and quite a bit of editing is necessary.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Load the data - we downloaded the data from the website and saved it into a .csv file</span>
<span class="kw">library</span>(tidyverse)
mushroom <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"dataset/Mushroom.csv"</span>, <span class="dt">col_names =</span> <span class="ot">FALSE</span>)
<span class="kw">glimpse</span>(mushroom)</code></pre></div>
<pre><code>## Observations: 8,124
## Variables: 23
## $ X1 <chr> "p", "e", "e", "p", "e", "e", "e", "e", "p", "e", "e", "e"...
## $ X2 <chr> "x", "x", "b", "x", "x", "x", "b", "b", "x", "b", "x", "x"...
## $ X3 <chr> "s", "s", "s", "y", "s", "y", "s", "y", "y", "s", "y", "y"...
## $ X4 <chr> "n", "y", "w", "w", "g", "y", "w", "w", "w", "y", "y", "y"...
## $ X5 <chr> "t", "t", "t", "t", "f", "t", "t", "t", "t", "t", "t", "t"...
## $ X6 <chr> "p", "a", "l", "p", "n", "a", "a", "l", "p", "a", "l", "a"...
## $ X7 <chr> "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f", "f"...
## $ X8 <chr> "c", "c", "c", "c", "w", "c", "c", "c", "c", "c", "c", "c"...
## $ X9 <chr> "n", "b", "b", "n", "b", "b", "b", "b", "n", "b", "b", "b"...
## $ X10 <chr> "k", "k", "n", "n", "k", "n", "g", "n", "p", "g", "g", "n"...
## $ X11 <chr> "e", "e", "e", "e", "t", "e", "e", "e", "e", "e", "e", "e"...
## $ X12 <chr> "e", "c", "c", "e", "e", "c", "c", "c", "e", "c", "c", "c"...
## $ X13 <chr> "s", "s", "s", "s", "s", "s", "s", "s", "s", "s", "s", "s"...
## $ X14 <chr> "s", "s", "s", "s", "s", "s", "s", "s", "s", "s", "s", "s"...
## $ X15 <chr> "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w"...
## $ X16 <chr> "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w"...
## $ X17 <chr> "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p", "p"...
## $ X18 <chr> "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w", "w"...
## $ X19 <chr> "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o", "o"...
## $ X20 <chr> "p", "p", "p", "p", "e", "p", "p", "p", "p", "p", "p", "p"...
## $ X21 <chr> "k", "n", "n", "k", "n", "k", "k", "n", "k", "k", "n", "k"...
## $ X22 <chr> "s", "n", "n", "s", "a", "n", "n", "s", "v", "s", "n", "s"...
## $ X23 <chr> "u", "g", "m", "u", "g", "g", "m", "m", "g", "m", "g", "m"...</code></pre>
<p>Basically we have 8124 mushrooms in the dataset. And each observation consists of 23 variables. As it stands, the data frame doesn’t look very meaningfull. We have to go back to the source to bring meaning to each of the variables and to the various levels of the categorical variables.</p>
</div>
<div id="tidy-the-data-1" class="section level2">
<h2><span class="header-section-number">11.2</span> Tidy the data</h2>
<p>This is the least fun part of the workflow.<br />
We’ll start by giving names to each of the variables, then we specify the category for each variable. It is not necessary to do so but it does add meaning to what we do.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co"># Rename the variables</span>
<span class="kw">colnames</span>(mushroom) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"edibility"</span>, <span class="st">"cap_shape"</span>, <span class="st">"cap_surface"</span>,
<span class="st">"cap_color"</span>, <span class="st">"bruises"</span>, <span class="st">"odor"</span>,
<span class="st">"gill_attachement"</span>, <span class="st">"gill_spacing"</span>, <span class="st">"gill_size"</span>,
<span class="st">"gill_color"</span>, <span class="st">"stalk_shape"</span>, <span class="st">"stalk_root"</span>,
<span class="st">"stalk_surface_above_ring"</span>, <span class="st">"stalk_surface_below_ring"</span>, <span class="st">"stalk_color_above_ring"</span>,
<span class="st">"stalk_color_below_ring"</span>, <span class="st">"veil_type"</span>, <span class="st">"veil_color"</span>,
<span class="st">"ring_number"</span>, <span class="st">"ring_type"</span>, <span class="st">"spore_print_color"</span>,
<span class="st">"population"</span>, <span class="st">"habitat"</span>)
<span class="co"># Defining the levels for the categorical variables </span>
## We make each variable as a factor
mushroom <-<span class="st"> </span>mushroom <span class="op">%>%</span><span class="st"> </span><span class="kw">map_df</span>(<span class="cf">function</span>(.x) <span class="kw">as.factor</span>(.x))
## We redefine each of the category for each of the variables
<span class="kw">levels</span>(mushroom<span class="op">$</span>edibility) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"edible"</span>, <span class="st">"poisonous"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>cap_shape) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"bell"</span>, <span class="st">"conical"</span>, <span class="st">"flat"</span>, <span class="st">"knobbed"</span>, <span class="st">"sunken"</span>, <span class="st">"convex"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>cap_color) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"buff"</span>, <span class="st">"cinnamon"</span>, <span class="st">"red"</span>, <span class="st">"gray"</span>, <span class="st">"brown"</span>, <span class="st">"pink"</span>,
<span class="st">"green"</span>, <span class="st">"purple"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>cap_surface) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"fibrous"</span>, <span class="st">"grooves"</span>, <span class="st">"scaly"</span>, <span class="st">"smooth"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>bruises) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"no"</span>, <span class="st">"yes"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>odor) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"almond"</span>, <span class="st">"creosote"</span>, <span class="st">"foul"</span>, <span class="st">"anise"</span>, <span class="st">"musty"</span>, <span class="st">"none"</span>, <span class="st">"pungent"</span>, <span class="st">"spicy"</span>, <span class="st">"fishy"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>gill_attachement) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"attached"</span>, <span class="st">"free"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>gill_spacing) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"close"</span>, <span class="st">"crowded"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>gill_size) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"broad"</span>, <span class="st">"narrow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>gill_color) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"buff"</span>, <span class="st">"red"</span>, <span class="st">"gray"</span>, <span class="st">"chocolate"</span>, <span class="st">"black"</span>, <span class="st">"brown"</span>, <span class="st">"orange"</span>,
<span class="st">"pink"</span>, <span class="st">"green"</span>, <span class="st">"purple"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_shape) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"enlarging"</span>, <span class="st">"tapering"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_root) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"missing"</span>, <span class="st">"bulbous"</span>, <span class="st">"club"</span>, <span class="st">"equal"</span>, <span class="st">"rooted"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_surface_above_ring) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"fibrous"</span>, <span class="st">"silky"</span>, <span class="st">"smooth"</span>, <span class="st">"scaly"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_surface_below_ring) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"fibrous"</span>, <span class="st">"silky"</span>, <span class="st">"smooth"</span>, <span class="st">"scaly"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_color_above_ring) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"buff"</span>, <span class="st">"cinnamon"</span>, <span class="st">"red"</span>, <span class="st">"gray"</span>, <span class="st">"brown"</span>, <span class="st">"pink"</span>,
<span class="st">"green"</span>, <span class="st">"purple"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>stalk_color_below_ring) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"buff"</span>, <span class="st">"cinnamon"</span>, <span class="st">"red"</span>, <span class="st">"gray"</span>, <span class="st">"brown"</span>, <span class="st">"pink"</span>,
<span class="st">"green"</span>, <span class="st">"purple"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>veil_type) <-<span class="st"> "partial"</span>
<span class="kw">levels</span>(mushroom<span class="op">$</span>veil_color) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"brown"</span>, <span class="st">"orange"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>ring_number) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"none"</span>, <span class="st">"one"</span>, <span class="st">"two"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>ring_type) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"evanescent"</span>, <span class="st">"flaring"</span>, <span class="st">"large"</span>, <span class="st">"none"</span>, <span class="st">"pendant"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>spore_print_color) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"buff"</span>, <span class="st">"chocolate"</span>, <span class="st">"black"</span>, <span class="st">"brown"</span>, <span class="st">"orange"</span>,
<span class="st">"green"</span>, <span class="st">"purple"</span>, <span class="st">"white"</span>, <span class="st">"yellow"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>population) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"abundant"</span>, <span class="st">"clustered"</span>, <span class="st">"numerous"</span>, <span class="st">"scattered"</span>, <span class="st">"several"</span>, <span class="st">"solitary"</span>)
<span class="kw">levels</span>(mushroom<span class="op">$</span>habitat) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"wood"</span>, <span class="st">"grasses"</span>, <span class="st">"leaves"</span>, <span class="st">"meadows"</span>, <span class="st">"paths"</span>, <span class="st">"urban"</span>, <span class="st">"waste"</span>)</code></pre></div>
<p>Let’s check our changes one last time before diving into in the next phase of our data analysis workflow.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">glimpse</span>(mushroom)</code></pre></div>
<pre><code>## Observations: 8,124
## Variables: 23
## $ edibility <fctr> poisonous, edible, edible, poisonous...
## $ cap_shape <fctr> convex, convex, bell, convex, convex...
## $ cap_surface <fctr> scaly, scaly, scaly, smooth, scaly, ...
## $ cap_color <fctr> brown, yellow, white, white, gray, y...
## $ bruises <fctr> yes, yes, yes, yes, no, yes, yes, ye...
## $ odor <fctr> pungent, almond, anise, pungent, non...
## $ gill_attachement <fctr> free, free, free, free, free, free, ...
## $ gill_spacing <fctr> close, close, close, close, crowded,...
## $ gill_size <fctr> narrow, broad, broad, narrow, broad,...
## $ gill_color <fctr> black, black, brown, brown, black, b...
## $ stalk_shape <fctr> enlarging, enlarging, enlarging, enl...
## $ stalk_root <fctr> equal, club, club, equal, equal, clu...
## $ stalk_surface_above_ring <fctr> smooth, smooth, smooth, smooth, smoo...
## $ stalk_surface_below_ring <fctr> smooth, smooth, smooth, smooth, smoo...
## $ stalk_color_above_ring <fctr> purple, purple, purple, purple, purp...
## $ stalk_color_below_ring <fctr> purple, purple, purple, purple, purp...
## $ veil_type <fctr> partial, partial, partial, partial, ...
## $ veil_color <fctr> white, white, white, white, white, w...
## $ ring_number <fctr> one, one, one, one, one, one, one, o...
## $ ring_type <fctr> pendant, pendant, pendant, pendant, ...
## $ spore_print_color <fctr> black, brown, brown, black, brown, b...
## $ population <fctr> scattered, numerous, numerous, scatt...
## $ habitat <fctr> urban, grasses, meadows, urban, gras...</code></pre>
<p>As each variables is categorical, let’s see how many categories are we speaking about?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">number_class <-<span class="st"> </span><span class="cf">function</span>(x){
x <-<span class="st"> </span><span class="kw">length</span>(<span class="kw">levels</span>(x))
}
x <-<span class="st"> </span>mushroom <span class="op">%>%</span><span class="st"> </span><span class="kw">map_dbl</span>(<span class="cf">function</span>(.x) <span class="kw">number_class</span>(.x)) <span class="op">%>%</span><span class="st"> </span><span class="kw">as_tibble</span>() <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">rownames_to_column</span>() <span class="op">%>%</span><span class="st"> </span><span class="kw">arrange</span>(<span class="kw">desc</span>(value))
<span class="kw">colnames</span>(x) <-<span class="st"> </span><span class="kw">c</span>(<span class="st">"Variable name"</span>, <span class="st">"Number of levels"</span>)
<span class="kw">print</span>(x)</code></pre></div>
<pre><code>## # A tibble: 23 x 2
## `Variable name` `Number of levels`
## <chr> <dbl>
## 1 gill_color 12
## 2 cap_color 10
## 3 stalk_color_above_ring 10
## 4 stalk_color_below_ring 10
## 5 odor 9
## 6 spore_print_color 9
## 7 habitat 7
## 8 cap_shape 6
## 9 population 6
## 10 stalk_root 5
## # ... with 13 more rows</code></pre>
</div>
<div id="understand-the-data-2" class="section level2">
<h2><span class="header-section-number">11.3</span> Understand the data</h2>
<p>This is the circular phase of our dealing with data. This is where each of the transforming, visualizing and modeling stage reinforce each other to create a better understanding.<br />
<img src="otherpics/data_workflow.png" alt="data workflow" /></p>
<div id="transform-the-data" class="section level3">
<h3><span class="header-section-number">11.3.1</span> Transform the data</h3>
<p>We noticed from the previous section an issue with the veil_type variable. It has only one factor. So basically, it does not bring any information. Furthermore, factor variable with only one level do create issues later on at the modeling stage. R will throw out an error for the categorical variable that has only one level.<br />
So let’s take away that column.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">mushroom <-<span class="st"> </span>mushroom <span class="op">%>%</span><span class="st"> </span><span class="kw">select</span>(<span class="op">-</span><span class="st"> </span>veil_type)</code></pre></div>
<p>Do we have any missing data? Most ML algorithms won’t work if we have missing data.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">map_dbl</span>(mushroom, <span class="cf">function</span>(.x) {<span class="kw">sum</span>(<span class="kw">is.na</span>(.x))})</code></pre></div>
<pre><code>## edibility cap_shape cap_surface
## 0 0 0
## cap_color bruises odor
## 0 0 0
## gill_attachement gill_spacing gill_size
## 0 0 0
## gill_color stalk_shape stalk_root
## 0 0 0
## stalk_surface_above_ring stalk_surface_below_ring stalk_color_above_ring
## 0 0 0
## stalk_color_below_ring veil_color ring_number
## 0 0 0
## ring_type spore_print_color population
## 0 0 0
## habitat
## 0</code></pre>
<p>Lucky us! We have no missing data.</p>
</div>
<div id="visualize-the-data" class="section level3">
<h3><span class="header-section-number">11.3.2</span> Visualize the data</h3>
<p>This is one of the most important step in the DS process. This stage can gives us unexpected insights and often allows us to ask the right questions.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(ggplot2)
<span class="kw">ggplot</span>(mushroom, <span class="kw">aes</span>(<span class="dt">x =</span> cap_surface, <span class="dt">y =</span> cap_color, <span class="dt">col =</span> edibility)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">alpha =</span> <span class="fl">0.5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_manual</span>(<span class="dt">breaks =</span> <span class="kw">c</span>(<span class="st">"edible"</span>, <span class="st">"poisonous"</span>),
<span class="dt">values =</span> <span class="kw">c</span>(<span class="st">"green"</span>, <span class="st">"red"</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_pic1-1.png" width="672" /></p>
<p>If we want to stay safe, better bet on <em>fibrous</em> surface. Stay especially away from <em>smooth</em> surface, except if they are purple or green.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(mushroom, <span class="kw">aes</span>(<span class="dt">x =</span> cap_shape, <span class="dt">y =</span> cap_color, <span class="dt">col =</span> edibility)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">alpha =</span> <span class="fl">0.5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_manual</span>(<span class="dt">breaks =</span> <span class="kw">c</span>(<span class="st">"edible"</span>, <span class="st">"poisonous"</span>),
<span class="dt">values =</span> <span class="kw">c</span>(<span class="st">"green"</span>, <span class="st">"red"</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_pic2-1.png" width="672" /></p>
<p>Again, in case one don’t know about mushroom, it is better to stay away from all shapes except maybe for <em>bell</em> shape mushrooms.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(mushroom, <span class="kw">aes</span>(<span class="dt">x =</span> gill_color, <span class="dt">y =</span> cap_color, <span class="dt">col =</span> edibility)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">alpha =</span> <span class="fl">0.5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_manual</span>(<span class="dt">breaks =</span> <span class="kw">c</span>(<span class="st">"edible"</span>, <span class="st">"poisonous"</span>),
<span class="dt">values =</span> <span class="kw">c</span>(<span class="st">"green"</span>, <span class="st">"red"</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_pic3-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">ggplot</span>(mushroom, <span class="kw">aes</span>(<span class="dt">x =</span> edibility, <span class="dt">y =</span> odor, <span class="dt">col =</span> edibility)) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">geom_jitter</span>(<span class="dt">alpha =</span> <span class="fl">0.5</span>) <span class="op">+</span><span class="st"> </span>
<span class="st"> </span><span class="kw">scale_color_manual</span>(<span class="dt">breaks =</span> <span class="kw">c</span>(<span class="st">"edible"</span>, <span class="st">"poisonous"</span>),
<span class="dt">values =</span> <span class="kw">c</span>(<span class="st">"green"</span>, <span class="st">"red"</span>))</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_pic3-2.png" width="672" /></p>
<p>Odor is defintely quite an informative predictor. Basically, if it smells <em>fishy</em>, <em>spicy</em> or <em>pungent</em> just stay away. If it smells like <em>anise</em> or <em>almond</em> you can go ahead. If it doesn’t smell anything, you have better chance that it is edible than not.</p>
<p>TO DO: put a comment on what we see TO DO: put a mosaic graph</p>
</div>
<div id="modeling" class="section level3">
<h3><span class="header-section-number">11.3.3</span> Modeling</h3>
<p>At this stage, we should have gathered enough information and insights on our data to choose appropriate modeling techniques.</p>
<p>Before we go ahead, we need to split the data into a training and testing set</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">set.seed</span>(<span class="dv">1810</span>)
mushsample <-<span class="st"> </span>caret<span class="op">::</span><span class="kw">createDataPartition</span>(<span class="dt">y =</span> mushroom<span class="op">$</span>edibility, <span class="dt">times =</span> <span class="dv">1</span>, <span class="dt">p =</span> <span class="fl">0.8</span>, <span class="dt">list =</span> <span class="ot">FALSE</span>)
train_mushroom <-<span class="st"> </span>mushroom[mushsample, ]
test_mushroom <-<span class="st"> </span>mushroom[<span class="op">-</span>mushsample, ]</code></pre></div>
<p>We can check the quality of the splits in regards to our predicted (dependent) variable.</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>(mushroom<span class="op">$</span>edibility)), <span class="dv">2</span>)</code></pre></div>
<pre><code>##
## edible poisonous
## 0.52 0.48</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>(train_mushroom<span class="op">$</span>edibility)), <span class="dv">2</span>)</code></pre></div>
<pre><code>##
## edible poisonous
## 0.52 0.48</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>(test_mushroom<span class="op">$</span>edibility)), <span class="dv">2</span>)</code></pre></div>
<pre><code>##
## edible poisonous
## 0.52 0.48</code></pre>
<p>It seems like we have the right splits.</p>
<div id="use-of-regression-tree" class="section level4">
<h4><span class="header-section-number">11.3.3.1</span> Use of Regression Tree</h4>
<p>As we have many categorical variables, regression tree is an ideal classification tools for such situation.<br />
We’ll use the <code>rpart</code> package. Let’s give it a try without any customization.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(rpart)
<span class="kw">library</span>(rpart.plot)
<span class="kw">set.seed</span>(<span class="dv">1810</span>)
model_tree <-<span class="st"> </span><span class="kw">rpart</span>(edibility <span class="op">~</span><span class="st"> </span>., <span class="dt">data =</span> train_mushroom, <span class="dt">method =</span> <span class="st">"class"</span>)
model_tree</code></pre></div>
<pre><code>## n= 6500
##
## node), split, n, loss, yval, (yprob)
## * denotes terminal node
##
## 1) root 6500 3133 edible (0.51800000 0.48200000)
## 2) odor=almond,anise,none 3468 101 edible (0.97087659 0.02912341)
## 4) spore_print_color=buff,chocolate,black,brown,orange,purple,white,yellow 3408 41 edible (0.98796948 0.01203052) *
## 5) spore_print_color=green 60 0 poisonous (0.00000000 1.00000000) *
## 3) odor=creosote,foul,musty,pungent,spicy,fishy 3032 0 poisonous (0.00000000 1.00000000) *</code></pre>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">caret<span class="op">::</span><span class="kw">confusionMatrix</span>(<span class="dt">data=</span><span class="kw">predict</span>(model_tree, <span class="dt">type =</span> <span class="st">"class"</span>),
<span class="dt">reference =</span> train_mushroom<span class="op">$</span>edibility,
<span class="dt">positive=</span><span class="st">"edible"</span>)</code></pre></div>
<pre><code>## Confusion Matrix and Statistics
##
## Reference
## Prediction edible poisonous
## edible 3367 41
## poisonous 0 3092
##
## Accuracy : 0.9937
## 95% CI : (0.9915, 0.9955)
## No Information Rate : 0.518
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 0.9874
## Mcnemar's Test P-Value : 4.185e-10
##
## Sensitivity : 1.0000
## Specificity : 0.9869
## Pos Pred Value : 0.9880
## Neg Pred Value : 1.0000
## Prevalence : 0.5180
## Detection Rate : 0.5180
## Detection Prevalence : 0.5243
## Balanced Accuracy : 0.9935
##
## 'Positive' Class : edible
## </code></pre>
<p>We have quite an issue here. 40 mushrooms have been predicted as edible but were actually poisonous. That should not be happening. So we’ll set up a penalty for wrongly predicting a mushroom as <code>edible</code> when in reality it is <code>poisonous</code>. A mistake the other way is not as bad. At worst we miss on a good recipe! So let’s redo our tree with a penalty for wrongly predicting poisonous. To do this, we introduce a penalty matrix that we’ll use as a parameter in our rpart function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">penalty_matrix <-<span class="st"> </span><span class="kw">matrix</span>(<span class="kw">c</span>(<span class="dv">0</span>, <span class="dv">1</span>, <span class="dv">10</span>, <span class="dv">0</span>), <span class="dt">byrow =</span> <span class="ot">TRUE</span>, <span class="dt">nrow =</span> <span class="dv">2</span>)
model_tree_penalty <-<span class="st"> </span><span class="kw">rpart</span>(edibility <span class="op">~</span><span class="st"> </span>., <span class="dt">data =</span> train_mushroom, <span class="dt">method =</span> <span class="st">"class"</span>,
<span class="dt">parms =</span> <span class="kw">list</span>(<span class="dt">loss =</span> penalty_matrix))
caret<span class="op">::</span><span class="kw">confusionMatrix</span>(<span class="dt">data=</span><span class="kw">predict</span>(model_tree_penalty, <span class="dt">type =</span> <span class="st">"class"</span>),
<span class="dt">reference =</span> train_mushroom<span class="op">$</span>edibility,
<span class="dt">positive=</span><span class="st">"edible"</span>)</code></pre></div>
<pre><code>## Confusion Matrix and Statistics
##
## Reference
## Prediction edible poisonous
## edible 3367 0
## poisonous 0 3133
##
## Accuracy : 1
## 95% CI : (0.9994, 1)
## No Information Rate : 0.518
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1.000
## Specificity : 1.000
## Pos Pred Value : 1.000
## Neg Pred Value : 1.000
## Prevalence : 0.518
## Detection Rate : 0.518
## Detection Prevalence : 0.518
## Balanced Accuracy : 1.000
##
## 'Positive' Class : edible
## </code></pre>
<p>So introducing a penalty did the job; it gave us a perfect prediction and saves us from a jounrey at the hospital.</p>
<p>Another way to increase the accuracy of our tree model is to play on the <code>cp</code> parameter.<br />
We start to build a tree with a very low <code>cp</code> (that is we’ll have a deep tree). The idea is that we then prune it later.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">model_tree <-<span class="st"> </span><span class="kw">rpart</span>(edibility <span class="op">~</span><span class="st"> </span>., <span class="dt">data =</span> train_mushroom,
<span class="dt">method =</span> <span class="st">"class"</span>, <span class="dt">cp =</span> <span class="fl">0.00001</span>)</code></pre></div>
<p>To prune a tree, we first have to find the <code>cp</code> that gives the lowest <code>xerror</code> or cross-validation error. We can find the lowest <code>xerror</code> using either the <code>printcp</code> or <code>plotcp</code> function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">printcp</span>(model_tree)</code></pre></div>
<pre><code>##
## Classification tree:
## rpart(formula = edibility ~ ., data = train_mushroom, method = "class",
## cp = 1e-05)
##
## Variables actually used in tree construction:
## [1] cap_surface habitat odor
## [4] spore_print_color stalk_color_below_ring stalk_root
##
## Root node error: 3133/6500 = 0.482
##
## n= 6500
##
## CP nsplit rel error xerror xstd
## 1 0.9677625 0 1.0000000 1.0000000 0.01285833
## 2 0.0191510 1 0.0322375 0.0322375 0.00318273
## 3 0.0063837 2 0.0130865 0.0130865 0.00203731
## 4 0.0022343 3 0.0067028 0.0067028 0.00146032
## 5 0.0011171 5 0.0022343 0.0022343 0.00084402
## 6 0.0000100 7 0.0000000 0.0022343 0.00084402</code></pre>
<p>We can see here that that the lowest <code>xerror</code> happen at the 5th split.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">plotcp</span>(model_tree)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_xvalplot1-1.png" width="672" /></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">model_tree<span class="op">$</span>cptable[<span class="kw">which.min</span>(model_tree<span class="op">$</span>cptable[, <span class="st">"xerror"</span>]), <span class="st">"CP"</span>]</code></pre></div>
<pre><code>## [1] 0.00111714</code></pre>
<p>So now we can start pruning our tree with the <code>cp</code> that gives the lowest cross-validation error.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">bestcp <-<span class="st"> </span><span class="kw">round</span>(model_tree<span class="op">$</span>cptable[<span class="kw">which.min</span>(model_tree<span class="op">$</span>cptable[, <span class="st">"xerror"</span>]), <span class="st">"CP"</span>], <span class="dv">4</span>)
model_tree_pruned <-<span class="st"> </span><span class="kw">prune</span>(model_tree, <span class="dt">cp =</span> bestcp)</code></pre></div>
<p>Let’s have a quick look at the tree as it stands</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">rpart.plot</span>(model_tree_pruned, <span class="dt">extra =</span> <span class="dv">104</span>, <span class="dt">box.palette =</span> <span class="st">"GnBu"</span>,
<span class="dt">branch.lty =</span> <span class="dv">3</span>, <span class="dt">shadow.col =</span> <span class="st">"gray"</span>, <span class="dt">nn =</span> <span class="ot">TRUE</span>)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/mushroom_treeplot1-1.png" width="672" /></p>
<p>How does the model perform on the train data?</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="co">#table(train_mushroom$edibility, predict(model_tree, type="class"))</span>
caret<span class="op">::</span><span class="kw">confusionMatrix</span>(<span class="dt">data=</span><span class="kw">predict</span>(model_tree_pruned, <span class="dt">type =</span> <span class="st">"class"</span>),
<span class="dt">reference =</span> train_mushroom<span class="op">$</span>edibility,
<span class="dt">positive=</span><span class="st">"edible"</span>)</code></pre></div>
<pre><code>## Confusion Matrix and Statistics
##
## Reference
## Prediction edible poisonous
## edible 3367 0
## poisonous 0 3133
##
## Accuracy : 1
## 95% CI : (0.9994, 1)
## No Information Rate : 0.518
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1.000
## Specificity : 1.000
## Pos Pred Value : 1.000
## Neg Pred Value : 1.000
## Prevalence : 0.518
## Detection Rate : 0.518
## Detection Prevalence : 0.518
## Balanced Accuracy : 1.000
##
## 'Positive' Class : edible
## </code></pre>
<p>It seems like we have a perfect accuracy on our training set. It is quite rare to have such perfect accuracy.</p>
<p>Let’s check how it fares on the testing set.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">test_tree <-<span class="st"> </span><span class="kw">predict</span>(model_tree, <span class="dt">newdata =</span> test_mushroom)
caret<span class="op">::</span><span class="kw">confusionMatrix</span>(<span class="dt">data =</span> <span class="kw">predict</span>(model_tree, <span class="dt">newdata =</span> test_mushroom, <span class="dt">type =</span> <span class="st">"class"</span>),
<span class="dt">reference =</span> test_mushroom<span class="op">$</span>edibility,
<span class="dt">positive =</span> <span class="st">"edible"</span>)</code></pre></div>
<pre><code>## Confusion Matrix and Statistics
##
## Reference
## Prediction edible poisonous
## edible 841 0
## poisonous 0 783
##
## Accuracy : 1
## 95% CI : (0.9977, 1)
## No Information Rate : 0.5179
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1.0000
## Specificity : 1.0000
## Pos Pred Value : 1.0000
## Neg Pred Value : 1.0000
## Prevalence : 0.5179
## Detection Rate : 0.5179
## Detection Prevalence : 0.5179
## Balanced Accuracy : 1.0000
##
## 'Positive' Class : edible
## </code></pre>
<p>Perfect prediction here as well.</p>
</div>
<div id="use-of-random-forest" class="section level4">
<h4><span class="header-section-number">11.3.3.2</span> Use of Random Forest</h4>
<p>We usually use random forest if a tree is not enough. In this case, as we have perfect prediction using a single tree, it is not really necessary to use a Random Forest algorithm. We just use for learning sake without tuning any of the parameters.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(randomForest)
model_rf <-<span class="st"> </span><span class="kw">randomForest</span>(edibility <span class="op">~</span><span class="st"> </span>., <span class="dt">ntree =</span> <span class="dv">50</span>, <span class="dt">data =</span> train_mushroom)
<span class="kw">plot</span>(model_rf)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/rf_errorplot1-1.png" width="672" /> The default number of trees for the random forest is 500; we just use 50 here. As we can see on the plot, above 20 trees, the error isn’t decreasing anymore. And actually, the error seems to be 0 or almost 0.<br />
The next step can tell us this more accurately.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">print</span>(model_rf)</code></pre></div>
<pre><code>##
## Call:
## randomForest(formula = edibility ~ ., data = train_mushroom, ntree = 50)
## Type of random forest: classification
## Number of trees: 50
## No. of variables tried at each split: 4
##
## OOB estimate of error rate: 0%
## Confusion matrix:
## edible poisonous class.error
## edible 3367 0 0
## poisonous 0 3133 0</code></pre>
<p>Altough it is not really necessary to this here as we have a perfect prediction, we can use the <code>confusionMatrix</code> function from the <code>caret</code> pacakge.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">caret<span class="op">::</span><span class="kw">confusionMatrix</span>(<span class="dt">data =</span> model_rf<span class="op">$</span>predicted, <span class="dt">reference =</span> train_mushroom<span class="op">$</span>edibility ,
<span class="dt">positive =</span> <span class="st">"edible"</span>)</code></pre></div>
<pre><code>## Confusion Matrix and Statistics
##
## Reference
## Prediction edible poisonous
## edible 3367 0
## poisonous 0 3133
##
## Accuracy : 1
## 95% CI : (0.9994, 1)
## No Information Rate : 0.518
## P-Value [Acc > NIR] : < 2.2e-16
##
## Kappa : 1
## Mcnemar's Test P-Value : NA
##
## Sensitivity : 1.000
## Specificity : 1.000
## Pos Pred Value : 1.000
## Neg Pred Value : 1.000
## Prevalence : 0.518
## Detection Rate : 0.518
## Detection Prevalence : 0.518
## Balanced Accuracy : 1.000
##
## 'Positive' Class : edible
## </code></pre>
<p>If we want to look at the most important variable in terms of predicting edibility in our model, we can do that using the <em>Mean Decreasing Gini</em></p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">varImpPlot</span>(model_rf, <span class="dt">sort =</span> <span class="ot">TRUE</span>,
<span class="dt">n.var =</span> <span class="dv">10</span>, <span class="dt">main =</span> <span class="st">"The 10 variables with the most predictive power"</span>)</code></pre></div>
<p><img src="machinelearningwithR_files/figure-html/rf_importance_variable-1.png" width="672" /></p>
<p>Another way to look at the predictible power of the variables is to use the <code>importance</code> extractor function.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(tibble)
<span class="kw">importance</span>(model_rf) <span class="op">%>%</span><span class="st"> </span><span class="kw">data.frame</span>() <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">rownames_to_column</span>(<span class="dt">var =</span> <span class="st">"Variable"</span>) <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">arrange</span>(<span class="kw">desc</span>(MeanDecreaseGini)) <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">head</span>(<span class="dv">10</span>)</code></pre></div>
<pre><code>## Variable MeanDecreaseGini
## 1 odor 1115.85522
## 2 spore_print_color 477.71557
## 3 gill_color 319.02467
## 4 stalk_surface_above_ring 235.59574
## 5 gill_size 194.56155
## 6 stalk_surface_below_ring 172.26749
## 7 stalk_root 132.26045
## 8 ring_type 129.88445
## 9 population 79.42030
## 10 gill_spacing 63.42436</code></pre>
<p>We could compare that with the important variables from the classification tree obtained above.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">model_tree_penalty<span class="op">$</span>variable.importance <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">as_tibble</span>() <span class="op">%>%</span><span class="st"> </span><span class="kw">rownames_to_column</span>(<span class="dt">var =</span> <span class="st">"variable"</span>) <span class="op">%>%</span><span class="st"> </span>
<span class="st"> </span><span class="kw">arrange</span>(<span class="kw">desc</span>(value)) <span class="op">%>%</span><span class="st"> </span><span class="kw">head</span>(<span class="dv">10</span>)</code></pre></div>
<pre><code>## # A tibble: 10 x 2
## variable value
## <chr> <dbl>
## 1 odor 848.00494
## 2 spore_print_color 804.39831
## 3 gill_color 503.71270
## 4 stalk_surface_above_ring 501.28385
## 5 stalk_surface_below_ring 453.92877
## 6 ring_type 450.29286
## 7 ring_number 170.56141
## 8 stalk_root 117.78800
## 9 habitat 98.22176
## 10 stalk_color_below_ring 74.72602</code></pre>
<p>Interestingly gill_size which is the 5th most important predictor in the random forest does not appear in the top 10 of our classification tree.</p>
<p>Now we apply our model to our testing set.</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">test_rf <-<span class="st"> </span><span class="kw">predict</span>(model_rf, <span class="dt">newdata =</span> test_mushroom)
<span class="co"># Quick check on our prediction</span>
<span class="kw">table</span>(test_rf, test_mushroom<span class="op">$</span>edibility)</code></pre></div>
<pre><code>##
## test_rf edible poisonous
## edible 841 0
## poisonous 0 783</code></pre>
<p>Perfect Prediction!</p>
</div>
<div id="use-of-svm" class="section level4">
<h4><span class="header-section-number">11.3.3.3</span> Use of SVM</h4>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r"><span class="kw">library</span>(e1071)
model_svm <-<span class="st"> </span><span class="kw">svm</span>(edibility <span class="op">~</span>. , <span class="dt">data=</span>train_mushroom, <span class="dt">cost =</span> <span class="dv">1000</span>, <span class="dt">gamma =</span> <span class="fl">0.01</span>)</code></pre></div>
<p>Check the prediction</p>
<div class="sourceCode"><pre class="sourceCode r"><code class="sourceCode r">test_svm <-<span class="st"> </span><span class="kw">predict</span>(model_svm, <span class="dt">newdata =</span> test_mushroom)
<span class="kw">table</span>(test_svm, test_mushroom<span class="op">$</span>edibility)</code></pre></div>
<pre><code>##
## test_svm edible poisonous
## edible 841 0
## poisonous 0 783</code></pre>
<p>And perfect prediction again!</p>
</div>
</div>
</div>
<div id="communication" class="section level2">
<h2><span class="header-section-number">11.4</span> Communication</h2>
<p>With some fine tuning, a regression tree managed to predict accurately the edibility of mushroom. They were 2 parameters to look at: the <code>cp</code>and the penalty matrix. Random Forest and SVM achieved similar results out of the box.<br />
The regression tree approach has to be prefered as it is a lot easier to grasp the results from a tree than from a SVM algorithm.</p>
<p>For sure I will take my little tree picture next time I go shrooming. That said, I will still only go with a good mycologist.</p>
</div>
</div>
</section>
</div>
</div>
</div>
<a href="case-study-predicting-survivalship-on-the-titanic.html" class="navigation navigation-prev " aria-label="Previous page"><i class="fa fa-angle-left"></i></a>
<a href="case-study-wisconsin-breast-cancer.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/20-mushroom.Rmd",
"text": "Suggest edit to this page"
},
"download": ["machinelearningwithR.pdf"],
"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>