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

Fox, John jfox @ending from mcm@@ter@c@
Wed Dec 12 19:01:16 CET 2018


Dear David,

You've already received a couple of suggestions to use the effects package. To clarify, the functions in the package can handle models fit by lmer() (for which it will plot fixed effects) with both factors and numeric predictors, interactions of arbitrary complexity, terms in numeric predictors with transformations, polynomials, and regression splines, and can add partial residuals and nonparametric smooths to the plot. 

See the help pages (in particular ?Effect, ?predictorEffect, and ?plot.eff) and vignettes for the package; there's a recent Journal of Statistical Software article describing partial residuals in effect plots and a book (Fox and Weisberg, An R Companion to Applied Regression, 2nd edition) with many examples.

I hope this helps,
 John

--------------------------------------
John Fox, Professor Emeritus
McMaster University
Hamilton, Ontario, Canada
Web: socialsciences.mcmaster.ca/jfox/



> -----Original Message-----
> From: R-sig-mixed-models [mailto:r-sig-mixed-models-bounces using r-
> project.org] On Behalf Of David Sidhu
> Sent: Tuesday, December 11, 2018 7:29 PM
> To: r-sig-mixed-models <r-sig-mixed-models using r-project.org>
> Subject: [R-sig-ME] Plotting interactions while controlling for other
> predictors.
> 
> 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.
> Dave
> 
> set.seed(182)
> require(data.table)
> require(lme4)
> require(ggplot2)
> 
> 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)
> library(ggplot2)
> 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
> 
> 
> 
> 
> 
> 
> _______________________________________________
> R-sig-mixed-models using r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models


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