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
Plotting interactions while controlling for other predictors.
4 messages · David Sidhu, Walid Mawass, Guillaume Adeux
Hi, The package 'effects' is a good tool for this sort of thing. It let's you plot the effect of the interaction term from a regression model while controlling for other predictors by using the mean value for each other variable by default while plotting the predictions for the interaction term based on the model. Hope this helps.
Walid Mawass Ph.D. candidate in Cellular and Molecular Biology Population Genetics Laboratory University of Qu?bec at Trois-Rivi?res 3351, boul. des Forges, C.P. 500 Trois-Rivi?res (Qu?bec) G9A 5H7 Telephone: 819-376-5011 poste 3384 On Wed, Dec 12, 2018, 11:51 AM David Sidhu <dsidhu at ucalgary.ca wrote: > 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 at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models > [[alternative HTML version deleted]]
Hi David, For plotting interactions between two continuous variables, maybe a gamm model might be more adapted? However, by defaut, in the visreg package, you can facet one of your continuous variables (which is subsetted relative to its distribution) and plot the other one accordingly. This would be coded the following way: visreg(m1, X2, by="X3") #overlay=TRUE to have them all combined in one plot, partial=TRUE to have the partial residuals (which do not include RE by default), by default X1 will be fixed to its median This will produce a conditional plot in which all random effects (or X1 in this case) are fixed at a certain level (you can also fix this at a particular value with cond=list(X1=XXXX) ). Nevertheless, this simply calls for predict.lmer(). It is still pretty convenient IMHO. Hope this helps. Sincerely, GA2 Le mer. 12 d?c. 2018 ? 17:51, David Sidhu <dsidhu at ucalgary.ca> a ?crit :
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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models
Sorry I hadn't seen X3 was a factor. The above example will then work how you want it to (one facet per level of X3) highlithing the effect of X2 on Y (per level of X3) while controlling for X1 and random effects (set to a particular level). This will be similar (not identical considering visreg fixes random effects at a particular level) to creating your own prediction matrix with new.dat=expand.grid(X1=median(X1),X2=seq(min(X2),max(X2),by=1),X3=c(1:3)) and feeding it to predict.merMod while predicting at the population level (re.form=~0). How partial residuals can be extracted however, I am not so sure. GA2 Le mer. 12 d?c. 2018 ? 18:06, Guillaume Adeux <guillaumesimon.a2 at gmail.com> a ?crit :
Hi David, For plotting interactions between two continuous variables, maybe a gamm model might be more adapted? However, by defaut, in the visreg package, you can facet one of your continuous variables (which is subsetted relative to its distribution) and plot the other one accordingly. This would be coded the following way: visreg(m1, X2, by="X3") #overlay=TRUE to have them all combined in one plot, partial=TRUE to have the partial residuals (which do not include RE by default), by default X1 will be fixed to its median This will produce a conditional plot in which all random effects (or X1 in this case) are fixed at a certain level (you can also fix this at a particular value with cond=list(X1=XXXX) ). Nevertheless, this simply calls for predict.lmer(). It is still pretty convenient IMHO. Hope this helps. Sincerely, GA2 Le mer. 12 d?c. 2018 ? 17:51, David Sidhu <dsidhu at ucalgary.ca> a ?crit :
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 at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-sig-mixed-models