class: center, middle <!-- make the interaction plots make more sense and relate to a question --> # When The Effect of One Category Depends on Another  --- class: center, middle # Etherpad <br><br> <center><h3>https://etherpad.wikimedia.org/p/607-interactions-nonlinearities-2022</h3></center> --- # The world isn't additive - Until now, we have assumed predictors combine additively - the effect of one is not dependent on the effect of the other -- - BUT - what if the effect of one variable depends on the level of another? -- - This is an **INTERACTION** and is quite common - Heck, a squared term is the interaction of a variable with itself! -- - Biology: The science of "It depends..." -- - This is challenging to think about and visualize, but if you can master it, you will go far! --- # We Have Explored Nonliearities Via Transforming our Response <img src="categorical_interactions_files/figure-html/loglinear-1.png" style="display: block; margin: auto;" /> --- # But We Have Also Always Tested for Non-Additivity of Predictors <img src="categorical_interactions_files/figure-html/unnamed-chunk-1-1.png" style="display: block; margin: auto;" /> --- # The Linear Model Can Accomodate Many Flavors of Nonlinearity `$$\hat{y_i} = \beta_0 + \beta_1 x_{1i} + \beta_2 x_{2i}$$` `$$y_i \sim N(\hat{y_i}, \sigma)$$` -- Could become... `$$\hat{y_i} = \beta_0 + \beta_1 x_{1i} + \beta_2 x_{1i}^2$$` -- Could be... `$$\hat{y_i} = \beta_0 + \beta_1 x_{1i} + \beta_2 x_{2i} + \beta_3 x_{1i}x_{2i}$$` -- .center[**It is ALL additive terms**] --- class: center, middle  --- # A Non-Additive World 1. Replicating Categorical Variable Combinations: Factorial Models 2. Evaluating Interaction Effects 3. How to Look at Means and Differences with an Interaction Effect --- # Intertidal Grazing! .center[  #### Do grazers reduce algal cover in the intertidal? ] --- # Experiment Replicated on Two Ends of a gradient  --- # Factorial Experiment  --- # Factorial Design  Note: You can have as many treatment types or observed category combinations as you want (and then 3-way, 4-way, etc. interactions) --- # The Data: See the dependency of one treatment on another? <img src="categorical_interactions_files/figure-html/plot_algae-1.png" style="display: block; margin: auto;" /> --- # If we had fit y ~ a + b, residuals look weird <img src="categorical_interactions_files/figure-html/graze_assumptions-1.png" style="display: block; margin: auto;" /> --- # A Factorial Model `$$\large y_{ijk} = \beta_{0} + \sum \beta_{i}x_{i} + \sum \beta_{j}x_{j} + \sum \beta_{ij}x_{ij} + \epsilon_{ijk}$$` `$$\large \epsilon_{ijk} \sim N(0, \sigma^{2} )$$` `$$\large x_{i} = 0,1, x_{j} = 0,1, x_{ij} = 0,1$$` -- - Note the new last term -- - Deviation due to *combination of categories i and j* -- <hr> This is still something that is in `$$\Large \boldsymbol{Y} = \boldsymbol{\beta X} + \boldsymbol{\epsilon}$$` --- # The Data (Four Rows) <table class="table table-striped" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> height </th> <th style="text-align:left;"> herbivores </th> <th style="text-align:right;"> sqrtarea </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> low </td> <td style="text-align:left;"> minus </td> <td style="text-align:right;"> 9.4055728 </td> </tr> <tr> <td style="text-align:left;"> low </td> <td style="text-align:left;"> plus </td> <td style="text-align:right;"> 11.9767608 </td> </tr> <tr> <td style="text-align:left;"> mid </td> <td style="text-align:left;"> minus </td> <td style="text-align:right;"> 0.7071068 </td> </tr> <tr> <td style="text-align:left;"> mid </td> <td style="text-align:left;"> plus </td> <td style="text-align:right;"> 0.7071068 </td> </tr> </tbody> </table> --- # The Dummy-Coded Treatment Contrasts <table class="table table-striped" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:right;"> (Intercept) </th> <th style="text-align:right;"> heightmid </th> <th style="text-align:right;"> herbivoresplus </th> <th style="text-align:right;"> heightmid:herbivoresplus </th> </tr> </thead> <tbody> <tr> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 0 </td> <td style="text-align:right;"> 0 </td> </tr> <tr> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> <td style="text-align:right;"> 1 </td> </tr> </tbody> </table> --- # Fitting with Least Squares ```r graze_int <- lm(sqrtarea ~ height + herbivores + height:herbivores, data=algae) ## OR graze_int <- lm(sqrtarea ~ height*herbivores, data=algae) ``` --- # Now We Are Linear/Additive <img src="categorical_interactions_files/figure-html/unnamed-chunk-2-1.png" style="display: block; margin: auto;" /> --- # Residuals A-OK <img src="categorical_interactions_files/figure-html/unnamed-chunk-3-1.png" style="display: block; margin: auto;" /> --- # HOV Good! <img src="categorical_interactions_files/figure-html/unnamed-chunk-4-1.png" style="display: block; margin: auto;" /> --- # No Outliers <img src="categorical_interactions_files/figure-html/unnamed-chunk-5-1.png" style="display: block; margin: auto;" /> --- # Collinearity is Tricky - unimportant for interaction <img src="categorical_interactions_files/figure-html/unnamed-chunk-6-1.png" style="display: block; margin: auto;" /> --- # A Non-Additive World 1. Replicating Categorical Variable Combinations: Factorial Models 2. .red[Evaluating Interaction Effects] 3. How to Look at Means and Differences with an Interaction Effect --- # What do the Coefficients Mean? <table class="table table-striped" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> term </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> std.error </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> (Intercept) </td> <td style="text-align:right;"> 32.91450 </td> <td style="text-align:right;"> 3.855532 </td> </tr> <tr> <td style="text-align:left;"> heightmid </td> <td style="text-align:right;"> -10.43090 </td> <td style="text-align:right;"> 5.452546 </td> </tr> <tr> <td style="text-align:left;"> herbivoresplus </td> <td style="text-align:right;"> -22.51075 </td> <td style="text-align:right;"> 5.452546 </td> </tr> <tr> <td style="text-align:left;"> heightmid:herbivoresplus </td> <td style="text-align:right;"> 25.57809 </td> <td style="text-align:right;"> 7.711064 </td> </tr> </tbody> </table> -- - Intercept chosen as basal condition (low, herbivores -) -- - Changing height to high is associated with a loss of 10 units of algae relative to low/- -- - Adding herbivores is associated with a loss of 22 units of algae relative to low/- -- - BUT - if you add herbivores and mid, that's also associated with an additional increase of 25 units of algae relative to mid and + alone - 25.5 - 22.5 - 10.4 = only a loss of 7.4 relative to low/- -- .center[**NEVER TRY AND INTERPRET ADDITIVE EFFECTS ALONE WHEN AN INTERACTION IS PRESENT**<Br>that way lies madness] --- # This view is intuitive <img src="categorical_interactions_files/figure-html/unnamed-chunk-7-1.png" style="display: block; margin: auto;" /> --- # This view is also intuitive <img src="categorical_interactions_files/figure-html/unnamed-chunk-8-1.png" style="display: block; margin: auto;" /> --- # We Can Still Look at R^2 ``` # R2 for Linear Regression R2: 0.228 adj. R2: 0.190 ``` Eh, not great, not bad... -- - Note: adding more interaction effects will always increase the R<sup>2</sup> so only add if warranted - NO FISHING! --- # A Non-Additive World 1. Replicating Categorical Variable Combinations: Factorial Models 2. Evaluating Interaction Effects] 3. .red[How to Look at Means and Differences with an Interaction Effect] --- # Posthoc Estimated Means and Interactions with Categorical Variables - Must look at **simple effects** first in the presence of an interaction - The effects of individual treatment combinations - If you have an interaction, this is what you do! -- - **Main effects describe effects of one variable in the absence of an interaction** - Useful only if there is no interaction - Or useful if one categorical variable can be absent --- # Estimated Means with No Interaction - Misleading! ``` herbivores emmean SE df lower.CL upper.CL minus 27.7 2.73 60 22.2 33.2 plus 18.0 2.73 60 12.5 23.4 Results are averaged over the levels of: height Confidence level used: 0.95 ``` ``` height emmean SE df lower.CL upper.CL low 21.7 2.73 60 16.2 27.1 mid 24.0 2.73 60 18.6 29.5 Results are averaged over the levels of: herbivores Confidence level used: 0.95 ``` --- # Posthoc Comparisons Averaging Over Blocks - Misleading! ``` contrast estimate SE df lower.CL upper.CL minus - plus 9.72 3.86 60 2.01 17.4 Results are averaged over the levels of: height Confidence level used: 0.95 ``` --- # Simple Effects Means ``` height herbivores emmean SE df lower.CL upper.CL low minus 32.9 3.86 60 25.20 40.6 mid minus 22.5 3.86 60 14.77 30.2 low plus 10.4 3.86 60 2.69 18.1 mid plus 25.6 3.86 60 17.84 33.3 Confidence level used: 0.95 ``` --- # Posthoc with Simple Effects <table class="table table-striped" style="margin-left: auto; margin-right: auto;"> <thead> <tr> <th style="text-align:left;"> contrast </th> <th style="text-align:right;"> estimate </th> <th style="text-align:right;"> SE </th> <th style="text-align:right;"> df </th> <th style="text-align:right;"> t.ratio </th> <th style="text-align:right;"> p.value </th> </tr> </thead> <tbody> <tr> <td style="text-align:left;"> low minus - mid minus </td> <td style="text-align:right;"> 10.430905 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 1.913034 </td> <td style="text-align:right;"> 0.0605194 </td> </tr> <tr> <td style="text-align:left;"> low minus - low plus </td> <td style="text-align:right;"> 22.510748 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 4.128484 </td> <td style="text-align:right;"> 0.0001146 </td> </tr> <tr> <td style="text-align:left;"> low minus - mid plus </td> <td style="text-align:right;"> 7.363559 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 1.350481 </td> <td style="text-align:right;"> 0.1819337 </td> </tr> <tr> <td style="text-align:left;"> mid minus - low plus </td> <td style="text-align:right;"> 12.079843 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> 2.215450 </td> <td style="text-align:right;"> 0.0305355 </td> </tr> <tr> <td style="text-align:left;"> mid minus - mid plus </td> <td style="text-align:right;"> -3.067346 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> -0.562553 </td> <td style="text-align:right;"> 0.5758352 </td> </tr> <tr> <td style="text-align:left;"> low plus - mid plus </td> <td style="text-align:right;"> -15.147189 </td> <td style="text-align:right;"> 5.452546 </td> <td style="text-align:right;"> 60 </td> <td style="text-align:right;"> -2.778003 </td> <td style="text-align:right;"> 0.0072896 </td> </tr> </tbody> </table> -- .center[**That's a Lot to Drink In!**] --- # Might be easier visually <img src="categorical_interactions_files/figure-html/graze_posthoc_plot-1.png" style="display: block; margin: auto;" /> --- # We are often interested in looking at differences within levels... ``` height = low: herbivores emmean SE df lower.CL upper.CL minus 32.9 3.86 60 25.20 40.6 plus 10.4 3.86 60 2.69 18.1 height = mid: herbivores emmean SE df lower.CL upper.CL minus 22.5 3.86 60 14.77 30.2 plus 25.6 3.86 60 17.84 33.3 Confidence level used: 0.95 ``` --- # We Can Then Look at Something Simpler... <img src="categorical_interactions_files/figure-html/graze_posthoc_plot2-1.png" style="display: block; margin: auto;" /> --- # Why think about interactions - It Depends is a rule in biology - Context dependent interactions everywhere - Using categorical predictors in a factorial design is an elegant way to see interactions without worrying about shapes of relationships - BUT - it all comes down to a general linear model! And the same inferential frameworks we have been dealing with since linear regression! --- # Final Thought - You can have 2, 3, and more-way interactions! .center[.middle[  ]]