首页 > 解决方案 > 使用 R 中的 forest_model 将多个逻辑回归模型放入一个森林图

问题描述

我想使用forestmodelR 中的包将多个逻辑回归模型(某些预测变量的 95%CI 的 OR)绘制成一个图形。

使用这个包我可以生成单独的森林图,但我不知道如何合并它们。

# Load packages and data
library(tidyverse)
library(forestmodel)

mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
str(mydata)

> 'data.frame': 400 obs. of  4 variables:
>  $ admit: int  0 1 1 1 0 1 1 0 1 0 ...
>  $ gre  : int  380 660 800 640 520 760 560 400 540 700 ...
>  $ gpa  : num  3.61 3.67 4 3.19 2.93 3 2.98 3.08 3.39 3.92 ...
>  $ rank : Factor w/ 4 levels "1","2","3","4": 3 3 1 4 4 2 1 2 3 2 ..

# Convert rank to factor. 
mydata$rank <- factor(mydata$rank)

# Fit two models predicting university admission based on rank of high school and gpa or gre.
mylogit1 <- glm(admit ~ gre + rank, data = mydata, family = "binomial")
mylogit2 <- glm(admit ~ gpa + rank, data = mydata, family = "binomial")

# Produce forest plots
plot1 <- forest_model(mylogit1)
plot1
plot2 <- forest_model(mylogit2)
plot2

我尝试了几种方法将两个模型绘制成一个图形:

# I tried several solutions:
forest_model(mylogit1, mylogit2) # after each other, or combined with c("mylogit1", "mylogit2")
forest_model(model_list = c(plot1, plot2)) #the same but with the model_list function

plotlist <- list(plot1, plot2) #making a list first and putting that in.
forest_model(model_list = c("plot1", "plot2"))

遗憾的是,没有任何效果。有什么帮助吗?谢谢!

标签: plotlogistic-regressionforestplot

解决方案


我发现如果您使用函数而不是 制作单独的森林图,这是可行forest_model的,并使用包中的函数forest_models排列它们,如下所示:ggarrangeggpubr

library(ggpubr)

ggarrange(plot1, plot2, ncol=2, nrow=1)

推荐阅读