-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Fitting.html
More file actions
1510 lines (1422 loc) · 115 KB
/
Copy pathTree_Fitting.html
File metadata and controls
1510 lines (1422 loc) · 115 KB
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
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Tree_Fitting.utf8.md</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/cosmo.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<style type="text/css">code{white-space: pre;}</style>
<style type="text/css" data-origin="pandoc">
a.sourceLine { display: inline-block; line-height: 1.25; }
a.sourceLine { pointer-events: none; color: inherit; text-decoration: inherit; }
a.sourceLine:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
a.sourceLine { text-indent: -1em; padding-left: 1em; }
}
pre.numberSource a.sourceLine
{ position: relative; left: -4em; }
pre.numberSource a.sourceLine::before
{ content: attr(data-line-number);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; pointer-events: all; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
}
pre.numberSource { margin-left: 3em; padding-left: 4px; }
div.sourceCode
{ color: #cccccc; background-color: #303030; }
@media screen {
a.sourceLine::before { text-decoration: underline; }
}
code span.al { color: #ffcfaf; } /* Alert */
code span.an { color: #7f9f7f; font-weight: bold; } /* Annotation */
code span.at { } /* Attribute */
code span.bn { color: #dca3a3; } /* BaseN */
code span.bu { } /* BuiltIn */
code span.cf { color: #f0dfaf; } /* ControlFlow */
code span.ch { color: #dca3a3; } /* Char */
code span.cn { color: #dca3a3; font-weight: bold; } /* Constant */
code span.co { color: #7f9f7f; } /* Comment */
code span.cv { color: #7f9f7f; font-weight: bold; } /* CommentVar */
code span.do { color: #7f9f7f; } /* Documentation */
code span.dt { color: #dfdfbf; } /* DataType */
code span.dv { color: #dcdccc; } /* DecVal */
code span.er { color: #c3bf9f; } /* Error */
code span.ex { } /* Extension */
code span.fl { color: #c0bed1; } /* Float */
code span.fu { color: #efef8f; } /* Function */
code span.im { } /* Import */
code span.in { color: #7f9f7f; font-weight: bold; } /* Information */
code span.kw { color: #f0dfaf; } /* Keyword */
code span.op { color: #f0efd0; } /* Operator */
code span.ot { color: #efef8f; } /* Other */
code span.pp { color: #ffcfaf; font-weight: bold; } /* Preprocessor */
code span.sc { color: #dca3a3; } /* SpecialChar */
code span.ss { color: #cc9393; } /* SpecialString */
code span.st { color: #cc9393; } /* String */
code span.va { } /* Variable */
code span.vs { color: #cc9393; } /* VerbatimString */
code span.wa { color: #7f9f7f; font-weight: bold; } /* Warning */
</style>
<script>
// apply pandoc div.sourceCode style to pre.sourceCode instead
(function() {
var sheets = document.styleSheets;
for (var i = 0; i < sheets.length; i++) {
if (sheets[i].ownerNode.dataset["origin"] !== "pandoc") continue;
try { var rules = sheets[i].cssRules; } catch (e) { continue; }
for (var j = 0; j < rules.length; j++) {
var rule = rules[j];
// check if there is a div.sourceCode rule
if (rule.type !== rule.STYLE_RULE || rule.selectorText !== "div.sourceCode") continue;
var style = rule.style.cssText;
// check if color or background-color is set
if (rule.style.color === '' && rule.style.backgroundColor === '') continue;
// replace div.sourceCode by a pre.sourceCode rule
sheets[i].deleteRule(j);
sheets[i].insertRule('pre.sourceCode{' + style + '}', j);
}
}
})();
</script>
<style type="text/css">
pre:not([class]) {
background-color: white;
}
</style>
<style type="text/css">
h1 {
font-size: 34px;
}
h1.title {
font-size: 38px;
}
h2 {
font-size: 30px;
}
h3 {
font-size: 24px;
}
h4 {
font-size: 18px;
}
h5 {
font-size: 16px;
}
h6 {
font-size: 12px;
}
.table th:not([align]) {
text-align: left;
}
</style>
<link rel="stylesheet" href="customstyles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
code {
color: inherit;
background-color: rgba(0, 0, 0, 0.04);
}
img {
max-width:100%;
height: auto;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
</style>
<style type="text/css">
/* padding for bootstrap navbar */
body {
padding-top: 51px;
padding-bottom: 40px;
}
/* offset scroll position for anchor links (for fixed navbar) */
.section h1 {
padding-top: 56px;
margin-top: -56px;
}
.section h2 {
padding-top: 56px;
margin-top: -56px;
}
.section h3 {
padding-top: 56px;
margin-top: -56px;
}
.section h4 {
padding-top: 56px;
margin-top: -56px;
}
.section h5 {
padding-top: 56px;
margin-top: -56px;
}
.section h6 {
padding-top: 56px;
margin-top: -56px;
}
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #ffffff;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script>
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.parent().addClass('active');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
background: white;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
.tocify-subheader {
display: inline;
}
.tocify-subheader .tocify-item {
font-size: 0.95em;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row-fluid">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Data Analysis and Visualization Portfolio</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Analyses
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="./reproducibe-graph.html">Figure Reproduction</a>
</li>
<li>
<a href="./Continuous_Outcome_Analysis.html">Continuous Outcome Analysis</a>
</li>
<li>
<a href="./Variable_Selection.html">Variable Outcome Analysis</a>
</li>
<li>
<a href="./Tree_Fitting.html">Tree Fitting</a>
</li>
</ul>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
Tidy Tuesdays
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="./tidy_tuesday1.html">Attempt 1</a>
</li>
</ul>
</li>
<li>
<a href="./author.html">About the Author</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="https://github.com/epid8060fall2019/AmandaSkarlupka-DataAnalysis">
<span class="fa fa-github fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div class="fluid-row" id="header">
</div>
<div id="overview" class="section level1">
<h1>Overview</h1>
<p>This document will guide you through some data analysis tasks with a focus on fitting tree-based models and training them. We’ll also (re)-visit some other topics.</p>
<p>While this is in some sense a stand-alone analysis, I assume that you have worked through the <em>Data Analysis</em> exercise and are familiar with the dataset and all the things we discovered during the cleaning process. We’ll use the same dataset here but focus on a different outcome. Other than that, the way to work through the exercise is like in the <em>Data Analysis</em> exercise, namely by writing/completing the missing code.</p>
</div>
<div id="project-setup" class="section level1">
<h1>Project setup</h1>
<p>We need a variety of different packages, which are loaded here. Install as needed. For this analysis, we’ll again use the <code>caret</code> package. If you use others, load them here.</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb1-1" data-line-number="1"><span class="kw">library</span>(<span class="st">'tidyr'</span>)</a>
<a class="sourceLine" id="cb1-2" data-line-number="2"></a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="kw">library</span>(<span class="st">'forcats'</span>)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="kw">library</span>(<span class="st">'ggplot2'</span>)</a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="kw">library</span>(<span class="st">'knitr'</span>)</a>
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="kw">library</span>(<span class="st">'caret'</span>)</a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="kw">library</span>(<span class="st">'doParallel'</span>)</a>
<a class="sourceLine" id="cb1-8" data-line-number="8"><span class="kw">library</span>(<span class="st">'rpart'</span>)</a>
<a class="sourceLine" id="cb1-9" data-line-number="9"><span class="kw">library</span>(<span class="st">'rpart.plot'</span>)</a>
<a class="sourceLine" id="cb1-10" data-line-number="10"><span class="kw">library</span>(<span class="st">'mda'</span>)</a>
<a class="sourceLine" id="cb1-11" data-line-number="11"><span class="kw">library</span>(<span class="st">'ranger'</span>)</a>
<a class="sourceLine" id="cb1-12" data-line-number="12"><span class="kw">library</span>(<span class="st">'e1071'</span>)</a>
<a class="sourceLine" id="cb1-13" data-line-number="13"><span class="kw">library</span>(<span class="st">'readr'</span>)</a>
<a class="sourceLine" id="cb1-14" data-line-number="14"><span class="kw">library</span>(<span class="st">'dplyr'</span>)</a></code></pre></div>
</div>
<div id="data-loading-and-cleaning" class="section level1">
<h1>Data loading and cleaning</h1>
<p>We will again use the Norovirus dataset.</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb2-1" data-line-number="1"><span class="co">#Write code that loads the dataset </span></a>
<a class="sourceLine" id="cb2-2" data-line-number="2"><span class="co">#You can of course re-use code you wrote in the other file.</span></a>
<a class="sourceLine" id="cb2-3" data-line-number="3">d <-<span class="st"> </span><span class="kw">read_csv</span>(<span class="st">"norodata.csv"</span>)</a></code></pre></div>
<pre><code>## Parsed with column specification:
## cols(
## .default = col_double(),
## Author = col_character(),
## EpiCurve = col_character(),
## TDComment = col_character(),
## AHComment = col_character(),
## Trans1 = col_character(),
## Trans2 = col_character(),
## Trans2_O = col_character(),
## Trans3 = col_character(),
## Trans3_O = col_character(),
## Vehicle_1 = col_character(),
## Veh1 = col_character(),
## Veh1_D_1 = col_character(),
## Veh2 = col_character(),
## Veh2_D_1 = col_character(),
## Veh3 = col_character(),
## Veh3_D_1 = col_character(),
## PCRSect = col_character(),
## OBYear = col_character(),
## Hemisphere = col_character(),
## season = col_character()
## # ... with 44 more columns
## )</code></pre>
<pre><code>## See spec(...) for full column specifications.</code></pre>
<pre><code>## Warning: 2 parsing failures.
## row col expected actual file
## 1022 CD a double GGIIb 'norodata.csv'
## 1022 gge a double Sindlesham 'norodata.csv'</code></pre>
</div>
<div id="looking-at-the-outcome" class="section level1">
<h1>Looking at the outcome</h1>
<p>For this analysis, we consider as our main outcome of interest the season. This is a categorical outcome with more than 2 categories, something we haven’t looked at before. Because it’s more than 2 categories, a basic logistic model won’t work. Fortunately, tree-based models can deal with multiple categories.</p>
<div class="sourceCode" id="cb6"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb6-1" data-line-number="1"><span class="co">#write code to take a look at the outcome variable (season)</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="kw">glimpse</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## chr [1:1022] "Fall" "Fall" "Fall" "Fall" "Fall" "Fall" "Fall" "Fall" ...</code></pre>
<div class="sourceCode" id="cb8"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb8-1" data-line-number="1">d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb8-2" data-line-number="2"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(season)) <span class="op">+</span></a>
<a class="sourceLine" id="cb8-3" data-line-number="3"><span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">stat =</span> <span class="st">"count"</span>)</a></code></pre></div>
<pre><code>## Warning: Ignoring unknown parameters: binwidth, bins, pad</code></pre>
<p><img src="Tree_Fitting_files/figure-html/checkoutcome-1.png" width="672" /> We already knew from previous explorations that some entries do not have a season. We could either code them as “other” and keep them in the model, or remove them. Since it’s hard to see any potential scientific reason why there should be a correlation between an “other” season and some variable, we’ll remove it here.</p>
<p>We also notice that while winter is dominant (makes sense, we know that norovirus is more common in winter), we got a decent number of outbreaks for each season, so we shouldn’t have a problem with (un)balanced data.</p>
<div class="sourceCode" id="cb10"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb10-1" data-line-number="1"><span class="co">#write code that removes all observations that have an empty/missing value for season</span></a>
<a class="sourceLine" id="cb10-2" data-line-number="2"><span class="co">#then drop the empty level and check that you have 4 categories for season left</span></a>
<a class="sourceLine" id="cb10-3" data-line-number="3"></a>
<a class="sourceLine" id="cb10-4" data-line-number="4">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb10-5" data-line-number="5"><span class="st"> </span><span class="kw">filter</span>(<span class="kw">is.na</span>(season) <span class="op">==</span><span class="st"> </span><span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb10-6" data-line-number="6"></a>
<a class="sourceLine" id="cb10-7" data-line-number="7">d <span class="op">%>%</span><span class="st"> </span></a>
<a class="sourceLine" id="cb10-8" data-line-number="8"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(season)) <span class="op">+</span></a>
<a class="sourceLine" id="cb10-9" data-line-number="9"><span class="st"> </span><span class="kw">geom_histogram</span>(<span class="dt">stat =</span> <span class="st">"count"</span>)</a></code></pre></div>
<pre><code>## Warning: Ignoring unknown parameters: binwidth, bins, pad</code></pre>
<p><img src="Tree_Fitting_files/figure-html/clean-outcome-1.png" width="672" /></p>
</div>
<div id="selecting-predictors" class="section level1">
<h1>Selecting predictors</h1>
<p>We will pick similar variables as previously, but with some changes. Keep the following variables: <code>Action1, CasesAll, Country, Deaths, EndMonth, GG2C4, Hemisphere, Hospitalizations, MeanD1, MeanI1, MedianD1, MedianI1, OBYear, Path1, RateAll, RiskAll, Season, Setting, StartMonth, State, Trans1, Vomit.</code></p>
<div class="sourceCode" id="cb12"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb12-1" data-line-number="1"><span class="co"># write code that retains the above mentioned variables</span></a>
<a class="sourceLine" id="cb12-2" data-line-number="2"></a>
<a class="sourceLine" id="cb12-3" data-line-number="3">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb12-4" data-line-number="4"><span class="st"> </span><span class="kw">select</span>(<span class="kw">c</span>(Action1, CasesAll, Country, Deaths, EndMonth, gg2c4, Hemisphere, Hospitalizations, MeanD1, MeanI1, MedianD1, MedianI1, OBYear, Path1, RateAll, RiskAll, season, Setting_<span class="dv">1</span>, StartMonth, State, Trans1, Vomit))</a></code></pre></div>
</div>
<div id="cleaning-predictors" class="section level1">
<h1>Cleaning predictors</h1>
<p>We’ll have to perform the usual cleaning steps. You might have realized by now that even for the same dataset, cleaning steps can differ based on the outcome (and as you see below, the model).</p>
<p>Let’s first check for missing values.</p>
<div class="sourceCode" id="cb13"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb13-1" data-line-number="1"><span class="co"># write code that looks at missing values</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">visdat<span class="op">::</span><span class="kw">vis_dat</span>(d)</a></code></pre></div>
<p><img src="Tree_Fitting_files/figure-html/check-reduced-data-1.png" width="672" /></p>
<div class="sourceCode" id="cb14"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb14-1" data-line-number="1"><span class="kw">glimpse</span>(d<span class="op">$</span>gg2c4)</a></code></pre></div>
<pre><code>## chr [1:955] "Yes" NA "Yes" NA "Yes" NA NA NA "Yes" NA NA NA NA NA NA ...</code></pre>
<div class="sourceCode" id="cb16"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb16-1" data-line-number="1">d<span class="op">$</span>gg2c4[<span class="kw">is.na</span>(d<span class="op">$</span>gg2c4)] <-<span class="st"> "No"</span></a>
<a class="sourceLine" id="cb16-2" data-line-number="2"></a>
<a class="sourceLine" id="cb16-3" data-line-number="3">visdat<span class="op">::</span><span class="kw">vis_dat</span>(d)</a></code></pre></div>
<p><img src="Tree_Fitting_files/figure-html/check-reduced-data-2.png" width="672" /></p>
<p>Looks like none of the new variables we included had a ton of missing, so we would probably be ok just removing any observation that has missing data. <strong>However</strong>, tree-based models can deal with missing data in predictors. Therefore, we’ll keep them for now. We’ll later compare how the model does or does not change if we remove those observations.</p>
<p>Let’s make sure everything has the right format (numeric/integer/factor). Adjust/recode variables as needed. You will likely find that as you convert <code>OBYear</code> to numeric, something doesn’t quite work. Take a look. Fix by removing the observation with the troublesome entry, then convert to numeric. Finally, remove the observations that have 0 as OByear - there are more than 1 now.</p>
<div class="sourceCode" id="cb17"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb17-1" data-line-number="1"><span class="co">#write code that cleans OBYear, convert it to numeric. Remove observations with OBYear = 0. </span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2"><span class="co">#also convert any other variables as needed</span></a>
<a class="sourceLine" id="cb17-3" data-line-number="3"><span class="kw">unique</span>(d<span class="op">$</span>OBYear)</a></code></pre></div>
<pre><code>## [1] "1999" "1998" "2006" "2004" "1993"
## [6] "2002" "2005" "1997" "2003" "1994"
## [11] "2008" "2000" "2001" "1995" "1996"
## [16] "2007" "2009" "1990" "0" "1983"
## [21] "2010" "1992" "2002-2007"</code></pre>
<div class="sourceCode" id="cb19"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb19-1" data-line-number="1">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb19-2" data-line-number="2"><span class="st"> </span><span class="kw">filter</span>(OBYear <span class="op">!=</span><span class="st"> "2002-2007"</span>)</a>
<a class="sourceLine" id="cb19-3" data-line-number="3">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb19-4" data-line-number="4"><span class="st"> </span><span class="kw">filter</span>(OBYear <span class="op">!=</span><span class="st"> "0"</span>)</a>
<a class="sourceLine" id="cb19-5" data-line-number="5"></a>
<a class="sourceLine" id="cb19-6" data-line-number="6">d<span class="op">$</span>OBYear <-<span class="st"> </span><span class="kw">as.numeric</span>(d<span class="op">$</span>OBYear)</a></code></pre></div>
<p>Look at the data to see what else we need to do.</p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1"><span class="kw">str</span>(d)</a></code></pre></div>
<pre><code>## Classes 'spec_tbl_df', 'tbl_df', 'tbl' and 'data.frame': 953 obs. of 22 variables:
## $ Action1 : chr "Unspecified" "Unspecified" "Unspecified" "Unspecified" ...
## $ CasesAll : num 15 65 27 4 15 6 40 10 116 45 ...
## $ Country : chr "Japan" "USA" "Other" "Other" ...
## $ Deaths : num 0 0 0 0 0 0 0 0 0 0 ...
## $ EndMonth : num 12 9 0 0 0 0 0 0 11 11 ...
## $ gg2c4 : chr "Yes" "No" "Yes" "No" ...
## $ Hemisphere : chr "Northern" "Northern" "Northern" "Northern" ...
## $ Hospitalizations: num 0 0 0 0 0 0 0 0 5 10 ...
## $ MeanD1 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ MeanI1 : num 0 0 0 0 0 0 0 0 0 0 ...
## $ MedianD1 : num 0 36 0 0 0 0 0 0 0 48 ...
## $ MedianI1 : num 0 37 0 0 0 0 0 0 0 31 ...
## $ OBYear : num 1999 1998 2006 2006 2006 ...
## $ Path1 : chr "No" "No" "Unspecified" "Unspecified" ...
## $ RateAll : num 0 39.8 20.8 100 60 ...
## $ RiskAll : num 0 108 130 4 25 ...
## $ season : chr "Fall" "Fall" "Fall" "Fall" ...
## $ Setting_1 : chr "Daycare Center" "Boxed lunch, football game" "buffet" "restaurant" ...
## $ StartMonth : num 11 9 9 10 11 11 11 11 11 11 ...
## $ State : chr "0" "NC, FL" "0" "0" ...
## $ Trans1 : chr "Unspecified" "Foodborne" "Foodborne" "Foodborne" ...
## $ Vomit : num 1 1 1 1 1 1 1 1 1 1 ...
## - attr(*, "problems")=Classes 'tbl_df', 'tbl' and 'data.frame': 2 obs. of 5 variables:
## ..$ row : int 1022 1022
## ..$ col : chr "CD" "gge"
## ..$ expected: chr "a double" "a double"
## ..$ actual : chr "GGIIb" "Sindlesham"
## ..$ file : chr "'norodata.csv'" "'norodata.csv'"
## - attr(*, "spec")=
## .. cols(
## .. id = col_double(),
## .. Author = col_character(),
## .. Pub_Year = col_double(),
## .. pubmedid = col_double(),
## .. EpiCurve = col_character(),
## .. TDComment = col_character(),
## .. AHComment = col_character(),
## .. Trans1 = col_character(),
## .. Trans1_O = col_double(),
## .. Trans2 = col_character(),
## .. Trans2_O = col_character(),
## .. Trans3 = col_character(),
## .. Trans3_O = col_character(),
## .. Risk1 = col_double(),
## .. Risk2 = col_double(),
## .. RiskAll = col_double(),
## .. Cases1 = col_double(),
## .. Cases2 = col_double(),
## .. CasesAll = col_double(),
## .. Rate1 = col_double(),
## .. Rate2 = col_double(),
## .. RateAll = col_double(),
## .. Hospitalizations = col_double(),
## .. Deaths = col_double(),
## .. Vehicle_1 = col_character(),
## .. Veh1 = col_character(),
## .. Veh1_D_1 = col_character(),
## .. Veh2 = col_character(),
## .. Veh2_D_1 = col_character(),
## .. Veh3 = col_character(),
## .. Veh3_D_1 = col_character(),
## .. PCRSect = col_character(),
## .. OBYear = col_character(),
## .. Hemisphere = col_character(),
## .. season = col_character(),
## .. MeanI1 = col_double(),
## .. MedianI1 = col_double(),
## .. Range_S_I1 = col_double(),
## .. Range_L_I1 = col_double(),
## .. MeanD1 = col_double(),
## .. MedianD1 = col_double(),
## .. Range_S_D1 = col_double(),
## .. Range_L_D1 = col_double(),
## .. MeanA1 = col_double(),
## .. MedianA1 = col_double(),
## .. Range_Y_A1 = col_character(),
## .. Range_O_A1 = col_double(),
## .. Action1 = col_character(),
## .. Action2_1 = col_character(),
## .. Secondary = col_character(),
## .. MeanI2 = col_double(),
## .. MedianI2 = col_double(),
## .. Range_S_I2 = col_double(),
## .. Range_L_I2 = col_double(),
## .. MeanD2 = col_double(),
## .. MedianD2 = col_double(),
## .. Range_S_D2 = col_double(),
## .. Range_L_D2 = col_double(),
## .. `Mea 2` = col_double(),
## .. `Media 2` = col_double(),
## .. Range_Y_A2 = col_double(),
## .. Range_O_A2 = col_double(),
## .. Comments_1 = col_character(),
## .. Path1 = col_character(),
## .. Path2_1 = col_character(),
## .. Country = col_character(),
## .. Category = col_character(),
## .. State = col_character(),
## .. Setting_1 = col_character(),
## .. StartMonth = col_double(),
## .. EndMonth = col_double(),
## .. GGA = col_double(),
## .. CA = col_double(),
## .. SA = col_character(),
## .. new_GGA = col_double(),
## .. new_CA = col_double(),
## .. new_SA = col_character(),
## .. SA_resolved_from = col_character(),
## .. GGB = col_double(),
## .. CB = col_character(),
## .. SB = col_character(),
## .. new_GGB = col_double(),
## .. new_CB = col_double(),
## .. new_SB = col_character(),
## .. SB_resolved_from = col_character(),
## .. GGC = col_double(),
## .. CC = col_double(),
## .. SC = col_character(),
## .. new_ggc = col_double(),
## .. new_cc = col_double(),
## .. new_sc = col_character(),
## .. SC_resolved_from = col_character(),
## .. GGD = col_double(),
## .. CD = col_double(),
## .. SD = col_character(),
## .. new_ggd = col_double(),
## .. new_cd = col_double(),
## .. new_sd = col_double(),
## .. SD_resolved_from = col_logical(),
## .. StrainOther = col_character(),
## .. strainother_rc = col_character(),
## .. gge = col_double(),
## .. ce = col_double(),
## .. se = col_character(),
## .. SE_resolved_from = col_character(),
## .. ggf = col_double(),
## .. cf = col_double(),
## .. sf = col_character(),
## .. ggg = col_double(),
## .. cg = col_double(),
## .. sg = col_character(),
## .. ggh = col_double(),
## .. ch = col_double(),
## .. sh = col_character(),
## .. ggi = col_double(),
## .. ci = col_double(),
## .. si = col_character(),
## .. ggj = col_double(),
## .. cj = col_double(),
## .. sj = col_character(),
## .. Country2 = col_character(),
## .. Veh1_D_2 = col_character(),
## .. Veh2_D_2 = col_character(),
## .. Veh3_D_2 = col_character(),
## .. Action2_2 = col_character(),
## .. Comments_2 = col_character(),
## .. Path2_2 = col_character(),
## .. Setting_2 = col_character(),
## .. category1 = col_character(),
## .. strainothergg2c4 = col_double(),
## .. gg2c4 = col_character(),
## .. Vomit = col_double(),
## .. IncInd = col_double(),
## .. SymInd = col_double(),
## .. PooledLat = col_double(),
## .. PooledSym = col_double(),
## .. PooledAge = col_double(),
## .. IndividualLatent = col_logical(),
## .. IndividualSymptomatic = col_character()
## .. )</code></pre>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1"><span class="kw">summary</span>(d)</a></code></pre></div>
<pre><code>## Action1 CasesAll Country Deaths
## Length:953 Min. : 1 Length:953 Min. :0.00000
## Class :character 1st Qu.: 9 Class :character 1st Qu.:0.00000
## Mode :character Median : 25 Mode :character Median :0.00000
## Mean : 129 Mean :0.05379
## 3rd Qu.: 64 3rd Qu.:0.00000
## Max. :32150 Max. :9.00000
## NA's :5 NA's :42
## EndMonth gg2c4 Hemisphere Hospitalizations
## Min. : 0.000 Length:953 Length:953 Min. : 0.0000
## 1st Qu.: 0.000 Class :character Class :character 1st Qu.: 0.0000
## Median : 0.000 Mode :character Mode :character Median : 0.0000
## Mean : 2.559 Mean : 0.7113
## 3rd Qu.: 4.000 3rd Qu.: 0.0000
## Max. :12.000 Max. :125.0000
## NA's :42
## MeanD1 MeanI1 MedianD1 MedianI1
## Min. : 0.000 Min. : 0.0000 Min. : 0.000 Min. : 0.000
## 1st Qu.: 0.000 1st Qu.: 0.0000 1st Qu.: 0.000 1st Qu.: 0.000
## Median : 0.000 Median : 0.0000 Median : 0.000 Median : 0.000
## Mean : 1.558 Mean : 0.7125 Mean : 2.611 Mean : 1.703
## 3rd Qu.: 0.000 3rd Qu.: 0.0000 3rd Qu.: 0.000 3rd Qu.: 0.000
## Max. :273.600 Max. :48.0000 Max. :235.200 Max. :65.000
##
## OBYear Path1 RateAll RiskAll
## Min. :1983 Length:953 Min. : 0.00 Min. : 0.0
## 1st Qu.:2000 Class :character 1st Qu.: 0.00 1st Qu.: 0.0
## Median :2003 Mode :character Median : 16.50 Median : 20.5
## Mean :2002 Mean : 27.04 Mean : 399.7
## 3rd Qu.:2005 3rd Qu.: 49.00 3rd Qu.: 110.8
## Max. :2010 Max. :105.00 Max. :35000.0
## NA's :103 NA's :115
## season Setting_1 StartMonth State
## Length:953 Length:953 Min. : 0.000 Length:953
## Class :character Class :character 1st Qu.: 2.000 Class :character
## Mode :character Mode :character Median : 5.000 Mode :character
## Mean : 5.894
## 3rd Qu.:10.000
## Max. :12.000
##
## Trans1 Vomit
## Length:953 Min. :0.0000
## Class :character 1st Qu.:0.0000
## Mode :character Median :1.0000
## Mean :0.5074
## 3rd Qu.:1.0000
## Max. :1.0000
## NA's :1</code></pre>
<div class="sourceCode" id="cb24"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb24-1" data-line-number="1"><span class="kw">unique</span>(d<span class="op">$</span>State)</a></code></pre></div>
<pre><code>## [1] "0"
## [2] "NC, FL"
## [3] "FL"
## [4] "LA, MD, MS, NC"
## [5] "AK"
## [6] "WI"
## [7] "WA, FL"
## [8] "TX"
## [9] "WV, MD, FL, NY, PA, VA"
## [10] "MD, TX, NC, PA, MS"
## [11] "NC"
## [12] "LA, MS, MD, NC"
## [13] "OR"
## [14] "VA"
## [15] "WY"
## [16] "GA"
## [17] "UT"
## [18] "PA"
## [19] "CA"
## [20] "MI"
## [21] "1"
## [22] "NY"
## [23] "CO"
## [24] "MA"
## [25] "HI"
## [26] "AZ"
## [27] "MD"
## [28] "KY"
## [29] "OH"
## [30] "14 states: CA, UT, KS, WI, IL, IN, OH, GA, FL, NC, VA, WV, NY, PA,"
## [31] "LA"
## [32] "SC"
## [33] "VT"
## [34] "DC"
## [35] "WA"
## [36] "WV"</code></pre>
<p>Some issues we noted previously: We need to remove the <code>Unspecified</code> entry in <code>Hemisphere</code> and recode <code>Action1</code> and <code>Path1</code> as described in the <em>Data Analysis exercise</em>, i.e., from <code>Unknown</code> to <code>Unspecified</code>. Also, we want to group the <code>Setting_1</code> variable into just <code>Restaurant</code> and <code>Other</code>. Again, remember that there are <code>restaurant</code> and <code>Restaurant</code> values, so you need to fix that too.</p>
<p>We’ll also recode the gg2c4 blank entries to “No”. We further note that <code>Action1</code> has a single <code>No</code> entry. Let’s remove that observation to prevent potential problems during cross-validation.</p>
<p>Let’s also lump country together, make 3 categories, Japan, USA, and Other.</p>
<p>As discussed previously, it makes sense to move the Waterborne to the Environmental in the <code>Trans1</code> variable. It also turns out that most outbreaks have no information for state, so best to drop the <code>State</code> variable.</p>
<p>Finally, move the outcome into the first column.</p>
<div class="sourceCode" id="cb26"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb26-1" data-line-number="1"><span class="co"># write code that performs the actions described above</span></a>
<a class="sourceLine" id="cb26-2" data-line-number="2"><span class="co">#remove the Unspecified entry in Hemisphere. There was no unspecified entry, so I just ended up changing it to a factor.</span></a>
<a class="sourceLine" id="cb26-3" data-line-number="3"><span class="kw">unique</span>(d<span class="op">$</span>Hemisphere)</a></code></pre></div>
<pre><code>## [1] "Northern" "Southern"</code></pre>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb28-1" data-line-number="1">d<span class="op">$</span>Hemisphere <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Hemisphere)</a>
<a class="sourceLine" id="cb28-2" data-line-number="2"><span class="kw">levels</span>(d<span class="op">$</span>Hemisphere)</a></code></pre></div>
<pre><code>## [1] "Northern" "Southern"</code></pre>
<div class="sourceCode" id="cb30"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb30-1" data-line-number="1"><span class="co">#recode Action1 and Path1:</span></a>
<a class="sourceLine" id="cb30-2" data-line-number="2">d<span class="op">$</span>Action1 <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Action1)</a>
<a class="sourceLine" id="cb30-3" data-line-number="3">d<span class="op">$</span>Action1 <-<span class="st"> </span><span class="kw">fct_collapse</span>(d<span class="op">$</span>Action1, <span class="dt">Unspecified =</span> <span class="kw">c</span>(<span class="st">"Unknown"</span>, <span class="st">"Unspecified"</span>), <span class="dt">Yes =</span> <span class="st">"Yes"</span>, <span class="dt">No =</span> <span class="st">"No"</span>)</a>
<a class="sourceLine" id="cb30-4" data-line-number="4">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb30-5" data-line-number="5"><span class="st"> </span><span class="kw">filter</span>(Action1 <span class="op">!=</span><span class="st"> "No"</span>)</a>
<a class="sourceLine" id="cb30-6" data-line-number="6">d<span class="op">$</span>Action1 <-<span class="st"> </span><span class="kw">droplevels</span>(d<span class="op">$</span>Action1)</a>
<a class="sourceLine" id="cb30-7" data-line-number="7"><span class="kw">levels</span>(d<span class="op">$</span>Action1)</a></code></pre></div>
<pre><code>## [1] "Unspecified" "Yes"</code></pre>
<div class="sourceCode" id="cb32"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb32-1" data-line-number="1">d<span class="op">$</span>Path1 <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Path1)</a>
<a class="sourceLine" id="cb32-2" data-line-number="2"><span class="kw">levels</span>(d<span class="op">$</span>Path1)</a></code></pre></div>
<pre><code>## [1] "No" "Unknown" "Unspecified" "Yes"</code></pre>
<div class="sourceCode" id="cb34"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb34-1" data-line-number="1">d<span class="op">$</span>Path1 <-<span class="st"> </span><span class="kw">fct_collapse</span>(d<span class="op">$</span>Path1, <span class="dt">Unspecified =</span> <span class="kw">c</span>(<span class="st">"Unknown"</span>, <span class="st">"Unspecified"</span>), <span class="dt">Yes =</span> <span class="st">"Yes"</span>, <span class="dt">No =</span> <span class="st">"No"</span>)</a>
<a class="sourceLine" id="cb34-2" data-line-number="2"><span class="kw">levels</span>(d<span class="op">$</span>Path1)</a></code></pre></div>
<pre><code>## [1] "No" "Unspecified" "Yes"</code></pre>
<div class="sourceCode" id="cb36"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb36-1" data-line-number="1"><span class="co">#Group setting_1 into only two factors</span></a>
<a class="sourceLine" id="cb36-2" data-line-number="2"></a>
<a class="sourceLine" id="cb36-3" data-line-number="3">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb36-4" data-line-number="4"><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">Setting =</span> <span class="kw">ifelse</span>(stringr<span class="op">::</span><span class="kw">str_detect</span>(d<span class="op">$</span>Setting_<span class="dv">1</span>, <span class="st">"[R|r]est*"</span>)<span class="op">==</span><span class="st"> </span><span class="ot">TRUE</span>, <span class="st">"Restaurant"</span>, <span class="st">"Other"</span>))</a>
<a class="sourceLine" id="cb36-5" data-line-number="5"></a>
<a class="sourceLine" id="cb36-6" data-line-number="6">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb36-7" data-line-number="7"><span class="st"> </span><span class="kw">select</span>(<span class="op">-</span>Setting_<span class="dv">1</span>)</a>
<a class="sourceLine" id="cb36-8" data-line-number="8"></a>
<a class="sourceLine" id="cb36-9" data-line-number="9">d<span class="op">$</span>Setting <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Setting)</a>
<a class="sourceLine" id="cb36-10" data-line-number="10"></a>
<a class="sourceLine" id="cb36-11" data-line-number="11"><span class="kw">str</span>(d<span class="op">$</span>Setting)</a></code></pre></div>
<pre><code>## Factor w/ 2 levels "Other","Restaurant": 1 1 1 2 1 2 1 2 1 1 ...</code></pre>
<div class="sourceCode" id="cb38"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb38-1" data-line-number="1"><span class="co">#lump together the countries</span></a>
<a class="sourceLine" id="cb38-2" data-line-number="2">d<span class="op">$</span>Country <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Country)</a>
<a class="sourceLine" id="cb38-3" data-line-number="3"><span class="kw">levels</span>(d<span class="op">$</span>Country)</a></code></pre></div>
<pre><code>## [1] "Australia" "Ca da" "Chi" "Croatia" "Denmark"
## [6] "France" "Iraq" "Israel" "Italy" "Japan"
## [11] "Multiple" "New Zealand" "Norway" "Other" "Scotland"
## [16] "Spain" "UK" "Unspecified" "USA"</code></pre>
<div class="sourceCode" id="cb40"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb40-1" data-line-number="1">d<span class="op">$</span>Country <-<span class="st"> </span><span class="kw">fct_collapse</span>(d<span class="op">$</span>Country, <span class="dt">Other =</span> <span class="kw">c</span>(<span class="st">"Australia"</span>, <span class="st">"Ca da"</span>, <span class="st">"Chi"</span>, <span class="st">"Croatia"</span>, <span class="st">"Denmark"</span>, <span class="st">"France"</span>, <span class="st">"Iraq"</span>, <span class="st">"Israel"</span>, <span class="st">"Italy"</span>, <span class="st">"Multiple"</span>, <span class="st">"New Zealand"</span>, <span class="st">"Norway"</span>, <span class="st">"Other"</span>, <span class="st">"Scotland"</span>, <span class="st">"Spain"</span>, <span class="st">"UK"</span>, <span class="st">"Unspecified"</span>), <span class="dt">USA =</span> <span class="st">"USA"</span>, <span class="dt">Japan =</span> <span class="st">"Japan"</span>)</a>
<a class="sourceLine" id="cb40-2" data-line-number="2"></a>
<a class="sourceLine" id="cb40-3" data-line-number="3"></a>
<a class="sourceLine" id="cb40-4" data-line-number="4"><span class="co">#Combine Environmental and Waterborne transmission --- I also moved Unknown and Unspecified into the same level of Unspecified</span></a>
<a class="sourceLine" id="cb40-5" data-line-number="5">d<span class="op">$</span>Trans1 <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Trans1)</a>
<a class="sourceLine" id="cb40-6" data-line-number="6"><span class="kw">levels</span>(d<span class="op">$</span>Trans1)</a></code></pre></div>
<pre><code>## [1] "Environmental" "Foodborne" "Person to Person"
## [4] "Unknown" "Unspecified" "Waterborne"</code></pre>
<div class="sourceCode" id="cb42"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb42-1" data-line-number="1">d<span class="op">$</span>Trans1 <-<span class="st"> </span><span class="kw">fct_collapse</span>(d<span class="op">$</span>Trans1, <span class="dt">Environmental =</span> <span class="kw">c</span>(<span class="st">"Environmental"</span>, <span class="st">"Waterborne"</span>), <span class="dt">Unspecified =</span> <span class="kw">c</span>(<span class="st">"Unknown"</span>, <span class="st">"Unspecified"</span>))</a>
<a class="sourceLine" id="cb42-2" data-line-number="2"><span class="kw">levels</span>(d<span class="op">$</span>Trans1)</a></code></pre></div>
<pre><code>## [1] "Environmental" "Foodborne" "Person to Person"
## [4] "Unspecified"</code></pre>
<div class="sourceCode" id="cb44"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb44-1" data-line-number="1"><span class="co">#drop the state variable because there are 841 observations of zero.</span></a>
<a class="sourceLine" id="cb44-2" data-line-number="2"><span class="kw">table</span>(d<span class="op">$</span>State)</a></code></pre></div>
<pre><code>##
## 0
## 841
## 1
## 1
## 14 states: CA, UT, KS, WI, IL, IN, OH, GA, FL, NC, VA, WV, NY, PA,
## 1
## AK
## 9
## AZ
## 2
## CA
## 2
## CO
## 1
## DC
## 1
## FL
## 7
## GA
## 2
## HI
## 2
## KY
## 1
## LA
## 1
## LA, MD, MS, NC
## 1
## LA, MS, MD, NC
## 1
## MA
## 3
## MD
## 2
## MD, TX, NC, PA, MS
## 1
## MI
## 6
## NC
## 18
## NC, FL
## 1
## NY
## 10
## OH
## 3
## OR
## 4
## PA
## 3
## SC
## 1
## TX
## 5
## UT
## 1
## VA
## 6
## VT
## 2
## WA
## 1
## WA, FL
## 1
## WI
## 5
## WV
## 1
## WV, MD, FL, NY, PA, VA
## 1
## WY
## 4</code></pre>
<div class="sourceCode" id="cb46"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb46-1" data-line-number="1">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb46-2" data-line-number="2"><span class="st"> </span><span class="kw">select</span>(<span class="op">-</span>State)</a>
<a class="sourceLine" id="cb46-3" data-line-number="3"></a>
<a class="sourceLine" id="cb46-4" data-line-number="4"><span class="co">#change the remaining character variables into factor variables</span></a>