我想使用R.中的forestmodel
包将多个逻辑回归模型(某些预测因子的CI为95%的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"))
遗憾的是,一切都没有奏效。有什么帮助吗?谢谢
我发现,如果用forest_model
函数而不是forest_models
制作单独的林图,并用ggpubr
包中的ggarrange
函数将其排列如下,这是可行的:
library(ggpubr)
ggarrange(plot1, plot2, ncol=2, nrow=1)
如果有人偶然发现这个问题:
解决方案是添加一个模型列表,而不是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)