尝试通过分类变量生成具有 prevalence/10,000 和 95% Wilson 置信区间的 table

Trying to generate a table with prevalence/10,000 with 95% Wilson confidence interval by categorical variable

除“termx”以外的所有变量均为“0”、“1”。 “1”代表“是”或“暴露” 我想通过变量“termx”

生成一个 table 的流行度为 /10,000(Wilson 的 95% CI)
str(b.pcalc)
tibble [141,648 x 5] (S3: tbl_df/tbl/data.frame)
$ pcalc_anenc  : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ pcalc_ence   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ pcalc_spin   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ pcalc_micr   : Factor w/ 2 levels "0","1": 1 1 1 1 1 1 1 1 1 1 ...
$ termx        : Factor w/ 7 levels "39-40 weeks",..: 5 5 1 5 4 5 5 5 5 5 ...
..- attr(*, "label")= chr "Pregnancy Terms"

我希望 table 看起来像这样:

| Birth defects |   <28 weeks | 28 to <32 weeks | 32 to <37 weeks | 37-38 weeks | 39-40 weeks | 41-<42 weeks | 42+ weeks | 
| ------------- |   --------- | --------------- | --------------- | ----------- | ----------- | ------------ | --------- |
| pcalc_anenc | 15.5 (2.7,87.0) | 19.7 (7.7,50.6) | 3.2 (1.4,7.5) | 0.2 (0.0,1.1) | 0.2 (0.0,1.1) | 0.0 (0.0,3.0) | 2.7 (0.7,10.0) | 
| pcalc_ence | 0.0 (0.0,59.0) | 4.9 (0.9,27.9) | 2.6 (1.0,6.6) | 1.7 (0.9,3.2) | 0.2 (0.0,1.1) | 0.0 (0.0,3.0) | 0.0 (0.0,5.2) | 
| pcalc_spin | 15.5 (2.7,87.0) | 4.9 (0.9,27.9) | 9.0 (5.4,15.1) | 4.9 (3.4,7.2) | 3.2 (1.9,5.1) | 1.6 (0.4,5.8) | 2.7 (0.7,10.0) | 

gtsummary 包在这里可以提供帮助。要获得每 10000 人的比率,您需要提供一个函数来四舍五入百分比并将它们扩展到 10000 人。示例如下!

library(gtsummary)
#> #BlackLivesMatter
packageVersion("gtsummary")
#> [1] '1.5.0.9003'

trial %>%
  select(response, death, trt) %>%
  tbl_summary(
    by = trt, 
    missing = "no", 
    statistic = everything() ~ "{p}",
    digits = everything() ~ purrr::partial(style_number, scale = 10000)
  ) %>%
  add_ci(
    method = everything() ~ "wilson",
    statistic = everything() ~ "{conf.low}, {conf.high}",
    style_fun = everything() ~ purrr::partial(style_number, scale = 10000),
  ) %>%
  modify_cols_merge("{stat_1} ({ci_stat_1})") %>%
  modify_cols_merge("{stat_2} ({ci_stat_2})") %>%
  as_kable() # convert to kable to display on SO
Characteristic Drug A, N = 98 Drug B, N = 102
Tumor Response 2,947 (2,079, 3,984) 3,367 (2,463, 4,401)
Patient Died 5,306 (4,276, 6,312) 5,882 (4,863, 6,834)

reprex package (v2.0.1)

于 2021-11-15 创建

如附图所示,想为每一列添加n

tbl_prev1 <- select(pcalc_anenc, pcalc_ence, pcalc_spin, termx) %>%
tbl_summary(
by = termx, 
missing = "no", 
statistic = everything() ~ "{p}",
digits = everything() ~ purrr::partial(style_number, scale = 10000, digits=1)
) %>%
add_ci(
method = everything() ~ "wilson.no.correct",
statistic = everything() ~ "{conf.low}, {conf.high}",
style_fun = everything() ~ purrr::partial(style_number, scale = 10000, digits=1)
) %>%
modify_cols_merge("{stat_1} ({ci_stat_1})") %>%
modify_cols_merge("{stat_2} ({ci_stat_2})") %>%
modify_cols_merge("{stat_3} ({ci_stat_3})") %>%  
modify_cols_merge("{stat_4} ({ci_stat_4})") %>%
modify_cols_merge("{stat_5} ({ci_stat_5})") %>% 
modify_cols_merge("{stat_6} ({ci_stat_6})") %>% 
modify_cols_merge("{stat_7} ({ci_stat_7})") %>%
modify_header(update = all_stat_cols() ~ "**{level}**, Prev (95% CI)") %>%
modify_footnote(update = starts_with("stat_") ~
"Prev=Birth Prevalence/10,000 births, (95% CI)")