r - 使用 gtsummary 进行泊松回归的风险数字和事件摘要
问题描述
在单变量和多变量泊松回归之后,我有一个 IRR 和 95% CI 的汇总表,它是用 gtsummary 创建的,看起来有点像这样。
对于逻辑回归模型,使用 tbl_summary 创建一些计数数据以附加到表的左侧非常简单。然而,对于泊松模型,我希望能够对风险天数和事件数进行求和,而不是计算。基础数据集的每一行都包含有风险的天数和一些事件,因此回归模型的运行方式如下:
glm(events ~ study_arm + strata_group,
offset = log(days_at_risk),
family=poisson(link = "log"),
data = df)
是否可以使用 gtsummary 创建两列,其中包含事件数的总和以及表中每一行的风险天数?(然后可以使用 tbl_merge 将其添加到我的表中。)
这是我想要实现的更完整的示例
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)
解决方案
您好,欢迎来到stackoverflow!
这是一个如何获取您要查找的表的示例。
- 使用该
add_nevent()
函数获取观察到的事件数的总和。 - 曝光时间的总和已经在表中(
.$table_body
)。添加列标题以取消隐藏曝光列。 - 计算比率,然后分配列标题和格式化函数。
快乐编程!
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 包创建于 2021-07-13 (v2.0.0 )
推荐阅读
- c++ - 如何在 FFmpeg 中指定容器格式?
- jquery - 带有 ajax 的放大弹出窗口;无法关闭按钮或下一个/上一个工作
- c++ - gcc如何找到-lglfw包?
- python - Pygame 和 PGZero - 仍然无法让它工作。目前在 Python 3.6.7 Windows 10 上,使用 Sublime Text
- java - 如何在不修改类中的方法的情况下让方法产生自定义输出?
- javascript - 如何获取 HERE 公共交通 API 的距离和持续时间?
- html - 网站内容在可滚动区域之外水平剪切
- python - python中zip函数的奇怪行为
- html - 如何使用 Spring Boot 插入多个单选按钮值
- django - 如何在日志中查看完整的 Heroku 堆栈跟踪?