Skip to content

Commit

Permalink
Merge branch 'main' into 1062_numeric_count_by@main
Browse files Browse the repository at this point in the history
Signed-off-by: Emily de la Rua <59304861+edelarua@users.noreply.github.com>
  • Loading branch information
edelarua authored Nov 28, 2023
2 parents 8b73276 + 2203bc7 commit f835825
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 15 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tern
Title: Create Common TLGs Used in Clinical Trials
Version: 0.9.2.9018
Version: 0.9.2.9019
Date: 2023-11-27
Authors@R: c(
person("Joe", "Zhu", , "joe.zhu@roche.com", role = c("aut", "cre")),
Expand Down
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# tern 0.9.2.9018
# tern 0.9.2.9019

### New Features
* Added `ref_group_position` function to place the reference group facet last, first or at a certain position.
Expand All @@ -18,6 +18,7 @@
* Added `annot_coxph_ref_lbls` parameter to `g_km` to enable printing the reference group in table labels when `annot_coxph = TRUE`.
* Added `x_lab` parameter to `g_lineplot` to customize x-axis label.
* Remove 25% padding of y-axis in `g_lineplot`.
* Added support for creating multiple risk difference columns, each comparing to a single comparison group. Multiple comparison groups can be specified as a vector via the `arm_y` argument.
* Allowed numeric vector as `count_by` input in `analyze_num_patients` and `summarize_num_patients`.

### Bug Fixes
Expand Down
32 changes: 23 additions & 9 deletions R/riskdiff.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@
#' `riskdiff` to `TRUE` in all following analyze function calls.
#'
#' @param arm_x (`character`)\cr Name of reference arm to use in risk difference calculations.
#' @param arm_y (`character`)\cr Name of arm to compare to reference arm in risk difference calculations.
#' @param col_label (`character`)\cr Label to use when rendering the risk difference column within the table.
#' @param arm_y (`character`)\cr Names of one or more arms to compare to reference arm in risk difference
#' calculations. A new column will be added for each value of `arm_y`.
#' @param col_label (`character`)\cr Labels to use when rendering the risk difference column within the table.
#' If more than one comparison arm is specified in `arm_y`, default labels will specify which two arms are
#' being compared (reference arm vs. comparison arm).
#' @param pct (`flag`)\cr whether output should be returned as percentages. Defaults to `TRUE`.
#'
#' @return A closure suitable for use as a split function (`split_fun`) within [rtables::split_cols_by()]
Expand All @@ -22,7 +25,7 @@
#' adae$AESEV <- factor(adae$AESEV)
#'
#' lyt <- basic_table() %>%
#' split_cols_by("ARMCD", split_fun = add_riskdiff(arm_x = "ARM A", arm_y = "ARM B")) %>%
#' split_cols_by("ARMCD", split_fun = add_riskdiff(arm_x = "ARM A", arm_y = c("ARM B", "ARM C"))) %>%
#' count_occurrences_by_grade(
#' var = "AESEV",
#' riskdiff = TRUE
Expand All @@ -34,13 +37,24 @@
#' @export
add_riskdiff <- function(arm_x,
arm_y,
col_label = "Risk Difference (%) (95% CI)",
col_label = paste0(
"Risk Difference (%) (95% CI)", if (length(arm_y) > 1) paste0("\n", arm_x, " vs. ", arm_y)
),
pct = TRUE) {
sapply(c(arm_x, arm_y, col_label), checkmate::assert_character, len = 1)
combodf <- tibble::tribble(
~valname, ~label, ~levelcombo, ~exargs,
paste("riskdiff", arm_x, arm_y, sep = "_"), col_label, c(arm_x, arm_y), list()
)
checkmate::assert_character(arm_x, len = 1)
checkmate::assert_character(arm_y, min.len = 1)
checkmate::assert_character(col_label, len = length(arm_y))

combodf <- tibble::tribble(~valname, ~label, ~levelcombo, ~exargs)
for (i in seq_len(length(arm_y))) {
combodf <- rbind(
combodf,
tibble::tribble(
~valname, ~label, ~levelcombo, ~exargs,
paste("riskdiff", arm_x, arm_y[i], sep = "_"), col_label[i], c(arm_x, arm_y[i]), list()
)
)
}
if (pct) combodf$valname <- paste0(combodf$valname, "_pct")
add_combo_levels(combodf)
}
Expand Down
12 changes: 8 additions & 4 deletions man/add_riskdiff.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/count_occurrences.md
Original file line number Diff line number Diff line change
Expand Up @@ -283,3 +283,21 @@
MEDIUM 5 (7.8%) 2 (3.0%) 1 (1.6%) 4.8 (-2.9 - 12.6) 2 (2.6%) 3 (4.7%) 2 (4.4%) -2.1 (-8.4 - 4.1) 3 (5.0%) 4 (8.7%) 1 (1.8%) -3.7 (-13.5 - 6.1)
HIGH 2 (3.1%) 5 (7.5%) 5 (8.1%) -4.3 (-11.9 - 3.3) 2 (2.6%) 5 (7.8%) 3 (6.7%) -5.2 (-12.7 - 2.2) 0 2 (4.3%) 3 (5.5%) -4.3 (-10.2 - 1.5)

---

Code
res
Output
Risk Difference (%) (95% CI) Risk Difference (%) (95% CI)
A: Drug X B: Placebo C: Combination A: Drug X vs. B: Placebo A: Drug X vs. C: Combination
(N=202) (N=177) (N=162) (N=379) (N=364)
————————————————————————————————————————————————————————————————————————————————————————————————————————————————
F
LOW 12 (5.9%) 11 (6.2%) 10 (6.2%) -0.3 (-5.1 - 4.6) -0.2 (-5.2 - 4.7)
MEDIUM 11 (5.4%) 13 (7.3%) 10 (6.2%) -1.9 (-6.9 - 3.1) -0.7 (-5.6 - 4.1)
HIGH 10 (5.0%) 7 (4.0%) 7 (4.3%) 1.0 (-3.2 - 5.1) 0.6 (-3.7 - 5.0)
M
LOW 12 (5.9%) 5 (2.8%) 6 (3.7%) 3.1 (-1.0 - 7.2) 2.2 (-2.1 - 6.6)
MEDIUM 10 (5.0%) 9 (5.1%) 4 (2.5%) -0.1 (-4.5 - 4.3) 2.5 (-1.3 - 6.3)
HIGH 4 (2.0%) 12 (6.8%) 11 (6.8%) -4.8 (-9.0 - -0.6) -4.8 (-9.1 - -0.5)

13 changes: 13 additions & 0 deletions tests/testthat/test-count_occurrences.R
Original file line number Diff line number Diff line change
Expand Up @@ -272,4 +272,17 @@ testthat::test_that("summarize_occurrences works as expected with risk differenc

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)

# Multiple comparison groups
result <- basic_table(show_colcounts = TRUE) %>%
split_cols_by("ARM", split_fun = add_riskdiff("A: Drug X", c("B: Placebo", "C: Combination"))) %>%
split_rows_by("SEX", child_labels = "visible") %>%
summarize_occurrences(
var = "BMRKR2",
riskdiff = TRUE
) %>%
build_table(tern_ex_adae)

res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

0 comments on commit f835825

Please sign in to comment.