使用 sjPlot 从 glmer 模型绘制随机斜率
Plotting random slopes from glmer model using sjPlot
过去,我使用过 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
包中的 sjp.glmer
我认为这是生成图形的代码。我似乎无法在 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"))
我尝试使用以下代码绘制模型。
plot_model(fit2,type="re")
plot_model(fit2,type="prob")
plot_model(fit2,type="eff")
我想我可能遗漏了一个标志,但在阅读文档后,我找不到那个标志可能是什么。
看起来这可能会满足您的要求:
(pp <- plot_model(fit2,type="pred",
terms=c("c12hour","grp"),pred.type="re"))
type="pred"
:绘制预测值
terms=c("c12hour", "grp")
:在预测中包含 c12hour
(作为 x 轴变量)和 grp
pred.type="re"
:随机效应
我还没能得到置信区间丝带(试过ci.lvl=0.9
,但运气不好...)
pp+facet_wrap(~group)
更接近链接博客 post 中显示的情节(每个随机效应级别都有自己的方面...)
Ben 已经 post 给出了正确答案。 sjPlot 使用 ggeffects-package 作为边际效应图,因此另一种方法是直接使用 ggeffects:
ggpredict(fit2, terms = c("c12hour", "grp"), type="re") %>% plot()
a new vignette 描述了如何获得混合模型/随机效应的边际效应。但是,当前无法使用此绘图类型的置信区间。
链接博客中的 type = "ri.prob"
选项 - post 没有针对协变量进行调整,这就是为什么我首先删除了该选项,然后在 gfeffects / sjPlot 中(正确地)重新实现了它。链接博客中显示的置信区间 - post 也不正确。一旦我弄清楚如何获得 CI 或预测区间,我也会添加此选项。
过去,我使用过 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
包中的sjp.glmer
我认为这是生成图形的代码。我似乎无法在 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"))
我尝试使用以下代码绘制模型。
plot_model(fit2,type="re")
plot_model(fit2,type="prob")
plot_model(fit2,type="eff")
我想我可能遗漏了一个标志,但在阅读文档后,我找不到那个标志可能是什么。
看起来这可能会满足您的要求:
(pp <- plot_model(fit2,type="pred",
terms=c("c12hour","grp"),pred.type="re"))
type="pred"
:绘制预测值terms=c("c12hour", "grp")
:在预测中包含c12hour
(作为 x 轴变量)和grp
pred.type="re"
:随机效应
我还没能得到置信区间丝带(试过ci.lvl=0.9
,但运气不好...)
pp+facet_wrap(~group)
更接近链接博客 post 中显示的情节(每个随机效应级别都有自己的方面...)
Ben 已经 post 给出了正确答案。 sjPlot 使用 ggeffects-package 作为边际效应图,因此另一种方法是直接使用 ggeffects:
ggpredict(fit2, terms = c("c12hour", "grp"), type="re") %>% plot()
a new vignette 描述了如何获得混合模型/随机效应的边际效应。但是,当前无法使用此绘图类型的置信区间。
链接博客中的 type = "ri.prob"
选项 - post 没有针对协变量进行调整,这就是为什么我首先删除了该选项,然后在 gfeffects / sjPlot 中(正确地)重新实现了它。链接博客中显示的置信区间 - post 也不正确。一旦我弄清楚如何获得 CI 或预测区间,我也会添加此选项。