Skip to content

[Bug]: analyze_vars_in_cols fails when combining mean_ci with max #1468

@KOBANAK

Description

@KOBANAK

What happened?

I have encountered an issue where analyze_vars_in_cols works as expected when .stats includes mean_ci, but throws an error if max is appended to the .stats list.
I have used Data Setup from : https://insightsengineering.github.io/tlg-catalog/stable/tables/pharmacokinetic/pkct01.html

library(dplyr)
library(tern)

adsl <- random.cdisc.data::cadsl %>%
  filter(ACTARM == "A: Drug X")
adpc <- random.cdisc.data::cadpc %>%
  filter(ACTARM == "A: Drug X", PARAM == "Plasma Drug X")

# Setting up the data
adpc_1 <- adpc %>%
  mutate(
    NFRLT = as.factor(NFRLT),
    AVALCAT1 = as.factor(AVALCAT1),
    NOMTPT = as.factor(paste(NFRLT, "/", PCTPT))
  ) %>%
  select(NOMTPT, ACTARM, VISIT, AVAL, PARAM, AVALCAT1)

adpc_1$NOMTPT <- factor(
  adpc_1$NOMTPT,
  levels = levels(adpc_1$NOMTPT)[order(as.numeric(gsub(".*?([0-9\\.]+).*", "\\1", levels(adpc_1$NOMTPT))))]
)

# Row structure
lyt_rows <- basic_table() %>%
  split_rows_by(
    var = "ACTARM",
    split_fun = drop_split_levels,
    split_label = "Treatment Group",
    label_pos = "topleft"
  ) %>%
  add_rowcounts(alt_counts = TRUE) %>%
  split_rows_by(
    var = "VISIT",
    split_fun = drop_split_levels,
    split_label = "Visit",
    label_pos = "topleft"
  ) %>%
  split_rows_by(
    var = "NOMTPT",
    split_fun = drop_split_levels,
    split_label = "Nominal Time (hr) / Timepoint",
    label_pos = "topleft",
    child_labels = "hidden"
  )

In the first case, the table builds correctly:

lyt <- lyt_rows %>%
  analyze_vars_in_cols(
    vars = c("AVAL", "AVALCAT1", rep("AVAL", 8)),
    .stats = c("n", "n_blq", "mean", "sd", "cv", "geom_mean", "geom_cv", "median", "min", "mean_ci"),
    .formats = c(
      n = "xx.", n_blq = "xx.", mean = format_sigfig(3), sd = format_sigfig(3), cv = "xx.x", 
      median = format_sigfig(3), geom_mean = format_sigfig(3), geom_cv = "xx.x", min = format_sigfig(3)
    ),
    .labels = c(
      n = "n", n_blq = "Number\nof\nLTRs/BLQs", mean = "Mean", sd = "SD", cv = "CV (%) Mean",
      geom_mean = "Geometric Mean", geom_cv = "CV % Geometric Mean", median = "Median", min = "Minimum"
    ),
    na_str = "NE"
  )

result <- build_table(lyt, df = adpc_1, alt_counts_df = adsl) %>% prune_table()

However, adding "max" and updating the variable count causes a failure:

lyt <- lyt_rows %>%
  analyze_vars_in_cols(
    vars = c("AVAL", "AVALCAT1", rep("AVAL", 9)),
    .stats = c("n", "n_blq", "mean", "sd", "cv", "geom_mean", "geom_cv", "median", "min", "mean_ci", "max"),
    .formats = c(
      n = "xx.", n_blq = "xx.", mean = format_sigfig(3), sd = format_sigfig(3), cv = "xx.x", 
      median = format_sigfig(3), geom_mean = format_sigfig(3), geom_cv = "xx.x", 
      min = format_sigfig(3), max = format_sigfig(3)
    ),
    .labels = c(
      n = "n", n_blq = "Number\nof\nLTRs/BLQs", mean = "Mean", sd = "SD", cv = "CV (%) Mean",
      geom_mean = "Geometric Mean", geom_cv = "CV % Geometric Mean", median = "Median", 
      min = "Minimum", max = "Maximum"
    ),
    na_str = "NE"
  )
result <- build_table(lyt, df = adpc_1, alt_counts_df = adsl) %>% prune_table()

Error: Error applying analysis function (var - NA): missing value where TRUE/FALSE needed
occured at (row) path: ACTARM[A: Drug X]->VISIT[Day 1]->NOMTPT[0 / Predose]

Any advice on how to resolve this error so I can include all statistics would be greatly appreciated. Thanks for your help!

sessionInfo()

R version 4.4.1 (2024-06-14)
Platform: wasm32-unknown-emscripten (32-bit)
Running under: emscripten

Matrix products: default


locale:
[1] fr_FR.UTF-8 C           fr_FR.UTF-8 fr_FR.UTF-8 fr_FR.UTF-8 C          

time zone: Europe/Paris
tzcode source: internal

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

other attached packages:
[1] dplyr_1.1.4              random.cdisc.data_0.3.16 tern_0.9.8              
[4] rtables_0.6.12           magrittr_2.0.3           formatters_0.5.11       
[7] htmltools_0.5.8.1        knitr_1.50               evaluate_1.0.3          

loaded via a namespace (and not attached):
 [1] Matrix_1.7-3     gtable_0.3.6     tidyselect_1.2.1 stringr_1.5.1   
 [5] webr_0.4.2       tidyr_1.3.1      splines_4.4.1    scales_1.3.0    
 [9] fastmap_1.2.0    lattice_0.22-7   ggplot2_3.5.2    R6_2.6.1        
[13] generics_0.1.3   rbibutils_2.3    backports_1.5.0  checkmate_2.3.2 
[17] tibble_3.2.1     munsell_0.5.1    pillar_1.10.2    rlang_1.1.6     
[21] broom_1.0.8      stringi_1.8.7    xfun_0.52        cli_3.6.4       
[25] withr_3.0.2      Rdpack_2.6.4     digest_0.6.37    grid_4.4.1      
[29] lifecycle_1.0.4  vctrs_0.6.5      glue_1.8.0       nestcolor_0.1.3 
[33] survival_3.8-3   colorspace_2.1-1 purrr_1.0.4      tools_4.4.1     
[37] pkgconfig_2.0.3

Relevant log output

Code of Conduct

  • I agree to follow this project's Code of Conduct.

Contribution Guidelines

  • I agree to follow this project's Contribution Guidelines.

Security Policy

  • I agree to follow this project's Security Policy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions