Topics Covered
Continuous by Categorical Interactions
Learn to model situations where the effect of a continuous variable (like
flipper_length_mm) on the outcome depends on a categorical group (like
species).
Adding Interaction Terms
Use * or : in R formulas. e.g.
body_mass_g ~ flipper_length_mm * species to test if regression slopes differ.
Plotting Real Interactions
Visualize the exact meaning of interaction effects with ggplot2 using
geom_smooth(method="lm") colored by group.
Scripts
interaction_penguins.R
A deep dive script on fitting additive vs. interaction models on the penguins dataset, along with plotting the slopes.
Key Concepts
- Main effects vs. Interaction effects: An interaction means the slope of one predictor changes depending on the value of another predictor.
- Reference Level Slopes: In a continuous-by-categorical interaction model, the main
continuous effect (e.g.
flipper_length_mm) is the slope for the reference category. - Interaction Coefficients: Terms like
flipper_length_mm:speciesChinstraprepresent the difference in slope compared to the reference baseline. - When "it depends" is the right answer: You can statistically prove that an effect is not uniform across all subdivisions in your data.
Homework
- Run
interaction_penguins.Rand carefully read the model summary. - Fit a new interaction model predicting
bill_length_mmwith the interaction ofbill_depth_mm * sex. - Identify if the interaction is statistically significant—does the relationship between bill depth and length depend on sex?
- Create an interaction plot showing separate regression lines per sex using
ggplot2. - Change the reference level for both
sexandspeciesusing therelevel()function, then re-run the interaction models. Interpret how the base coefficients adjust!