# **Additive model**
coef(penguin_additive_model)• 21. Interactions summary
Links to: Summary. Questions. Glossary. R functions. R packages. More resources.
Chapter summary
Sometimes the impact of changing one variable depends on the value of another. Such statistical interactions can both illuminate biological processes and complicate stiatical analyses and interpretation. This chapter explains the best practices when modelling statistical interactions. I then show how we can think of an interaction of a variable with itself as a polynomial regression. This section serves to show that “linear models” can fit curved data.
Practice Questions
Try these questions using the penguins data.
Setup
penguin_additive_model <- lm(body_mass ~ sex + species, data = penguins)
penguin_interaction_model <- lm(body_mass ~ sex + species + sex:species, data = penguins)sex:species interaction in penguin_interaction_model asks whether
Consider the coefficients from the models
Additive model
| (Intercept) | sexmale | speciesChinstrap | speciesGentoo |
|---|---|---|---|
| 3372 | 668 | 27 | 1378 |
Q2) For the additive model, predict the mean body mass of a Female Gentoo penguin: \(\widehat{Y}_{\text{female Gentoo}}\) =
Q3) For the additive model, predict the mean body mass of a Male Gentoo penguin: \(\widehat{Y}_{\text{male Gentoo}}\) =
Interaction model
| (Intercept) | sexmale | speciesChinstrap | speciesGentoo | sexmale:speciesChinstrap | sexmale:speciesGentoo |
|---|---|---|---|---|---|
| 3369 | 675 | 158 | 1311 | -263 | 130 |
Q4) For the interaction model, predict the mean body mass of a Female Gentoo penguin: \(\widehat{Y}_{\text{female Gentoo}}\) =
Q5) For the interaction model, predict the mean body mass of a Male Gentoo penguin: \(\widehat{Y}_{\text{male Gentoo}}\) =
Visualizing the interaction
Testing the interaction
library(car)
Anova(penguin_interaction_model, type = "II")Anova Table (Type II tests)
Response: body_mass
Sum Sq Df F value Pr(>F)
sex 37090262 1 387.460 < 2.2e-16 ***
species 143401584 2 749.016 < 2.2e-16 ***
sex:species 1676557 2 8.757 0.0001973 ***
Residuals 31302628 327
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
car::Anova() rather than base R’s anova() here?
sex:species means we reject the null that
Consider summary() output
summary(penguin_interaction_model)
Call:
lm(formula = body_mass ~ sex + species + sex:species, data = penguins)
Residuals:
Min 1Q Median 3Q Max
-827.21 -213.97 11.03 206.51 861.03
Coefficients:
Estimate Std. Error t value Pr(>|t|)
(Intercept) 3368.84 36.21 93.030 < 2e-16 ***
sexmale 674.66 51.21 13.174 < 2e-16 ***
speciesChinstrap 158.37 64.24 2.465 0.01420 *
speciesGentoo 1310.91 54.42 24.088 < 2e-16 ***
sexmale:speciesChinstrap -262.89 90.85 -2.894 0.00406 **
sexmale:speciesGentoo 130.44 76.44 1.706 0.08886 .
---
Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
Residual standard error: 309.4 on 327 degrees of freedom
Multiple R-squared: 0.8546, Adjusted R-squared: 0.8524
F-statistic: 384.3 on 5 and 327 DF, p-value: < 2.2e-16
Q9) Consider the (Intercept) p-value.
- Q9.A) This tests the null hypothesis that
- Q9.B) This test is silly because
.
All but the final answer are correct.
emmeans output
Consider the following output from emmeans() (I know we used model based but i like the warnings emmeans provides).
emmeans(penguin_interaction_model, pairwise ~ sex)NOTE: Results may be misleading due to involvement in interactions
contrast estimate SE df t.ratio p.value
female - male -631 35.7 327 -17.659 <0.0001
Results are averaged over the levels of: species
emmeans(penguin_interaction_model, pairwise ~ species)NOTE: Results may be misleading due to involvement in interactions
contrast estimate SE df t.ratio p.value
Adelie - Chinstrap -26.9 45.4 327 -0.593 0.8241
Adelie - Gentoo -1376.1 38.2 327 -36.007 <0.0001
Chinstrap - Gentoo -1349.2 47.0 327 -28.682 <0.0001
Results are averaged over the levels of: sex
P value adjustment: tukey method for comparing a family of 3 estimates
Glossary of Terms
Statistical interaction: A pattern in a model where the association between one explanatory variable and the response depends on another explanatory variable.
Biological interaction: A situation where two biological factors combine in a way that changes the outcome, such that their joint effect is not simply the sum of their separate effects.
Interaction term: The part of a model that allows the effect of one explanatory variable to depend on another explanatory variable, such as
sex:speciesorpetal_area_mm:petal_color.Additive model: A model where explanatory variables contribute separately to the prediction. In an additive model, the model adds the effects of each predictor but does not let one predictor change the effect of another.
Main effect: The association between one explanatory variable and the response, apart from any interaction terms. In models with interactions, main effects often require extra care.
Partial R²: The proportion of remaining variation in the response associated with a model term after accounting for the other terms in the model.
Polynomial regression: A regression model that includes powers of a predictor, such as
xandx^2, to describe curved relationships.Quadratic term: A squared predictor, such as
I(x^2), used to model curvature.
Key R Functions
lm(y ~ x1 * x2, data = data)fits a model with the main effects ofx1andx2, plus their interaction.lm(y ~ x1 + x2 + x1:x2, data = data)fits the same interaction model, but writes the interaction term explicitly.:specifies an interaction term between predictors in a model formula.*expands to main effects plus their interaction. For example,x1 * x2meansx1 + x2 + x1:x2.I(x^2)tells R to include the squared value ofxin a model formula.poly(x, degree = 2)includes polynomial terms forx, often used for quadratic or higher-order relationships.car::Anova(model, type = "II")gives Type II term-level tests.car::Anova(model, type = "III")gives Type III term-level tests, often used when interpreting models with interactions.emmeans::emmeans(model, pairwise ~ x)estimates marginal means and pairwise contrasts forx.emmeans::emmeans(model, pairwise ~ x | z)estimates pairwise contrasts forxseparately within levels ofz.emmeans::contrast(...)tests specific contrasts among estimated means.modelbased::estimate_means(model, by = ...)estimates model-based means or predictions.modelbased::estimate_slopes(model, trend = ..., by = ...)estimates slopes within levels or values of another predictor.geom_smooth(method = "lm")plots fitted linear-model relationships, including separate fitted lines when data are grouped.
R Packages Introduced
modelbased: Estimates model-based predictions, means, contrasts, and slopes. Useful here for turning interaction models into interpretable quantities.emmeans: Estimates marginal means and contrasts. Useful for comparing groups, especially when interactions make raw coefficients hard to read.honestlm: Provides helper functions for teaching and visualizing linear models, including interaction-friendly model plots and partial \(R^2\).car: ProvidesAnova(), which we use for Type II and Type III term-level tests.
Additional resources
Interactions in factorial designs, by Carlisle Rainey.Describes why interactions are hard to estimate and why studies often have less power for interactions than for main effects, and has an accompanying R packge.
You need 16 times the sample size to estimate an interaction than to estimate a main effect, explained, by Andrew Gelman.
Interaction Effects: A brief introduction from Penn State’s Stat462 site. I like these guys.
Interaction variables. A youtube video from Nathan Wozny
.