Interaction terms in regression model allow the effect of one predictor to vary with levels of another predictor.
- Without an interaction, the effect of one predictor is modeled to be the same across all levels of the other predictor (i.e. a main effect)
A variable representing the interaction of two variables is formed by taking the product of those two variables.
Generally, the two component variables and the interaction variable are all entered into the regression together.
- In R, we use the
*
operator between predictors to request that both the interaction and the two component variables be added to lm()
model
Below, we model the interaction of number of kin and group size. Kin number should have greater effect in big groups, because competition increases with group size and coalitionary support from kin matters more.
# Multiple linear regression with interaction between two continuous variables
mKG <- lm( weight ~ kin + group_size + kin*group_size, data=d)
# Extract model summary
sum_mKG <- summary(mKG)
sum_mKG
##
## Call:
## lm(formula = weight ~ kin + group_size + kin * group_size, data = d)
##
## Residuals:
## Min 1Q Median 3Q Max
## -1.17226 -0.37270 -0.00907 0.39110 1.33113
##
## Coefficients:
## Estimate Std. Error t value Pr(>|t|)
## (Intercept) 3.35099 0.28257 11.859 <2e-16 ***
## kin -0.12715 0.12073 -1.053 0.2949
## group_size -0.06214 0.02669 -2.328 0.0220 *
## kin:group_size 0.02459 0.01133 2.170 0.0324 *
## ---
## Signif. codes: 0 '***' 0.001 '**' 0.01 '*' 0.05 '.' 0.1 ' ' 1
##
## Residual standard error: 0.5533 on 96 degrees of freedom
## Multiple R-squared: 0.1456, Adjusted R-squared: 0.1189
## F-statistic: 5.452 on 3 and 96 DF, p-value: 0.001668
# Add confidence intervals
ci_mKG <- confint(mKG)
sum_mKG <- cbind(sum_mKG$coefficients, ci_mKG)
round(sum_mKG, 4) # rounding
## Estimate Std. Error t value Pr(>|t|) 2.5 % 97.5 %
## (Intercept) 3.3510 0.2826 11.8591 0.0000 2.7901 3.9119
## kin -0.1272 0.1207 -1.0531 0.2949 -0.3668 0.1125
## group_size -0.0621 0.0267 -2.3283 0.0220 -0.1151 -0.0092
## kin:group_size 0.0246 0.0113 2.1704 0.0324 0.0021 0.0471
The interpretation of the intercept is the same as before: the expected weight for a capuchin with no close kin and from a group size of zero is
3.351 kg.
The interpretation of coefficients for variables involved in an interaction is different from a main effects model. Namely, these are the simple or conditional effects when the other variable is zero.
- For each additional kin, we expect weight to decrease by
0.1272 kg when group size is zero, although the sign of this effect is not clear from the data (95% CI =
[-0.3668, 0.1125]).
- For each additional group member, we expect weight to decrease by
0.0621 kg when number of close kin is zero, (95% CI =
[-0.1151, -0.0092]).
The interaction coefficient expresses the change in a variable’s effect when the interacting variable is increased by one-unit:
- For each additional kin, the effect of group size is expected to increase by
0.0246 kg/monkey, (95% CI =
[0.002, 0.047]).
- For each additional group member, the effect of kin is expected to increase by
0.0246 kg/kin.
The effect of kin at any particular group size can be calculated by adding the coefficient for kin,
0.1272, to the interaction coefficient,
0.0246, multiplied by group size.
\[\begin{aligned} kin.effect_{(group\_size=g)} &= - 0.1272 + 0.0246*g \\
kin.effect_{(group\_size=2)} &= - 0.1272 + 0.0246*10 \\
&= 0.1188
\end{aligned}\]
Interactions can be difficult to understand from just examining the coefficients, so graphs of model predictions can greatly help.
- predict the outcome across a range of values of predictors involved in the interaction
- one predictor should be graphed along the x-axis
- the other predictor should be used to group lines (using different colors or line patterns) or to create separate panels
# Calculate predictions
new_data <- expand.grid(group_size=c(5,10,15), kin = c(0,2,4))
predictions <- predict(mKG, newdata=new_data, interval="confidence")
new_data <- cbind(new_data, as.data.frame(predictions))
names(new_data) <- c("group_size","kin","weight","lwr","upr")
new_data
## group_size kin weight lwr upr
## 1 5 0 3.040281 2.721467 3.359095
## 2 10 0 2.729567 2.559185 2.899950
## 3 15 0 2.418854 2.107790 2.729918
## 4 5 2 3.031873 2.835853 3.227893
## 5 10 2 2.967053 2.855005 3.079101
## 6 15 2 2.902233 2.691468 3.112999
## 7 5 4 3.023465 2.667883 3.379046
## 8 10 4 3.204539 3.001180 3.407898
## 9 15 4 3.385612 3.019975 3.751250
# Subset dataset
dsub <- d[d$group_size%in%c(5,10,15) & d$kin%in%c(0,2,4),]
# Plot
pKxG1 <- ggplot(dsub, aes(kin, weight)) +
geom_point() +
geom_ribbon(data=new_data, aes(x=kin, ymin=lwr, ymax=upr, fill=factor(group_size)), alpha=0.25) +
geom_line(data=new_data, aes(kin, weight, color=factor(group_size)), linewidth = 1) +
facet_grid(. ~ group_size, scales="free") +
scale_fill_manual(values=c("5"="skyblue","10"="deepskyblue3" ,"15"="deepskyblue4"),
name="Group size") +
scale_color_manual(values = c("5"="skyblue","10"="deepskyblue3","15"="deepskyblue4") ,
name="Group size") +
labs(x = "Number of Kin", y = "Weight") +
theme_bw()
pKxG1
The impact of the number of kin depends on group size. In small groups (n=5), there is essentially no effect of kinship on an individual’s weight. However, in large groups (n=15), individuals are expected to weigh more with each additional kin member.
# Subset dataset
dsub <- d[d$group_size%in%c(5,10,15) & d$kin%in%c(0,2,4),]
pKG2 <- ggplot(dsub, aes(group_size, weight)) +
geom_point() +
geom_ribbon(data = new_data, aes(x = group_size, ymin = lwr, ymax = upr, fill=factor(kin)), alpha = 0.25) +
geom_line(data = new_data, aes(group_size, weight, color=factor(kin)), linewidth = 1) +
scale_fill_manual(values = c("0"="orange2","2"="salmon1" ,"4"="orange4"),name="Number of Kin" ) +
scale_color_manual(values = c("0"="orange2","2"="salmon1","4"="orange4" ), name="Number of Kin") +
labs(x = "Group size", y = "Weight") +
theme_bw()
pKG2
The effect of group size on weight depends on the number of kin that an individual has. Individuals with no kin members in their group are expected to weigh less as the group size increases. Individuals who have four kin members in the group are expected to weigh more as the group size increases.