You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在R中绘制多模型的指数形式比值比及95%置信区间

负二项回归暴露变量指数化估计值(OR)的点须图/森林图绘制方法

我有三个负二项回归模型的估计值,希望仅将所有模型中暴露变量的指数估计值(比值比)绘制到单个图中(如森林图或点须图)。此前我使用dwplot绘制,但未得到指数形式的结果。请问有哪些R包或代码可以生成类似的指数形式图?以下是用于生成点须图的数据:

df <- structure(list(term = c("(Intercept)", "class3b1", "class3b3", 
"(Intercept)", "class3b1", "class3b3", "(Intercept)", "class3b1", 
"class3b3"), estimate = c(`(Intercept)` = 0.733889003356554, 
class3b1 = 0.199515823440595, class3b3 = 0.131388675627862, `(Intercept)` = 1.79754882887546, 
class3b1 = 0.36102170683221, class3b3 = 0.139655070741534, `(Intercept)` = 1.60943791243411, 
class3b1 = 0.362583380624898, class3b3 = 0.145763961060158), 
    std.error = c(`(Intercept)` = 0.754245712715592, class3b1 = 0.241859302693499, 
    class3b3 = 0.168773219758597, `(Intercept)` = 0.276527974096511, 
    class3b1 = 0.245193969634718, class3b3 = 0.17297021770553, 
    `(Intercept)` = 0.0857726461773874, class3b1 = 0.241888740986895, 
    class3b3 = 0.170656445866946), statistic = c(`(Intercept)` = 0.973010507032588, 
    class3b1 = 0.824925157803153, class3b3 = 0.778492439830158, 
    `(Intercept)` = 6.50042309371601, class3b1 = 1.472392275267, 
    class3b3 = 0.807393738610467, `(Intercept)` = 18.7639997617144, 
    class3b1 = 1.49896757966317, class3b3 = 0.854136861456757
    ), p.value = c(`(Intercept)` = 0.330548080081182, class3b1 = 0.409414082399228, 
    class3b3 = 0.436278761845928, `(Intercept)` = 8.00944274910155e-11, 
    class3b1 = 0.140914977949294, class3b3 = 0.419439673184529, 
    `(Intercept)` = 1.48771581352615e-78, class3b1 = 0.133882042874179, 
    class3b3 = 0.393029161859892), model = c("Adjusted for family and individual level features", 
    "Adjusted for family and individual level features", "Adjusted for family and individual level features", 
    "Adjusted for family level features", "Adjusted for family level features", 
    "Adjusted for family level features", "Univariate model", 
    "Univariate model", "Univariate model")), row.names = c(NA, 
-9L), class = c("tbl_df", "tbl", "data.frame"))

方法1:修正dotwhisker包的dwplot实现指数化输出

你之前用dwplot没得到指数形式,是因为默认输出原始系数,只要添加transform = exp参数,同时过滤掉截距项只保留暴露变量即可:

library(dotwhisker)
library(ggplot2)

# 过滤截距,仅保留暴露变量
df_exposure <- df[df$term != "(Intercept)", ]

# 绘制指数化的点须图
dwplot(df_exposure, 
       xlab = "比值比 (OR)",
       transform = exp) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") +
  theme_bw()

这个方法直接利用dotwhisker内置参数完成指数转换,操作简单快捷。

方法2:用ggplot2手动绘制(自定义度更高)

如果需要调整图形细节(比如点样式、颜色、布局),可以手动计算*比值比(OR)*和95%置信区间,再用ggplot2绘制:

library(ggplot2)
library(dplyr)

# 数据预处理:过滤截距,计算OR值及置信区间
df_plot <- df %>%
  filter(term != "(Intercept)") %>%
  mutate(
    OR = exp(estimate),
    lower = exp(estimate - 1.96 * std.error),
    upper = exp(estimate + 1.96 * std.error)
  )

# 绘制森林图/点须图
ggplot(df_plot, aes(x = OR, y = term, color = model)) +
  geom_vline(xintercept = 1, linetype = "dashed", color = "gray50") +
  geom_point(position = position_dodge(width = 0.5), size = 2) +
  geom_errorbarh(aes(xmin = lower, xmax = upper), 
                 position = position_dodge(width = 0.5), height = 0.2) +
  labs(x = "比值比 (OR)", y = "暴露变量", color = "模型") +
  theme_bw() +
  theme(legend.position = "bottom")

这种方式可以完全自定义图形元素,满足个性化的可视化需求。

内容的提问来源于stack exchange,提问作者YYM17

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 16:12:04