将事件和后续时间添加到tbl_uvregression

问题描述

我最近开始使用{gtsummary}包来对回归输出进行制表,发现它真的很有帮助!

我遇到的一个问题与生存分析数据的呈现有关。

我想列出每个分类变量每个级别的事件数和随访时间。

df <- data.frame(
  time = sample(1:1000,100,replace = TRUE),event = sample(0:1,gender = sample(c("M","F"),size = 100,stringsAsFactors = FALSE
)

tbl_uvregression(
  df,method = survival::coxph,y = survival::Surv(time = time,event = event)
)

认情况下,输出包括一列N,该列引用模型中每个变量包含的主题数。我希望这样,并为分类变量gender的每个级别分配并提供后续时间。

可以使用{gtsummary}软件包完成此操作,还是需要单独计算并以某种方式将其合并到表中?

非常感谢!

解决方法

这是gtsummary在github上的一个未解决问题。一种使用gtsummary的解决方案可以提供一定数量的事件:

tbl_survfit(survfit(Surv(time,event) ~gender,df),times = c(50,100)) %>% 
  add_nevent() %>% 
  add_n()
,

计划将add_n()add_nevent()更新为包含一个参数,以选择性地将Ns直接添加到分类变量的级别。

同时,您可以使用这些计数构建tbl_summary()表,并将它们与Cox模型结果合并。下面的示例,gtsummary网站(http://www.danieldsjoberg.com/gtsummary/articles/gallery.html#regression-tables-1)上的表库中也有类似的示例。

library(gtsummary)
library(dplyr)

df <- data.frame(
  time = sample(1:1000,100,replace = TRUE),event = sample(0:1,gender = sample(c("M","F"),size = 100,stringsAsFactors = FALSE
)

# calculate the total N by gender
tbl_n <- df %>% select(gender) %>% 
  tbl_summary(statistic = everything() ~ "{n}") %>%
  modify_header(stat_0 ~ "**N**") %>%
  modify_footnote(everything() ~ NA)

# calculate the event N by gender
tbl_nevent <- df %>% filter(event == 1) %>% select(gender) %>% 
  tbl_summary(statistic = everything() ~ "{n}") %>%
  modify_header(stat_0 ~ "**Event N**")  %>%
  modify_footnote(everything() ~ NA)

# build cox models
tbl_cox <-
  tbl_uvregression(
  df,method = survival::coxph,y = survival::Surv(time = time,event = event),exponentiate = TRUE,hide_n = TRUE
)

# merge tbls together
tbl_merge(list(tbl_n,tbl_nevent,tbl_cox)) %>%
  modify_spanning_header(everything() ~ NA)

enter image description here

我还没有考虑过为这些表格增加随访时间的方法。但是我会考虑最好的实现。就像通过@Mike提到的tbl_survfit()函数家族一样。