使用 R 中的 tbl_regression 函数在一列中使用 95% CI 的回归系数?

问题描述

为了测试疾病组(categorical_variable)和疾病(结果;二分法)之间是否存在关联,我正在运行逻辑回归。为了检查其他变量的混淆,我运行了 3 个具有不同协变量数量的模型。

为了显示 OR 和 CI,我使用了 tbl_regression function 包中的 gtsummary(我使用了 dplyr 函数 mutate 在圆括号中显示 CI)。但是,这将 CI 显示在与 OR 不同的列中,但我希望它们与 OR 后的圆括号中的 CI 位于同一列中。

我的代码

library(gtsummary)
library(dplyr)

Model 1 <-  
  glm(data=wide_dataset,formula=outcome ~ categorical_variable,family=binomial(link="logit") %>%
  tbl_regression(
    exponentiate = TRUE) %>% 
  # remove the p-value column
  modify_column_hide(column=p.value) %>%
  # CI in round brackets:
  modify_table_body(mutate,ci=gsub("(\\d\\.\\d{,4})(,)(\\d\\.\\d{,4})","\\(\\1 \\3\\)",ci))

Model 1

提前致谢!

解决方法

使用开发版软件包(将于下周发布到 CRAN)将优势比和 CI 放在同一列中要容易得多。

如果使用 JAMA 期刊主题,OR 和 CI 将自动合并为一列。

remotes::install_github("ddsjoberg/gtsummary")
library(gtsummary)
packageVersion("gtsummary")
#> [1] '1.3.7.9016'

# set the JAMA theme to display OR and CI in same column
theme_gtsummary_journal("jama")
#> Setting theme `JAMA`

tbl <-
  glm(response ~ age + grade,data = trial,family = binomial) %>%
  tbl_regression(exponentiate = TRUE) %>%
  modify_column_hide(p.value)

enter image description here reprex package (v2.0.0) 于 2021 年 4 月 7 日创建

此代码也可以在不设置 JAMA 期刊主题的情况下使用。

tbl <-
  glm(response ~ age + grade,family = binomial) %>%
  tbl_regression(exponentiate = TRUE) %>%
  # merge OR and CI into single column
  modify_table_styling(
    column = estimate,rows = !is.na(estimate),cols_merge_pattern = "{estimate} ({conf.low} to {conf.high})"
  ) %>%
  modify_header(estimate ~ "**OR (95% CI)**") %>%
  modify_column_hide(c(ci,p.value))

听起来您想比较多个模型。以下是将它们放在同一个表中的方法。

list(tbl,tbl,tbl) %>%
  tbl_merge(tab_spanner = paste0("**Model ",1:3,"**"))

enter image description here