7.6 Practice
The practice problems for Chapter 7 ask you to fit models with interactions, interpret the coefficients, and visualize the predicted effects.
Easy problems
7E1. For each of the following models, write down the linear interaction model in mathematical notation:
(a) y ~ x * z
(b) y ~ x + z + x:z
(c) y ~ x * z * w
7E2. In the model , what is the slope of when ?
7E3. Why is the interaction symmetric? Explain in your own words.
Medium problems
7M1. Fit a model with an interaction between two continuous predictors. Plot the predicted outcome at three values of the moderator.
# 7M1: Fit and visualize a continuous interaction
set.seed(42)
n <- 100
x <- runif(n, 0, 10)
z <- runif(n, 0, 10)
y <- 1 + 2 * x + 1 * z + 0.3 * x * z + rnorm(n, sd = 2)
m <- lm(y ~ x * z)
# Plot predictions at z = 2, 5, 8
x_seq <- seq(0, 10, length.out = 100)
z_vals <- c(2, 5, 8)
colors <- c("firebrick", "steelblue", "darkgreen")
plot(NULL, xlim = c(0, 10), ylim = range(y),
xlab = "x", ylab = "y", main = "7M1: Interaction at different z values")
for (i in 1:3) {
pred <- coef(m)[1] + coef(m)[2] _ x_seq + coef(m)[3] _ z*vals[i] +
coef(m)[4] * x*seq * z_vals[i]
lines(x_seq, pred, lwd = 2, col = colors[i])
}
legend("topleft", legend = paste("z =", z_vals), col = colors, lwd = 2)
cat("Slope of x at z = 2:", round(coef(m)[2] + coef(m)[4] _ 2, 3), "\n")
cat("Slope of x at z = 5:", round(coef(m)[2] + coef(m)[4] _ 5, 3), "\n")
cat("Slope of x at z = 8:", round(coef(m)[2] + coef(m)[4] * 8, 3), "\n")7M2. Center both predictors and refit the model. How do the main effects change? Does the interaction change?
7M3. Fit a model with a categorical x continuous interaction. Plot the separate regression lines for each category.
# 7M3: Categorical x continuous interaction
set.seed(42)
n <- 150
group <- sample(1:3, n, replace = TRUE)
x <- runif(n, 0, 10)
# Different slopes for each group
slopes <- c(1, 2, 3)
y <- 10 + slopes[group] * x + rnorm(n, sd = 2)
m <- lm(y ~ group * x)
cat("Categorical x continuous interaction:\n")
cat(" Group 1 slope:", round(coef(m)[2], 3), "\n")
cat(" Group 2 slope:", round(coef(m)[2] + coef(m)[4], 3), "\n")
cat(" Group 3 slope:", round(coef(m)[2] + coef(m)[5], 3), "\n")
# Plot
colors <- c("steelblue", "firebrick", "darkgreen")
plot(x, y, pch = 16, col = colors[group],
main = "7M3: Categorical x continuous interaction",
xlab = "x", ylab = "y")
x*seq <- seq(0, 10, length.out = 100)
for (g in 1:3) {
if (g == 1) {
pred <- coef(m)[1] + coef(m)[2] * x*seq
} else {
pred <- coef(m)[1] + coef(m)[1 + g] + (coef(m)[2] + coef(m)[2 + g]) * x_seq
}
lines(x_seq, pred, lwd = 2, col = colors[g])
}
legend("topleft", legend = paste("Group", 1:3), col = colors, lwd = 2)Hard problems
7H1. Simulate data with a true interaction. Fit models with and without the interaction. Compare their AIC. Does the interaction model win?
# 7H1: Compare models with and without interaction
set.seed(42)
n <- 100
x <- runif(n, 0, 10)
z <- runif(n, 0, 10)
y <- 1 + 2 * x + 1 * z + 0.5 * x * z + rnorm(n, sd = 2)
m_no <- lm(y ~ x + z)
m_int <- lm(y ~ x * z)
cat("AIC comparison:\n")
cat(" No interaction:", round(AIC(m_no), 2), "\n")
cat(" With interaction:", round(AIC(m_int), 2), "\n")
cat(" Difference:", round(AIC(m_no) - AIC(m_int), 2), "\n")
if (AIC(m_int) < AIC(m_no)) {
cat("\nThe interaction model is preferred.\n")
} else {
cat("\nThe no-interaction model is preferred.\n")
}7H2. Fit a model with a 3-way interaction. Interpret the results by plotting predictions at different combinations of the three predictors.
7H3. Return to the tulip example (Section 7.1). Fit the interaction model and compute the posterior distribution of the light effect at each water level.