如何将多个 glmer 模型绘制成一个图?

How to plot multiple glmer models into one single plot?

我有两个 glmer 模型,每个模型都有两个协变量,我试图将它们绘制成一个图形。

MWE:

## generalized linear mixed model
library(lattice)

cbpp$response <- sample(c(0,1), replace=TRUE, size=nrow(cbpp))
gm1 <- glmer(response ~ size + incidence + (1 | herd),
              data = cbpp, family = binomial)

cbpp$obs <- 1:nrow(cbpp)
gm2 <- glmer(response ~ size + incidence + (1 | herd) +  (1|obs),
              family = binomial, data = cbpp)

我正在尝试针对每个模型的每个协变量绘制预测值。我找到了 sjPlot 库和 plot_model 函数,它们可以在使用 type = "pred" 时绘制这些预测。在每个模型上单独调用此函数效果很好,并为每个模型生成两个单独的数字:

但是我不熟悉 R,而且我很难尝试在同一个图形上绘制 4 个图。

plot_model 函数有一个 grid 参数,仅适用于具有泊松分布的模型。对于 gm1gm2,我在调用 plot_model(gm1, type = "pred", grid = TRUE) 时遇到以下错误:

Error in if (attr(x, "logistic", exact = TRUE) == "1" && attr(x, "is.trial",  : missing value where TRUE/FALSE needed

无论如何,我无法使用它在一张图中绘制三个模型,所以我尝试了三种不同的方法。首先,我看到了 plot_models 函数,它以多个模型作为输入。当我尝试将两个模型作为参数传递时,调用 plot_models(gm1, gm2) 我收到以下错误:

Error: $ operator not defined for this S4 class

其次,我尝试使用 par 函数设置 mfrow 然后再次调用 plot_model 但没有成功。我没有收到任何错误,但图表一直显示为单独的数字。

第三,我尝试使用 gridExtra 库。打电话

p1 <- plot_model(gm1, type = "pred")
p2 <- plot_model(gm2, type = "pred")
grid.arrange(p1, p2)

导致以下错误:

Error in gList(list(ppt = list(data = list(x = c(-2, -1, 0, 1, 2, 3, 4,  : only 'grobs' allowed in "gList"

有人对此有见解吗?

编辑

这似乎有效:

pp1 <- plot_model(gm1,type="pred")
pp2 <- plot_model(gm2,type="pred")
plot_grid(c(pp1,pp2))