7.1 Building an interaction
An interaction occurs when the effect of one variable depends on the value of another. Modeling interactions is essential when the slope of a relationship changes across groups or contexts.
7.1.1 What is an interaction?
Without an interaction, the model assumes that the effect of each predictor is constant — the same for all values of the other predictors. With an interaction, the effect of one predictor becomes a function of the other.
The term is the interaction term. It allows the slope of to depend on and vice versa.
# Demonstrate an interaction
# The effect of study time on test scores depends on sleep
set.seed(42)
n <- 200
# Study hours
study <- runif(n, 0, 10)
# Sleep hours
sleep <- runif(n, 4, 9)
# Test score: effect of study depends on sleep
# When sleep is low, study has less effect
# When sleep is high, study has more effect
score <- 50 + 3 _ study + 2 _ sleep + 0.5 _ study _ sleep + rnorm(n, sd = 5)
# Fit model without interaction
m_no <- lm(score ~ study + sleep)
# Fit model with interaction
m_int <- lm(score ~ study * sleep)
cat("Without interaction:\n")
cat(" study:", round(coef(m_no)[2], 3), "\n")
cat(" sleep:", round(coef(m_no)[3], 3), "\n")
cat("\nWith interaction:\n")
cat(" study:", round(coef(m_int)[2], 3), "\n")
cat(" sleep:", round(coef(m_int)[3], 3), "\n")
cat(" study:sleep:", round(coef(m_int)[4], 3), "\n")
cat("\nThe interaction term (0.5) means:\n")
cat(" Each extra hour of sleep increases the study effect by 0.5\n")7.1.2 The tulips example
The book uses a tulip experiment: tulip growth depends on both light and water, and the effect of light depends on water availability.
# Simulate the tulip data
set.seed(42)
n <- 27
# Light and water levels (0, 1, 2)
light <- rep(c(0, 1, 2), each = 9)
water <- rep(rep(c(0, 1, 2), each = 3), 3)
# Blooms: interaction of light and water
blooms <- 10 + 5 _ light + 5 _ water + 3 _ light _ water + rnorm(n, sd = 2)
# Fit models
m_main <- lm(blooms ~ light + water)
m_int <- lm(blooms ~ light * water)
cat("Main effects only:\n")
cat(" light:", round(coef(m_main)[2], 2), "\n")
cat(" water:", round(coef(m_main)[3], 2), "\n")
cat("\nWith interaction:\n")
cat(" light:", round(coef(m_int)[2], 2), "\n")
cat(" water:", round(coef(m_int)[3], 2), "\n")
cat(" light:water:", round(coef(m_int)[4], 2), "\n")
# Visualize
par(mfrow = c(1, 3))
for (w in 0:2) {
idx <- water == w
plot(light[idx], blooms[idx], pch = 16, col = "steelblue",
xlim = c(0, 2), ylim = range(blooms),
main = sprintf("Water = %d", w),
xlab = "Light", ylab = "Blooms")
abline(lm(blooms[idx] ~ light[idx]), lwd = 2, col = "firebrick")
}
par(mfrow = c(1, 1))7.1.3 Interpreting the interaction
The interaction coefficient tells us how the slope of changes per unit increase in :
So the slope of is when , and it increases by for each unit increase in .
Interactions are not optional when the theory demands them. If your theory says that the effect of education on income depends on social class, then you must include an interaction. Failing to do so forces the model to assume a constant effect, which contradicts the theory. The model is a golem — it will not add interactions on its own. You must tell it to. And once you add an interaction, you must interpret it carefully: the main effects are no longer "the effect" of each variable — they are the effect when the other variable is zero.
Summary of Section 7.1
- An interaction means the effect of one predictor depends on the value of another.
- The interaction term allows the slope to vary.
- The slope of is — it depends on .
- Interactions should be included when theory demands them, not just when they are significant.
- With an interaction, main effects are the effect when the other variable is zero.