-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path14d_HigherOrderTerms_Interaction.html
More file actions
1248 lines (1206 loc) · 85.5 KB
/
14d_HigherOrderTerms_Interaction.html
File metadata and controls
1248 lines (1206 loc) · 85.5 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.5.43">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
<meta name="dcterms.date" content="2024-08-03">
<title>Using Higher-Order Terms: Interaction</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 { 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 { display: inline-block; 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="14d_HigherOrderTerms_Interaction_files/libs/clipboard/clipboard.min.js"></script>
<script src="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/quarto.js"></script>
<script src="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/popper.min.js"></script>
<script src="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/tippy.umd.min.js"></script>
<script src="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/anchor.min.js"></script>
<link href="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/tippy.css" rel="stylesheet">
<link href="14d_HigherOrderTerms_Interaction_files/libs/quarto-html/quarto-syntax-highlighting.css" rel="stylesheet" id="quarto-text-highlighting-styles">
<script src="14d_HigherOrderTerms_Interaction_files/libs/bootstrap/bootstrap.min.js"></script>
<link href="14d_HigherOrderTerms_Interaction_files/libs/bootstrap/bootstrap-icons.css" rel="stylesheet">
<link href="14d_HigherOrderTerms_Interaction_files/libs/bootstrap/bootstrap.min.css" rel="stylesheet" id="quarto-bootstrap" data-mode="light">
<script src="14d_HigherOrderTerms_Interaction_files/libs/kePrint-0.0.1/kePrint.js"></script>
<link href="14d_HigherOrderTerms_Interaction_files/libs/lightable-0.0.1/lightable.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6"></script>
<script src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-chtml-full.js" type="text/javascript"></script>
<script type="text/javascript">
const typesetMath = (el) => {
if (window.MathJax) {
// MathJax Typeset
window.MathJax.typeset([el]);
} else if (window.katex) {
// KaTeX Render
var mathElements = el.getElementsByClassName("math");
var macros = [];
for (var i = 0; i < mathElements.length; i++) {
var texText = mathElements[i].firstChild;
if (mathElements[i].tagName == "SPAN") {
window.katex.render(texText.data, mathElements[i], {
displayMode: mathElements[i].classList.contains('display'),
throwOnError: false,
macros: macros,
fleqn: false
});
}
}
}
}
window.Quarto = {
typesetMath
};
</script>
</head>
<body>
<div id="quarto-content" class="page-columns page-rows-contents page-layout-article">
<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="#where-we-stand" id="toc-where-we-stand" class="nav-link active" data-scroll-target="#where-we-stand">Where We Stand</a></li>
<li><a href="#objectives" id="toc-objectives" class="nav-link" data-scroll-target="#objectives">Objectives</a></li>
<li><a href="#models-including-interaction-step_interact" id="toc-models-including-interaction-step_interact" class="nav-link" data-scroll-target="#models-including-interaction-step_interact">Models Including Interaction (<code>step_interact()</code>)</a>
<ul class="collapse">
<li><a href="#interactions-in-action" id="toc-interactions-in-action" class="nav-link" data-scroll-target="#interactions-in-action">Interactions in Action!</a></li>
</ul></li>
<li><a href="#your-task" id="toc-your-task" class="nav-link" data-scroll-target="#your-task">Your Task</a></li>
</ul>
<div class="quarto-alternate-formats"><h2>Other Formats</h2><ul><li><a href="14d_HigherOrderTerms_Interaction.pdf"><i class="bi bi-file-pdf"></i>PDF</a></li></ul></div></nav>
</div>
<main class="content" id="quarto-document-content">
<header id="title-block-header" class="quarto-title-block default">
<div class="quarto-title">
<h1 class="title">Using Higher-Order Terms: Interaction</h1>
</div>
<div class="quarto-title-meta">
<div>
<div class="quarto-title-meta-heading">Published</div>
<div class="quarto-title-meta-contents">
<p class="date">August 3, 2024</p>
</div>
</div>
</div>
</header>
<section id="where-we-stand" class="level2">
<h2 class="anchored" data-anchor-id="where-we-stand">Where We Stand</h2>
<p>In the last notebook, we saw how to model curvi-linear relationships. We added curvature to our models by feature engineering with <code>step_poly()</code> as a component of a <code>recipe()</code>. In that notebook, we saw that introducing curvature can improve model performance and can help us model more complex relationships, but results in models which are more difficult to interpret.</p>
<p>In this notebook, we’ll consider another way that we can potentially improve model performance. That is, by allowing predictors to interact with one another. Interactions allow for the relationship between two variables (a predictor and a response) to depend on the value of a third variable (another predictor). The impact of an interaction term on a model depends on the types of variables which are interacting – we’ll explore the possibilities in this notebook. Similar to models including polynomial terms, we’ll sometimes observed improved <em>fit</em> when using models with interaction terms, but those resulting models become more difficult to interpret.</p>
</section>
<section id="objectives" class="level2">
<h2 class="anchored" data-anchor-id="objectives">Objectives</h2>
<ul>
<li>Use exploratory data analysis to identify visual evidence for interaction between predictors.</li>
<li>Use <code>step_interact()</code> to create additional model terms corresponding to the interaction (product) between two or more predictors.</li>
<li>Fit, assess, reduce, interpret, and utilize models including interactions terms.</li>
</ul>
<hr>
</section>
<section id="models-including-interaction-step_interact" class="level2">
<h2 class="anchored" data-anchor-id="models-including-interaction-step_interact">Models Including Interaction (<code>step_interact()</code>)</h2>
<p>We can use interaction terms when we expect that the association between our response and one predictor depends on the value of a second predictor. There are three types of interactions between two variables:</p>
<ul>
<li><p>Interactions between <em>two categorical predictors</em> result in a shift of intercept, associated with combinations of categories. This is very similar to what happened when we first introduced categorical predictors – there we got a different intercept for each level of the categorical predictor. Now, we’ll get an adjustment to the intercept for each combination of levels of the corresponding predictors.</p>
<ul>
<li>For example, if we allow <code>species</code> and <code>year</code> to interact in a model to predict penguin <code>body_mass_g</code>, we’ll have potentially unique intercepts for each <code>species</code> and observed <code>year</code> combination.</li>
</ul></li>
<li><p>Interactions between <em>a categorical predictor and a numerical predictor</em> allow for different slopes/curvatures in the association between the response and corresponding predictor across different levels of the categorical variable.</p>
<ul>
<li>For example, if we allow <code>species</code> and <code>bill_depth_mm</code> to interact in a model to predict penguin <code>body_mass_g</code>, we’ll allow for the association between <code>bill_depth_mm</code> and <code>body_mass_g</code> to be different across the three <code>species</code> of penguin.</li>
</ul></li>
<li><p>Interactions between two numerical variables allow for the association between the response (<span class="math inline">\(y\)</span>) and a predictor (<span class="math inline">\(x_1\)</span>) to depend on the value of a second predictor (<span class="math inline">\(x_2\)</span>).</p>
<ul>
<li>There is not necessarily a nice <em>slope</em> or <em>intercept</em> interpretation here, however, interactions between pairs of numerical predictors can introduce curvature to your regression model. Simply put, if a model includes an interaction term <span class="math inline">\(\beta x_1 x_2\)</span>, in order to know the expected impact of a unit increase of <span class="math inline">\(x_1\)</span> on the response variable (<span class="math inline">\(y\)</span>), we must decide which level of the variable <span class="math inline">\(x_2\)</span> we are interested in first.</li>
<li>As an example, if we allow <code>bill_depth_mm</code> to interact with <code>bill_length_mm</code> in a model predicting penguin <code>body_mass_g</code>, then we are saying that the expected impact of a unit increase in <code>bill_length_mm</code> on <code>body_mass_g</code> will be different for penguins having different <code>bill_depth_mm</code> values.</li>
</ul></li>
</ul>
<section id="interactions-in-action" class="level3">
<h3 class="anchored" data-anchor-id="interactions-in-action">Interactions in Action!</h3>
<p>Let’s see these different types of interactions in action with the penguins data. We’ll look at the three scenarios above one-by-one and then we’ll try pulling everything together.</p>
<section id="interactions-between-two-categorical-variables" class="level4">
<h4 class="anchored" data-anchor-id="interactions-between-two-categorical-variables">Interactions Between Two Categorical Variables</h4>
<p>Let’s say that we propose a model which predicts penguin <code>body_mass_g</code> using <code>flipper_length_mm</code>, <code>species</code>, <code>year</code>, and an interaction between <code>species</code> and <code>year</code>. Such a model is of the following form:</p>
<p><span class="math inline">\(\begin{align}\mathbb{E}\left[\text{body\_mass\_g}\right] = &\beta_0 + \beta_1 \cdot \text{flipper\_length\_mm} +\\ &\beta_2\cdot\text{Chinstrap} + \beta_3\cdot\text{Gentoo} +\\ &\beta_4\cdot\text{year\_08} + \beta_5\cdot\text{year\_09} +\\
&\beta_6\cdot\left(\text{Chinstrap}\right)\left(\text{year\_08}\right) +\\ &\beta_7\cdot\left(\text{Chinstrap}\right)\left(\text{year\_09}\right) +\\ &\beta_8\cdot\left(\text{Gentoo}\right)\left(\text{year\_08}\right) +\\ &\beta_9\cdot\left(\text{Gentoo}\right)\left(\text{year\_09}\right)\end{align}\)</span></p>
<p>There’s lots of coefficients in this model! Let’s fit it and see what we actually end up with. In order to fit this model, we’ll need to include some feature engineering <code>step_*()</code>s. First, we’ll need to obtain <em>dummy</em> variables for the levels of the <code>species</code> and <code>island</code> predictors – we’ll use <code>step_dummy()</code> for that. Then, we’ll want to allow those dummy variables to interact – we’ll use <code>step_interact()</code>, with a few tricks.</p>
<ul>
<li><code>step_interact()</code> requires the use of a tilde (<code>~</code>) prior to defining the terms that should interact with one another.</li>
<li>We’ll denote terms that should interact with one another via the colon (<code>:</code>).</li>
<li>We replaced <code>species</code> and <code>island</code> with corresponding dummy variables when we used <code>step_dummy()</code>, so those columns will not be available in the transformed data being passed to <code>step_interact()</code>. Instead, we’ll have a series of columns like <code>species_X</code>, <code>species_Y</code>, … , and <code>island_A</code>, <code>island_B</code>,… – rather than defining all of these individual interactions, we’ll use the <code>starts_with()</code> selector function to obtain the groups of columns we want interacting with one another.</li>
</ul>
<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>mass_cat_inter_spec <span class="ot"><-</span> <span class="fu">linear_reg</span>() <span class="sc">%>%</span></span>
<span id="cb1-2"><a href="#cb1-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_engine</span>(<span class="st">"lm"</span>)</span>
<span id="cb1-3"><a href="#cb1-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-4"><a href="#cb1-4" aria-hidden="true" tabindex="-1"></a>mass_cat_inter_rec <span class="ot"><-</span> <span class="fu">recipe</span>(body_mass_g <span class="sc">~</span> flipper_length_mm <span class="sc">+</span> species <span class="sc">+</span> year, <span class="at">data =</span> penguins_train) <span class="sc">%>%</span></span>
<span id="cb1-5"><a href="#cb1-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_dummy</span>(species) <span class="sc">%>%</span></span>
<span id="cb1-6"><a href="#cb1-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_mutate</span>(<span class="at">year =</span> <span class="fu">as.factor</span>(year)) <span class="sc">%>%</span></span>
<span id="cb1-7"><a href="#cb1-7" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_dummy</span>(year) <span class="sc">%>%</span></span>
<span id="cb1-8"><a href="#cb1-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_interact</span>(<span class="sc">~</span> <span class="fu">starts_with</span>(<span class="st">"species"</span>)<span class="sc">:</span><span class="fu">starts_with</span>(<span class="st">"year"</span>))</span>
<span id="cb1-9"><a href="#cb1-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-10"><a href="#cb1-10" aria-hidden="true" tabindex="-1"></a>mass_cat_wf <span class="ot"><-</span> <span class="fu">workflow</span>() <span class="sc">%>%</span></span>
<span id="cb1-11"><a href="#cb1-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_model</span>(mass_cat_inter_spec) <span class="sc">%>%</span></span>
<span id="cb1-12"><a href="#cb1-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_recipe</span>(mass_cat_inter_rec)</span>
<span id="cb1-13"><a href="#cb1-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-14"><a href="#cb1-14" aria-hidden="true" tabindex="-1"></a>mass_cat_fit <span class="ot"><-</span> mass_cat_wf <span class="sc">%>%</span></span>
<span id="cb1-15"><a href="#cb1-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(penguins_train)</span>
<span id="cb1-16"><a href="#cb1-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb1-17"><a href="#cb1-17" aria-hidden="true" tabindex="-1"></a>mass_cat_fit <span class="sc">%>%</span></span>
<span id="cb1-18"><a href="#cb1-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">glance</span>() <span class="sc">%>%</span> </span>
<span id="cb1-19"><a href="#cb1-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb1-20"><a href="#cb1-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">adj.r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">sigma</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">logLik</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">AIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">BIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">deviance</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df.residual</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">nobs</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.80644</td>
<td style="text-align: right;">0.7993586</td>
<td style="text-align: right;">367.5427</td>
<td style="text-align: right;">113.8804</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">9</td>
<td style="text-align: right;">-1870.299</td>
<td style="text-align: right;">3762.598</td>
<td style="text-align: right;">3801.595</td>
<td style="text-align: right;">33231550</td>
<td style="text-align: right;">246</td>
<td style="text-align: right;">256</td>
</tr>
</tbody>
</table>
</div>
<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>mass_cat_fit <span class="sc">%>%</span></span>
<span id="cb2-2"><a href="#cb2-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">extract_fit_engine</span>() <span class="sc">%>%</span></span>
<span id="cb2-3"><a href="#cb2-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">tidy</span>() <span class="sc">%>%</span></span>
<span id="cb2-4"><a href="#cb2-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb2-5"><a href="#cb2-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">term</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">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">-4933.59608</td>
<td style="text-align: right;">691.592270</td>
<td style="text-align: right;">-7.1336773</td>
<td style="text-align: right;">0.0000000</td>
</tr>
<tr class="even">
<td style="text-align: left;">flipper_length_mm</td>
<td style="text-align: right;">46.42457</td>
<td style="text-align: right;">3.704239</td>
<td style="text-align: right;">12.5328209</td>
<td style="text-align: right;">0.0000000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">species_Chinstrap</td>
<td style="text-align: right;">-315.26370</td>
<td style="text-align: right;">104.431973</td>
<td style="text-align: right;">-3.0188427</td>
<td style="text-align: right;">0.0028044</td>
</tr>
<tr class="even">
<td style="text-align: left;">species_Gentoo</td>
<td style="text-align: right;">35.38641</td>
<td style="text-align: right;">144.099729</td>
<td style="text-align: right;">0.2455689</td>
<td style="text-align: right;">0.8062207</td>
</tr>
<tr class="odd">
<td style="text-align: left;">year_X2008</td>
<td style="text-align: right;">-186.53149</td>
<td style="text-align: right;">84.902008</td>
<td style="text-align: right;">-2.1970209</td>
<td style="text-align: right;">0.0289520</td>
</tr>
<tr class="even">
<td style="text-align: left;">year_X2009</td>
<td style="text-align: right;">-330.22761</td>
<td style="text-align: right;">88.313278</td>
<td style="text-align: right;">-3.7392747</td>
<td style="text-align: right;">0.0002296</td>
</tr>
<tr class="odd">
<td style="text-align: left;">species_Chinstrap_x_year_X2008</td>
<td style="text-align: right;">54.03892</td>
<td style="text-align: right;">158.852806</td>
<td style="text-align: right;">0.3401823</td>
<td style="text-align: right;">0.7340095</td>
</tr>
<tr class="even">
<td style="text-align: left;">species_Chinstrap_x_year_X2009</td>
<td style="text-align: right;">104.68961</td>
<td style="text-align: right;">146.791942</td>
<td style="text-align: right;">0.7131836</td>
<td style="text-align: right;">0.4764081</td>
</tr>
<tr class="odd">
<td style="text-align: left;">species_Gentoo_x_year_X2008</td>
<td style="text-align: right;">-41.65877</td>
<td style="text-align: right;">126.758321</td>
<td style="text-align: right;">-0.3286472</td>
<td style="text-align: right;">0.7427019</td>
</tr>
<tr class="even">
<td style="text-align: left;">species_Gentoo_x_year_X2009</td>
<td style="text-align: right;">267.92841</td>
<td style="text-align: right;">129.193714</td>
<td style="text-align: right;">2.0738502</td>
<td style="text-align: right;">0.0391339</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>There are some interesting things happening here. Several of our model terms have large <code>p.value</code>s, indicating that they may not be significant predictors of penguin <code>body_mass_g</code>. However, if we look more closely, <em>some</em> of the terms are significant.</p>
<ul>
<li>The <code>species_Gentoo</code> term is not significantly different from the base level (<code>species_Adelie</code>), but the <code>species_Chinstrap</code> term is significantly different from the base level.</li>
<li>Both year categories are significantly different from the base year (<code>year_2007</code>).</li>
<li>The interaction between <code>species</code> and <code>year</code> is significant for Gentoo penguins in the year <span class="math inline">\(2009\)</span>, but not for any of the other combinations of <code>species</code> and <code>year</code>.</li>
</ul>
<p>If all levels of a categorical predictor (or interaction) show insignificant <code>p.value</code>s, then we would drop that predictor (or the corresponding interaction) from the model. Since <em>some</em> of the terms resulting from the categorical predictor (or interaction) are statistically significant, then we’ll keep all of the corresponding model terms.</p>
<ul>
<li>(<span class="math inline">\(\star\)</span>) There are some other things we <em>could</em> do here – we can address some of them, as well as their benefits and drawbacks, in class.</li>
</ul>
<p>Okay, let’s look at the model. The model including estimated <span class="math inline">\(\beta\)</span>-coefficients is:</p>
<p><span class="math inline">\(\begin{align}\mathbb{E}\left[\text{body\_mass\_g}\right] = &-4933.6 + 46.42\cdot \text{flipper\_length\_mm} +\\ &\left(-315.26\right)\cdot\text{Chinstrap} + 35.39\cdot\text{Gentoo} +\\ &\left(-186.53\right)\cdot\text{year\_08} + \left(-330.23\right)\cdot\text{year\_09} +\\
&54.04\cdot\left(\text{Chinstrap}\right)\left(\text{year\_08}\right) +\\ &104.69\cdot\left(\text{Chinstrap}\right)\left(\text{year\_09}\right) +\\ &\left(-41.66\right)\cdot\left(\text{Gentoo}\right)\left(\text{year\_08}\right) &+\\ 267.93\cdot\left(\text{Gentoo}\right)\left(\text{year\_09}\right)\end{align}\)</span></p>
<p>From this, we can make interpretations as follows:</p>
<ul>
<li>On average, controlling for <code>species</code> and observation <code>year</code>, a unit increase in penguin <code>flipper_length_mm</code> is associated with a penguin <code>body_mass_g</code> increase of about <span class="math inline">\(46.42\)</span> grams.</li>
<li>Controlling for <code>flipper_length_mm</code>, Chinstrap penguins are about <span class="math inline">\(315.26\)</span> grams less massive than Adelies, on average. The data used to fit the model doesn’t seem to suggest significant changes in Chinstrap mass year over year.</li>
<li>Controlling for <code>flipper_length_mm</code>, the data used to fit the model doesn’t seem to suggest that Gentoo penguins have significantly different <code>body_mass_g</code> than Adelie penguins. However, there does seem to be a change in the year <span class="math inline">\(2009\)</span> – Gentoos observed during that year seem to be much more massive than Adelies in that year (by about $267.92 - (-330.23) grams).</li>
<li>Controlling for <code>flipper_length_mm</code>, Adelie penguins seem to have less mass year-over-year. Approximately <span class="math inline">\(186.5\)</span> grams lower than their average <span class="math inline">\(2007\)</span> <code>body_mass_g</code> in <span class="math inline">\(2008\)</span> and approximately <span class="math inline">\(330.23\)</span> grams lower than their average <span class="math inline">\(2007\)</span> <code>body_mass_g</code> in <span class="math inline">\(2009\)</span>.</li>
</ul>
<p>Now, let’s look at our model graphically!</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>new_data <span class="ot"><-</span> <span class="fu">crossing</span>(<span class="at">flipper_length_mm =</span> <span class="fu">seq</span>(<span class="fu">min</span>(penguins_train<span class="sc">$</span>flipper_length_mm, </span>
<span id="cb3-2"><a href="#cb3-2" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb3-3"><a href="#cb3-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(penguins_train<span class="sc">$</span>flipper_length_mm, </span>
<span id="cb3-4"><a href="#cb3-4" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb3-5"><a href="#cb3-5" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="dv">1</span>),</span>
<span id="cb3-6"><a href="#cb3-6" aria-hidden="true" tabindex="-1"></a> <span class="at">species =</span> <span class="fu">c</span>(<span class="st">"Adelie"</span>, <span class="st">"Chinstrap"</span>, <span class="st">"Gentoo"</span>),</span>
<span id="cb3-7"><a href="#cb3-7" aria-hidden="true" tabindex="-1"></a> <span class="at">year =</span> <span class="fu">c</span>(<span class="dv">2007</span>, <span class="dv">2008</span>, <span class="dv">2009</span>)</span>
<span id="cb3-8"><a href="#cb3-8" aria-hidden="true" tabindex="-1"></a>)</span>
<span id="cb3-9"><a href="#cb3-9" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-10"><a href="#cb3-10" aria-hidden="true" tabindex="-1"></a>new_data <span class="ot"><-</span> mass_cat_fit <span class="sc">%>%</span></span>
<span id="cb3-11"><a href="#cb3-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">augment</span>(new_data)</span>
<span id="cb3-12"><a href="#cb3-12" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-13"><a href="#cb3-13" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb3-14"><a href="#cb3-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb3-15"><a href="#cb3-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb3-16"><a href="#cb3-16" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g,</span>
<span id="cb3-17"><a href="#cb3-17" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species,</span>
<span id="cb3-18"><a href="#cb3-18" aria-hidden="true" tabindex="-1"></a> <span class="at">shape =</span> <span class="fu">as.factor</span>(year)),</span>
<span id="cb3-19"><a href="#cb3-19" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>) <span class="sc">+</span> </span>
<span id="cb3-20"><a href="#cb3-20" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data,</span>
<span id="cb3-21"><a href="#cb3-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb3-22"><a href="#cb3-22" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb3-23"><a href="#cb3-23" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species, </span>
<span id="cb3-24"><a href="#cb3-24" aria-hidden="true" tabindex="-1"></a> <span class="at">linetype =</span> <span class="fu">as.factor</span>(year))) <span class="sc">+</span> </span>
<span id="cb3-25"><a href="#cb3-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb3-26"><a href="#cb3-26" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>,</span>
<span id="cb3-27"><a href="#cb3-27" aria-hidden="true" tabindex="-1"></a> <span class="at">linetype =</span> <span class="st">"Year"</span>,</span>
<span id="cb3-28"><a href="#cb3-28" aria-hidden="true" tabindex="-1"></a> <span class="at">shape =</span> <span class="st">"Year"</span>)</span>
<span id="cb3-29"><a href="#cb3-29" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-30"><a href="#cb3-30" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb3-31"><a href="#cb3-31" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb3-32"><a href="#cb3-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb3-33"><a href="#cb3-33" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g,</span>
<span id="cb3-34"><a href="#cb3-34" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species),</span>
<span id="cb3-35"><a href="#cb3-35" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>, </span>
<span id="cb3-36"><a href="#cb3-36" aria-hidden="true" tabindex="-1"></a> <span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span> </span>
<span id="cb3-37"><a href="#cb3-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data,</span>
<span id="cb3-38"><a href="#cb3-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb3-39"><a href="#cb3-39" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb3-40"><a href="#cb3-40" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species),</span>
<span id="cb3-41"><a href="#cb3-41" aria-hidden="true" tabindex="-1"></a> <span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb3-42"><a href="#cb3-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_grid</span>(species <span class="sc">~</span> year) <span class="sc">+</span> </span>
<span id="cb3-43"><a href="#cb3-43" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb3-44"><a href="#cb3-44" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>)</span>
<span id="cb3-45"><a href="#cb3-45" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb3-46"><a href="#cb3-46" aria-hidden="true" tabindex="-1"></a>(p1 <span class="sc">/</span> p2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="14d_HigherOrderTerms_Interaction_files/figure-html/unnamed-chunk-2-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The single plot above which shows the model with all of the varied intercepts may be difficult to read, but it is clear that the slope of each line is the same. That is, the estimated association between <code>flipper_length_mm</code> and <code>body_mass_g</code> is constant across each of the combinations of <code>species</code>/<code>year</code> category. The intercepts are all that were varied! The plot on the right is much more readable, but the impact of the categorical variables and the corresponding interactions is less obvious from that plot.</p>
</section>
<section id="interaction-between-a-categorical-and-numerical-predictor" class="level4">
<h4 class="anchored" data-anchor-id="interaction-between-a-categorical-and-numerical-predictor">Interaction Between a Categorical and Numerical Predictor</h4>
<p>Let’s say that we propose a model which predicts penguin <code>body_mass_g</code> using <code>flipper_length_mm</code>, <code>species</code>, and an interaction between <code>species</code> and <code>flipper_length_mm</code>. Such a model is of the following form:</p>
<p><span class="math inline">\(\begin{align}\mathbb{E}\left[\text{body\_mass\_g}\right] = &\beta_0 + \beta_1 \cdot \text{flipper\_length\_mm} + \beta_2\cdot\text{Chinstrap} + \beta_3\cdot\text{Gentoo} +\\ &\beta_4\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{Chinstrap}\right) + \beta_5\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{Gentoo}\right)\end{align}\)</span></p>
<p>We’ll build this model similar to the previous one. We’ll use <code>step_dummy()</code> to convert <code>species</code> to corresponding dummy variables and then we’ll use <code>step_interact()</code> to obtain interactions between each level of the <code>species</code> variable and the <code>flipper_length_mm</code>.</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>mass_catnum_spec <span class="ot"><-</span> <span class="fu">linear_reg</span>() <span class="sc">%>%</span></span>
<span id="cb5-2"><a href="#cb5-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_engine</span>(<span class="st">"lm"</span>)</span>
<span id="cb5-3"><a href="#cb5-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-4"><a href="#cb5-4" aria-hidden="true" tabindex="-1"></a>mass_catnum_rec <span class="ot"><-</span> <span class="fu">recipe</span>(body_mass_g <span class="sc">~</span> flipper_length_mm <span class="sc">+</span> species, <span class="at">data =</span> penguins_train) <span class="sc">%>%</span></span>
<span id="cb5-5"><a href="#cb5-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_dummy</span>(species) <span class="sc">%>%</span></span>
<span id="cb5-6"><a href="#cb5-6" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_interact</span>(<span class="sc">~</span> <span class="fu">starts_with</span>(<span class="st">"species"</span>)<span class="sc">:</span>flipper_length_mm)</span>
<span id="cb5-7"><a href="#cb5-7" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-8"><a href="#cb5-8" aria-hidden="true" tabindex="-1"></a>mass_catnum_wf <span class="ot"><-</span> <span class="fu">workflow</span>() <span class="sc">%>%</span></span>
<span id="cb5-9"><a href="#cb5-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_model</span>(mass_catnum_spec) <span class="sc">%>%</span></span>
<span id="cb5-10"><a href="#cb5-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_recipe</span>(mass_catnum_rec)</span>
<span id="cb5-11"><a href="#cb5-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-12"><a href="#cb5-12" aria-hidden="true" tabindex="-1"></a>mass_catnum_fit <span class="ot"><-</span> mass_catnum_wf <span class="sc">%>%</span></span>
<span id="cb5-13"><a href="#cb5-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(penguins_train)</span>
<span id="cb5-14"><a href="#cb5-14" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb5-15"><a href="#cb5-15" aria-hidden="true" tabindex="-1"></a>mass_catnum_fit <span class="sc">%>%</span></span>
<span id="cb5-16"><a href="#cb5-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">glance</span>() <span class="sc">%>%</span></span>
<span id="cb5-17"><a href="#cb5-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb5-18"><a href="#cb5-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">adj.r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">sigma</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">logLik</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">AIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">BIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">deviance</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df.residual</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">nobs</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.7952182</td>
<td style="text-align: right;">0.7911226</td>
<td style="text-align: right;">375.0103</td>
<td style="text-align: right;">194.1623</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">5</td>
<td style="text-align: right;">-1877.513</td>
<td style="text-align: right;">3769.025</td>
<td style="text-align: right;">3793.842</td>
<td style="text-align: right;">35158182</td>
<td style="text-align: right;">250</td>
<td style="text-align: right;">256</td>
</tr>
</tbody>
</table>
</div>
<div class="sourceCode cell-code" id="cb6"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb6-1"><a href="#cb6-1" aria-hidden="true" tabindex="-1"></a>mass_catnum_fit <span class="sc">%>%</span></span>
<span id="cb6-2"><a href="#cb6-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">extract_fit_engine</span>() <span class="sc">%>%</span></span>
<span id="cb6-3"><a href="#cb6-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">tidy</span>() <span class="sc">%>%</span></span>
<span id="cb6-4"><a href="#cb6-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb6-5"><a href="#cb6-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">term</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">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">-2797.759952</td>
<td style="text-align: right;">1059.375697</td>
<td style="text-align: right;">-2.6409516</td>
<td style="text-align: right;">0.0087881</td>
</tr>
<tr class="even">
<td style="text-align: left;">flipper_length_mm</td>
<td style="text-align: right;">34.282823</td>
<td style="text-align: right;">5.582906</td>
<td style="text-align: right;">6.1406767</td>
<td style="text-align: right;">0.0000000</td>
</tr>
<tr class="odd">
<td style="text-align: left;">species_Chinstrap</td>
<td style="text-align: right;">-129.708846</td>
<td style="text-align: right;">1708.747729</td>
<td style="text-align: right;">-0.0759087</td>
<td style="text-align: right;">0.9395524</td>
</tr>
<tr class="even">
<td style="text-align: left;">species_Gentoo</td>
<td style="text-align: right;">-4216.630301</td>
<td style="text-align: right;">1702.555778</td>
<td style="text-align: right;">-2.4766474</td>
<td style="text-align: right;">0.0139247</td>
</tr>
<tr class="odd">
<td style="text-align: left;">species_Chinstrap_x_flipper_length_mm</td>
<td style="text-align: right;">-0.366684</td>
<td style="text-align: right;">8.853089</td>
<td style="text-align: right;">-0.0414188</td>
<td style="text-align: right;">0.9669951</td>
</tr>
<tr class="even">
<td style="text-align: left;">species_Gentoo_x_flipper_length_mm</td>
<td style="text-align: right;">21.389349</td>
<td style="text-align: right;">8.284884</td>
<td style="text-align: right;">2.5817319</td>
<td style="text-align: right;">0.0104008</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We’ve obtained our estimated model,</p>
<p><span class="math inline">\(\begin{align}\mathbb{E}\left[\text{body\_mass\_g}\right] \approx &-2797.76 + 34.38 \cdot \text{flipper\_length\_mm} + \left(-129.71\right)\cdot\text{Chinstrap} + \left(-4216.6\right)\cdot\text{Gentoo} +\\ &\left(-0.37\right)\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{Chinstrap}\right) + 21.39\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{Gentoo}\right)\end{align}\)</span></p>
<p>Notice that this model allows for different slopes and intercepts for each species. For example:</p>
<p><span class="math display">\[\begin{array}{lcl} \text{Adelie} & : & \mathbb{E}\left[\text{body\_mass\_g}\right] \approx -2797.76 + 34.28\cdot\text{flipper\_length\_mm}\\
\text{Chinstrap} & : & \mathbb{E}\left[\text{body\_mass\_g}\right] \approx \left(-2797.76 -129.71\right) + \left(34.28 - 0.37\right)\cdot\text{flipper\_length\_mm}\\
\text{Gentoo} & : & \mathbb{E}\left[\text{body\_mass\_g}\right] \approx \left(-2797.76 -4216.63\right) + \left(34.28 + 21.39\right)\cdot\text{flipper\_length\_mm}\end{array}\]</span></p>
<p>From the three models above, we can make our usual interpretations. Let’s see those models graphically.</p>
<div class="cell">
<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>new_data <span class="ot"><-</span> <span class="fu">crossing</span>(<span class="at">flipper_length_mm =</span> <span class="fu">seq</span>(<span class="fu">min</span>(penguins_train<span class="sc">$</span>flipper_length_mm, </span>
<span id="cb7-2"><a href="#cb7-2" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb7-3"><a href="#cb7-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(penguins_train<span class="sc">$</span>flipper_length_mm, </span>
<span id="cb7-4"><a href="#cb7-4" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb7-5"><a href="#cb7-5" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="dv">1</span>),</span>
<span id="cb7-6"><a href="#cb7-6" aria-hidden="true" tabindex="-1"></a> <span class="at">species =</span> <span class="fu">c</span>(<span class="st">"Adelie"</span>, <span class="st">"Chinstrap"</span>, <span class="st">"Gentoo"</span>)</span>
<span id="cb7-7"><a href="#cb7-7" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb7-8"><a href="#cb7-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-9"><a href="#cb7-9" aria-hidden="true" tabindex="-1"></a>new_data <span class="ot"><-</span> mass_catnum_fit <span class="sc">%>%</span></span>
<span id="cb7-10"><a href="#cb7-10" aria-hidden="true" tabindex="-1"></a> <span class="fu">augment</span>(new_data)</span>
<span id="cb7-11"><a href="#cb7-11" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-12"><a href="#cb7-12" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb7-13"><a href="#cb7-13" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb7-14"><a href="#cb7-14" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb7-15"><a href="#cb7-15" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g,</span>
<span id="cb7-16"><a href="#cb7-16" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species),</span>
<span id="cb7-17"><a href="#cb7-17" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>) <span class="sc">+</span> </span>
<span id="cb7-18"><a href="#cb7-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data,</span>
<span id="cb7-19"><a href="#cb7-19" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb7-20"><a href="#cb7-20" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb7-21"><a href="#cb7-21" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species)) <span class="sc">+</span></span>
<span id="cb7-22"><a href="#cb7-22" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb7-23"><a href="#cb7-23" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>)</span>
<span id="cb7-24"><a href="#cb7-24" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-25"><a href="#cb7-25" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb7-26"><a href="#cb7-26" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb7-27"><a href="#cb7-27" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb7-28"><a href="#cb7-28" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g,</span>
<span id="cb7-29"><a href="#cb7-29" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species),</span>
<span id="cb7-30"><a href="#cb7-30" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>,</span>
<span id="cb7-31"><a href="#cb7-31" aria-hidden="true" tabindex="-1"></a> <span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span> </span>
<span id="cb7-32"><a href="#cb7-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data,</span>
<span id="cb7-33"><a href="#cb7-33" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb7-34"><a href="#cb7-34" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb7-35"><a href="#cb7-35" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> species),</span>
<span id="cb7-36"><a href="#cb7-36" aria-hidden="true" tabindex="-1"></a> <span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span></span>
<span id="cb7-37"><a href="#cb7-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">facet_wrap</span>(<span class="sc">~</span> species) <span class="sc">+</span></span>
<span id="cb7-38"><a href="#cb7-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb7-39"><a href="#cb7-39" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>)</span>
<span id="cb7-40"><a href="#cb7-40" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb7-41"><a href="#cb7-41" aria-hidden="true" tabindex="-1"></a>(p1 <span class="sc">/</span> p2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="14d_HigherOrderTerms_Interaction_files/figure-html/unnamed-chunk-4-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>The visual shows what we see numerically in the model summary output – the data do not provide convincing evidence that the association between body mass and flipper length is different (in either intercept or slope) between Adelie and Chinstrap penguins. The data do suggest, however, that both the slope and intercept in this model is different between Adelie and Gentoo penguins.</p>
</section>
<section id="interaction-between-two-numerical-predictors" class="level4">
<h4 class="anchored" data-anchor-id="interaction-between-two-numerical-predictors">Interaction Between Two Numerical Predictors</h4>
<p>Let’s say that we propose a model which predicts penguin <code>body_mass_g</code> using <code>flipper_length_mm</code>, <code>bill_length_mm</code>, and an interaction between these two predictors. Such a model is of the following form:</p>
<p><span class="math display">\[\mathbb{E}\left[\text{body\_mass\_g}\right] = \beta_0 + \beta_1 \cdot \text{flipper\_length\_mm} + \beta_2\cdot\text{bill\_length\_mm} + \beta_3\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{bill\_length\_mm}\right)\]</span></p>
<p>We’ll build this model similar to the previous one. Since both of our variables are numerical, we no longer need <code>step_dummy()</code> and we’ll go straight to <code>step_interact()</code>.</p>
<div class="cell">
<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>mass_num_spec <span class="ot"><-</span> <span class="fu">linear_reg</span>() <span class="sc">%>%</span></span>
<span id="cb9-2"><a href="#cb9-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">set_engine</span>(<span class="st">"lm"</span>)</span>
<span id="cb9-3"><a href="#cb9-3" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-4"><a href="#cb9-4" aria-hidden="true" tabindex="-1"></a>mass_num_rec <span class="ot"><-</span> <span class="fu">recipe</span>(body_mass_g <span class="sc">~</span> flipper_length_mm <span class="sc">+</span> bill_length_mm, <span class="at">data =</span> penguins_train) <span class="sc">%>%</span></span>
<span id="cb9-5"><a href="#cb9-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">step_interact</span>(<span class="sc">~</span> flipper_length_mm<span class="sc">:</span>bill_length_mm)</span>
<span id="cb9-6"><a href="#cb9-6" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-7"><a href="#cb9-7" aria-hidden="true" tabindex="-1"></a>mass_num_wf <span class="ot"><-</span> <span class="fu">workflow</span>() <span class="sc">%>%</span></span>
<span id="cb9-8"><a href="#cb9-8" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_model</span>(mass_num_spec) <span class="sc">%>%</span></span>
<span id="cb9-9"><a href="#cb9-9" aria-hidden="true" tabindex="-1"></a> <span class="fu">add_recipe</span>(mass_num_rec)</span>
<span id="cb9-10"><a href="#cb9-10" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-11"><a href="#cb9-11" aria-hidden="true" tabindex="-1"></a>mass_num_fit <span class="ot"><-</span> mass_num_wf <span class="sc">%>%</span></span>
<span id="cb9-12"><a href="#cb9-12" aria-hidden="true" tabindex="-1"></a> <span class="fu">fit</span>(penguins_train)</span>
<span id="cb9-13"><a href="#cb9-13" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb9-14"><a href="#cb9-14" aria-hidden="true" tabindex="-1"></a>mass_num_fit <span class="sc">%>%</span></span>
<span id="cb9-15"><a href="#cb9-15" aria-hidden="true" tabindex="-1"></a> <span class="fu">glance</span>() <span class="sc">%>%</span></span>
<span id="cb9-16"><a href="#cb9-16" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb9-17"><a href="#cb9-17" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: right;" data-quarto-table-cell-role="th">r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">adj.r.squared</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">sigma</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">logLik</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">AIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">BIC</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">deviance</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">df.residual</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">nobs</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: right;">0.7755339</td>
<td style="text-align: right;">0.7728617</td>
<td style="text-align: right;">391.0593</td>
<td style="text-align: right;">290.2213</td>
<td style="text-align: right;">0</td>
<td style="text-align: right;">3</td>
<td style="text-align: right;">-1889.26</td>
<td style="text-align: right;">3788.521</td>
<td style="text-align: right;">3806.247</td>
<td style="text-align: right;">38537702</td>
<td style="text-align: right;">252</td>
<td style="text-align: right;">256</td>
</tr>
</tbody>
</table>
</div>
<div class="sourceCode cell-code" id="cb10"><pre class="sourceCode r code-with-copy"><code class="sourceCode r"><span id="cb10-1"><a href="#cb10-1" aria-hidden="true" tabindex="-1"></a>mass_num_fit <span class="sc">%>%</span></span>
<span id="cb10-2"><a href="#cb10-2" aria-hidden="true" tabindex="-1"></a> <span class="fu">extract_fit_engine</span>() <span class="sc">%>%</span></span>
<span id="cb10-3"><a href="#cb10-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">tidy</span>() <span class="sc">%>%</span></span>
<span id="cb10-4"><a href="#cb10-4" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable</span>() <span class="sc">%>%</span></span>
<span id="cb10-5"><a href="#cb10-5" aria-hidden="true" tabindex="-1"></a> <span class="fu">kable_styling</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="table table-hover table-striped caption-top table-sm small" data-quarto-postprocess="true">
<thead>
<tr class="header">
<th style="text-align: left;" data-quarto-table-cell-role="th">term</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">statistic</th>
<th style="text-align: right;" data-quarto-table-cell-role="th">p.value</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td style="text-align: left;">(Intercept)</td>
<td style="text-align: right;">5701.959158</td>
<td style="text-align: right;">3379.8251437</td>
<td style="text-align: right;">1.6870574</td>
<td style="text-align: right;">0.0928297</td>
</tr>
<tr class="even">
<td style="text-align: left;">flipper_length_mm</td>
<td style="text-align: right;">-10.287765</td>
<td style="text-align: right;">17.4109650</td>
<td style="text-align: right;">-0.5908785</td>
<td style="text-align: right;">0.5551314</td>
</tr>
<tr class="odd">
<td style="text-align: left;">bill_length_mm</td>
<td style="text-align: right;">-242.479461</td>
<td style="text-align: right;">72.9847879</td>
<td style="text-align: right;">-3.3223288</td>
<td style="text-align: right;">0.0010251</td>
</tr>
<tr class="even">
<td style="text-align: left;">flipper_length_mm_x_bill_length_mm</td>
<td style="text-align: right;">1.264581</td>
<td style="text-align: right;">0.3718553</td>
<td style="text-align: right;">3.4007338</td>
<td style="text-align: right;">0.0007813</td>
</tr>
</tbody>
</table>
</div>
</div>
<p>We’ve obtained our estimated model and the interaction between <code>flipper_length_mm</code> and <code>bill_length_mm</code> is statistically significant. The estimated mode is</p>
<p><span class="math display">\[\mathbb{E}\left[\text{body\_mass\_g}\right] \approx 5701.96 + \left(-10.29\right)\cdot \text{flipper\_length\_mm} + \left(-242.48\right)\cdot\text{bill\_length\_mm} + 1.26\cdot\left(\text{flipper\_length\_mm}\right)\left(\text{bill\_length\_mm}\right)\]</span></p>
<p>Notice that this model allows for the expected increase in body mass due to an increased flipper length to depend on the bill length. Similarly, the expected increase in body mass due to an increased bill length depends on the flipper length in this model.</p>
<p>It will be easiest to understand this phenomenon by calculating a few values or by plotting our models using various assumed levels for our predictors.</p>
<div class="cell">
<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>new_data_flipper <span class="ot"><-</span> <span class="fu">crossing</span>(<span class="at">flipper_length_mm =</span> <span class="fu">seq</span>(<span class="fu">min</span>(penguins_train<span class="sc">$</span>flipper_length_mm, </span>
<span id="cb11-2"><a href="#cb11-2" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb11-3"><a href="#cb11-3" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(penguins_train<span class="sc">$</span>flipper_length_mm,</span>
<span id="cb11-4"><a href="#cb11-4" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb11-5"><a href="#cb11-5" aria-hidden="true" tabindex="-1"></a> <span class="at">by =</span> <span class="dv">1</span>),</span>
<span id="cb11-6"><a href="#cb11-6" aria-hidden="true" tabindex="-1"></a> <span class="at">bill_length_mm =</span> <span class="fu">c</span>(<span class="dv">35</span>, <span class="dv">40</span>, <span class="dv">45</span>, <span class="dv">50</span>)</span>
<span id="cb11-7"><a href="#cb11-7" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb11-8"><a href="#cb11-8" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-9"><a href="#cb11-9" aria-hidden="true" tabindex="-1"></a>new_data_bill <span class="ot"><-</span> <span class="fu">crossing</span>(<span class="at">bill_length_mm =</span> <span class="fu">seq</span>(<span class="fu">min</span>(penguins_train<span class="sc">$</span>bill_length_mm, </span>
<span id="cb11-10"><a href="#cb11-10" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb11-11"><a href="#cb11-11" aria-hidden="true" tabindex="-1"></a> <span class="fu">max</span>(penguins_train<span class="sc">$</span>bill_length_mm, </span>
<span id="cb11-12"><a href="#cb11-12" aria-hidden="true" tabindex="-1"></a> <span class="at">na.rm =</span> <span class="cn">TRUE</span>),</span>
<span id="cb11-13"><a href="#cb11-13" aria-hidden="true" tabindex="-1"></a> <span class="at">length.out =</span> <span class="dv">100</span>),</span>
<span id="cb11-14"><a href="#cb11-14" aria-hidden="true" tabindex="-1"></a> <span class="at">flipper_length_mm =</span> <span class="fu">c</span>(<span class="dv">175</span>, <span class="dv">190</span>, <span class="dv">200</span>, <span class="dv">210</span>, <span class="dv">220</span>)</span>
<span id="cb11-15"><a href="#cb11-15" aria-hidden="true" tabindex="-1"></a> )</span>
<span id="cb11-16"><a href="#cb11-16" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-17"><a href="#cb11-17" aria-hidden="true" tabindex="-1"></a>new_data_flipper <span class="ot"><-</span> mass_num_fit <span class="sc">%>%</span></span>
<span id="cb11-18"><a href="#cb11-18" aria-hidden="true" tabindex="-1"></a> <span class="fu">augment</span>(new_data_flipper)</span>
<span id="cb11-19"><a href="#cb11-19" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-20"><a href="#cb11-20" aria-hidden="true" tabindex="-1"></a>new_data_bill <span class="ot"><-</span> mass_num_fit <span class="sc">%>%</span></span>
<span id="cb11-21"><a href="#cb11-21" aria-hidden="true" tabindex="-1"></a> <span class="fu">augment</span>(new_data_bill)</span>
<span id="cb11-22"><a href="#cb11-22" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-23"><a href="#cb11-23" aria-hidden="true" tabindex="-1"></a>p1 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb11-24"><a href="#cb11-24" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb11-25"><a href="#cb11-25" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb11-26"><a href="#cb11-26" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g),</span>
<span id="cb11-27"><a href="#cb11-27" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>) <span class="sc">+</span> </span>
<span id="cb11-28"><a href="#cb11-28" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data_flipper,</span>
<span id="cb11-29"><a href="#cb11-29" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> flipper_length_mm,</span>
<span id="cb11-30"><a href="#cb11-30" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb11-31"><a href="#cb11-31" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="fu">as.factor</span>(bill_length_mm))) <span class="sc">+</span> </span>
<span id="cb11-32"><a href="#cb11-32" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb11-33"><a href="#cb11-33" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>,</span>
<span id="cb11-34"><a href="#cb11-34" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"Bill Length (mm)"</span>)</span>
<span id="cb11-35"><a href="#cb11-35" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-36"><a href="#cb11-36" aria-hidden="true" tabindex="-1"></a>p2 <span class="ot"><-</span> <span class="fu">ggplot</span>() <span class="sc">+</span></span>
<span id="cb11-37"><a href="#cb11-37" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_point</span>(<span class="at">data =</span> penguins_train,</span>
<span id="cb11-38"><a href="#cb11-38" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> bill_length_mm,</span>
<span id="cb11-39"><a href="#cb11-39" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> body_mass_g),</span>
<span id="cb11-40"><a href="#cb11-40" aria-hidden="true" tabindex="-1"></a> <span class="at">alpha =</span> <span class="fl">0.5</span>,</span>
<span id="cb11-41"><a href="#cb11-41" aria-hidden="true" tabindex="-1"></a> <span class="at">show.legend =</span> <span class="cn">FALSE</span>) <span class="sc">+</span> </span>
<span id="cb11-42"><a href="#cb11-42" aria-hidden="true" tabindex="-1"></a> <span class="fu">geom_line</span>(<span class="at">data =</span> new_data_bill,</span>
<span id="cb11-43"><a href="#cb11-43" aria-hidden="true" tabindex="-1"></a> <span class="fu">aes</span>(<span class="at">x =</span> bill_length_mm,</span>
<span id="cb11-44"><a href="#cb11-44" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> .pred,</span>
<span id="cb11-45"><a href="#cb11-45" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="fu">as.factor</span>(flipper_length_mm))) <span class="sc">+</span> </span>
<span id="cb11-46"><a href="#cb11-46" aria-hidden="true" tabindex="-1"></a> <span class="fu">labs</span>(<span class="at">x =</span> <span class="st">"Flipper Length (mm)"</span>,</span>
<span id="cb11-47"><a href="#cb11-47" aria-hidden="true" tabindex="-1"></a> <span class="at">y =</span> <span class="st">"Body Mass (g)"</span>,</span>
<span id="cb11-48"><a href="#cb11-48" aria-hidden="true" tabindex="-1"></a> <span class="at">color =</span> <span class="st">"Flipper Length (mm)"</span>)</span>
<span id="cb11-49"><a href="#cb11-49" aria-hidden="true" tabindex="-1"></a></span>
<span id="cb11-50"><a href="#cb11-50" aria-hidden="true" tabindex="-1"></a>(p1 <span class="sc">/</span> p2)</span></code><button title="Copy to Clipboard" class="code-copy-button"><i class="bi"></i></button></pre></div>
<div class="cell-output cell-output-stderr">
<pre><code>Warning: Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).
Removed 2 rows containing missing values or values outside the scale range
(`geom_point()`).</code></pre>
</div>
<div class="cell-output-display">
<div>
<figure class="figure">
<p><img src="14d_HigherOrderTerms_Interaction_files/figure-html/unnamed-chunk-6-1.png" class="img-fluid figure-img" width="672"></p>
</figure>
</div>
</div>
</div>
<p>In each case, we can see that the slope of the modeled association between penguin body mass and the plotted predictor depends on the <em>level</em> (value) of the third variable.</p>
<hr>
</section>
</section>
</section>
<section id="your-task" class="level2">
<h2 class="anchored" data-anchor-id="your-task">Your Task</h2>
<ol type="1">
<li>Use what you’ve learned to build a model to predict penguin <code>body_mass_g</code> using <code>bill_depth_mm</code>.</li>
<li>Update your model to include a <em>main effects</em> term for <code>species</code>.</li>
<li>Update your model with an additional term allowing for interaction between <code>species</code> and <code>bill_depth_mm</code>.</li>
<li>Interpret what you just discovered!</li>
</ol>
</section>
</main>
<!-- /main column -->
<script id="quarto-html-after-body" type="application/javascript">
window.document.addEventListener("DOMContentLoaded", function (event) {
const toggleBodyColorMode = (bsSheetEl) => {
const mode = bsSheetEl.getAttribute("data-mode");
const bodyEl = window.document.querySelector("body");
if (mode === "dark") {
bodyEl.classList.add("quarto-dark");
bodyEl.classList.remove("quarto-light");
} else {
bodyEl.classList.add("quarto-light");
bodyEl.classList.remove("quarto-dark");
}
}
const toggleBodyColorPrimary = () => {
const bsSheetEl = window.document.querySelector("link#quarto-bootstrap");
if (bsSheetEl) {
toggleBodyColorMode(bsSheetEl);
}
}
toggleBodyColorPrimary();
const icon = "";
const anchorJS = new window.AnchorJS();
anchorJS.options = {
placement: 'right',
icon: icon
};
anchorJS.add('.anchored');
const isCodeAnnotation = (el) => {
for (const clz of el.classList) {
if (clz.startsWith('code-annotation-')) {
return true;
}
}
return false;
}
const onCopySuccess = function(e) {
// button target
const button = e.trigger;
// don't keep focus
button.blur();
// flash "checked"
button.classList.add('code-copy-button-checked');
var currentTitle = button.getAttribute("title");
button.setAttribute("title", "Copied!");
let tooltip;
if (window.bootstrap) {
button.setAttribute("data-bs-toggle", "tooltip");
button.setAttribute("data-bs-placement", "left");
button.setAttribute("data-bs-title", "Copied!");
tooltip = new bootstrap.Tooltip(button,
{ trigger: "manual",
customClass: "code-copy-button-tooltip",
offset: [0, -8]});
tooltip.show();
}
setTimeout(function() {
if (tooltip) {
tooltip.hide();
button.removeAttribute("data-bs-title");
button.removeAttribute("data-bs-toggle");
button.removeAttribute("data-bs-placement");
}
button.setAttribute("title", currentTitle);
button.classList.remove('code-copy-button-checked');
}, 1000);
// clear code selection
e.clearSelection();
}
const getTextToCopy = function(trigger) {
const codeEl = trigger.previousElementSibling.cloneNode(true);
for (const childEl of codeEl.children) {
if (isCodeAnnotation(childEl)) {
childEl.remove();
}
}
return codeEl.innerText;
}
const clipboard = new window.ClipboardJS('.code-copy-button:not([data-in-quarto-modal])', {
text: getTextToCopy
});
clipboard.on('success', onCopySuccess);
if (window.document.getElementById('quarto-embedded-source-code-modal')) {
// For code content inside modals, clipBoardJS needs to be initialized with a container option
// TODO: Check when it could be a function (https://github.com/zenorocha/clipboard.js/issues/860)
const clipboardModal = new window.ClipboardJS('.code-copy-button[data-in-quarto-modal]', {
text: getTextToCopy,
container: window.document.getElementById('quarto-embedded-source-code-modal')
});
clipboardModal.on('success', onCopySuccess);
}
var localhostRegex = new RegExp(/^(?:http|https):\/\/localhost\:?[0-9]*\//);
var mailtoRegex = new RegExp(/^mailto:/);
var filterRegex = new RegExp('/' + window.location.host + '/');
var isInternal = (href) => {
return filterRegex.test(href) || localhostRegex.test(href) || mailtoRegex.test(href);
}
// Inspect non-navigation links and adorn them if external
var links = window.document.querySelectorAll('a[href]:not(.nav-link):not(.navbar-brand):not(.toc-action):not(.sidebar-link):not(.sidebar-item-toggle):not(.pagination-link):not(.no-external):not([aria-hidden]):not(.dropdown-item):not(.quarto-navigation-tool):not(.about-link)');
for (var i=0; i<links.length; i++) {
const link = links[i];
if (!isInternal(link.href)) {
// undo the damage that might have been done by quarto-nav.js in the case of
// links that we want to consider external
if (link.dataset.originalHref !== undefined) {
link.href = link.dataset.originalHref;
}
}
}
function tippyHover(el, contentFn, onTriggerFn, onUntriggerFn) {
const config = {
allowHTML: true,
maxWidth: 500,
delay: 100,
arrow: false,
appendTo: function(el) {
return el.parentElement;
},
interactive: true,
interactiveBorder: 10,
theme: 'quarto',
placement: 'bottom-start',
};
if (contentFn) {
config.content = contentFn;
}
if (onTriggerFn) {
config.onTrigger = onTriggerFn;
}
if (onUntriggerFn) {
config.onUntrigger = onUntriggerFn;
}
window.tippy(el, config);
}
const noterefs = window.document.querySelectorAll('a[role="doc-noteref"]');
for (var i=0; i<noterefs.length; i++) {
const ref = noterefs[i];
tippyHover(ref, function() {
// use id or data attribute instead here
let href = ref.getAttribute('data-footnote-href') || ref.getAttribute('href');
try { href = new URL(href).hash; } catch {}
const id = href.replace(/^#\/?/, "");
const note = window.document.getElementById(id);
if (note) {
return note.innerHTML;
} else {
return "";
}
});
}
const xrefs = window.document.querySelectorAll('a.quarto-xref');
const processXRef = (id, note) => {
// Strip column container classes
const stripColumnClz = (el) => {
el.classList.remove("page-full", "page-columns");
if (el.children) {
for (const child of el.children) {
stripColumnClz(child);
}
}
}
stripColumnClz(note)
if (id === null || id.startsWith('sec-')) {
// Special case sections, only their first couple elements
const container = document.createElement("div");
if (note.children && note.children.length > 2) {
container.appendChild(note.children[0].cloneNode(true));
for (let i = 1; i < note.children.length; i++) {
const child = note.children[i];
if (child.tagName === "P" && child.innerText === "") {
continue;
} else {
container.appendChild(child.cloneNode(true));
break;
}
}
if (window.Quarto?.typesetMath) {
window.Quarto.typesetMath(container);
}