insightsengineering / tern

Table, Listings, and Graphs (TLG) library for common outputs used in clinical trials
https://insightsengineering.github.io/tern/
Other
77 stars 22 forks source link

[Bug]: summarize_ancova.R: fix sum_contrasts_level statement (line 170 in summarize_ancova.R/function s_ancova) #1168

Closed augi-be closed 10 months ago

augi-be commented 10 months ago

What happened?

A bug happened! When treatment group name is a substring of other group (eg "Treatment" , and "Treatment subgroup") or contain special characters (like + or () the following statement does not result in the desired contrast level selection. sum_contrasts_level <- sum_contrasts[grepl(sum_level, sum_contrasts$contrast), ] In the first situation (see anl2 in the below code), it will return a dataframe of 2 rows, rather than just 1. In the latter (see anl3 in the below code), sum_contrasts_level will have 0 rows.

library(scda)
library(rtables)
library(dplyr)
library(tern)

adsl <- synthetic_cdisc_data("latest")$adsl
adqs <- synthetic_cdisc_data("latest")$adqs

adsl$ARMCD2 <- factor(adsl$ARMCD
                      ,levels=c("ARM A","ARM B","ARM C")
                      ,labels=c("ARM A","ARM A Subgroup","ARM C"))

adqs$ARMCD2 <- factor(adqs$ARMCD
                      ,levels=c("ARM A","ARM B","ARM C")
                      ,labels=c("ARM A","ARM A Subgroup","ARM C"))

adsl$ARMCD3 <- factor(adsl$ARMCD
                      ,levels=c("ARM A","ARM B","ARM C")
                      ,labels=c("ARM A","ARM B (x)","ARM C"))

adqs$ARMCD3 <- factor(adqs$ARMCD
                      ,levels=c("ARM A","ARM B","ARM C")
                      ,labels=c("ARM A","ARM B (x)","ARM C"))

adqs_single <- adqs %>%
  filter(
    AVISIT == "WEEK 1 DAY 8", # single time point
    PARAMCD == "FKSI-FWB" # single end point
  ) %>%
  mutate(CHG = ifelse(BMEASIFL == "Y", CHG, NA)) # only analyze evaluable population

unique(adqs_single$ARMCD)
unique(adqs_single$ARMCD2)
unique(adqs_single$ARMCD3)

anl1 <- basic_table() %>%
  split_cols_by("ARMCD", ref_group = "ARM C") %>%
  add_colcounts() %>%
  summarize_ancova(
    vars = "CHG",
    variables = list(arm = "ARMCD", covariates = NULL),
    table_names = "unadj",
    conf_level = 0.95, var_labels = "Unadjusted comparison",
    .labels = c(lsmean = "Mean", lsmean_diff = "Difference in Means")
  ) %>% 
  build_table(adqs_single, alt_counts_df = adsl)

anl1

anl2 <- basic_table() %>%
  split_cols_by("ARMCD2", ref_group = "ARM C") %>%
  add_colcounts() %>%
  summarize_ancova(
    vars = "CHG",
    variables = list(arm = "ARMCD2", covariates = NULL),
    table_names = "unadj",
    conf_level = 0.95, var_labels = "Unadjusted comparison",
    .labels = c(lsmean = "Mean", lsmean_diff = "Difference in Means")
  ) %>% 
  build_table(adqs_single, alt_counts_df = adsl)

anl2

anl3 <- basic_table() %>%
  split_cols_by("ARMCD3", ref_group = "ARM C") %>%
  add_colcounts() %>%
  summarize_ancova(
    vars = "CHG",
    variables = list(arm = "ARMCD3", covariates = NULL),
    table_names = "unadj",
    conf_level = 0.95, var_labels = "Unadjusted comparison",
    .labels = c(lsmean = "Mean", lsmean_diff = "Difference in Means")
  ) %>% 
  build_table(adqs_single, alt_counts_df = adsl)

anl3

sessionInfo()

other attached packages:
[1] tern_0.9.2                      rtables_0.6.5                  
[3] magrittr_2.0.3                  formatters_0.5.4               
[5] scda_0.1.6                      dplyr_1.1.3

Relevant log output

Error in format_value(rawvalues(x), format = format, output = output,  : 
  cell <0.5917492876519231.97468593831888> and format xx.xx are of different length

anl3

Difference in Means in 3rd column is not displayed:

                        ARM C        ARM A       ARM B (x)
                        (N=132)      (N=134)       (N=134) 
———————————————————————————————————————————————————————————
Unadjusted comparison                                      
  n                       62           68            73    
  Mean                   3.09         3.68          5.07   
  Difference in Means                 0.59                 
    95% CI                        (-3.73, 4.91)            
    p-value                          0.7873

Code of Conduct

Contribution Guidelines

Security Policy