【问题标题】:Odds ratio plot on log scale in R [closed]R中对数刻度的优势比图[关闭]
【发布时间】:2018-01-26 12:20:12
【问题描述】:

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

【问题讨论】:

  • 我认为您的链接已损坏...您可以尝试再次粘贴吗?
  • 请添加一些基本的示例数据和代码,以显示您在绘制这些方面的能力。
  • 也看看How to make a reproducible example in R? 按优先顺序,要么(a)使用内置数据,(b)共享代码来模拟一些示例数据,要么(c)使用dput()以可复制粘贴的方式分享您的一些真实数据。还要显示您尝试过的代码以及您卡在哪里 - 从这里不清楚您是否可以分别绘制每个模型的结果图并且不知道如何组合它们,如果您不知道如何从模型中得到你想要的结果,或者介于两者之间。
  • @victoria - 最好将这些内容编辑到您的问题中,代码在 cmets 中显示效果不佳。

标签: r plot statistics logistic-regression forestplot


【解决方案1】:

这周我正在制作类似的图,发现首先生成置信区间垂直运行的图效果最好,然后在最后使用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() 

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-22
    • 2010-11-13
    相关资源
    最近更新 更多