结合 R 中的 2 个预测概率图和 SjPlot 包

Combine 2 Predicted Probabilities graphs in R with SjPlot Package

假设我有这个代码:

dt <- data.frame(x = (1:9), z = c(6,5,3,7,8,3,1,6,7), y = c(1,0,1,1,0,0,0,1,0))
model1 <- glm(y ~ x + z, family = binomial(link = "logit"),
              data = dt)
model2 <- glm(y ~ x + z, family = binomial(link = "logit"),
              data = dt)
summary(model1)
summary(model2)
library(sjPlot)
m <- plot_model(model1, title = "Predicted probabilities", type = "pred", terms = "x")
n <- plot_model(model2, title = "Predicted probabilities", type = "pred", terms = "x")

我想合并两个图表,这样我就可以将它们保存在一个文件中。在这两个图中,您都可以从 sjPlot 包中找到预测概率。

我怎样才能做到这一点?

我已经尝试 par(mfrow=c(2,2)) 但它根本不起作用。

感谢任何帮助!

由于您尝试了 par() 函数,我假设您想要并排创建两个图表。您可以使用库 gridExtragrid.arrange() 函数实现此目的,如下所示:

使用代码:

library(sjPlot)
library(gridExtra)

dt <- data.frame(x = (1:9), z = c(6,5,3,7,8,3,1,6,7), y = c(1,0,1,1,0,0,0,1,0))

model1 <- 0
model2 <- 0

model1 <- glm(y ~ x + z, family = binomial(link = "logit"),
              data = dt)
model2 <- glm(y ~ x^4 + z, family = binomial(link = "logit"),
              data = dt)
summary(model1)
summary(model2)

m <- plot_model(model1, title = "Predicted probabilities", type = "pred", terms = "x")
n <- plot_model(model2, title = "Predicted probabilities", type = "pred", terms = "x")

grid.arrange(m, n, ncol = 1, heights = c(1, 1))

如果你想让它们并排而不是一个在另一个上面,只需将 ncol 值更改为 2