Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reorganize and group defaults for stats and their default formats/labels -> addition of auto for DMT01 #1051

Merged
merged 77 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
77 commits
Select commit Hold shift + click to select a range
5c5191f
Init from other repo
Melkiades Aug 31, 2023
f7ddd77
renaming
Melkiades Aug 31, 2023
6322689
news
Melkiades Aug 31, 2023
221f6a8
adding docs and init testing
Melkiades Aug 31, 2023
3c8a9e8
fixes and complete tests
Melkiades Sep 1, 2023
6820a06
typo renaming
Melkiades Sep 1, 2023
434f261
starts unpacking summary_stats
Melkiades Sep 1, 2023
29d77c8
added parameters for pval and filtering
Melkiades Sep 1, 2023
58bd86a
Merge branch 'main' into 994_reorganize_defaults@main
Melkiades Sep 1, 2023
c522ab5
keep the footprint small for now
Melkiades Sep 1, 2023
0bf54be
more checks
Melkiades Sep 4, 2023
e351ffd
fixing type with better simpler way (keeping it higher level)
Melkiades Sep 5, 2023
45a5028
documenting
Melkiades Sep 5, 2023
5d4bbd4
styling
Melkiades Sep 5, 2023
42ba731
fixing analyze_vars_in_cols
Melkiades Sep 5, 2023
2dfa7e9
roxygenize
Melkiades Sep 5, 2023
5aefbb7
styiling
Melkiades Sep 5, 2023
285b4b1
upd
Melkiades Sep 5, 2023
ffbc09d
some simplification
Melkiades Sep 5, 2023
7523644
fixing imp tests
Melkiades Sep 5, 2023
0a35c27
fixes and NEWS
Melkiades Sep 5, 2023
75daa8a
change in internal value name
Melkiades Sep 5, 2023
bf28184
add to NEWS
Melkiades Sep 5, 2023
e44cbb2
These changes in values seem related to changes in emmeans (might nee…
Melkiades Sep 5, 2023
7703114
couple of fixes
Melkiades Sep 5, 2023
ebca702
adding a couple of handling for pvals
Melkiades Sep 5, 2023
ce6873c
it is in get_stats now
Melkiades Sep 5, 2023
c7c980e
fix tests
Melkiades Sep 5, 2023
4ba5455
fixes
Melkiades Sep 6, 2023
fc646dd
final fix
Melkiades Sep 6, 2023
75928f8
Update NEWS.md
Melkiades Sep 6, 2023
bf5ff61
Merge 75928f825f72090875b9f6fa35f2e15a0a5fe73d into 263e67151897da733…
Melkiades Sep 6, 2023
f88c3f5
[skip actions] Restyle files
github-actions[bot] Sep 6, 2023
2e455e3
styling
Melkiades Sep 6, 2023
9c97ec2
Merge branch '994_reorganize_defaults@main' of github.com:insightseng…
Melkiades Sep 6, 2023
a00b940
I think last fix
Melkiades Sep 6, 2023
35dea36
spell check fix
Melkiades Sep 6, 2023
d4d1ba4
fix
Melkiades Sep 6, 2023
8347c31
NEWS review
Melkiades Sep 7, 2023
0882056
Apply suggestions from code review
Melkiades Sep 7, 2023
431dcfc
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 7, 2023
8a2b1df
adding format_auto
Melkiades Sep 7, 2023
e707a11
integration of auto
Melkiades Sep 7, 2023
35b7ece
roxygenizing a ton apparently
Melkiades Sep 7, 2023
2f57ef8
rename
Melkiades Sep 7, 2023
370f0ed
more renaming
Melkiades Sep 7, 2023
2e9584e
fixes to docs
Melkiades Sep 7, 2023
5639dae
Merge 2e9584e0944d0f2a6cea8f69f950e9809652b6b3 into 263e67151897da733…
Melkiades Sep 7, 2023
4df8e84
[skip actions] Restyle files
github-actions[bot] Sep 7, 2023
41d6336
used only once: removed
Melkiades Sep 7, 2023
f2ba822
renaming to plural
Melkiades Sep 7, 2023
420d99b
renaming labels
Melkiades Sep 7, 2023
4abd74a
rox
Melkiades Sep 7, 2023
fd15ea0
Merge branch '994_reorganize_defaults@main' of github.com:insightseng…
Melkiades Sep 7, 2023
7cbdee7
fixes and removal of test- summary_stats
Melkiades Sep 7, 2023
0cc47d3
removing the source too
Melkiades Sep 7, 2023
fc7a10e
Merge 0cc47d32442f24aaf1c6506f9f2a4c57397b950b into 263e67151897da733…
Melkiades Sep 7, 2023
f843772
[skip actions] Restyle files
github-actions[bot] Sep 7, 2023
f4e5499
update snapshots
Melkiades Sep 7, 2023
43e0dac
Merge branch '994_reorganize_defaults@main' of github.com:insightseng…
Melkiades Sep 7, 2023
b2b80de
fixes
Melkiades Sep 7, 2023
2f8b60b
putting type on higher default
Melkiades Sep 7, 2023
85988f0
last fixes
Melkiades Sep 7, 2023
437855a
tiny fix
Melkiades Sep 7, 2023
26cb616
renaming
Melkiades Sep 8, 2023
732d9d5
Apply suggestions from code review
Melkiades Sep 8, 2023
053f518
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 8, 2023
edd3e3a
Merge branch '994_reorganize_defaults@main' of github.com:insightseng…
Melkiades Sep 8, 2023
45139af
fixed pkg_down
Melkiades Sep 8, 2023
a23e0ac
lint fixes
Melkiades Sep 8, 2023
8e47119
Merge branch 'main' into 994_reorganize_defaults@main
Melkiades Sep 8, 2023
8a5e168
Merge branch 'main' into 994_reorganize_defaults@main
Melkiades Sep 8, 2023
25486e5
lintfix
Melkiades Sep 8, 2023
23763c4
Merge branch '994_reorganize_defaults@main' of github.com:insightseng…
Melkiades Sep 8, 2023
a6ea43f
Merge branch 'main' into 994_reorganize_defaults@main
Melkiades Sep 11, 2023
ab41f94
[skip actions] Roxygen Man Pages Auto Update
dependabot-preview[bot] Sep 11, 2023
c6f64cb
docs
Melkiades Sep 11, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ Collate:
'survival_timepoint.R'
'utils.R'
'utils_checkmate.R'
'utils_defaults_handling.R'
'utils_factor.R'
'utils_grid.R'
'utils_rtables.R'
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,10 @@ export(g_km)
export(g_lineplot)
export(g_step)
export(g_waterfall)
export(get_format_from_stats)
export(get_label_from_stats)
export(get_smooths)
export(get_stats)
export(groups_list_to_df)
export(h_adlb_worsen)
export(h_adsl_adlb_merge_using_worst_flag)
Expand Down Expand Up @@ -274,6 +277,8 @@ export(tabulate_rsp_biomarkers)
export(tabulate_rsp_subgroups)
export(tabulate_survival_biomarkers)
export(tabulate_survival_subgroups)
export(tern_default_formats)
export(tern_default_labels)
export(test_proportion_diff)
export(to_n)
export(to_string_matrix)
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* Refactored `a_summary` to no longer use helper function `create_afun_summary`.
* Refactored `summarize_vars` and `compare_vars` to use refactored `a_summary`.
* Created new internal helper functions `ungroup_stats` to ungroup statistics calculated for factor variables, and `a_summary_internal` to perform calculations for `a_summary`.
* Grouping of valid methods names and their relative defaut formats and labels into new source file `utils_defaults_handling.R`.

### Bug Fixes
* Fixed bug in `s_count_occurrences_by_grade` so that "missing" grade always appears as the final level.
Expand Down
140 changes: 140 additions & 0 deletions R/utils_defaults_handling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#' Defaults for stats methods names and their relative formats/labels
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
#'
#' @description `r lifecycle::badge("stable")`
#'
#' Utility functions to get valid statistic methods for different method groups
#' (`.stats`) and their relative formats (`.formats`) and labels (`.labels`). This utility
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
#' is used across `tern`, but some of its working principles can be seen in [analyze_vars()].
#'
#' @param method_group (`string`) \cr Indicates the group of statistical methods that
#' we need the defaults from.
#' @param stats (`character`) \cr statistical methods to get defaults formats for.
#'
#' @name default_stats_and_formats
NULL

#' @describeIn default_stats_and_formats Get defaults statistical methods for different
#' groups of methods.
#'
#' @return
#' * `get_stats()` returns a character vector with all default statistical methods.
#'
#' @export
get_stats <- function(method_group) {
checkmate::assert_string(method_group)
switch (method_group,
"count_occurrences" = c("count", "count_fraction", "fraction"),
"summarize_num_patients" = c("unique", "nonunique", "unique_count"),
stop(method_group, " is a method_group that has no default statistical method.")
)
}

#' @describeIn default_stats_and_formats Get formats from vector of statistical methods. If not
#' present `NULL` is returned.
#'
#' @return
#' * `get_format_from_stats()` returns a named list of formats, they being a value from
#' [formatters::list_valid_format_labels()] or a custom function (e.g. [formatting_functions]).
#'
#' @seealso [formatting_functions]
#'
#' @export
get_format_from_stats <- function(stats) {
checkmate::assert_character(stats, min.len = 1)
default_formats <- tern_default_formats()
which_fmt <- match(stats, names(default_formats))

ret <- vector("list", length = length(stats))
ret[!is.na(which_fmt)] <- default_formats[which_fmt]

setNames(ret, stats)
}

#' @describeIn default_stats_and_formats Get labels from vector of statistical methods.
#'
#' @return
#' * `get_label_from_stats()` returns a named list of default labels (if present
#' otherwise `NULL`).
#'
#' @export
get_label_from_stats <- function(stats) {
checkmate::assert_character(stats, min.len = 1)
default_lbl <- tern_default_labels()
which_lbl <- match(stats, names(default_lbl))

ret <- vector("list", length = length(stats))
ret[!is.na(which_lbl)] <- default_lbl[which_lbl]

setNames(ret, stats)
}
#' @describeIn default_stats_and_formats Function that produce the complete named list
#' of default formats for `tern`.
#'
#' @return
#' * `tern_default_formats()` returns a complete named list of default formats for `tern`.
#'
#' @export
tern_default_formats <- function() {
Melkiades marked this conversation as resolved.
Show resolved Hide resolved
c(
list(
# "count" = "xx", # Conflict coming from count_occurences
# "count_fraction" = format_count_fraction_fixed_dp, # Conflict coming from count_occurences
fraction = format_fraction_fixed_dp
),
list(
unique = format_count_fraction_fixed_dp,
nonunique = "xx",
unique_count = "xx"
),
# coming from tern:::summary_formats() but it should be exported and more general
list(
# n = "xx.", # Duplication in summary_formats
count = "xx.",
count_fraction = format_count_fraction,
n_blq = "xx.",
# pval = "x.xxxx | (<0.0001)" # Duplication in summary_formats
),
list(
n = "xx.",
sum = "xx.x",
mean = "xx.x",
sd = "xx.x",
se = "xx.x",
mean_sd = "xx.x (xx.x)",
mean_se = "xx.x (xx.x)",
mean_ci = "(xx.xx, xx.xx)",
mean_sei = "(xx.xx, xx.xx)",
mean_sdi = "(xx.xx, xx.xx)",
mean_pval = "xx.xx",
median = "xx.x",
mad = "xx.x",
median_ci = "(xx.xx, xx.xx)",
quantiles = "xx.x - xx.x",
iqr = "xx.x",
range = "xx.x - xx.x",
min = "xx.x",
max = "xx.x",
median_range = "xx.x (xx.x - xx.x)",
cv = "xx.x",
geom_mean = "xx.x",
geom_mean_ci = "(xx.xx, xx.xx)",
geom_cv = "xx.x",
pval = "x.xxxx | (<0.0001)"
)
)
}
#' @describeIn default_stats_and_formats Function that produce the complete named list
#' of default labels for `tern`.
#'
#' @return
#' * `tern_default_labels()` returns a complete named list of default labels for `tern`.
#'
#' @export
tern_default_labels <- function() {
c(
list(
"unique" = "Number of patients with at least one event",
"nonunique" = "Number of events"
)
)
}
77 changes: 77 additions & 0 deletions man/default_stats_and_formats.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 24 additions & 0 deletions tests/testthat/test_utils_defaults_handling.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
testthat::context("Utilities for list of defaults stats/formats/labels")

testthat::test_that("get_stats works as expected", {
res <- testthat::expect_silent()
testthat::expect_snapshot(res)

testthat::expect_error(get_stats("dont_exist"), regexp = "dont_exist is a method*")
})

testthat::test_that("get_format_from_stats works as expected", {
sts <- get_stats("count_occurrences")
res <- testthat::expect_silent(get_format_from_stats(sts))
testthat::expect_snapshot(res)

testthat::expect_error(get_stats("dont_exist"), regexp = "dont_exist is a method*")
})

testthat::test_that("get_label_from_stats works as expected", {
sts <- get_stats("count_occurrences")
res <- testthat::expect_silent(get_label_from_stats(sts))
testthat::expect_snapshot(res)

# testthat::expect_error(get_label_from_stats("Maas"), regexp = "dont_exist is a method*")
})