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

Viewed 95

All the variables other than "termx" are "0", "1". "1" represents "Yes" or "Exposed" I would want to generate a table with prevalence /10,000 (Wilson's 95% CI) by the variable "termx"

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"

I would like the table to look like this:

| 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) | 
2 Answers

The gtsummary package can assist here. To get the rates per 10000 people, you need to provide a function to round the percentages and scale them 10,000 people. Example below!

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)

Created on 2021-11-15 by the reprex package (v2.0.1)

Want to add n for each column as in picture attached

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)")

Want to add number as indicated in the picture

Related