insightsengineering / tern

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

[Bug]: summarize_ancova/s_ancova #771

Closed augi-be closed 1 year ago

augi-be commented 1 year ago

What happened?

A bug happened! For the confidence interval for difference in lsmeans, always the 95% level is presented, despite the label showing the level passed into parameter conf_level.

This is happening in the function s_ancova, where the conf_level parameter is not passed onto the parameter level in the emmeans::contrast function call.

Corrected code would look like this: emmeans_contrasts <- emmeans::contrast(emmeans_fit, method = "trt.vs.ctrl", ref = ref_key,level = conf_level)

sessionInfo()

R version 4.2.1 (2022-06-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

Matrix products: default
BLAS/LAPACK: /usr/lib64/libopenblasp-r0.3.3.so

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C               LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8    LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C             LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] scda_0.1.5                   scda.2021_0.1.5              teal.modules.general_0.2.15 
 [4] shinyTree_0.2.7              ggmosaic_0.3.3               ggplot2_3.4.0               
 [7] teal.modules.clinical_0.8.14 teal_0.12.0                  teal.transform_0.2.0        
[10] teal.data_0.1.2              shiny_1.7.3                  tidyr_1.2.1                 
[13] stringr_1.4.1                haven_2.5.1                  tern_0.7.10                 
[16] rtables_0.5.3                formatters_0.3.4             magrittr_2.0.3              
[19] dplyr_1.0.10                

loaded via a namespace (and not attached):
  [1] TH.data_1.1-1       colorspace_2.0-3    ellipsis_0.3.2      rsconnect_0.8.28    estimability_1.4.1 
  [6] rstudioapi_0.14     farver_2.1.1        ggrepel_0.9.2       DT_0.26             fansi_1.0.3        
 [11] mvtnorm_1.1-3       codetools_0.2-18    splines_4.2.1       R.methodsS3_1.8.2   cachem_1.0.6       
 [16] knitr_1.40          jsonlite_1.8.3      broom_1.0.1         R.oo_1.25.0         readr_2.1.3        
 [21] compiler_4.2.1      httr_1.4.4          emmeans_1.8.2       backports_1.4.1     assertthat_0.2.1   
 [26] Matrix_1.5-1        fastmap_1.1.0       lazyeval_0.2.2      cli_3.4.1           later_1.3.0        
 [31] htmltools_0.5.3     tools_4.2.1         coda_0.19-4         gtable_0.3.1        glue_1.6.2         
 [36] Rcpp_1.0.9          carData_3.0-5       jquerylib_0.1.4     styler_1.8.1        vctrs_0.5.0        
 [41] teal.logger_0.1.1   crosstalk_1.2.0     xfun_0.34           rbibutils_2.2.9     mime_0.12          
 [46] lifecycle_1.0.3     sparkline_2.0       MASS_7.3-57         zoo_1.8-11          scales_1.2.1       
 [51] ragg_1.2.4          hms_1.1.2           promises_1.2.0.1    sandwich_3.0-2      yaml_2.3.6         
 [56] memoise_2.0.1       teal.slice_0.2.0    teal.widgets_0.2.0  sass_0.4.2          stringi_1.7.8      
 [61] checkmate_2.1.0     teal.reporter_0.1.1 Rdpack_2.4          rlang_1.0.6         pkgconfig_2.0.3    
 [66] systemfonts_1.0.4   evaluate_0.18       lattice_0.20-45     teal.code_0.2.0     fontawesome_0.4.0  
 [71] purrr_0.3.5         htmlwidgets_1.5.4   labeling_0.4.2      tidyselect_1.2.0    logger_0.2.2       
 [76] R6_2.5.1            generics_0.1.3      multcomp_1.4-20     DBI_1.1.3           pillar_1.8.1       
 [81] withr_2.5.0         survival_3.4-0      abind_1.4-5         tibble_3.1.8        crayon_1.5.2       
 [86] car_3.1-1           shinyWidgets_0.7.4  utf8_1.2.2          plotly_4.10.1       tzdb_0.3.0         
 [91] rmarkdown_2.18      grid_4.2.1          data.table_1.14.4   forcats_0.5.2       digest_0.6.30      
 [96] xtable_1.8-4        R.cache_0.16.0      httpuv_1.6.6        R.utils_2.12.1      textshaping_0.3.6  
[101] munsell_0.5.0       viridisLite_0.4.1   bslib_0.4.1         shinyjs_2.1.0

Relevant log output

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

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

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

ancova_variables <- list(arm = "ARMCD", covariates = c("BASE", "STRATA1"))

lty <- basic_table() %>%
  split_cols_by("ARMCD", ref_group = "ARM A") %>%
  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")
  ) %>%
  summarize_ancova(
    vars = "CHG",
    variables = ancova_variables,
    table_names = "adj",
    conf_level = 0.95, var_labels = "Adjusted comparison (covariates BASE and STRATA1) with 95% CI"
  ) %>%
  summarize_ancova(
    vars = "CHG",
    variables = ancova_variables,
    table_names = "adj2",
    conf_level = 0.90, var_labels = "Adjusted comparison (covariates BASE and STRATA1) with 90% CI",
    .stats = c("lsmean_diff_ci")
  ) 

result <- lty %>% build_table(adqs_single, alt_counts_df = adsl)

as_html(result)

Code of Conduct

Contribution Guidelines

Security Policy

shajoezhu commented 1 year ago

Hi @JiaLiu0001 , can you have a look at this one please?

JiaLiu0001 commented 1 year ago

Yes, this is a bug in s_ancova. conf_level should be included in emmeans::contrast() to pass the conf_level from parameters. Thank you @iaugustypers Will get it fixed.