0

我在绘制 GLM logit 模型的结果以在对数尺度上显示为优势比时遇到了困难。最终,我想从不同的模型中获得估计,并将结果绘制在一张图上,如下所示(https://www.ctspedia.org/do/view/CTS...ClinAEGraph001)。你有什么见解吗?

4

1 回答 1

5

本周我制作了类似的图,发现首先使用垂直运行的置信区间生成它们效果最好,然后coord_flip()在最后使用转换为水平:

library(tidyverse)
library(ggplot2)
library(broom)

set.seed(1234)

# Using the builtin Titanic dataset as example data for a GLM
tdf = as.data.frame(Titanic)

m1 = glm(Survived == "Yes" ~ Class + Sex, data = tdf, family = "binomial", weights = Freq)
m1_preds = tidy(m1, conf.int = TRUE, exponentiate = TRUE) %>%
    mutate(Model = "m1")
# Create modified data by mixing up the frequencies - doesn't do anything meaningful,
#   just a way to get different coefficients
tdf$FreqScrambled = sample(tdf$Freq)
m2 = glm(Survived == "Yes" ~ Class + Sex, data = tdf, 
         family = "binomial", weights = FreqScrambled)
m2_preds = tidy(m2, conf.int = TRUE, exponentiate = TRUE) %>%
    mutate(Model = "m2")

# At this point we have a table of odds ratios and confidence intervals
#   for plotting
ors = bind_rows(m1_preds, m2_preds)
ors


dodger = position_dodge(width = 0.3)
# Elements like pointrange and position_dodge only work when the outcome
#   is mapped to y, need to go through with OR set as y then flip at the
#   end
ggplot(ors, aes(y = estimate, x = term, colour = Model)) +
        geom_pointrange(aes(ymin = conf.low, ymax = conf.high),
                       position = dodger,
                       size = 1.2) +
        geom_hline(yintercept = 1.0, linetype = "dotted", size = 1) +
        scale_y_log10(breaks = c(0.1, 0.2, 0.5, 1.0, 2.0, 5.0, 10),
                      minor_breaks = NULL) +
        labs(y = "Odds ratio", x = "Effect") +
        coord_flip(ylim = c(0.1, 10)) +
        theme_bw() 

结果是如下所示的森林图:

在此处输入图像描述

于 2017-08-18T02:08:01.987 回答