-
Notifications
You must be signed in to change notification settings - Fork 0
/
Variable_Selection.html
1669 lines (1581 loc) · 128 KB
/
Variable_Selection.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
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" />
<meta name="author" content="Amanda Skarlupka" />
<meta name="date" content="2019-10-30" />
<title>Variable Selection Analysis</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">
<h1 class="title toc-ignore">Variable Selection Analysis</h1>
<h4 class="author">Amanda Skarlupka</h4>
<h4 class="date">2019-10-30</h4>
</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 performing variable selection. For this exercise, we consider a categorical outcome.</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. 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"><span class="kw">library</span>(<span class="st">'dplyr'</span>)</a>
<a class="sourceLine" id="cb1-3" data-line-number="3"><span class="kw">library</span>(<span class="st">'readr'</span>)</a>
<a class="sourceLine" id="cb1-4" data-line-number="4"><span class="kw">library</span>(<span class="st">'forcats'</span>)</a>
<a class="sourceLine" id="cb1-5" data-line-number="5"><span class="kw">library</span>(<span class="st">'ggplot2'</span>)</a>
<a class="sourceLine" id="cb1-6" data-line-number="6"><span class="kw">library</span>(<span class="st">'knitr'</span>)</a>
<a class="sourceLine" id="cb1-7" data-line-number="7"><span class="kw">library</span>(<span class="st">'mlr'</span>) <span class="co">#for model fitting.</span></a></code></pre></div>
<pre><code>## Loading required package: ParamHelpers</code></pre>
<pre><code>##
## Attaching package: 'mlr'</code></pre>
<pre><code>## The following object is masked from 'package:e1071':
##
## impute</code></pre>
<pre><code>## The following object is masked from 'package:caret':
##
## train</code></pre>
<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="kw">library</span>(<span class="st">'parallelMap'</span>) <span class="co">#for using multiple processors when running models through mlr</span></a>
<a class="sourceLine" id="cb6-2" data-line-number="2"><span class="kw">library</span>(<span class="st">'tidyverse'</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="cb7"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb7-1" data-line-number="1"><span class="co">#Write code that loads the dataset </span></a>
<a class="sourceLine" id="cb7-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="cb7-3" data-line-number="3"></a>
<a class="sourceLine" id="cb7-4" data-line-number="4">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, our main outcome of interest is if an outbreak was caused by the G2.4 strain of norovirus or not, and how other factors might be correlated with that strain.</p>
<div class="sourceCode" id="cb11"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb11-1" data-line-number="1"><span class="co">#write code to take a look at the outcome variable (gg2c4)</span></a>
<a class="sourceLine" id="cb11-2" data-line-number="2"></a>
<a class="sourceLine" id="cb11-3" data-line-number="3">d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb11-4" data-line-number="4"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> gg2c4)) <span class="op">+</span></a>
<a class="sourceLine" id="cb11-5" data-line-number="5"><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="Variable_Selection_files/figure-html/checkoutcome-1.png" width="672" /> Overall, it looks ok, a decent amout in each category (i.e. no unbalanced data). However, we see an odd coding, there are only 2 types of entries, either “Yes” or "“, i.e. an empty space. The codebook tells us that this is how things were coded, if it was G2.4 it got a”Yes“, if it wasn’t it did get an empty space instead of a”No“. That’s somewhat strange and while the analysis should work, it is better to re-code the empty slots with”No".</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 to change the empty values in gg2c4 to "No"</span></a>
<a class="sourceLine" id="cb13-2" data-line-number="2"></a>
<a class="sourceLine" id="cb13-3" data-line-number="3">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="cb13-4" data-line-number="4">d<span class="op">$</span>gg2c4 <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>gg2c4)</a>
<a class="sourceLine" id="cb13-5" data-line-number="5">d<span class="op">$</span>gg2c4</a></code></pre></div>
<pre><code>## [1] Yes No Yes No Yes No No No Yes No No No No No No No No
## [18] No Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No
## [35] Yes Yes Yes No No Yes No No No No No No No No No No Yes
## [52] Yes No No No Yes No No Yes No No No No No Yes No No No
## [69] Yes No No Yes No No No No No No Yes Yes Yes Yes Yes No No
## [86] No No Yes No No No No No Yes Yes Yes No Yes No No No No
## [103] No No Yes Yes No No No No No No No No Yes Yes No No Yes
## [120] Yes Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No No
## [137] No No No No No No Yes No No No No Yes Yes No No No No
## [154] No No No No No No No No Yes Yes No No Yes No No No No
## [171] No Yes Yes Yes Yes No No No No No No No No Yes No No No
## [188] Yes Yes Yes Yes No Yes Yes No No No Yes Yes Yes No Yes Yes No
## [205] No No No No No No No No No No No No No No No No No
## [222] No No No No No No No No No Yes No No Yes No No No No
## [239] No No No No No No No No Yes No No No No No No No No
## [256] No No No No No No Yes No Yes No No No No No No No No
## [273] No Yes No No No No No Yes No No No Yes No No No No No
## [290] Yes No No No No No No No No No No No No No No No Yes
## [307] No No No No No No Yes No Yes Yes No No No No No No No
## [324] No No No No No No No No No No No Yes Yes No No No No
## [341] No Yes Yes No No Yes No No No No No No Yes Yes Yes Yes No
## [358] No No No No Yes Yes No No No No No No No Yes No Yes No
## [375] Yes No Yes No No No No No No No No No No No No No No
## [392] No Yes Yes Yes Yes No No No Yes Yes No No No Yes No No No
## [409] No No Yes No No No Yes No No No Yes No No No Yes Yes Yes
## [426] No No No No No Yes No No Yes No No Yes Yes No Yes No No
## [443] No No No No No No No No No No Yes No No No No No No
## [460] Yes No No No No No No No No No No No No No No No No
## [477] No No No No Yes No No No No Yes Yes Yes Yes Yes Yes No No
## [494] No No No No No No No No No No No Yes No No No No Yes
## [511] No No No No No Yes No Yes No No No Yes No No No No No
## [528] No No No No No No No No No No Yes Yes No Yes Yes Yes Yes
## [545] Yes Yes Yes Yes No No No No Yes No Yes Yes Yes No No No Yes
## [562] Yes Yes No No No No No No No No No No No No No No No
## [579] No Yes No No No No No No No No No Yes No No No Yes No
## [596] No No No No No No No No No No No No No No No No No
## [613] No No No No No No No No No No No No No No No No No
## [630] No No No No No No No No No No No No No No Yes Yes No
## [647] No No No Yes No Yes No No Yes Yes Yes Yes No Yes Yes No No
## [664] Yes Yes No No No No No Yes No No No No No No No No Yes
## [681] No No Yes No No Yes No No Yes Yes No No No Yes No No No
## [698] No No No No Yes No No No No No Yes No Yes No Yes Yes No
## [715] No No Yes No No No No No No Yes No No No Yes No No No
## [732] Yes Yes Yes No No Yes No Yes Yes No Yes Yes Yes Yes Yes Yes Yes
## [749] Yes Yes Yes No Yes Yes No No No No Yes Yes Yes No Yes Yes Yes
## [766] No Yes No Yes Yes Yes No Yes No No Yes No Yes No No No No
## [783] No No No No No No No No Yes No No Yes Yes Yes No No No
## [800] No No No No No No No No No Yes No No No No No No No
## [817] No No No No No No No No No No No Yes No No Yes Yes No
## [834] No No No Yes No Yes No No No Yes Yes Yes No Yes No Yes Yes
## [851] No Yes Yes No Yes Yes Yes Yes Yes Yes Yes Yes Yes Yes No No No
## [868] No No No No No No No Yes No Yes Yes Yes No Yes No Yes Yes
## [885] Yes Yes No No No No No No No No No No No No No No Yes
## [902] Yes Yes Yes No No No No No No No No No Yes Yes Yes Yes Yes
## [919] No Yes Yes No Yes Yes Yes Yes Yes Yes Yes No Yes No Yes Yes No
## [936] Yes Yes No No Yes No No No No No No No No No No No No
## [953] No No No No No No No No No No Yes No No Yes No No No
## [970] No No No No No No No Yes No Yes Yes Yes Yes Yes Yes Yes Yes
## [987] Yes Yes No No No No No No No No Yes Yes Yes Yes Yes Yes No
## [1004] Yes Yes Yes Yes Yes No No No No No No No Yes No Yes No Yes
## [1021] Yes Yes
## Levels: No Yes</code></pre>
</div>
<div id="selecting-predictors" class="section level1">
<h1>Selecting predictors</h1>
<p>We will pick similar variables as previously, with some adjustments based on what we learned before. Keep the following variables: <code>Action1, CasesAll, Country, Deaths, GG2C4, Hemisphere, Hospitalizations, MeanD1, MeanI1, MedianD1, MedianI1, OBYear, Path1, RiskAll, Season, Setting, Trans1, Vomit.</code></p>
<div class="sourceCode" id="cb15"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb15-1" data-line-number="1">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb15-2" data-line-number="2"><span class="st"> </span>dplyr<span class="op">::</span><span class="kw">select</span>(Action1, CasesAll, Country, Deaths, gg2c4, Hemisphere, Hospitalizations, MeanD1, MeanI1, MedianD1, MedianI1, OBYear, Path1, RiskAll, season, Setting_<span class="dv">1</span>, Trans1, Vomit)</a>
<a class="sourceLine" id="cb15-3" data-line-number="3"><span class="kw">dim</span>(d)</a></code></pre></div>
<pre><code>## [1] 1022 18</code></pre>
</div>
<div id="cleaning-predictors" class="section level1">
<h1>Cleaning predictors</h1>
<p>We’ll likely have to perform similar cleaning steps as before. A difference is that earlier we had to drop about half of the observations because no outcome was available. Here, we have outcome for every outbreak. Thus, there is more data to clean, which - as we will see - introduces a few issues we didn’t have before, since we dropped the troublesome observations early in the process.</p>
<p>Let’s first check for missing values.</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 looks at missing values and changes the charcters to factors</span></a>
<a class="sourceLine" id="cb17-2" data-line-number="2">d <-<span class="st"> </span><span class="kw">as.data.frame</span>(<span class="kw">unclass</span>(d))</a>
<a class="sourceLine" id="cb17-3" data-line-number="3"><span class="kw">print</span>(<span class="kw">colSums</span>(<span class="kw">is.na</span>(d)))</a></code></pre></div>
<pre><code>## Action1 CasesAll Country Deaths
## 0 7 0 43
## gg2c4 Hemisphere Hospitalizations MeanD1
## 0 0 43 0
## MeanI1 MedianD1 MedianI1 OBYear
## 0 0 0 0
## Path1 RiskAll season Setting_1
## 0 120 67 0
## Trans1 Vomit
## 0 1</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">visdat<span class="op">::</span><span class="kw">vis_dat</span>(d)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/check-reduced-data-1.png" width="672" /></p>
<div class="sourceCode" id="cb20"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb20-1" data-line-number="1">visdat<span class="op">::</span><span class="kw">vis_miss</span>(d)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/check-reduced-data-2.png" width="672" /></p>
<p>Looks like we have again some missing in <code>Hospitalization</code> and <code>Deaths</code>, and some more in <code>RiskAll</code>. Since the missing is not excessive, and to make our life easier, we’ll drop them for now. Note however the ‘blocks’ of missing values for RiskAll. Given that these outbreaks should be entered fairly randomly into the spreadsheet, it is strange to see the NA show up in such blocks. For a real data analysis, it would be worth looking closer and checking why there is a clustering like that.</p>
<div class="sourceCode" id="cb21"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb21-1" data-line-number="1"><span class="co"># write code to remove any observations with NA</span></a>
<a class="sourceLine" id="cb21-2" data-line-number="2"></a>
<a class="sourceLine" id="cb21-3" data-line-number="3">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb21-4" data-line-number="4"><span class="st"> </span><span class="kw">filter</span>(<span class="kw">is.na</span>(Hospitalizations) <span class="op">==</span><span class="st"> </span><span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb21-5" data-line-number="5"></a>
<a class="sourceLine" id="cb21-6" data-line-number="6">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb21-7" data-line-number="7"><span class="st"> </span><span class="kw">filter</span>(<span class="kw">is.na</span>(Deaths) <span class="op">==</span><span class="st"> </span><span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb21-8" data-line-number="8"></a>
<a class="sourceLine" id="cb21-9" data-line-number="9">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb21-10" data-line-number="10"><span class="st"> </span><span class="kw">filter</span>(<span class="kw">is.na</span>(RiskAll) <span class="op">==</span><span class="st"> </span><span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb21-11" data-line-number="11"></a>
<a class="sourceLine" id="cb21-12" data-line-number="12">visdat<span class="op">::</span><span class="kw">vis_dat</span>(d)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/further-reduce-data-1.png" width="672" /></p>
<div class="sourceCode" id="cb22"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb22-1" data-line-number="1">visdat<span class="op">::</span><span class="kw">vis_miss</span>(d)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/further-reduce-data-2.png" width="672" /></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="cb23"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb23-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="cb23-2" data-line-number="2"><span class="co">#also convert any other variables as needed</span></a>
<a class="sourceLine" id="cb23-3" data-line-number="3"></a>
<a class="sourceLine" id="cb23-4" data-line-number="4"><span class="kw">str</span>(d)</a></code></pre></div>
<pre><code>## 'data.frame': 859 obs. of 18 variables:
## $ Action1 : Factor w/ 4 levels "No","Unknown",..: 3 3 3 3 3 3 3 3 3 4 ...
## $ CasesAll : num 15 65 27 4 15 6 40 10 116 45 ...
## $ Country : Factor w/ 22 levels "Australia","Austria",..: 12 22 17 17 17 17 17 17 17 22 ...
## $ Deaths : num 0 0 0 0 0 0 0 0 0 0 ...
## $ gg2c4 : Factor w/ 2 levels "No","Yes": 2 1 2 1 2 1 1 1 2 1 ...
## $ Hemisphere : Factor w/ 3 levels "Northern","Southern",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ 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 : Factor w/ 23 levels "0","1983","1990",..: 11 10 19 19 19 19 19 19 17 5 ...
## $ Path1 : Factor w/ 4 levels "No","Unknown",..: 1 1 3 3 3 3 3 3 1 3 ...
## $ RiskAll : num 0 108 130 4 25 ...
## $ season : Factor w/ 4 levels "Fall","Spring",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Setting_1 : Factor w/ 387 levels "0","2 communities near Apalachicola Bay",..: 119 30 37 309 37 351 37 309 199 1 ...
## $ Trans1 : Factor w/ 6 levels "Environmental",..: 5 2 2 2 2 2 2 2 5 2 ...
## $ Vomit : num 1 1 1 1 1 1 1 1 1 1 ...</code></pre>
<div class="sourceCode" id="cb25"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb25-1" data-line-number="1">d<span class="op">$</span>Vomit <-<span class="st"> </span><span class="kw">as.factor</span>(d<span class="op">$</span>Vomit)</a>
<a class="sourceLine" id="cb25-2" data-line-number="2"></a>
<a class="sourceLine" id="cb25-3" data-line-number="3">d<span class="op">$</span>OBYear <-<span class="st"> </span><span class="kw">as.numeric</span>(<span class="kw">levels</span>(d<span class="op">$</span>OBYear))[d<span class="op">$</span>OBYear]</a></code></pre></div>
<pre><code>## Warning: NAs introduced by coercion</code></pre>
<div class="sourceCode" id="cb27"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb27-1" data-line-number="1">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb27-2" data-line-number="2"><span class="st"> </span><span class="kw">filter</span>(OBYear <span class="op">!=</span><span class="st"> </span><span class="dv">0</span>)</a>
<a class="sourceLine" id="cb27-3" data-line-number="3"></a>
<a class="sourceLine" id="cb27-4" data-line-number="4">d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb27-5" data-line-number="5"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> OBYear)) <span class="op">+</span></a>
<a class="sourceLine" id="cb27-6" data-line-number="6"><span class="st"> </span><span class="kw">geom_bar</span>()</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/adjust-1.png" width="672" /></p>
<div class="sourceCode" id="cb28"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb28-1" data-line-number="1"><span class="kw">str</span>(d)</a></code></pre></div>
<pre><code>## 'data.frame': 853 obs. of 18 variables:
## $ Action1 : Factor w/ 4 levels "No","Unknown",..: 3 3 3 3 3 3 3 3 3 4 ...
## $ CasesAll : num 15 65 27 4 15 6 40 10 116 45 ...
## $ Country : Factor w/ 22 levels "Australia","Austria",..: 12 22 17 17 17 17 17 17 17 22 ...
## $ Deaths : num 0 0 0 0 0 0 0 0 0 0 ...
## $ gg2c4 : Factor w/ 2 levels "No","Yes": 2 1 2 1 2 1 1 1 2 1 ...
## $ Hemisphere : Factor w/ 3 levels "Northern","Southern",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ 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 : Factor w/ 4 levels "No","Unknown",..: 1 1 3 3 3 3 3 3 1 3 ...
## $ RiskAll : num 0 108 130 4 25 ...
## $ season : Factor w/ 4 levels "Fall","Spring",..: 1 1 1 1 1 1 1 1 1 1 ...
## $ Setting_1 : Factor w/ 387 levels "0","2 communities near Apalachicola Bay",..: 119 30 37 309 37 351 37 309 199 1 ...
## $ Trans1 : Factor w/ 6 levels "Environmental",..: 5 2 2 2 2 2 2 2 5 2 ...
## $ Vomit : Factor w/ 2 levels "0","1": 2 2 2 2 2 2 2 2 2 2 ...</code></pre>
<p>Next, we remove the <code>Unspecified</code> entry in <code>Hemisphere</code> and recode <code>Action1</code> and <code>Path1</code> as described in the Data Analysis script, i.e. from <code>Unknown</code> to <code>Unspecified</code>. Also do the same grouping into just <code>Restaurant</code> and <code>Other</code> with the <code>Setting_1</code> variable. Again, remember that there are <code>restaurant</code> and <code>Restaurant</code> values, so you need to fix that too.</p>
<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"># write code that performs the actions described above</span></a>
<a class="sourceLine" id="cb30-2" data-line-number="2"><span class="co"># at the end, use the droplevels() command to remove empty factor levels</span></a>
<a class="sourceLine" id="cb30-3" data-line-number="3"></a>
<a class="sourceLine" id="cb30-4" data-line-number="4"><span class="co">#remove the Unspecified entry in Hemisphere:</span></a>
<a class="sourceLine" id="cb30-5" data-line-number="5"><span class="kw">unique</span>(d<span class="op">$</span>Hemisphere)</a></code></pre></div>
<pre><code>## [1] Northern Southern Unspecified
## Levels: Northern Southern Unspecified</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="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb32-2" data-line-number="2"><span class="st"> </span><span class="kw">filter</span>(Hemisphere <span class="op">!=</span><span class="st"> "Unspecified"</span>) <span class="op">%>%</span></a>
<a class="sourceLine" id="cb32-3" data-line-number="3"><span class="st"> </span><span class="kw">droplevels</span>()</a>
<a class="sourceLine" id="cb32-4" data-line-number="4"></a>
<a class="sourceLine" id="cb32-5" data-line-number="5"><span class="kw">unique</span>(d<span class="op">$</span>Hemisphere)</a></code></pre></div>
<pre><code>## [1] Northern Southern
## Levels: Northern Southern</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"><span class="co">#recode Action1 and Path1:</span></a>
<a class="sourceLine" id="cb34-2" data-line-number="2"><span class="kw">levels</span>(d<span class="op">$</span>Action1)</a></code></pre></div>
<pre><code>## [1] "No" "Unknown" "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">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="cb36-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="cb38"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb38-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="cb38-2" data-line-number="2"></a>
<a class="sourceLine" id="cb38-3" data-line-number="3"><span class="kw">levels</span>(d<span class="op">$</span>Action1)</a></code></pre></div>
<pre><code>## [1] "No" "Unspecified" "Yes"</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"><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="cb42"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb42-1" data-line-number="1"><span class="co">#Group setting_1 into only two factors</span></a>
<a class="sourceLine" id="cb42-2" data-line-number="2"></a>
<a class="sourceLine" id="cb42-3" data-line-number="3">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb42-4" data-line-number="4"><span class="st"> </span><span class="kw">mutate</span>(<span class="dt">Setting =</span> <span class="kw">ifelse</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="cb42-5" data-line-number="5"></a>
<a class="sourceLine" id="cb42-6" data-line-number="6">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb42-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="cb42-8" data-line-number="8"></a>
<a class="sourceLine" id="cb42-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="cb42-10" data-line-number="10"></a>
<a class="sourceLine" id="cb42-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>
<div id="data-visualization" class="section level1">
<h1>Data visualization</h1>
<p>Next, let’s create a few plots showing the outcome and the predictors. For the continuous predictors, I suggest scatter/box/violinplots with the outcome on the x-axis.</p>
<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">#write code that produces plots showing our outcome of interest on the x-axis and each numeric predictor on the y-axis.</span></a>
<a class="sourceLine" id="cb44-2" data-line-number="2"><span class="co">#you can use the facet_wrap functionality in ggplot for it, or do it some other way.</span></a>
<a class="sourceLine" id="cb44-3" data-line-number="3">d <-<span class="st"> </span>d[,<span class="kw">c</span>(<span class="dv">5</span>, <span class="dv">1</span><span class="op">:</span><span class="dv">4</span>, <span class="dv">6</span><span class="op">:</span><span class="dv">18</span>)]</a>
<a class="sourceLine" id="cb44-4" data-line-number="4">d <span class="op">%>%</span><span class="st"> </span></a>
<a class="sourceLine" id="cb44-5" data-line-number="5"><span class="st"> </span><span class="kw">gather</span>(<span class="kw">c</span>(CasesAll, Deaths, Hospitalizations, MeanD1, MeanI1, MedianD1, MedianI1, RiskAll, OBYear), <span class="dt">key =</span> <span class="st">"var"</span>, <span class="dt">value =</span> <span class="st">"value"</span>) <span class="op">%>%</span></a>
<a class="sourceLine" id="cb44-6" data-line-number="6"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> gg2c4, <span class="dt">y =</span> value)) <span class="op">+</span></a>
<a class="sourceLine" id="cb44-7" data-line-number="7"><span class="st"> </span><span class="kw">geom_violin</span>() <span class="op">+</span></a>
<a class="sourceLine" id="cb44-8" data-line-number="8"><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>var, <span class="dt">scales =</span> <span class="st">'free'</span>)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/plots-1-1.png" width="672" /></p>
<p>Things look ok, apart from the skew in the predictors we discussed previously.</p>
<p>Next, let’s create plots for the categorical variabless. You can use for instance <code>geom_count</code> for it, or some other representation. If you prefer lots of tables, that’s ok too.</p>
<div class="sourceCode" id="cb45"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb45-1" data-line-number="1"><span class="co">#write code that produces plots or tables showing our outcome of interest and each categorical predictor.</span></a>
<a class="sourceLine" id="cb45-2" data-line-number="2"></a>
<a class="sourceLine" id="cb45-3" data-line-number="3">d <span class="op">%>%</span><span class="st"> </span></a>
<a class="sourceLine" id="cb45-4" data-line-number="4"><span class="st"> </span><span class="kw">gather</span>(<span class="kw">c</span>(Action1, Country, Hemisphere, Path1, season, Trans1, Vomit, Setting), <span class="dt">key =</span> <span class="st">"var"</span>, <span class="dt">value =</span> <span class="st">"value"</span>) <span class="op">%>%</span></a>
<a class="sourceLine" id="cb45-5" data-line-number="5"><span class="st"> </span><span class="kw">ggplot</span>(<span class="kw">aes</span>(<span class="dt">x =</span> gg2c4, <span class="dt">y =</span> value)) <span class="op">+</span></a>
<a class="sourceLine" id="cb45-6" data-line-number="6"><span class="st"> </span><span class="kw">geom_count</span>() <span class="op">+</span></a>
<a class="sourceLine" id="cb45-7" data-line-number="7"><span class="st"> </span><span class="kw">facet_wrap</span>(<span class="op">~</span><span class="st"> </span>var, <span class="dt">scales =</span> <span class="st">'free'</span>)</a></code></pre></div>
<pre><code>## Warning: attributes are not identical across measure variables;
## they will be dropped</code></pre>
<p><img src="Variable_Selection_files/figure-html/plots-2-1.png" width="672" /></p>
<p>You should see from plots or tables that some of the categories are small, e.g. for Action1, the “No” category is very small. Very few entries for a given factor create problems during cross-validation (since we can’t have a level show up in the holdout if it wasn’t part of the fitting set). So let’s look at those factor variables a bit closer and fix as needed.</p>
<p>I didn’t initially get the results described. I initially interpreted the previous step where we dropped NAs to mean that we drop ALL NAs, not just the ones in the three categories.</p>
<div class="sourceCode" id="cb47"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb47-1" data-line-number="1"><span class="co">#write code that looks at tables/summaries of factors </span></a>
<a class="sourceLine" id="cb47-2" data-line-number="2"></a>
<a class="sourceLine" id="cb47-3" data-line-number="3"><span class="kw">table</span>(d<span class="op">$</span>Hemisphere)</a></code></pre></div>
<pre><code>##
## Northern Southern
## 799 53</code></pre>
<div class="sourceCode" id="cb49"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb49-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>Path1)</a></code></pre></div>
<pre><code>##
## No Unspecified Yes
## 197 615 40</code></pre>
<div class="sourceCode" id="cb51"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb51-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>##
## Fall Spring Summer Winter
## 135 187 103 371</code></pre>
<div class="sourceCode" id="cb53"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb53-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>Setting)</a></code></pre></div>
<pre><code>##
## Other Restaurant
## 671 181</code></pre>
<div class="sourceCode" id="cb55"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb55-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>Trans1)</a></code></pre></div>
<pre><code>##
## Environmental Foodborne Person to Person Unknown
## 11 339 119 61
## Unspecified Waterborne
## 261 61</code></pre>
<div class="sourceCode" id="cb57"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb57-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>Action1)</a></code></pre></div>
<pre><code>##
## No Unspecified Yes
## 1 679 172</code></pre>
<div class="sourceCode" id="cb59"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb59-1" data-line-number="1"><span class="kw">table</span>(d<span class="op">$</span>Country)</a></code></pre></div>
<pre><code>##
## Japan Multiple Other Unspecified USA
## 316 16 410 12 98</code></pre>
<div class="sourceCode" id="cb61"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb61-1" data-line-number="1"><span class="kw">str</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## Factor w/ 4 levels "Fall","Spring",..: 1 1 1 1 1 1 1 1 1 1 ...</code></pre>
<div class="sourceCode" id="cb63"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb63-1" data-line-number="1"><span class="kw">levels</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## [1] "Fall" "Spring" "Summer" "Winter"</code></pre>
<p>You should see from your explorations above that there is only a single entry for <em>No</em> in <code>Action1</code>, and small entries for <em>Multiple</em> and <em>Unspecified</em> in <code>Country</code> and <em>Environmental</em> in <code>Trans1</code>. The single <em>No</em> should be fixed, the other somewhat small groupings might be ok. It depends on scientific rationale and method of analysis if you should modify/group those or not. We’ll do that.</p>
<p>Change things as follows: Remove the observation for which action is <em>No</em>, combine Countries into 3 groups Japan/USA/Other, and since I don’t even know what biologically the difference is between <em>Environmental</em> and <em>Waterborne</em> transmission (seems the same route to me based on my norovirus knowledge), we’ll move the Waterborne into the environmental. Finally, I noticed there is a blank category for season. That likely means it wasn’t stated in the paper. Let’s recode that as <em>Unknown</em>. Finally, re-order data such that the outcome is the first column and again remove empty factor levels. Then look at your resulting data frame.</p>
<div class="sourceCode" id="cb65"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb65-1" data-line-number="1"><span class="co">#write code that does the actions described above</span></a>
<a class="sourceLine" id="cb65-2" data-line-number="2"></a>
<a class="sourceLine" id="cb65-3" data-line-number="3"><span class="co">#Remove observation with "No" for action1...I don't have that observation. I think I dropped it when I dropped NAs. </span></a>
<a class="sourceLine" id="cb65-4" data-line-number="4"></a>
<a class="sourceLine" id="cb65-5" data-line-number="5">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb65-6" data-line-number="6"><span class="st"> </span><span class="kw">filter</span>(Action1 <span class="op">!=</span><span class="st"> "No"</span>)</a>
<a class="sourceLine" id="cb65-7" data-line-number="7"></a>
<a class="sourceLine" id="cb65-8" data-line-number="8"><span class="co">#Combine "Countries into 3 groups":</span></a>
<a class="sourceLine" id="cb65-9" data-line-number="9"></a>
<a class="sourceLine" id="cb65-10" data-line-number="10"><span class="kw">levels</span>(d<span class="op">$</span>Country)</a></code></pre></div>
<pre><code>## [1] "Japan" "Multiple" "Other" "Unspecified" "USA"</code></pre>
<div class="sourceCode" id="cb67"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb67-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">"Austria"</span>, <span class="st">"Brazil"</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">"Netherlands"</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></code></pre></div>
<pre><code>## Warning: Unknown levels in `f`: Australia, Austria, Brazil, Ca da, Chi,
## Croatia, Denmark, France, Iraq, Israel, Italy, Netherlands, New Zealand,
## Norway, Scotland, Spain, UK</code></pre>
<div class="sourceCode" id="cb69"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb69-1" data-line-number="1"><span class="kw">levels</span>(d<span class="op">$</span>Country)</a></code></pre></div>
<pre><code>## [1] "Japan" "Other" "USA"</code></pre>
<div class="sourceCode" id="cb71"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb71-1" data-line-number="1"><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="cb71-2" data-line-number="2"></a>
<a class="sourceLine" id="cb71-3" data-line-number="3"><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="cb73"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb73-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="cb73-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="cb75"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb75-1" data-line-number="1"><span class="co">#Recode the blanks for season into Unknown</span></a>
<a class="sourceLine" id="cb75-2" data-line-number="2"></a>
<a class="sourceLine" id="cb75-3" data-line-number="3"><span class="kw">levels</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## [1] "Fall" "Spring" "Summer" "Winter"</code></pre>
<div class="sourceCode" id="cb77"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb77-1" data-line-number="1"><span class="kw">unique</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## [1] Fall Spring Summer Winter <NA>
## Levels: Fall Spring Summer Winter</code></pre>
<div class="sourceCode" id="cb79"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb79-1" data-line-number="1">d<span class="op">$</span>season <-<span class="st"> </span>forcats<span class="op">::</span><span class="kw">fct_explicit_na</span>(d<span class="op">$</span>season, <span class="st">"Unknown"</span>)</a>
<a class="sourceLine" id="cb79-2" data-line-number="2"><span class="kw">unique</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## [1] Fall Spring Summer Winter Unknown
## Levels: Fall Spring Summer Winter Unknown</code></pre>
<div class="sourceCode" id="cb81"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb81-1" data-line-number="1"><span class="kw">levels</span>(d<span class="op">$</span>season)</a></code></pre></div>
<pre><code>## [1] "Fall" "Spring" "Summer" "Winter" "Unknown"</code></pre>
<div class="sourceCode" id="cb83"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb83-1" data-line-number="1">d <-<span class="st"> </span><span class="kw">droplevels</span>(d)</a>
<a class="sourceLine" id="cb83-2" data-line-number="2"></a>
<a class="sourceLine" id="cb83-3" data-line-number="3"><span class="co">#I still have on NA in vomit, so I'm going to drop that to get to the 850 observations</span></a>
<a class="sourceLine" id="cb83-4" data-line-number="4"></a>
<a class="sourceLine" id="cb83-5" data-line-number="5"><span class="co">#Drop NAs in Vomit</span></a>
<a class="sourceLine" id="cb83-6" data-line-number="6"></a>
<a class="sourceLine" id="cb83-7" data-line-number="7">d <-<span class="st"> </span>d <span class="op">%>%</span></a>
<a class="sourceLine" id="cb83-8" data-line-number="8"><span class="st"> </span><span class="kw">filter</span>(<span class="kw">is.na</span>(Vomit) <span class="op">==</span><span class="st"> </span><span class="ot">FALSE</span>)</a>
<a class="sourceLine" id="cb83-9" data-line-number="9"></a>
<a class="sourceLine" id="cb83-10" data-line-number="10">visdat<span class="op">::</span><span class="kw">vis_dat</span>(d)</a></code></pre></div>
<p><img src="Variable_Selection_files/figure-html/more-factor-cleaning-1.png" width="672" /></p>
<p>At this step, you should have a dataframe containing 850 observations, and 18 variables: 1 outcome, 9 numeric/integer predictors, and 8 factor variables. There should be no missing values. The outcome, <code>gg2c4</code>, should be in the 1st slot.</p>
</div>
<div id="data-splitting" class="section level1">
<h1>Data splitting</h1>
<p>We could do data splitting again as we did in the previous exercise, to have a final test set. But since you saw how it works in the previous exercise, we skip it here. We use the full data to fit to our models. We’ll still use cross-validation to get a more honest estimate of model performance. For a real data analysis, the choice to keep some for a final test or not is based on your goals. If your focus is predictive performance, you should consider this split. If your focus is inference or exploratory analysis, you might want to skip this.</p>
</div>
<div id="model-fitting" class="section level1">
<h1>Model fitting</h1>
<p>So I had planned to use <code>caret</code> exclusivley for this course. Last time I tried feature/subset selection with <code>caret</code>, I found it buggy and not too well documented. I had hoped this had improved since. Unfortunately, I was again not really able to get things to work. So even though I said we likely won’t use the <code>mlr</code> package, I decided that to be able to nicely practice feature/subset selection, we need to do so. It’s not a bad idea to get familiar with that package, at times <code>caret</code> can do things <code>mlr</code> can’t do and the reverse. So knowing how to use both is good. We’ll thus use <code>mlr</code> to do our model fitting in this exercise.</p>
</div>
<div id="parallelization" class="section level1">
<h1>Parallelization</h1>
<p><code>mlr</code> allows you to run things in parallel by using multiple cores (<code>caret</code> does too). For instance, if you do 5x cross-validation 5x repeated, you essentially run a very similar piece of code 25 times. Normally, you would run one at a time. But if you have it on a machine with 25 cores, all those 25 could run at the same time, giving you a speed-up of 25 (or close to, there is usually some overhead related to doing things in parallel). This kind of parallel computing is sometimes called <em>embarassingly parallel</em>, because it’s so embarassingly simple to split the task into parallel streams.</p>
<p>Since doing the subset selection below starts getting slow, and because it’s a good topic to know about, we are going to use parallelization here. <code>mlr</code> uses the package <code>parallelMap</code> for this. All you need to do is specify the number of cores/processors you want to use and start the parallel system, and <code>mlr</code> then automatically does things in parallel if feasible.</p>
<div class="sourceCode" id="cb84"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb84-1" data-line-number="1"><span class="co">#set the number of cores you want to use. Note that this is actually the number of 'logical processors', which is 2x the number of cores. On a windows machine, the number of cores/logical processors can be found in the task manager. You should only set it to what your computer has (or less). So if your computer has say 4 or 6 cores, you can set it to that, or some lower number. Setting it to a number higher than the cores your computer has doesn't further speed up things, in fact it slows things down.</span></a>
<a class="sourceLine" id="cb84-2" data-line-number="2">ncpu=<span class="dv">4</span>;</a>
<a class="sourceLine" id="cb84-3" data-line-number="3"><span class="co">#if you don't want to run things in parallel, or don't have multiple cores (unlikely nowadays), </span></a>
<a class="sourceLine" id="cb84-4" data-line-number="4"><span class="co">#just comment out the line below.</span></a>
<a class="sourceLine" id="cb84-5" data-line-number="5"><span class="kw">parallelStartSocket</span>(ncpu, <span class="dt">show.info=</span><span class="ot">FALSE</span>) </a></code></pre></div>
</div>
<div id="setup" class="section level1">
<h1>Setup</h1>
<p>Some setup settings that are used in various code chunks below.</p>
<div class="sourceCode" id="cb85"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb85-1" data-line-number="1">outcome <-<span class="st"> </span>d<span class="op">$</span>gg2c4 </a>
<a class="sourceLine" id="cb85-2" data-line-number="2">outcomename =<span class="st"> "gg2c4"</span></a>
<a class="sourceLine" id="cb85-3" data-line-number="3">predictors <-<span class="st"> </span>d[,<span class="op">-</span><span class="dv">1</span>]</a>
<a class="sourceLine" id="cb85-4" data-line-number="4">npred=<span class="kw">ncol</span>(predictors)</a>
<a class="sourceLine" id="cb85-5" data-line-number="5"><span class="co">#set sampling method for performance evaluation</span></a>
<a class="sourceLine" id="cb85-6" data-line-number="6"><span class="co">#here, we use 5-fold cross-validation, 5-times repeated</span></a>
<a class="sourceLine" id="cb85-7" data-line-number="7">sampling_choice =<span class="st"> </span><span class="kw">makeResampleDesc</span>(<span class="st">"RepCV"</span>, <span class="dt">reps =</span> <span class="dv">5</span>, <span class="dt">folds =</span> <span class="dv">5</span>)</a></code></pre></div>
<div id="a-null-model" class="section level2">
<h2>A null model</h2>
<p>To define a null model, we need to determine what performance measure we want to track. As mentioned in the course materials, there are different performance measures. Accuracy or misclassification error is simple, it just counts the number of times the model got it right/wrong. We’ll start with that one, and then try another one later. <code>mlr</code> allows for a lot of different performance measures for both categorical and continuous outcomes, see <a href="https://mlr.mlr-org.com/articles/tutorial/performance.html">here</a> and <a href="https://mlr.mlr-org.com/articles/tutorial/measures.html">here</a>.</p>
<p>For accuracy, the simplest null model always predicts the most frequent category. We can use that as baseline performance.</p>
<div class="sourceCode" id="cb86"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb86-1" data-line-number="1"><span class="co">#write code that computes accuracy for a null model</span></a>
<a class="sourceLine" id="cb86-2" data-line-number="2"><span class="kw">table</span>(d<span class="op">$</span>gg2c4)</a></code></pre></div>
<pre><code>##
## No Yes
## 591 259</code></pre>
<div class="sourceCode" id="cb88"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb88-1" data-line-number="1"><span class="co">#the null model always predicts "No" becuase this is the most frequent category of our expected outcome. </span></a>
<a class="sourceLine" id="cb88-2" data-line-number="2"></a>
<a class="sourceLine" id="cb88-3" data-line-number="3"><span class="kw">measureACC</span>(d<span class="op">$</span>gg2c4, <span class="st">"No"</span>)</a></code></pre></div>
<pre><code>## [1] 0.6952941</code></pre>
<p>You should find that the null model has an accuracy of around 0.69.</p>
</div>
<div id="single-predictor-models" class="section level2">
<h2>Single predictor models</h2>
<p>Now let’s consider single predictor models, i.e. we’ll fit the outcome to each predictor one at a time to get an idea of the importance of individual predictors. To evaluate our model performance, we will use cross-validation. Since our outcome is categorical, we’ll use a logistic model.</p>
<div class="sourceCode" id="cb90"><pre class="sourceCode r"><code class="sourceCode r"><a class="sourceLine" id="cb90-1" data-line-number="1"><span class="kw">set.seed</span>(<span class="dv">1111</span>) <span class="co">#makes each code block reproducible</span></a>
<a class="sourceLine" id="cb90-2" data-line-number="2"><span class="co">#set learner/model. this corresponds to a logistic model.</span></a>
<a class="sourceLine" id="cb90-3" data-line-number="3"><span class="co">#mlr calls different models different "learners"</span></a>
<a class="sourceLine" id="cb90-4" data-line-number="4">learner_name =<span class="st"> "classif.binomial"</span>;</a>
<a class="sourceLine" id="cb90-5" data-line-number="5">mylearner =<span class="st"> </span><span class="kw">makeLearner</span>(learner_name, <span class="dt">predict.type =</span> <span class="st">"prob"</span>)</a>
<a class="sourceLine" id="cb90-6" data-line-number="6"><span class="co"># this will contain the results</span></a>
<a class="sourceLine" id="cb90-7" data-line-number="7">unifmat=<span class="kw">data.frame</span>(<span class="dt">variable =</span> <span class="kw">rep</span>(<span class="dv">0</span>,npred), <span class="dt">Accuracy =</span> <span class="kw">rep</span>(<span class="dv">0</span>,npred))</a>
<a class="sourceLine" id="cb90-8" data-line-number="8"><span class="co"># loop over each predictor, build simple dataset with just outcome and that predictor, fit it to a glm/logistic model</span></a>
<a class="sourceLine" id="cb90-9" data-line-number="9"><span class="cf">for</span> (nn <span class="cf">in</span> <span class="dv">1</span><span class="op">:</span>npred)</a>
<a class="sourceLine" id="cb90-10" data-line-number="10">{</a>
<a class="sourceLine" id="cb90-11" data-line-number="11"> unidata =<span class="st"> </span><span class="kw">data.frame</span>(<span class="dt">gg2c4 =</span> outcome, d[,nn<span class="op">+</span><span class="dv">1</span>] )</a>
<a class="sourceLine" id="cb90-12" data-line-number="12"> <span class="co">## Generate the task, i.e. define outcome and predictors to be fit</span></a>
<a class="sourceLine" id="cb90-13" data-line-number="13"> mytask =<span class="st"> </span><span class="kw">makeClassifTask</span>(<span class="dt">id=</span><span class="st">'unianalysis'</span>, <span class="dt">data =</span> unidata, <span class="dt">target =</span> outcomename, <span class="dt">positive =</span> <span class="st">"Yes"</span>)</a>
<a class="sourceLine" id="cb90-14" data-line-number="14"> model =<span class="st"> </span><span class="kw">resample</span>(mylearner, <span class="dt">task =</span> mytask, <span class="dt">resampling =</span> sampling_choice, <span class="dt">show.info =</span> <span class="ot">FALSE</span>, <span class="dt">measures =</span> acc )</a>
<a class="sourceLine" id="cb90-15" data-line-number="15"> unifmat[nn,<span class="dv">1</span>] =<span class="st"> </span><span class="kw">names</span>(predictors)[nn] </a>
<a class="sourceLine" id="cb90-16" data-line-number="16"> unifmat[nn,<span class="dv">2</span>] =<span class="st"> </span>model<span class="op">$</span>aggr</a>
<a class="sourceLine" id="cb90-17" data-line-number="17">}</a>
<a class="sourceLine" id="cb90-18" data-line-number="18"><span class="kw">kable</span>(unifmat)</a></code></pre></div>
<table>
<thead>
<tr class="header">
<th align="left">variable</th>
<th align="right">Accuracy</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td align="left">Action1</td>
<td align="right">0.6952941</td>
</tr>
<tr class="even">
<td align="left">CasesAll</td>
<td align="right">0.6941176</td>
</tr>
<tr class="odd">
<td align="left">Country</td>
<td align="right">0.6952941</td>
</tr>
<tr class="even">
<td align="left">Deaths</td>
<td align="right">0.6976471</td>
</tr>
<tr class="odd">
<td align="left">Hemisphere</td>
<td align="right">0.6952941</td>
</tr>
<tr class="even">
<td align="left">Hospitalizations</td>
<td align="right">0.6943529</td>
</tr>
<tr class="odd">
<td align="left">MeanD1</td>
<td align="right">0.6952941</td>
</tr>
<tr class="even">
<td align="left">MeanI1</td>
<td align="right">0.6952941</td>
</tr>
<tr class="odd">
<td align="left">MedianD1</td>
<td align="right">0.6952941</td>
</tr>
<tr class="even">
<td align="left">MedianI1</td>
<td align="right">0.6952941</td>
</tr>
<tr class="odd">