ddsjoberg / gtsummary

Presentation-Ready Data Summary and Analytic Result Tables
http://www.danieldsjoberg.com/gtsummary
Other
1.05k stars 121 forks source link

`tbl_ard_summary()` with mixed-type `by` variables has unintuitive results: check for mixed type and message users? #2000

Open ddsjoberg opened 3 weeks ago

ddsjoberg commented 3 weeks ago
library(survival)
library(cards)
library(gtsummary)
packageVersion("cards")
#> [1] '0.2.2.9009'

bind_ard(
  ard_continuous(trial, variables = age, by = trt),
  survfit(Surv(ttdeath, death) ~ trt, trial) |> 
    cardx::ard_survival_survfit(times = c(12, 24)) |> 
    dplyr::mutate(context = "categorical") # this will no loner be needed later (handled in another issue)
) |> 
  tbl_ard_summary(
    by = trt,
    statistic = time ~ "{estimate}% (95% CI {conf.low}%, {conf.high}%)"
  ) |> 
  as_kable()
Characteristic Drug A Drug B 1 2
age 46.0 (37.0, 60.0) 48.0 (39.0, 56.0)
time
12 0.9% (95% CI 0.9%, 1.0%) 0.9% (95% CI 0.8%, 0.9%)
24 0.5% (95% CI 0.4%, 0.6%) 0.4% (95% CI 0.3%, 0.5%)

Created on 2024-09-25 with reprex v2.1.1

ddsjoberg commented 3 weeks ago

So.....this is actually correct behaviour. The survival results convert the trt levels to factors, and as such, they are being recognized as different levels.

But if I found this confusing, then others will as well. I wonder if there is some kind of message we could pass to users, if the by levels are mixed type?? 🤔