insightsengineering / cards

CDISC Analysis Results Data
https://insightsengineering.github.io/cards/
24 stars 0 forks source link

Order of the AE grades isn't in the order of the variable #221

Closed Nolan-Steed closed 2 months ago

Nolan-Steed commented 3 months ago

When I generate an ard for some reason it orders the adverse event grades as 1, 3, 2. shown below. This variable is a factor and the levels are in the correct order 1, 2, 3.

Here is an example

library(dplyr)
library(cards)
library(rmarkdown)

adae <- cards::ADAE |> 
  dplyr::mutate(TRTA = factor(TRTA),
                AETOXGR = factor(case_when(
                  AESEV == 'MILD' ~ '1',
                  AESEV == 'MODERATE' ~ '2',
                  AESEV == 'SEVERE' ~ '3'
                ), levels = c('1', '2', '3')),
                GRD_GRP = factor(case_when(
                  AESEV == 'MILD' | AESEV == 'MODERATE' ~ 'Grade 1-2',
                  AESEV == 'SEVERE' ~ 'Grade 3'
                ), levels = c('Grade 1-2', 'Grade 3'))) |> 
  filter(
    .by = c(TRTA, AEBODSYS),
    AEBODSYS %in% c("CARDIAC DISORDERS", "EYE DISORDERS"),
    AEDECOD %in% AEDECOD[1:3]
  )  |> 
  select(USUBJID, TRTA, AEBODSYS, AEDECOD, AETOXGR, GRD_GRP)

adae$TOTAL_VAR <- "- Any adverse events - "

card_ae_grd <-
  adae |> 
  # keep on AE per SOC
  slice(1L, .by = c(USUBJID, TRTA, AETOXGR)) |> 
  cards::ard_hierarchical(
    by = TRTA, 
    variables = AETOXGR,
    denominator = adsl
  ) |> 
  filter(stat_name %in% c("n", "p")) 
  # If you uncomment these 2 lines it seems to fix the issue 
  # |> 
  # arrange(unlist(variable_level))

card_ae_grd