question regarding plotting interaction effects

I try to understand interactions in multiple linear regression and especially how to plot them and there is one uncertainty that emerged I hope somebody can rule out for me. So if you have done the MLR and the interaction term is significant and you now want to do the post hoc test. We have to do this in a homework.

Most resources that cover this topic explain it in a model with only 4 coefficients:
$\hat{Y} = A + B_1X_1 + B_2X_2 + B_3X_1X_2$

Or written in another way:
$\hat{Y} = (B_1 + B_3X_2)X_1 + (A + B_2X_2)$

Then I can calculate the simple slopes for different values of $X_2$, for example if $X_2$ is the variable gender (male = 0 and female =1), then I get 2 the 2 lines:
$Z_{males} = B_1 + X_1 + A$
$Z_{females} = (B_1 + B_3)X_1 + (A + B_2)$

If I understood the material, then I could plugin 2 values for $X_1$, e.g. the min and max values and then plot the lines.



But what if I have 5 coefficients so that we have a regression equation like this: $\hat{Y} = A + B_1X_1 + B_2X_2 + B_3X_1X_2 + B_4X_3$

Or written in another way:
$\hat{Y} = (B_1 + B_3X_2)X_1 + (A + B_2X_2 + B_4X_3)$

When I then want to make the 2 lines for males and females, I thought that I then just have to ignore the term $B_4X_3$. Is that correct? And if I do not ignore this term, which values will I have to plugin for $X_3$ to plot the 2 lines to make the interaction plot?

By the way, if there is an easy way to plot this with ggplot2, please let me know. My plan was to make use geom_abline() to make each line manually.

The best/easiest way that I've seen to do this is just to plot the predicted curves alone based on the regression line. You can do this since there are no higher order interactions or interactions involving X_3.

Another approach is to use prediction to "subtract off the variability" of factors we don't care about. In your 5 "predictor" example (only 3 covariates, an interaction, and an intercept), you can take the estimate of $\beta_4$, multiply it by $X_3$, and take $Z = Y-\beta_4X_3$ so that $Z$ is a kind of residual which only depends on the factors you do care about, namely $X_1$ and $X_2$.

As an example:

set.seed(123)
x <- matrix(rnorm(2000), 1000, 2)
w <- sample(0:1, 1000, replace=T)
y <- x %*% rnorm(2) + 1.2 * x[, 1]*w + rnorm(1000)
fit <- lm(y ~ x[,1]*w + x[,2])
z <- y - coef(fit)['x[, 2]']*x[, 2]
coplot(z ~ x[, 1 ]|factor(w))


• thanks @AdamO! So, if I understand you correctly: If there is no higher order interaction involving $X_3$, I can just leave out $\beta_4X_3$ to plot the interaction between $X_1$ and $X_2$? This would answer my question. I have to do this at least one time by hand for the homework (drawing the interaction lines like e.g. here: courses.washington.edu/smartpsy/interactions.htm). The second approach I will think about. I do not yet understand it but I am sure I will soon. I need to get more and more understanding first... Oct 3, 2017 at 18:17
• To "Just leave out B4X3", you plot the the fitted lines with slopes (B1) and (B1 + B3) on the same scale and label them "first order trends: males / females). Oct 3, 2017 at 18:22