I would like to add a column to my gtsummary::tbl_regression output object called 'Incidence Rate' which is calculated using the 'N' and 'Event N' columns already produced using the gtsummary::add_n and gtsummary::add_nevent functions.
I have attempted this in a few ways including trying to use the gtsummary::add_stat() function, but this does not seem to work with tbl_regression objects. Having tried to calculate the column manually with dplyr::mutate() and the tbl_regression object's '_data' element, I've been unable to get this to appear in the final output. add_stat example, which doesn't work:
add_nevent_ex <-
glm(response ~ trt, trial, family = binomial) %>%
tbl_regression() %>%
add_n() %>%
add_nevent() %>%
add_stat(fns = list(IncidenceRate = Event N / N))
I managed to accomplish this in the end by doing the following:
add_nevent_ex <-
glm(response ~ trt, trial, family = binomial) %>%
tbl_regression() %>%
add_n(location = "level") %>%
add_nevent(location = "level") %>%
modify_table_body(
~ .x %>%
dplyr::mutate(incidence_rate = (n_event / n_obs)*1000))
%>%
# assigning header labels
modify_header(incidence_rate = "**Rate Per 1,000**")
This applies the rate on the 'levels' of the model as opposed to the 'labels'. If you wished to modify this to apply to the 'labels', just replace dplyr::mutate(incidence_rate = (n_event / n_obs)*1000))
with dplyr::mutate(incidence_rate = (N_event / N_obs)*1000))
.
Many thanks to the package's author, Daniel D. Sjoberg, for the help.