使用R中的forest_model将多个逻辑回归模型集成到一个森林图中

问题描述 投票:0回答:2

我想使用 R 中的

forestmodel
包将多个逻辑回归模型(某些预测变量的 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"))

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

r plot logistic-regression r-forestplot
2个回答
0
投票

我发现,如果您使用

forest_model
函数而不是
forest_models
制作单独的森林图,并使用
ggarrange
包中的
ggpubr
函数排列它们,如下所示:

library(ggpubr)

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

0
投票

如果有人偶然发现这个问题/问题:

解决方案是添加模型列表而不是forest_model输出作为model_list的参数。 OP 创建了单独的 Forest_model 图,然后尝试使用这些图的列表作为 model_list 的参数。

modelGlucose <- glm(EndPoint ~ Age + Sex + Glucose,data=data, family=binomial())
modelHbA1c <- glm(EndPoint ~ Age + Sex + HbA1c, data=data, family=binomial())

models <- list(Glucose=modelGlucose,HbA1c=modelHbA1c)

forest_model(model_list=models,covariates=c("Glucose","HbA1c"), merge_models=T)
© www.soinside.com 2019 - 2024. All rights reserved.