如何将多个glmer模型绘制到一个图中? [英] How to plot multiple glmer models into one single plot?

查看:292
本文介绍了如何将多个glmer模型绘制到一个图中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个glmer模型,每个模型都有两个协变量,我想将它们绘制到一个图中.

I have two glmer models with two covariates each that I'm trying to plot into a single figure.

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"时绘制这些预测.在每个模型上单独调用此函数都可以正常工作,并且为每个模型生成两个单独的图形,如下所示:

I am trying to plot the predicted values againts each covariate for each model. I found the sjPlot library and the plot_model function, which can plot these predictions when using type = "pred". Calling this function individually on each model works perfect and yields two separate figures like this for each model:

但是我对R并不熟悉,我很难在同一张图上绘制4个图.

However I'm not familiar with R and I am having a hard time trying to plot the 4 plots on the same figure.

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

The plot_model function has a grid parameter, which only works for models with a Poisson distirbution. For gm1 and gm2, I am getting the following error when I call 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)时出现以下错误:

Anyway, I would not be able to plot the three models in one figure using this so I tried three different approaches. First, I saw the plot_models function, which takes multiple models as input. When I try to pass the two models as arguments, calling plot_models(gm1, gm2) I get the following error:

Error: $ operator not defined for this S4 class

第二,我尝试使用par函数设置mfrow,然后再次调用plot_model,但没有成功.我没有任何错误,但是这些图始终显示为单个数字.

Second, I tried using the par function setting the mfrow and then calling plot_model again without success. I don't get any error but the plots keep showing as individual figures.

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

Third, I tried using the gridExtra library. Calling

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

这篇关于如何将多个glmer模型绘制到一个图中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆