Plotting random slopes from glmer model using sjPlot - r

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.

Related

GAM residuals missing in plot

I am applying a GAM model to my data: cell abundance over time.
The model works just fine (although I am aware of a pattern in my resiudals, but this is a different issue not relevant here).
It just fails to display the partial residuals in the final plot, although i set residuals = TRUE. Here is my output:
https://i.stack.imgur.com/C1MlY.png
also I used mgcv package.
Previously this code worked as I wanted, but on different data. Any ideas on why it is not working are welcome!
GAM_EA <- mgcv::gam(EUB_FISH ~ s(Day, by = Heatwave), data = HnH, method = "REML")
gam.check(GAM_EA) #Checking the model
mgcv::anova.gam(GAM_EA) #Retrieving the statistical results. See ?anova.gam
summary.gam(GAM_EA)
plot(GAM_EA, shift = coef(GAM_EA)[1], residuals = TRUE)
See argument by.resid in ?plot.gam. They way these are used in plot.gam would been meaningless for factor by terms unless you were to subset the partial residuals and plot only the residuals for observations in the specific level of the by factor.

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

Does *metafor* package in R provide forest plot for robust random effects models

I have fit a robust random-effects meta-regression model using metafor package in R.
My full data, as well as reproducible R code appear below.
Questions:
(1) What are the meaning and interpretation of grey-colored diamonds appearing over CIs?
(2) I won't get an overall mean effect when I have moderators, correct?
library(metafor)
d <- read.csv("https://raw.githubusercontent.com/izeh/m/master/d.csv", h = T) ## DATA
res <- robust(rma.uni(yi = dint, sei = SD, mods = ~es.type, data = d, slab = d$study.name),
cluster = d$id)
forest(res)
1) Quoting from help(forest.rma): "For models involving moderators, the fitted value for each study is added as a polygon to the plot." So, the grey-colored diamonds (or polygons) are the fitted values and the width of the diamonds/polygons reflects the width of the CI for the fitted values.
2) No, since there is no longer a single overall effect when your model includes moderators.

How to extract slope and intercept values for different groups from interact_plots in jtools when plotting linear fixed effects models

I am trying to extract the slope and intercept for each of my groups from my linear mixed effects models. The model was constructed using lmer in the lme4 library, and I can view the results for each group using interact_plot from the jtools library. How do I get the slope and intercept for each of these lines?
I know that I can use the summary() or summ() to see the estimates for the fixed effects and the variances of the random effects but I cannot see the estimates of the random effects. Therefore, I cannot accurately calculate the slope and intercepts of the models.
>library(lme4)
> cond_waterxsilver <- lmer(LnAg ~ LnVolume + (LnVolume | FilterID) + SilverType + WaterType + SilverType*WaterType + SilverType*LnVolume + WaterType*LnVolume, data=capwater_removed.data)
> library(jtools)
> interact_plot(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType)
I am just trying to get the slope and intercepts for the six lines from the model (two different WaterType and three different SilverType). Is there a tool within jtools or another package that can help me with extracting the slope and intercepts from my model?
I'm the developer of this package!
A short note: this and the other function I'm going to mention have just been moved to a new package, called interactions, which is in the process of being added to CRAN. Assuming you haven't updated to the newest version of jtools (2.0.0; just came out days ago), these functions are still available in the jtools package. If you do update to jtools 2.0.0, you'll need to follow this link for instructions on how to download interactions before it gets to CRAN.
There should be a simple answer to your question. The sim_slopes (short for "simple slopes") function should give you what you're looking for.
sim_slopes(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType, cond.int = TRUE)
This will print out the conditional slopes and intercepts (the intercepts are only printed when cond.int = TRUE.
If you need to program with those values, you can save the sim_slopes object.
ss <- sim_slopes(cond_ranin_waterxsilver, pred = LnVolume, modx = WaterType, mod2 = SilverType, cond.int = TRUE)
ss$slopes # Matrix of slopes with test statistics, etc.
ss$ints # Matrix of intercepts with test statistics, etc.

Plot Non-linear Mixed Model Over Original Fitted Data

I'm trying to plot the resultant curve from fitting a non-linear mixed model. It should be something like a curve of a normal distribution but skewed to the right. I followed previous links here and here, but when I use my data I can not make it happen for different difficulties (see below).
Here is the dataset
and code
s=read.csv("GRVMAX tadpoles.csv")
t=s[s$SPP== levels(s$SPP)[1],]
head(t)
vmax=t[t$PERFOR=="VMAX",]
colnames(vmax)[6]="vmax"
vmax$TEM=as.numeric(as.character(vmax$TEM));
require(lme4)
start =c(TEM=25)
is.numeric(start)
nm1 <- nlmer ( vmax ~ deriv(TEM)~TEM|INDIVIDUO,nlpars=start, nAGQ =0,data= vmax)# this gives an error suggesting nlpars is not numeric, despite start is numeric...:~/
After that, I want to plot the curve over the original data
with(vmax,plot(vmax ~ (TEM)))
x=vmax$TEM
lines(x, predict(nm1, newdata = data.frame(TEM = x, INDIVIDUO = "ACI5")))
Any hint?
Thanks in advance

Resources