[R-sig-ME] Plotting interactions while controlling for other predictors.

David Sidhu d@idhu @ending from uc@lg@ry@c@
Wed Dec 12 01:28:39 CET 2018

I am interested in creating a plot of an interaction that accounts for other variables present in a model.

I am attaching a reproducible example of my try so far, but I don’t believe this is quite getting at what I’d like.

Essentially, I want to show the interaction between X2 and X3, while controlling for X1.

Thank you very much.


X1 = rnorm(1000)
X2 = rnorm(1000)
X3 = sample(1:3, size = 1000, replace = TRUE)
X3 <- as.factor(X3)
DV = rnorm(1000)

Item <- rep(1:10, times = 100)
Subject <- rep(1:100, each = 10)

d <- as.data.frame(cbind(X1, X2, X3, DV, Item, Subject))

m1 <- lmer(DV ~ X1 + X2*X3 + (1|Subject) + (1|Item), data = d)

d$Pred <- predict(m1)
ggplot(d, aes(x = X2, y = Pred, col = factor(X3), group = X3)) + geom_jitter(alpha = .2) + geom_smooth(method = "lm")

David M. Sidhu, MSc
PhD Candidate
Department of Psychology
University of Calgary

More information about the R-sig-mixed-models mailing list