2

I have a summary table of IRRs and 95% CI following univariable and multivariable Poisson regression, created with gtsummary that looks a bit like this.

For a logistic regression model it is pretty straightforward to use tbl_summary to create some count data to append to the left of the table. However for a Poisson model I'd like to be able to sum, rather than count, the number of days at risk and the number of events. Each row of the underlying dataset contains a number of days at risk and a number of events, such that the regression model is run like this:

glm(events ~ study_arm + strata_group, 
     offset = log(days_at_risk), 
     family=poisson(link = "log"), 
     data = df)

Is it possible to use gtsummary to create two columns with sums of the number of events and the number of days at risk for each of the rows of the table? (This can then be added to my table using tbl_merge.)

Here is a more complete example of what I'd like to achieve

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)
ophelia
  • 23
  • 4
  • 1
    hi and welcome to SO. It would help to include your data from `df` in the question so others can help. I cannot reproduce the issue you are having and can't offer a suggestion with the current information presented. – Mike Jul 13 '21 at 15:09
  • 1
    this post will demonstrate how to provide relevant information that will help your chances to get an answer : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Mike Jul 13 '21 at 15:10
  • 2
    I've added an example of what I'd like to achieve – ophelia Jul 13 '21 at 18:29

1 Answers1

1

Hi and welcome to stackoverflow!

Here's an example of how to get the table you're looking for.

  1. Use the add_nevent() function to get the sum of the number of observed events.
  2. The sum of the exposure times is already in the table (.$table_body). Add a column header to unhide the exposure coulmn.
  3. Calculate the rate, then assign a column header and a formatting function.

Happy Programming!

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

enter image description here

Created on 2021-07-13 by the reprex package (v2.0.0)

Daniel D. Sjoberg
  • 8,820
  • 2
  • 12
  • 28
  • 1
    Amazing, thanks so much, for others where the final line is a rate rather than a percentage, just replace the last line: `modify_fmt_fun(rate ~ partial(style_number, digits=1))` – ophelia Jul 13 '21 at 21:34