Plotting an interaction while controlling for other variables in the model.

15 views
Skip to first unread message

David Sidhu

unread,
Dec 11, 2018, 7:24:44 PM12/11/18
to ggplot2

(This obviously isn't my research question, but I'm just using it as an illustration.)

Imagine that I'm interested in seeing if there is an interaction between hair colour and weight, as predictors of a person's speed, after accounting for height.

Is the plot that I create below the best (or at least ONE) way of showing this interaction after accounting for height?

Speed = rnorm(1000)
Height = rnorm(1000)
Weight = rnorm(1000)
HairColour = sample(1:3, size = 1000, replace = TRUE)
HairColour <- as.factor(HairColour)
data <- as.data.table(cbind(Speed, Height, Weight, HairColour))

m1 <- lm(Speed ~ Height + Weight*HairColour, data = data)

data$PredictedSpeed <- predict(m1)

ggplot(data, aes(x = Weight, y = PredictedSpeed, col = factor(HairColour), group = HairColour)) + geom_jitter(alpha = .2) + geom_smooth(method = "lm")

Ben Bolker

unread,
Dec 11, 2018, 7:39:49 PM12/11/18
to dms...@gmail.com, ggplot2
You could take a look at *partial residuals* plots, e.g.
https://github.com/hohenstein/remef
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups "ggplot2" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages