-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogistic_regression.html
More file actions
1338 lines (1291 loc) · 73 KB
/
logistic_regression.html
File metadata and controls
1338 lines (1291 loc) · 73 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" lang="en" xml:lang="en"><head>
<meta charset="utf-8">
<meta name="generator" content="quarto-1.3.450">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<title>Introduction to Data Science - 9 Logistic regression</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
div.columns{display: flex; gap: min(4vw, 1.5em);}
div.column{flex: auto; overflow-x: auto;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
ul.task-list li input[type="checkbox"] {
width: 0.8em;
margin: 0 0.8em 0.2em -1em; /* quarto-specific, see https://github.com/quarto-dev/quarto-cli/issues/4556 */
vertical-align: middle;
}
/* CSS for syntax highlighting */
pre > code.sourceCode { white-space: pre; position: relative; }
pre > code.sourceCode > span { display: inline-block; line-height: 1.25; }
pre > code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
pre > code.sourceCode { white-space: pre-wrap; }
pre > code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; 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
{ }
@media screen {
pre > code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
</style>
<script src="site_libs/quarto-nav/quarto-nav.js"></script>
<script src="site_libs/quarto-nav/headroom.min.js"></script>
<script src="site_libs/clipboard/clipboard.min.js"></script>
<script src="site_libs/quarto-search/autocomplete.umd.js"></script>
<script src="site_libs/quarto-search/fuse.min.js"></script>
<script src="site_libs/quarto-search/quarto-search.js"></script>
<meta name="quarto:offset" content="./">
<link href="./linear_regression.html" rel="next">
<link href="./topic_modelling.html" rel="prev">
<script src="site_libs/quarto-html/quarto.js"></script>
<script src="site_libs/quarto-html/popper.min.js"></script>
<script src="site_libs/quarto-html/tippy.umd.min.js"></script>
<script src="site_libs/quarto-html/anchor.min.js"></script>
<link href="site_libs/quarto-html/tippy.css" rel="stylesheet">
<link href="site_libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="site_libs/bootstrap/bootstrap.min.js"></script>
<link href="site_libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="site_libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script id="quarto-search-options" type="application/json">{
"location": "sidebar",
"copy-button": false,
"collapse-after": 3,
"panel-placement": "start",
"type": "textbox",
"limit": 20,
"language": {
"search-no-results-text": "No results",
"search-matching-documents-text": "matching documents",
"search-copy-link-title": "Copy link to search",
"search-hide-matches-text": "Hide additional matches",
"search-more-match-text": "more match in this document",
"search-more-matches-text": "more matches in this document",
"search-clear-button-title": "Clear",
"search-detached-cancel-button-title": "Cancel",
"search-submit-button-title": "Submit",
"search-label": "Search"
}
}</script>
<script src="site_libs/kePrint-0.0.1/kePrint.js"></script>
<link href="site_libs/lightable-0.0.1/lightable.css" rel="stylesheet">
</head>
<body class="nav-sidebar floating">
<div id="quarto-search-results"></div>
<header id="quarto-header" class="headroom fixed-top">
<nav class="quarto-secondary-nav">
<div class="container-fluid d-flex">
<button type="button" class="quarto-btn-toggle btn" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
<i class="bi bi-layout-text-sidebar-reverse"></i>
</button>
<nav class="quarto-page-breadcrumbs" aria-label="breadcrumb"><ol class="breadcrumb"><li class="breadcrumb-item"><a href="./logistic_regression.html"><span class="chapter-number">9</span> <span class="chapter-title">Logistic regression</span></a></li></ol></nav>
<a class="flex-grow-1" role="button" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass" aria-controls="quarto-sidebar" aria-expanded="false" aria-label="Toggle sidebar navigation" onclick="if (window.quartoToggleHeadroom) { window.quartoToggleHeadroom(); }">
</a>
<button type="button" class="btn quarto-search-button" aria-label="" onclick="window.quartoOpenSearch();">
<i class="bi bi-search"></i>
</button>
</div>
</nav>
</header>
<!-- content -->
<div id="quarto-content" class="quarto-container page-columns page-rows-contents page-layout-article">
<!-- sidebar -->
<nav id="quarto-sidebar" class="sidebar collapse collapse-horizontal sidebar-navigation floating overflow-auto">
<div class="pt-lg-2 mt-2 text-left sidebar-header">
<div class="sidebar-title mb-0 py-0">
<a href="./">Introduction to Data Science</a>
</div>
</div>
<div class="mt-2 flex-shrink-0 align-items-center">
<div class="sidebar-search">
<div id="quarto-search" class="" title="Search"></div>
</div>
</div>
<div class="sidebar-menu-container">
<ul class="list-unstyled mt-1">
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./index.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">Course overview</span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./intro.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">1</span> <span class="chapter-title">What is data science?</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./getting_started_with_R.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">2</span> <span class="chapter-title">Getting started with R</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./reading_and_tidying_data.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">3</span> <span class="chapter-title">Reading and tidying data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./working_with_strings.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">4</span> <span class="chapter-title">Working with strings</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./publishing_with_r.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">5</span> <span class="chapter-title">Publishing with R</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./summarizing_data.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">6</span> <span class="chapter-title">Summarizing data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./visualizing_data.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">7</span> <span class="chapter-title">Visualizing data</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./topic_modelling.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">8</span> <span class="chapter-title">Topic Modelling</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./logistic_regression.html" class="sidebar-item-text sidebar-link active">
<span class="menu-text"><span class="chapter-number">9</span> <span class="chapter-title">Logistic regression</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./linear_regression.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">10</span> <span class="chapter-title">Linear regression</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./shinyapps.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">11</span> <span class="chapter-title">Shiny apps</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./shinyapps_part2.html" class="sidebar-item-text sidebar-link">
<span class="menu-text"><span class="chapter-number">12</span> <span class="chapter-title">ShinyApps</span></span></a>
</div>
</li>
<li class="sidebar-item">
<div class="sidebar-item-container">
<a href="./references.html" class="sidebar-item-text sidebar-link">
<span class="menu-text">References</span></a>
</div>
</li>
</ul>
</div>
</nav>
<div id="quarto-sidebar-glass" data-bs-toggle="collapse" data-bs-target="#quarto-sidebar,#quarto-sidebar-glass"></div>
<!-- margin-sidebar -->
<div id="quarto-margin-sidebar" class="sidebar margin-sidebar">
<nav id="TOC" role="doc-toc" class="toc-active">
<h2 id="toc-title">Table of contents</h2>
<ul>
<li><a href="#learning-objectives" id="toc-learning-objectives" class="nav-link active" data-scroll-target="#learning-objectives"><span class="header-section-number">9.1</span> Learning objectives</a></li>
<li><a href="#introduction" id="toc-introduction" class="nav-link" data-scroll-target="#introduction"><span class="header-section-number">9.2</span> Introduction</a></li>
<li><a href="#logistic-regression" id="toc-logistic-regression" class="nav-link" data-scroll-target="#logistic-regression"><span class="header-section-number">9.3</span> Logistic regression</a>
<ul class="collapse">
<li><a href="#logistic-function" id="toc-logistic-function" class="nav-link" data-scroll-target="#logistic-function"><span class="header-section-number">9.3.1</span> Logistic function</a></li>
<li><a href="#odds" id="toc-odds" class="nav-link" data-scroll-target="#odds"><span class="header-section-number">9.3.2</span> Odds</a></li>
</ul></li>
<li><a href="#logistic-regression-example" id="toc-logistic-regression-example" class="nav-link" data-scroll-target="#logistic-regression-example"><span class="header-section-number">9.4</span> Logistic regression example</a>
<ul class="collapse">
<li><a href="#loading-and-preparing-data" id="toc-loading-and-preparing-data" class="nav-link" data-scroll-target="#loading-and-preparing-data"><span class="header-section-number">9.4.1</span> Loading and preparing data</a></li>
<li><a href="#visualizing-the-relationships" id="toc-visualizing-the-relationships" class="nav-link" data-scroll-target="#visualizing-the-relationships"><span class="header-section-number">9.4.2</span> Visualizing the relationships</a></li>
<li><a href="#creating-the-model" id="toc-creating-the-model" class="nav-link" data-scroll-target="#creating-the-model"><span class="header-section-number">9.4.3</span> Creating the model</a></li>
</ul></li>
<li><a href="#summary" id="toc-summary" class="nav-link" data-scroll-target="#summary"><span class="header-section-number">9.5</span> Summary</a></li>
<li><a href="#lab" id="toc-lab" class="nav-link" data-scroll-target="#lab"><span class="header-section-number">9.6</span> Lab</a></li>
</ul>
</nav>
</div>
<!-- main -->
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title"><span class="chapter-number">9</span> <span class="chapter-title">Logistic regression</span></h1>
</div>
<div class="quarto-title-meta">
</div>
</header>
<section id="learning-objectives" class="level2" data-number="9.1">
<h2 data-number="9.1" class="anchored" data-anchor-id="learning-objectives"><span class="header-section-number">9.1</span> Learning objectives</h2>
<ul>
<li>Logistic regression</li>
</ul>
</section>
<section id="introduction" class="level2" data-number="9.2">
<h2 data-number="9.2" class="anchored" data-anchor-id="introduction"><span class="header-section-number">9.2</span> Introduction</h2>
<p>Regression is a method used to determine the relationship between a dependent variable (the variable we want to predict) and one or more independent variables (the predictors available to make the prediction). There are a wide variety of regression methods, but in this course we will learn two: the <strong>logistic regression</strong>, which is used to predict a <strong>categorical dependent variable</strong>, and the <strong>linear regression</strong>, which is used to predict a <strong>continuous dependent variable</strong>.</p>
<p>In this chapter, we focus on the <strong>binomial</strong> logistic regression (we will refer to it as logistic regression or simply regression in the rest of the chapter), which means that our dependent variable is dichotomous (e.g., yes or no, pass vs fail). Ordinal logistic regression (for ordinal dependent variables) and multinominal logistic regression (for variables with more than 2 categories) are beyond the scope of the course.</p>
</section>
<section id="logistic-regression" class="level2" data-number="9.3">
<h2 data-number="9.3" class="anchored" data-anchor-id="logistic-regression"><span class="header-section-number">9.3</span> Logistic regression</h2>
<section id="logistic-function" class="level3" data-number="9.3.1">
<h3 data-number="9.3.1" class="anchored" data-anchor-id="logistic-function"><span class="header-section-number">9.3.1</span> Logistic function</h3>
<p>Logistic regression is called this way because it fits a <strong>logistic function</strong> (an s-shaped curve) to the data to model the relationship between the predictors and a categorical outcome. More specifically, it models the <strong>probability</strong> of an outcome (the dependent categorical variable) based on the value of the independent variable. Here’s an example:</p>
<p><img src="files/logistic_function.jpg" class="img-fluid" width="515"></p>
<p>The model gives us, for each value of the independent variable (Copper content, in this example), the probability (odds) that the painting is an original. The point where the logistic curve reaches .5 (50%) on the y-axis is where the cut-off happens: the model predicts that any painting with a copper content above that point is an original.</p>
</section>
<section id="odds" class="level3" data-number="9.3.2">
<h3 data-number="9.3.2" class="anchored" data-anchor-id="odds"><span class="header-section-number">9.3.2</span> Odds</h3>
<p>We can convert probabilities into odds by dividing the probability <em>p</em> of the one outcome by the probability of the other outcome, so because there are only two outcomes, then <code>odds = p / 1-p</code>. For example, say we have a bag of 10 balls, 2 red and 8 black. If we draw a ball at random, we have a 8/10 = 80% chance of drawing a black ball. The odds of drawing a black ball are thus 0.8/(1-0.8) = 0.8/0.2 = <strong>4</strong>. There is a 4 to 1 chance that we’ll draw a black ball over a red one.</p>
<p>However, the output of the logistic regression model is the natural logarithm of the odds: <code>log odds = ln(p/1-p)</code>, so it is not so easily interpreted.</p>
</section>
</section>
<section id="logistic-regression-example" class="level2" data-number="9.4">
<h2 data-number="9.4" class="anchored" data-anchor-id="logistic-regression-example"><span class="header-section-number">9.4</span> Logistic regression example</h2>
<section id="loading-and-preparing-data" class="level3" data-number="9.4.1">
<h3 data-number="9.4.1" class="anchored" data-anchor-id="loading-and-preparing-data"><span class="header-section-number">9.4.1</span> Loading and preparing data</h3>
<div class="cell">
<div class="sourceCode cell-code" id="cb1"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb1-1"><a href="#cb1-1" aria-hidden="true" tabindex="-1"></a><span class="fu">library</span>(tidyverse)</span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">read_csv</span>(<span class="st">"https://pmongeon.github.io/info6270/files/data/titanic.csv"</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data) <span class="sc">%>%</span> </span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">PassengerId</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Pclass</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Name</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Sex</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">SibSp</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Parch</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Ticket</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Cabin</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Embarked</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: left;">Braund, Mr. Owen Harris</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">A/5 21171</td>
<td style="text-align: right;">7.2500</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">S</td>
</tr>
<tr class="even">
<td style="text-align: right;">2</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">Cumings, Mrs. John Bradley (Florence Briggs Thayer)</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">PC 17599</td>
<td style="text-align: right;">71.2833</td>
<td style="text-align: left;">C85</td>
<td style="text-align: left;">C</td>
</tr>
<tr class="odd">
<td style="text-align: right;">3</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">3</td>
<td style="text-align: left;">Heikkinen, Miss. Laina</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">STON/O2. 3101282</td>
<td style="text-align: right;">7.9250</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">S</td>
</tr>
<tr class="even">
<td style="text-align: right;">4</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">Futrelle, Mrs. Jacques Heath (Lily May Peel)</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">113803</td>
<td style="text-align: right;">53.1000</td>
<td style="text-align: left;">C123</td>
<td style="text-align: left;">S</td>
</tr>
<tr class="odd">
<td style="text-align: right;">5</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: left;">Allen, Mr. William Henry</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">373450</td>
<td style="text-align: right;">8.0500</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">S</td>
</tr>
<tr class="even">
<td style="text-align: right;">6</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: left;">Moran, Mr. James</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">330877</td>
<td style="text-align: right;">8.4583</td>
<td style="text-align: left;">NA</td>
<td style="text-align: left;">Q</td>
</tr>
</tbody>
</table>
</div>
</div>
<section id="choose-a-set-of-predictors-independent-variables" class="level4" data-number="9.4.1.1">
<h4 data-number="9.4.1.1" class="anchored" data-anchor-id="choose-a-set-of-predictors-independent-variables"><span class="header-section-number">9.4.1.1</span> Choose a set of predictors (independent variables)</h4>
<p>Looking at our dataset, we can identify some variables that we think might affect the probability that a passenger survived. In our model, we will choose <strong>Sex, Age, Pclass, and Fare.</strong> The <strong>SibSp</strong> and <strong>Parch</strong> variables represent, respectively, the combined number of siblings and spouses and the combined number of parents and children a passenger has on board. We will add them together to create a fifth predictor called <strong>FamilySize</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb2"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb2-1"><a href="#cb2-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">FamilySize =</span> SibSp <span class="sc">+</span> Parch)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<p>We can now remove the variables that we don’t need in our model by selecting the ones we want to keep.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb3"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb3-1"><a href="#cb3-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">select</span>(Survived, Sex, Age, Fare, FamilySize, Pclass)</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data) <span class="sc">%>%</span> </span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Sex</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FamilySize</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Pclass</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">7.2500</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: right;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">71.2833</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">7.9250</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: right;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">53.1000</td>
<td style="text-align: right;">1</td>
<td style="text-align: right;">1</td>
</tr>
<tr class="odd">
<td style="text-align: right;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">8.0500</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
</tr>
<tr class="even">
<td style="text-align: right;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">NA</td>
<td style="text-align: right;">8.4583</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="set-categorical-variables-as-factors" class="level4" data-number="9.4.1.2">
<h4 data-number="9.4.1.2" class="anchored" data-anchor-id="set-categorical-variables-as-factors"><span class="header-section-number">9.4.1.2</span> Set categorical variables as factors</h4>
<p>Setting categorical variables as factors is always necessary when fitting regression models in R. In our case there are three: Sex, Survived, and Pclass.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb4"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb4-1"><a href="#cb4-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb4-2"><a href="#cb4-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Sex =</span> <span class="fu">as_factor</span>(Sex),</span>
<span id="cb4-3"><a href="#cb4-3" aria-hidden="true" tabindex="-1"></a> <span class="at">Survived =</span> <span class="fu">as_factor</span>(Survived),</span>
<span id="cb4-4"><a href="#cb4-4" aria-hidden="true" tabindex="-1"></a> <span class="at">Pclass =</span> <span class="fu">as_factor</span>(Pclass))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
</section>
<section id="dealing-with-missing-data" class="level4" data-number="9.4.1.3">
<h4 data-number="9.4.1.3" class="anchored" data-anchor-id="dealing-with-missing-data"><span class="header-section-number">9.4.1.3</span> Dealing with missing data</h4>
<p>We can count the number of empty cells for each variable to see if some data is missing. We do this for each variable in the set.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb5"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb5-1"><a href="#cb5-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>Survived))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb7"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb7-1"><a href="#cb7-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>Sex))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb9"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb9-1"><a href="#cb9-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>Age))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 177</code></pre>
</div>
<div class="sourceCode cell-code" id="cb11"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb11-1"><a href="#cb11-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>Fare))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb13"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb13-1"><a href="#cb13-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>FamilySize))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
<div class="sourceCode cell-code" id="cb15"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb15-1"><a href="#cb15-1" aria-hidden="true" tabindex="-1"></a><span class="fu">sum</span>(<span class="fu">is.na</span>(data<span class="sc">$</span>Pclass))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stdout">
<pre><code>[1] 0</code></pre>
</div>
</div>
<p>Once we have identified that some columns contain missing data, we have two choices. We do nothing and these cases will be left out of the regression model, or we fill the empty cells in some way (this is called imputation). We have many missing values (177 out of 891 observations is quite large) and leaving out these observations could negatively affect the performance of our regression model. Therefore, we will assign the average age for all 177 missing age values, which is a typical <em>imputation mechanism</em> to replace NA values with an estimate based on the available data.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb17"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb17-1"><a href="#cb17-1" aria-hidden="true" tabindex="-1"></a><span class="co"># We use na.rm = TRUE otherwise mean(Age) would return NA due to the missing values.</span></span>
<span id="cb17-2"><a href="#cb17-2" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb17-3"><a href="#cb17-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Age =</span> <span class="fu">replace_na</span>(Age, <span class="fu">round</span>(<span class="fu">mean</span>(Age, <span class="at">na.rm=</span><span class="cn">TRUE</span>),<span class="dv">0</span>)))</span>
<span id="cb17-4"><a href="#cb17-4" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb17-5"><a href="#cb17-5" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data) <span class="sc">%>%</span> </span>
<span id="cb17-6"><a href="#cb17-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb17-7"><a href="#cb17-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Sex</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FamilySize</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Pclass</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">7.2500</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">3</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">71.2833</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">7.9250</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">53.1000</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">1</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">8.0500</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
</tr>
<tr class="even">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">30</td>
<td style="text-align: right;">8.4583</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
</section>
<section id="visualizing-the-relationships" class="level3" data-number="9.4.2">
<h3 data-number="9.4.2" class="anchored" data-anchor-id="visualizing-the-relationships"><span class="header-section-number">9.4.2</span> Visualizing the relationships</h3>
<p>To explore the relationship between variables. we can visualize the distribution of independent variable values for each value of the dependent variable. We can use box plots or violin plots for continuous independent variables and bar charts for the categorical variables. To make the process faster, let’s briefly untidy the data and use the <strong>gather()</strong> function to create key-value pairs for each observation of the dependent variable.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb18"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb18-1"><a href="#cb18-1" aria-hidden="true" tabindex="-1"></a>data_untidy <span class="ot"><-</span> <span class="fu">gather</span>(data, <span class="at">key =</span> <span class="st">"Variable"</span>, <span class="at">value =</span> <span class="st">"Value"</span>,</span>
<span id="cb18-2"><a href="#cb18-2" aria-hidden="true" tabindex="-1"></a> <span class="sc">-</span>Survived) <span class="co"># Creates key-value pairs for all columns except Survive</span></span>
<span id="cb18-3"><a href="#cb18-3" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data_untidy) <span class="sc">%>%</span> </span>
<span id="cb18-4"><a href="#cb18-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb18-5"><a href="#cb18-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Variable</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">male</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">female</td>
</tr>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">female</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">female</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">male</td>
</tr>
<tr class="even">
<td style="text-align: left;">0</td>
<td style="text-align: left;">Sex</td>
<td style="text-align: left;">male</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We can now easily create box plots for all our independent variables and outcome.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb19"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb19-1"><a href="#cb19-1" aria-hidden="true" tabindex="-1"></a>data_untidy <span class="sc">%>%</span> </span>
<span id="cb19-2"><a href="#cb19-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(Variable <span class="sc">!=</span> <span class="st">"Pclass"</span> <span class="sc">&</span> Variable <span class="sc">!=</span> <span class="st">"Sex"</span>) <span class="sc">%>%</span></span>
<span id="cb19-3"><a href="#cb19-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(Survived, <span class="fu">as.numeric</span>(Value))) <span class="sc">+</span></span>
<span id="cb19-4"><a href="#cb19-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> Variable, <span class="at">scales =</span> <span class="st">"free_y"</span>) <span class="sc">+</span></span>
<span id="cb19-5"><a href="#cb19-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_boxplot</span>(<span class="at">draw_quantiles =</span> <span class="fu">c</span>(<span class="fl">0.25</span>, <span class="fl">0.5</span>, <span class="fl">0.75</span>))</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="logistic_regression_files/figure-html/10.7-1.png" class="img-fluid" width="672"></p>
</div>
</div>
<p>And now we make a bar chart for our categorical independent variables.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb20"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb20-1"><a href="#cb20-1" aria-hidden="true" tabindex="-1"></a>data_untidy <span class="sc">%>%</span></span>
<span id="cb20-2"><a href="#cb20-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">filter</span>(Variable <span class="sc">==</span> <span class="st">"Pclass"</span> <span class="sc">|</span> Variable <span class="sc">==</span> <span class="st">"Sex"</span>) <span class="sc">%>%</span></span>
<span id="cb20-3"><a href="#cb20-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">ggplot</span>(<span class="fu">aes</span>(Value, <span class="at">fill =</span> Survived)) <span class="sc">+</span></span>
<span id="cb20-4"><a href="#cb20-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> Variable, <span class="at">scales =</span> <span class="st">"free_x"</span>) <span class="sc">+</span></span>
<span id="cb20-5"><a href="#cb20-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_bar</span>(<span class="at">position =</span> <span class="st">"fill"</span>)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<p><img src="logistic_regression_files/figure-html/10.8-1.png" class="img-fluid" width="672"></p>
</div>
</div>
</section>
<section id="creating-the-model" class="level3" data-number="9.4.3">
<h3 data-number="9.4.3" class="anchored" data-anchor-id="creating-the-model"><span class="header-section-number">9.4.3</span> Creating the model</h3>
<p>The following code generates our logistic regression model using the <code>glm()</code> function (glm stands for general linear model). The syntax is <code>gml(predicted variable ~ predictor1 + predictor2 + preductor3..., data, family)</code> where data is our dataset and the family is the type of regression model we want to create. In our case, the family is <strong>binomial</strong>.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb21"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb21-1"><a href="#cb21-1" aria-hidden="true" tabindex="-1"></a>model <span class="ot"><-</span> <span class="fu">glm</span>(Survived <span class="sc">~</span> Sex <span class="sc">+</span> Age <span class="sc">+</span> Fare <span class="sc">+</span> FamilySize <span class="sc">+</span> Pclass,</span>
<span id="cb21-2"><a href="#cb21-2" aria-hidden="true" tabindex="-1"></a> <span class="at">data =</span> data,</span>
<span id="cb21-3"><a href="#cb21-3" aria-hidden="true" tabindex="-1"></a> <span class="at">family =</span> binomial)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
</div>
<section id="model-summary" class="level4" data-number="9.4.3.1">
<h4 data-number="9.4.3.1" class="anchored" data-anchor-id="model-summary"><span class="header-section-number">9.4.3.1</span> Model summary</h4>
<p>Now that we have created our model, we can look at the coefficients (estimates) which tell us about the relationship between our predictors and the predicted variable. The <strong>Pr(>|z|)</strong> column represents the <strong>p-value</strong>, which determines whether the effect observed is statistically significant. It is common to use 0.05 as the threshold for statistical significance, so all the effects in our model are statistically significant (p < 0.05) except for the fare (p > 0.05).</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb22"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb22-1"><a href="#cb22-1" aria-hidden="true" tabindex="-1"></a><span class="fu">summary</span>(model)<span class="sc">$</span>coefficients <span class="sc">%>%</span> </span>
<span id="cb22-2"><a href="#cb22-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb22-3"><a href="#cb22-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Estimate</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Std. Error</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">z value</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Pr(>|z|)</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">1.0377996</td>
<td style="text-align: right;">0.3932615</td>
<td style="text-align: right;">2.638956</td>
<td style="text-align: right;">0.0083162</td>
</tr>
<tr class="even">
<td style="text-align: left;">Sexfemale</td>
<td style="text-align: right;">2.7763022</td>
<td style="text-align: right;">0.1985984</td>
<td style="text-align: right;">13.979481</td>
<td style="text-align: right;">0.0000000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Age</td>
<td style="text-align: right;">-0.0388614</td>
<td style="text-align: right;">0.0078206</td>
<td style="text-align: right;">-4.969087</td>
<td style="text-align: right;">0.0000007</td>
</tr>
<tr class="even">
<td style="text-align: left;">Fare</td>
<td style="text-align: right;">0.0032132</td>
<td style="text-align: right;">0.0024551</td>
<td style="text-align: right;">1.308796</td>
<td style="text-align: right;">0.1906036</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FamilySize</td>
<td style="text-align: right;">-0.2435093</td>
<td style="text-align: right;">0.0676841</td>
<td style="text-align: right;">-3.597733</td>
<td style="text-align: right;">0.0003210</td>
</tr>
<tr class="even">
<td style="text-align: left;">Pclass2</td>
<td style="text-align: right;">-1.0021830</td>
<td style="text-align: right;">0.2929527</td>
<td style="text-align: right;">-3.420972</td>
<td style="text-align: right;">0.0006240</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Pclass3</td>
<td style="text-align: right;">-2.1318527</td>
<td style="text-align: right;">0.2891435</td>
<td style="text-align: right;">-7.372993</td>
<td style="text-align: right;">0.0000000</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="converting-log-odds-to-odds-ratio" class="level4" data-number="9.4.3.2">
<h4 data-number="9.4.3.2" class="anchored" data-anchor-id="converting-log-odds-to-odds-ratio"><span class="header-section-number">9.4.3.2</span> Converting log odds to odds ratio</h4>
<p>As we mentioned above, the coefficients produced by the model are log odds, which are difficult to interpret. We can convert them to odds ratio, which are easier to interpret. This can be done with the <code>exp()</code> function. We can now see that according to our model, female passengers were 16 times more likely to survive than male passengers.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb23"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb23-1"><a href="#cb23-1" aria-hidden="true" tabindex="-1"></a><span class="fu">bind_rows</span>(<span class="fu">exp</span>(model<span class="sc">$</span>coefficients)) <span class="sc">%>%</span> </span>
<span id="cb23-2"><a href="#cb23-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb23-3"><a href="#cb23-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">(Intercept)</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Sexfemale</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FamilySize</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Pclass2</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Pclass3</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">2.822998</td>
<td style="text-align: right;">16.05953</td>
<td style="text-align: right;">0.961884</td>
<td style="text-align: right;">1.003218</td>
<td style="text-align: right;">0.7838722</td>
<td style="text-align: right;">0.3670772</td>
<td style="text-align: right;">0.1186173</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="adding-confidence-intervals" class="level4" data-number="9.4.3.3">
<h4 data-number="9.4.3.3" class="anchored" data-anchor-id="adding-confidence-intervals"><span class="header-section-number">9.4.3.3</span> Adding confidence intervals</h4>
<p>The confidence intervals are an estimation of the precision odds ratio. In the example below, We use a 95% confidence interval which means that we are 95% of our estimated coefficients for a predictor are between the 2.5<sup>th</sup> percentile and the 97.5th percentile (the two values reported in the tables). If we were using a sample to make claims about a population, which does not really apply here due to the unique case of the titanic, we could then think of the confidence interval as indicating a 95% probability that the true coefficient for the entire population is situated in between the two values.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb24"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb24-1"><a href="#cb24-1" aria-hidden="true" tabindex="-1"></a>odds_ratio <span class="ot"><-</span> <span class="fu">cbind</span>(<span class="at">Odds_Ratio =</span> <span class="fu">exp</span>(model<span class="sc">$</span>coefficients), </span>
<span id="cb24-2"><a href="#cb24-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">exp</span>(<span class="fu">confint</span>(model, <span class="at">level =</span> .<span class="dv">95</span>)))</span>
<span id="cb24-3"><a href="#cb24-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb24-4"><a href="#cb24-4" aria-hidden="true" tabindex="-1"></a>odds_ratio <span class="sc">%>%</span> </span>
<span id="cb24-5"><a href="#cb24-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb24-6"><a href="#cb24-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th"></th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Odds_Ratio</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">2.5 %</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">97.5 %</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">2.8229984</td>
<td style="text-align: right;">1.3083312</td>
<td style="text-align: right;">6.1328871</td>
</tr>
<tr class="even">
<td style="text-align: left;">Sexfemale</td>
<td style="text-align: right;">16.0595260</td>
<td style="text-align: right;">10.9723363</td>
<td style="text-align: right;">23.9218257</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Age</td>
<td style="text-align: right;">0.9618840</td>
<td style="text-align: right;">0.9469679</td>
<td style="text-align: right;">0.9764876</td>
</tr>
<tr class="even">
<td style="text-align: left;">Fare</td>
<td style="text-align: right;">1.0032184</td>
<td style="text-align: right;">0.9987147</td>
<td style="text-align: right;">1.0086269</td>
</tr>
<tr class="odd">
<td style="text-align: left;">FamilySize</td>
<td style="text-align: right;">0.7838722</td>
<td style="text-align: right;">0.6827452</td>
<td style="text-align: right;">0.8907643</td>
</tr>
<tr class="even">
<td style="text-align: left;">Pclass2</td>
<td style="text-align: right;">0.3670772</td>
<td style="text-align: right;">0.2060871</td>
<td style="text-align: right;">0.6511201</td>
</tr>
<tr class="odd">
<td style="text-align: left;">Pclass3</td>
<td style="text-align: right;">0.1186173</td>
<td style="text-align: right;">0.0670531</td>
<td style="text-align: right;">0.2089431</td>
</tr>
</tbody>
</table>
</div>
</div>
</section>
<section id="model-predictions" class="level4" data-number="9.4.3.4">
<h4 data-number="9.4.3.4" class="anchored" data-anchor-id="model-predictions"><span class="header-section-number">9.4.3.4</span> Model predictions</h4>
<p>First we add to our data the probability that the passenger survived as calculated by the model.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb25"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb25-1"><a href="#cb25-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> <span class="fu">tibble</span>(data,</span>
<span id="cb25-2"><a href="#cb25-2" aria-hidden="true" tabindex="-1"></a> <span class="at">Probability =</span> model<span class="sc">$</span>fitted.values)</span>
<span id="cb25-3"><a href="#cb25-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb25-4"><a href="#cb25-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data) <span class="sc">%>%</span> </span>
<span id="cb25-5"><a href="#cb25-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb25-6"><a href="#cb25-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Sex</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FamilySize</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Pclass</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Probability</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">7.2500</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.1025490</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">38</td>
<td style="text-align: right;">71.2833</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">0.9107566</td>
</tr>
<tr class="odd">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">26</td>
<td style="text-align: right;">7.9250</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.6675927</td>
</tr>
<tr class="even">
<td style="text-align: left;">1</td>
<td style="text-align: left;">female</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">53.1000</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">1</td>
<td style="text-align: right;">0.9153720</td>
</tr>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">35</td>
<td style="text-align: right;">8.0500</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.0810373</td>
</tr>
<tr class="even">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">30</td>
<td style="text-align: right;">8.4583</td>
<td style="text-align: right;">0</td>
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.0968507</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>Then we obtain the prediction by creating a new variable called prediction and setting the value to 1 if the calculated probability of survival is greater than 50% and 0 otherwise.</p>
<div class="cell">
<div class="sourceCode cell-code" id="cb26"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb26-1"><a href="#cb26-1" aria-hidden="true" tabindex="-1"></a>data <span class="ot"><-</span> data <span class="sc">%>%</span> </span>
<span id="cb26-2"><a href="#cb26-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">mutate</span>(<span class="at">Prediction =</span> <span class="fu">if_else</span>(Probability <span class="sc">></span> <span class="fl">0.5</span>,<span class="dv">1</span>,<span class="dv">0</span>))</span>
<span id="cb26-3"><a href="#cb26-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb26-4"><a href="#cb26-4" aria-hidden="true" tabindex="-1"></a><span class="fu">head</span>(data) <span class="sc">%>%</span> </span>
<span id="cb26-5"><a href="#cb26-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kbl</span>() <span class="sc">%>%</span> </span>
<span id="cb26-6"><a href="#cb26-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_classic</span>()</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output-display">
<table class="lightable-classic table table-sm table-striped small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">Survived</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Sex</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Age</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Fare</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">FamilySize</th>
<th style="text-align: left;" data-quarto-table-cell-role="th">Pclass</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Probability</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">Prediction</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">0</td>
<td style="text-align: left;">male</td>
<td style="text-align: right;">22</td>
<td style="text-align: right;">7.2500</td>
<td style="text-align: right;">1</td>
<td style="text-align: left;">3</td>
<td style="text-align: right;">0.1025490</td>
<td style="text-align: right;">0</td>
</tr>
<tr class="even">