3-way interaction in plot_model - r

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"))

Related

plotting effect of the interaction between 2 explanatory variables on the response variable from a generalized linear mixed effect model (glmer) in R

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!

Plotting Marginal effects of interactions with continuous and dummy

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

Plotting random slopes from glmer model using sjPlot

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.

How to plot log odds for a binary outcome against a continuous variable in ggplot2?

I am trying to generate some graphs to visualize the functional form of different variables for exploratory data analysis and simple modeling. I have a binary outcome variable, and some continuous predictor variables. What I would like to do is make a graph in ggplot2 that compares the log odds of my outcome variable in the Y axis to the predictor variable in the X-axis. This way I can visually estimate if the predictor variable needs to be transformed before using it in a linear regression.
Disclaimer: I am a novice at both R and Statistics.
So far the closes thing I've found is in the Hmisc package:
library(tidyverse)
library(Hmisc)
df <- mtcars %>% mutate(mpg = if_else((mpg > 18), 1, 0))
plsmo(
df$hp,
df$mpg,
method = "lowess",
datadensity = TRUE,
ylab = "Log odds of mpg > 18 as a function of hp",
fun = plogis,
lty = 3
)
Even in this case, it doesn't quite make a scatterplot of the outcome variables, and instead represents the data densities in the form of the tick marks on the line. If I could at least replicate that graph, it might be a good starting point. My first thought would be to do it manually by breaking the predictor variable into bins and calculating the odds for each bin, but this might require a large degree of fiddling for each graph.
This is close but doesn't help much with seeing relationships.
Thanks!

Dummy variables in R

I'm constructing a linear model to evaluate the effect of distances from a habitat boundary on the richness of an order of insects. There are some differences in equipment used so I am including equipment as a categorical variable to ensure that it hasn't had a significant affect on richness.
The categorical factor is 3 leveled so I asked r to produced dummy variables in the lm by using the code:
lm(Richness ~ Distances + factor(Equipment), data = Data)
When I ask for the summary of the model I can see two of the levels with their coefficients. I am assuming that this means r is using one of the levels as the "standard" to compare the coefficients of the other levels to.
How can I find the coefficient for the third level in order to see what effect it has on the model?
Thank you
You can do lm(y~x-1) to remove the intercept, which in your case is the reference level of one of the factors. That being said, there are statistical reasons for using one of the levels as a reference.
To determine how to extract your coefficient, here is a simple example:
# load data
data(mtcars)
head(mtcars)
# what are the means of wt given the factor carb?
(means <- with(mtcars, tapply(wt, factor(carb), mean)))
# run the lm
mod <- with(mtcars, lm(wt~factor(carb)))
# extract the coefficients
coef(mod)
# the intercept is the reference level (i.e., carb 1)
coef(mod)[1]
coef(mod)[2:6]
coef(mod)[1] + coef(mod)[2:6]
means
So you can see that the coefficients are simply added to the reference level (i.e., intercept) in this simple case. However, if you have a covariate, it gets more complicated
mod2 <- lm(wt ~ factor(carb) + disp, data=mtcars)
summary(mod2)
The intercept is now the carb 1 when disp = 0.

Resources