I have a doubt about my homework on how to plot multiple interactions and I would be very grateful if anyone could help. I know this is a popular topic, but I have not seen questions addressing my specific problem. I hope is not a repetition
I have a model with 3 explanatory variables and two interactions. Two variables are continuous and one is a dummy. I would like to plot into a single image the marginal effect of the three interactions.
the model is something like this
y = a+ b_1c1 + b_2c2 + b_3d + b_4c1c2 + b_5c1d
In sum, I have one continuous variable (c1), that is interacted both with the other continuous variable (c2) and the dummy variable (d).
The code below uses data from the MASS package to allow a reproducible example.
library(interplot)
library(MASS) # to take the dataset from
data<- cars93
library(interplot) # for plotting interations
and I create my model
model<- lm(Price ~ Horsepower*EngineSize # c1*c2 interaction
+ Horsepower*Man.trans.avail, #c1*d interaction
data = data)
I would like to plot the estimated effects of the Horsepower on price as a function of the EngineSize for Man.trans.avail=0 (No),
and plot that same conditional effect of Horsepower on price as a function of the EngineSize for Man.trans.avail=1 (Yes). Possibly putting them into the same plot.
Using the interplot function I am able to see the conditional Horsepower on price as a function of the EngineSize but without controlling for Man.trans.avail
interplot(m=model, var1 = "Horsepower", var2 = "EngineSize", hist = TRUE)
The result plots the marginal effect line of Horsepower and EngineSize, however, is not able to plot two lines for different values of Man.trans.avail and I was wondering whether you had some idea on how to plot marginal effects in similar circumstances.
I thank you a lot in advance for your replies
Related
I have the following code
glmer(Success ~ Trial*Treatment + 1|ID + Origin + Shelter, family = binomial, data = rama)
It gives me a significant effect of the interaction between Trial and Treatment on the response variable success.
I would like to plot this interaction.
I tried to use
plot(Success ~ Trial*Treatment, data = rama)
But it doesn't plot the interaction, rather makes two different plots, one Success vs Trial and another Success vs Treatment.
Ideally, I would like to merge these two graphs, a bit as what I would get if it was an LME instead of a GLME and I plotted (if success was a continuous variable) and I'd use the following code
boxplot(Success ~ Trial*Treatment, data = rama)
I would be super happy if anyone has an idea.
Thanks a lot!
I have fit a mixed-effects model and included a 3-way interaction between my fixed effects which are:
two categorical variables: A1(level1, level2), A2 (level1, level2)
continuous: B
model <- lmer( dependent variable~ A1*A2 * B + random factors, data)
To visualise the interaction, I am using plot_model from the "sjPlot" package:
plot_model(model, type="int", terms=c("A1", "A2", "B"))
The output seems to have broken down my continuous variable (B) into two separate categories (high B, low B) and then plot the interaction for each of the categories in two separate windows.
My question is:
What criterion does the "sjPlot" package use to categorise my continuous variable? What determines "high B" and what determines "low B"? And I wonder if there is any other way to visualise a three-way interaction which is more informative.
Thank you!
welcome to SO. I use sjPlot quite a bit and took this opportunity to consolidate some of my knowledge about it.
There is also the fact that calling plot_model(..., type = "int) will use the order of the variables in the regression formula to decide how to plot that interaction. Here I've drawn up an example using the 'mtcars' data-set included in R. I transform two of the binary variables into factors first so it matches your example.
library(sjPlot)
mtcars$vs <- as.factor(as.character(mtcars$vs))
mtcars$am <- as.factor(as.character(mtcars$am))
m1 <- lm(mpg ~ vs * am * hp,
data = mtcars)
plot_model(m1,
type = "int")
This code produces the following plot, where two values of the continuous variable hp have been selected for plotting in two separate windows:
Ben is correct, and these values are pulled from the mdrt.values argument, which defaults to "minmax", meaning these values should be the highest and lowest in that column, which we can verify:
range(mtcars$hp)
[1] 52 335
There are other options for this argument, some of which will be better or worse depending on your case. As you only have one continuous variable, we might want to show the whole predictor along the x-axis. We can do this in a few ways, one of which is changing the model formula so that hp is first.
m2 <- lm(mpg ~ hp * vs * am,
data = mtcars)
plot_model(m2,
type = "int")
However, this is probably not the best way to do this, as sometimes models take a while to fit and re-fitting the model for plotting is a waste of time/electricity. It's useful to know here that the type = "int" call is just for convenience, and that we can also plot interactions by setting type="pred" and then passing the effects we want to be plotted using the terms argument. Thefollowing code will make a very similar plot to the second one, but using the first model that we fit. Changing the order of the terms included within c() will change the plot.
plot_model(m1,
type = "pred",
terms = c("hp", "vs", "am"))
I have constructed a mixed effect model using lmer() with the aim of comparing the growth in reading scores for four different groups of children as they age.
I would like to plot a graph of the 4 different slopes with confidence intervals in R in order to visualize this relationship but I keep getting stuck.
I have tried to use the plot function and some versions of the ggplot as I have done for previous lm() models but it isn't working so far. Here is my attempted model which I hope looks at how the change in reading scores over time(age) interacts with a child's SESDLD grouping (this indicated whether a child has a language problem and whether or not they are high or low income).
AgeSES.model <- lmer(ReadingMeasure ~ Age.c*SESDLD1 + (1|childid), data = reshapedomit, REML = FALSE)
The ReadingMeasure is a continuous score, age.c is centered age measured in months. SESDLD1 is a categorical measure which has 4 levels. I would expect four positive slopes of ReadingMeasure growth with different intercepts and probably differing slopes.
I would really appreciate any pointers on how to do this!
Thank you so much!!
The type of plot I would like to achieve - this was done in Stata
In the past, I had used the sjp.glmer from the package sjPlot to visualize the different slopes from a generalized mixed effects model. However, with the new package, I can't figure out how to plot the individual slopes, as in the figure for the probabilities of fixed effects by (random) group level, located here
Here is the code that, I think, should allow for the production of the figure. I just can't seem to get it in the new version of sjPlot.
library(lme4)
library(sjPlot)
data(efc)
# create binary response
efc$hi_qol = 0
efc$hi_qol[efc$quol_5 > mean(efc$quol_5,na.rm=T)] = 1
# prepare group variable
efc$grp = as.factor(efc$e15relat)
# data frame for 2nd fitted model
mydf <- na.omit(data.frame(hi_qol = as.factor(efc$hi_qol),
sex = as.factor(efc$c161sex),
c12hour = as.numeric(efc$c12hour),
neg_c_7 = as.numeric(efc$neg_c_7),
grp = efc$grp))
# fit 2nd model
fit2 <- glmer(hi_qol ~ sex + c12hour + neg_c_7 + (1|grp),
data = mydf,
family = binomial("logit"))
I have tried to graph the model using the following code.
plot_model(fit2,type="re")
plot_model(fit2,type="prob")
plot_model(fit2,type="eff")
I think that I may be missing a flag, but after reading through the documentation, I can't find out what that flag may be.
Looks like this might do what you want:
(pp <- plot_model(fit2,type="pred",
terms=c("c12hour","grp"),pred.type="re"))
type="pred": plot predicted values
terms=c("c12hour", "grp"): include c12hour (as the x-axis variable) and grp in the predictions
pred.type="re": random effects
I haven't been able to get confidence-interval ribbons yet (tried ci.lvl=0.9, but no luck ...)
pp+facet_wrap(~group) comes closer to the plot shown in the linked blog post (each random-effects level gets its own facet ...)
Ben already posted the correct answer. sjPlot uses the ggeffects-package for marginal effects plot, so an alternative would be using ggeffects directly:
ggpredict(fit2, terms = c("c12hour", "grp"), type="re") %>% plot()
There's a new vignette describing how to get marginal effects for mixed models / random effects. However, confidence intervals are currently not available for this plot-type.
The type = "ri.prob" option in the linked blog-post did not adjust for covariates, that's why I first removed that option and later re-implemented it (correctly) in ggeffects / sjPlot. The confidence intervals shown in the linked blog-post are not correct, either. Once I figure out a way how to obtain CI or prediction intervals, I'll add this option as well.
I would like to visualize the effect of a significant interaction term in the following mixed effects model.
c1 <- lmer(log.weight ~ time + I(time^2) + temp + precip + time:precip + time:temp + (1|indiv), data = noctrl)
This model includes fixed effects of 'time' (simple and quadratic term), 'temperature', 'precipitation' and two interactions on the logarithmized response 'weight'. All terms are significant and the models assumptions of normality and homogeneity are met.
I’ve been using the 'effects' package to produce interaction plots to show the effect of the interactions. When I try to show the interaction of time and temperature (time:temp) with the following code I’m not sure whether the resulting plot correctly shows this interaction.
ef2 <- effect(term="time:temp", c1, multiline=TRUE)
y <- as.data.frame(ef2)
ggplot(y , aes(time, fit, color=temp)) + geom_point() + geom_errorbar(aes(ymin=fit-se, ymax=fit+se), width=0.4)
I need help understanding the resulting plot please. How come despite this interaction term is highly significant, the SEs overlap at each value of x?
Am I using the effects package correctly? Or is it because I need to include the quadratic term of time I(time^2) in the interaction terms as well?
Thank you very much for the clarification.