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

Refactor `estimate_incidence_rate` #1300

Closed edelarua closed 2 months ago

edelarua commented 2 months ago

Pull Request

Fixes #1299

edelarua commented 2 months ago

Example with similar structure to AET02_PTYR created using refactored function:

library(tern)

adsl <- random.cdisc.data::cadsl %>% df_explicit_na()
adaette <- random.cdisc.data::cadaette %>% df_explicit_na()

anl <- adaette %>%
  dplyr::filter(PARAM == "Time to first occurrence of any adverse event") %>%
  dplyr::mutate(n_events = 1 - CNSR)

lyt <- basic_table(show_colcounts = TRUE) %>%
  split_cols_by("ARM") %>%
  estimate_incidence_rate(
    vars = "AVAL",
    n_events = "n_events",
    .stats = c("person_years", "n_events", "rate")
  ) %>%
  split_rows_by("SEX", child_labels = "visible") %>%
  estimate_incidence_rate(
    vars = "AVAL",
    n_events = "n_events",
    .stats = c("n_unique", "n_rate"),
    summarize = TRUE,
    label_fmt = "%.labels"
  ) %>%
  split_rows_by("STRATA1") %>%
  estimate_incidence_rate(
    vars = "AVAL",
    n_events = "n_events",
    .stats = "n_rate",
    summarize = TRUE,
    label_fmt = "%s"
  )

result <- lyt %>% build_table(anl, alt_counts_df = adsl)

result
                                                                      A: Drug X   B: Placebo   C: Combination
                                                                       (N=134)     (N=134)        (N=132)    
—————————————————————————————————————————————————————————————————————————————————————————————————————————————
Total patient-years at risk                                             162.4       103.9          172.6     
Number of adverse events observed                                        78          104             67      
AE rate per 100 patient-years                                           48.03       100.14         38.82     
F                                                                                                            
  Total number of patients with at least one adverse event               45           63             36      
  Number of adverse events observed (AE rate per 100 patient-years)   45 (46.7)   63 (97.5)      36 (42.9)   
    A                                                                 12 (49.0)   18 (69.3)       9 (31.0)   
    B                                                                 15 (50.6)   23 (113.5)     14 (58.6)   
    C                                                                 18 (42.6)   22 (119.4)     13 (42.0)   
M                                                                                                            
  Total number of patients with at least one adverse event               33           41             31      
  Number of adverse events observed (AE rate per 100 patient-years)   33 (50.0)   41 (104.6)     31 (35.0)   
    A                                                                 9 (40.4)    13 (83.6)      10 (33.7)   
    B                                                                 16 (80.4)   15 (165.6)     10 (34.4)   
    C                                                                 8 (33.6)    13 (89.0)      11 (36.8)   
github-actions[bot] commented 2 months ago

Unit Tests Summary

    1 files     84 suites   1m 13s :stopwatch:   855 tests   843 :white_check_mark:  12 :zzz: 0 :x: 1 836 runs  1 154 :white_check_mark: 682 :zzz: 0 :x:

Results for commit 8ce2246d.

:recycle: This comment has been updated with latest results.

github-actions[bot] commented 2 months ago

Unit Test Performance Difference

Test Suite $Status$ Time on main $±Time$ $±Tests$ $±Skipped$ $±Failures$ $±Errors$
estimate_incidence_rate 💀 $0.37$ $-0.37$ $-22$ $-9$ $0$ $0$
h_incidence_rate 👶 $+0.10$ $+10$ $+5$ $0$ $0$
incidence_rate 👶 $+0.54$ $+20$ $+8$ $0$ $0$
Additional test case details | Test Suite | $Status$ | Time on `main` | $±Time$ | Test Case | |:-----|:----:|:----:|:----:|:-----| | estimate_incidence_rate | 💀 | $0.03$ | $-0.03$ | control_incidence_rate_fails_with_wrong_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | control_incidence_rate_works_with_customized_parameters | | estimate_incidence_rate | 💀 | $0.11$ | $-0.11$ | estimate_incidence_rate_n_rate_statistic_works_as_expected | | estimate_incidence_rate | 💀 | $0.11$ | $-0.11$ | estimate_incidence_rate_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | h_incidence_rate_byar_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | h_incidence_rate_exact_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | h_incidence_rate_normal_log_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.01$ | $-0.01$ | h_incidence_rate_normal_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | h_incidence_rate_works_as_expected_with_healthy_input | | estimate_incidence_rate | 💀 | $0.02$ | $-0.02$ | s_incidence_rate_works_as_expected_with_healthy_input | | h_incidence_rate | 👶 | | $+0.02$ | h_incidence_rate_byar_works_as_expected_with_healthy_input | | h_incidence_rate | 👶 | | $+0.02$ | h_incidence_rate_exact_works_as_expected_with_healthy_input | | h_incidence_rate | 👶 | | $+0.02$ | h_incidence_rate_normal_log_works_as_expected_with_healthy_input | | h_incidence_rate | 👶 | | $+0.02$ | h_incidence_rate_normal_works_as_expected_with_healthy_input | | h_incidence_rate | 👶 | | $+0.03$ | h_incidence_rate_works_as_expected_with_healthy_input | | incidence_rate | 👶 | | $+0.02$ | a_incidence_rate_works_with_customized_arguments | | incidence_rate | 👶 | | $+0.02$ | a_incidence_rate_works_with_default_arguments | | incidence_rate | 👶 | | $+0.03$ | control_incidence_rate_fails_with_wrong_input | | incidence_rate | 👶 | | $+0.02$ | control_incidence_rate_works_with_customized_parameters | | incidence_rate | 👶 | | $+0.08$ | estimate_incidence_rate_works_as_expected_with_custom_input | | incidence_rate | 👶 | | $+0.09$ | estimate_incidence_rate_works_as_expected_with_default_input | | incidence_rate | 👶 | | $+0.14$ | estimate_incidence_rate_works_with_custom_arguments_with_summarize_TRUE | | incidence_rate | 👶 | | $+0.13$ | estimate_incidence_rate_works_with_default_arguments_with_summarize_TRUE | | incidence_rate | 👶 | | $+0.01$ | s_incidence_rate_works_as_expected_with_healthy_input |

Results for commit 5f3a15b34a2ab1f6201553ad6f28309aa7095dc9

♻️ This comment has been updated with latest results.

github-actions[bot] commented 2 months ago

badge

Code Coverage Summary

Filename                                   Stmts    Miss  Cover    Missing
---------------------------------------  -------  ------  -------  ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
R/abnormal_by_baseline.R                      65       0  100.00%
R/abnormal_by_marked.R                        55       5  90.91%   92-96
R/abnormal_by_worst_grade_worsen.R           116       3  97.41%   262-264
R/abnormal_by_worst_grade.R                   60       0  100.00%
R/abnormal.R                                  43       0  100.00%
R/analyze_variables.R                        166       2  98.80%   492, 632
R/analyze_vars_in_cols.R                     176      13  92.61%   178, 221, 235-236, 244-252
R/bland_altman.R                              92       1  98.91%   43
R/combination_function.R                       9       0  100.00%
R/compare_variables.R                         84       2  97.62%   255, 314
R/control_incidence_rate.R                    10       0  100.00%
R/control_logistic.R                           7       0  100.00%
R/control_step.R                              23       1  95.65%   58
R/control_survival.R                          15       0  100.00%
R/count_cumulative.R                          50       1  98.00%   73
R/count_missed_doses.R                        34       0  100.00%
R/count_occurrences_by_grade.R               113       1  99.12%   166
R/count_occurrences.R                        115       1  99.13%   115
R/count_patients_events_in_cols.R             67       1  98.51%   59
R/count_patients_with_event.R                 47       0  100.00%
R/count_patients_with_flags.R                 58       0  100.00%
R/count_values.R                              27       0  100.00%
R/cox_regression_inter.R                     154       0  100.00%
R/cox_regression.R                           161       0  100.00%
R/coxph.R                                    167       7  95.81%   191-195, 238, 253, 261, 267-268
R/d_pkparam.R                                406       0  100.00%
R/decorate_grob.R                            113       0  100.00%
R/desctools_binom_diff.R                     621      64  89.69%   53, 88-89, 125-126, 129, 199, 223-232, 264, 266, 286, 290, 294, 298, 353, 356, 359, 362, 422, 430, 439, 444-447, 454, 457, 466, 469, 516-517, 519-520, 522-523, 525-526, 593, 604-616, 620, 663, 676, 680
R/df_explicit_na.R                            30       0  100.00%
R/estimate_multinomial_rsp.R                  50       1  98.00%   64
R/estimate_proportion.R                      205      11  94.63%   82-89, 93, 98, 319, 485
R/fit_rsp_step.R                              36       0  100.00%
R/fit_survival_step.R                         36       0  100.00%
R/formatting_functions.R                     183       2  98.91%   143, 278
R/g_forest.R                                 585      59  89.91%   241, 253-256, 261-262, 278, 288-291, 336-339, 346, 415, 502, 515, 519-520, 525-526, 539, 555, 602, 633, 708, 717, 723, 742, 797-817, 820, 831, 850, 905, 908, 1043-1048
R/g_ipp.R                                    133       0  100.00%
R/g_km.R                                     350      57  83.71%   286-289, 308-310, 364-367, 401, 429, 433-476, 483-487
R/g_lineplot.R                               243      22  90.95%   196, 370-377, 416-426, 518, 526
R/g_step.R                                    68       1  98.53%   109
R/g_waterfall.R                               47       0  100.00%
R/h_adsl_adlb_merge_using_worst_flag.R        73       0  100.00%
R/h_biomarkers_subgroups.R                    46       0  100.00%
R/h_cox_regression.R                         110       0  100.00%
R/h_incidence_rate.R                          45       0  100.00%
R/h_km.R                                     508      41  91.93%   137, 189-194, 287, 378, 380-381, 392-394, 413, 420-421, 423-425, 433-435, 460, 465-468, 651-654, 1108-1119
R/h_logistic_regression.R                    468       3  99.36%   203-204, 273
R/h_map_for_count_abnormal.R                  54       0  100.00%
R/h_pkparam_sort.R                            15       0  100.00%
R/h_response_biomarkers_subgroups.R           90      12  86.67%   50-55, 107-112
R/h_response_subgroups.R                     178      18  89.89%   257-270, 329-334
R/h_stack_by_baskets.R                        64       1  98.44%   89
R/h_step.R                                   180       0  100.00%
R/h_survival_biomarkers_subgroups.R           88       6  93.18%   111-116
R/h_survival_duration_subgroups.R            207      18  91.30%   259-271, 336-341
R/imputation_rule.R                           17       0  100.00%
R/incidence_rate.R                            84       5  94.05%   66-69, 149
R/logistic_regression.R                      102       0  100.00%
R/missing_data.R                              21       3  85.71%   32, 66, 76
R/odds_ratio.R                               109       0  100.00%
R/prop_diff_test.R                            91       0  100.00%
R/prop_diff.R                                265      15  94.34%   69-72, 104, 289-296, 439, 604
R/prune_occurrences.R                         57       0  100.00%
R/response_biomarkers_subgroups.R             69       6  91.30%   196-201
R/response_subgroups.R                       213       8  96.24%   100-105, 260-261
R/riskdiff.R                                  65       5  92.31%   102-105, 114
R/rtables_access.R                            38       0  100.00%
R/score_occurrences.R                         20       1  95.00%   124
R/split_cols_by_groups.R                      49       0  100.00%
R/stat.R                                      59       0  100.00%
R/summarize_ancova.R                         106       2  98.11%   182, 187
R/summarize_change.R                          30       0  100.00%
R/summarize_colvars.R                         10       0  100.00%
R/summarize_coxreg.R                         172       0  100.00%
R/summarize_glm_count.R                      209       3  98.56%   192-193, 489
R/summarize_num_patients.R                    94       4  95.74%   116-118, 265
R/summarize_patients_exposure_in_cols.R       96       1  98.96%   55
R/survival_biomarkers_subgroups.R             78       6  92.31%   117-122
R/survival_coxph_pairwise.R                   79      11  86.08%   50-51, 63-71
R/survival_duration_subgroups.R              211       6  97.16%   124-129
R/survival_time.R                             79       0  100.00%
R/survival_timepoint.R                       113       7  93.81%   124-130
R/utils_checkmate.R                           68       0  100.00%
R/utils_default_stats_formats_labels.R       124       0  100.00%
R/utils_factor.R                             109       2  98.17%   84, 302
R/utils_ggplot.R                             110       0  100.00%
R/utils_grid.R                               126       5  96.03%   164, 279-286
R/utils_rtables.R                            100       4  96.00%   39, 46, 403-404
R/utils_split_funs.R                          52       2  96.15%   82, 94
R/utils.R                                    141       7  95.04%   118, 121, 124, 128, 137-138, 332
TOTAL                                      10512     457  95.65%

Diff against main

Filename                Stmts    Miss  Cover
--------------------  -------  ------  --------
R/h_incidence_rate.R      +45       0  +100.00%
R/incidence_rate.R        -16      -2  +1.05%
TOTAL                     +29      -2  +0.03%

Results for commit: 8ce2246dd2790191c75437095c1af83ba0e411a8

Minimum allowed coverage is 80%

:recycle: This comment has been updated with latest results

shajoezhu commented 2 months ago

block this PR by https://github.com/insightsengineering/tern/pull/1311