Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting random slopes from glmer model using sjPlot

Tags:

r

graphics

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.

like image 882
user44796 Avatar asked Jan 27 '23 00:01

user44796


1 Answers

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

enter image description here

like image 198
Ben Bolker Avatar answered Feb 03 '23 16:02

Ben Bolker