rgtsummaryr-cards

Using multiple by groups with tbl_ard_summary from GTSUMMARY


I am trying to generate table from ARDs generated by CARDS package using GTSUMMARY tbl_ard_summary function. I have successfully generated this using tbl_strata & tbl_summary as follows:

#Summary by TRT & GRADE
trial %>%
  mutate(grade = paste("Grade", grade)) %>%
  tbl_strata(
    strata = trt,
    .tbl_fun = ~.x %>%
      tbl_summary(
        by = grade,
        include = c(age, ttdeath),
        type = c(age, ttdeath) ~ "continuous2",
        statistic = c(age, ttdeath) ~ c("{N_obs}", "{mean} ({sd})",
                                        "{median} ({p25}, {p75})", "{min}, {max}")) %>%
    add_n(),
    .header = "**{strata}**, N = {n}"
    )

I am having trouble creating the same with tbl_ard_summary with multiple groups. I have generated ARDs using following:

#ARD table for AGE, TTDEATH by TRT & GRADE
ard.cont <- ard_continuous(
  trial,
  by = c(trt, grade),
  variables = c(age, ttdeath)) %>%
  bind_ard(
    ard_attributes(
      trial,
      variables = c(trt, grade, age, ttdeath)))

When I try to generate the report table using the ARD above, it's not working. Below is the code I tried.

#Summary from ARD
trial %>% tbl_strata(
  strata = trt,
  ~.x %>%
    tbl_ard_summary(
      cards = ard.cont,
      by = grade,
      include = c(age, ttdeath),
      type = c(age, ttdeath) ~ "continuous2",
      statistic = c(age, ttdeath) ~ c("{N}", "{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}")
    ))

Is it feasible to generate this from ARDs currently? Any help is really appreciated!


Solution

  • There is an open issue about this, and should be addressed shortly. https://github.com/ddsjoberg/gtsummary/issues/1852

    In the meantime, the example below includes two ways to use tbl_strata() with ARDs. Hope it helps!

    library(gtsummary)
    library(cards)
    
    # build ARD outside the `tbl_strata()` function
    ard.cont <- 
      trial %>%
      mutate(grade = paste("Grade", grade)) %>%
      ard_continuous(
        by = c(grade, trt),
        variables = c(age, ttdeath)
      )
    
    ard.cont |>
      tbl_strata(
        strata = group2_level,
        ~.x %>%
          dplyr::select(-all_ard_group_n(2)) |> 
          tbl_ard_summary(
            by = grade,
            include = c(age, ttdeath),
            type = c(age, ttdeath) ~ "continuous2",
            statistic = c(age, ttdeath) ~ c("{N}", "{mean} ({sd})", "{median} ({p25}, {p75})", "{min}, {max}"),
            label = list(age = "Age", ttdeath = "Time to Death")
          ) 
      )
    
    
    
    # build ARD in the `tbl_strata()` function
    trial %>%
      mutate(grade = paste("Grade", grade)) %>%
      tbl_strata(
        strata = trt,
        .tbl_fun = ~.x |> 
          ard_stack(
            .by = grade, 
            ard_continuous(variables = c(age, ttdeath)),
            .attributes = TRUE, 
            .missing = TRUE,
            .total_n = TRUE
          ) |> 
          tbl_ard_summary(
            by = grade,
            include = c(age, ttdeath),
            type = c(age, ttdeath) ~ "continuous2",
            statistic = c(age, ttdeath) ~ c("{N_obs}", "{mean} ({sd})",
                                            "{median} ({p25}, {p75})", "{min}, {max}")
          ),
        .header = "**{strata}**, N = {n}"
      )
    

    enter image description here