Reputation: 163
I would like to add the prevalence of an outcome to a univariable regression table generated with gtsummary::tbl_uvregression. Is there a way to do this without converting the tbl_uvregression object to a table and calculating it with the dplyr::mutate function as in the example below?
PS: Daniel Sjoberg, thank you for a very helpful package!
options(digits = 3)
library(gtsummary)
#> #BlackLivesMatter
library(tidyverse)
library(knitr)
library(geepack)
packageVersion("gtsummary")
#> [1] '1.5.1'
data(dietox, package = "geepack")
dietox %>%
mutate(big_pig = if_else(Weight > 50, 1, 0)) %>%
select(big_pig, Pig, Evit, Cu, Litter) %>%
tbl_uvregression(
y = big_pig,
method = geeglm,
method.args = list(id = Pig, family = poisson, corstr = "independence"),
include = -Pig,
exponentiate = TRUE
) %>%
add_n(location = c("level", "label")) %>%
add_nevent(location = c("level", "label")) %>%
as_tibble() %>%
mutate(Prevalence = (`**Event N**` / as.integer(`**N**`))*100) %>%
select(`**Characteristic**`,`**N**`,`**Event N**`,Prevalence, everything()) %>%
kable()
Characteristic | N | Event N | Prevalence | IRR | 95% CI | p-value |
---|---|---|---|---|---|---|
Evit | 861 | 519 | 60.3 | NA | NA | NA |
Evit000 | 276 | 167 | 60.5 | NA | NA | NA |
Evit100 | 286 | 178 | 62.2 | 1.03 | 0.95, 1.12 | 0.5 |
Evit200 | 299 | 174 | 58.2 | 0.96 | 0.88, 1.05 | 0.4 |
Cu | 861 | 519 | 60.3 | NA | NA | NA |
Cu000 | 274 | 164 | 59.9 | NA | NA | NA |
Cu035 | 300 | 176 | 58.7 | 0.98 | 0.91, 1.06 | 0.6 |
Cu175 | 287 | 179 | 62.4 | 1.04 | 0.96, 1.13 | 0.3 |
Litter | 861 | 519 | 60.3 | NA | NA | NA |
1 | 48 | 31 | 64.6 | NA | NA | NA |
2 | 36 | 25 | 69.4 | 1.08 | 0.99, 1.17 | 0.091 |
3 | 48 | 25 | 52.1 | 0.81 | 0.64, 1.02 | 0.077 |
4 | 48 | 29 | 60.4 | 0.94 | 0.86, 1.01 | 0.10 |
5 | 36 | 25 | 69.4 | 1.08 | 0.90, 1.28 | 0.4 |
6 | 48 | 31 | 64.6 | 1.00 | 0.93, 1.08 | >0.9 |
7 | 24 | 12 | 50.0 | 0.77 | 0.61, 0.98 | 0.035 |
8 | 48 | 28 | 58.3 | 0.90 | 0.75, 1.08 | 0.3 |
9 | 33 | 18 | 54.5 | 0.84 | 0.72, 0.99 | 0.043 |
10 | 36 | 24 | 66.7 | 1.03 | 0.98, 1.09 | 0.3 |
11 | 36 | 20 | 55.6 | 0.86 | 0.78, 0.95 | 0.002 |
12 | 36 | 21 | 58.3 | 0.90 | 0.78, 1.04 | 0.2 |
13 | 36 | 21 | 58.3 | 0.90 | 0.86, 0.95 | <0.001 |
16 | 48 | 32 | 66.7 | 1.03 | 0.93, 1.14 | 0.5 |
17 | 36 | 24 | 66.7 | 1.03 | 0.98, 1.09 | 0.3 |
18 | 36 | 21 | 58.3 | 0.90 | 0.86, 0.95 | <0.001 |
20 | 48 | 30 | 62.5 | 0.97 | 0.89, 1.05 | 0.5 |
21 | 48 | 30 | 62.5 | 0.97 | 0.79, 1.19 | 0.8 |
22 | 48 | 27 | 56.2 | 0.87 | 0.74, 1.03 | 0.11 |
23 | 48 | 24 | 50.0 | 0.77 | 0.63, 0.95 | 0.016 |
24 | 36 | 21 | 58.3 | 0.90 | 0.86, 0.95 | <0.001 |
Created on 2022-02-11 by the reprex package (v2.0.1)
Upvotes: 3
Views: 372
Reputation: 11595
A gtsummary table contains an internal data frame called .$table_body
, and you can modify that data frame directly using the modify_table_body()
function. In the example below, I calculate the prevalence and format the results to display in the table. I also expose the already existing column exposure
, which is the sum of the exposure/followup time. Happy Programming!
library(gtsummary)
library(tidyverse)
library(geepack)
packageVersion("gtsummary")
#> [1] '1.5.2'
data(dietox, package = "geepack")
tbl <-
dietox %>%
mutate(big_pig = if_else(Weight > 50, 1, 0)) %>%
select(big_pig, Pig, Evit, Cu) %>%
tbl_uvregression(
y = big_pig,
method = geeglm,
method.args = list(id = Pig, family = poisson, corstr = "independence"),
include = -Pig,
exponentiate = TRUE
) %>%
add_n(location = c("level", "label")) %>%
add_nevent(location = c("level", "label")) %>%
# calculate prev
modify_table_body(
~.x %>%
mutate(
prev = stat_nevent / stat_n * 100
) %>%
relocate(prev, exposure, .after = stat_nevent)
) %>%
# format the newly added stats
modify_header(prev = "**Prevalence**", exposure = "**Total Exposure Time**") %>%
modify_fmt_fun(list(prev = function(x) paste0(style_sigfig(x), "%"),
exposure = style_sigfig))
Created on 2022-02-11 by the reprex package (v2.0.1)
Upvotes: 3