使用 gtsummary 进行泊松回归的风险数字和事件摘要

Summary of numbers at risk and events for Poisson regression using gtsummary

我有一个 table 的 IRR 摘要和 95% CI 遵循单变量和多变量泊松回归,使用 gtsummary 创建,看起来有点像 this。

对于逻辑回归模型,使用 tbl_summary 创建一些计数数据以附加到 table 的左侧非常简单。但是,对于泊松模型,我希望能够对处于风险中的天数和事件数进行求和而不是计数。基础数据集的每一行都包含一些风险天数和一些事件,因此回归模型 运行 如下所示:

glm(events ~ study_arm + strata_group, 
     offset = log(days_at_risk), 
     family=poisson(link = "log"), 
     data = df)

是否可以使用 gtsummary 为 table 的每一行创建两列,其中包含事件数和风险天数的总和? (然后可以使用 tbl_merge 添加到我的 table。)

这是我想要实现的更完整的示例

df = tibble(
  study_arm = c("control", "intervention", "control", "intervention", "control", "intervention", "control", "intervention"),
  events = c(3,4,12,6,0,3,11,9),
  strata_group = c("A", "A", "A", "A", "B", "B", "B", "B"),
  days_at_risk = c(100,100,200,200,300,300,100,100)
)

m=glm(events ~ study_arm + strata_group, 
     offset = log(days_at_risk), 
     family=poisson(link = "log"), 
     data = df)

tbl_regression(m, exponentiate = T)

#this is the summary I wish to be able to generate with tbl_summary so I can merge it with the tbl_regression output
bind_rows(
  df %>% group_by(study_arm) %>% 
  summarise(n_events = sum(events), 
            total_days_at_risk = sum(days_at_risk), 
            rate=n_events/total_days_at_risk) %>% 
  mutate(row_group = "study_arm") %>% rename(characteristic=study_arm),
df %>% group_by(strata_group) %>% 
  summarise(n_events = sum(events), 
            total_days_at_risk = sum(days_at_risk), 
            rate=n_events/total_days_at_risk) %>% 
  mutate(row_group = "strata_group") %>% rename(characteristic=strata_group)
) %>% 
  select(row_group, characteristic, n_events, total_days_at_risk, rate)

您好,欢迎来到 Whosebug!

下面是如何获取您正在寻找的 table 的示例。

  1. 使用add_nevent()函数获取观察到的事件数的总和。
  2. 曝光次数的总和已经在table(.$table_body)。添加一列 header 以取消隐藏曝光列。
  3. 计算比率,然后分配一个列 header 和一个格式化函数。

编程愉快!

library(gtsummary)
library(tidyverse)

df <- tibble(
  study_arm = c("control", "intervention", "control", "intervention", "control", "intervention", "control", "intervention"),
  events = c(3, 4, 12, 6, 0, 3, 11, 9),
  strata_group = c("A", "A", "A", "A", "B", "B", "B", "B"),
  days_at_risk = c(100, 100, 200, 200, 300, 300, 100, 100)
)

m <- 
  glm(events ~ study_arm + strata_group,
      offset = log(days_at_risk),
      family = poisson(link = "log"),
      data = df
  )

tbl <-
  tbl_regression(m, exponentiate = T) %>%
  # add sum of the number events
  add_nevent(location = "level") %>%
  # add the sum of the exposure times.
  # this column is present in the table by default, but the column is hidden
  # adding the column header unhides the column
  modify_header(exposure ~ "**Exposure**") %>%
  # calculate the rate and add to tbl
  # after the column is added to the table, we need to add
  # a column header and tell gtsummary how to format the new column
  modify_table_body(
    ~.x %>%
      mutate(rate = stat_nevent / exposure, 
             .after = stat_nevent)
  ) %>%
  modify_header(rate ~ "**Rate**") %>%
  modify_fmt_fun(rate ~ partial(style_percent, symbol = TRUE))

reprex package (v2.0.0)

于 2021-07-13 创建