Skip to content

Commit

Permalink
Add n_rate statistic to estimate_incidence_rate() (#1295)
Browse files Browse the repository at this point in the history
Fixes #1294
  • Loading branch information
edelarua committed Sep 5, 2024
1 parent 716a290 commit 6b41d95
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 5 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* Added the `.formats` argument to `tabulate_rsp_subgroups` and `tabulate_survival_subgroups` to allow users to specify formats.
* Added the `riskdiff` argument to `tabulate_rsp_subgroups` and `tabulate_survival_subgroups` to allow users to add a risk difference table column, and function `control_riskdiff` to specify settings for the risk difference column.
* Added warning to `tabulate_rsp_subgroups` when `pval` statistic is selected but `df` has not been correctly generated to add p-values to the output table.
* Added `n_rate` statistic as a non-default option to `estimate_incidence_rate` which returns both number of events observed and estimated incidence rate.

### Bug Fixes
* Fixed a bug in `a_surv_time` that threw an error when split only has `"is_event"`.
Expand Down
12 changes: 9 additions & 3 deletions R/incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ NULL
#' - `n_events`: Total number of events observed.
#' - `rate`: Estimated incidence rate.
#' - `rate_ci`: Confidence interval for the incidence rate.
#' - `n_rate`: Total number of events observed & estimated incidence rate.
#'
#' @keywords internal
s_incidence_rate <- function(df,
Expand Down Expand Up @@ -77,7 +78,11 @@ s_incidence_rate <- function(df,
person_years = formatters::with_label(person_years, "Total patient-years at risk"),
n_events = formatters::with_label(n_events, "Number of adverse events observed"),
rate = formatters::with_label(result$rate, paste("AE rate per", num_pt_year, "patient-years")),
rate_ci = formatters::with_label(result$rate_ci, f_conf_level(conf_level))
rate_ci = formatters::with_label(result$rate_ci, f_conf_level(conf_level)),
n_rate = formatters::with_label(
c(n_events, result$rate),
paste("Number of adverse events observed (AE rate per", num_pt_year, "patient-years)")
)
)
}

Expand All @@ -94,7 +99,8 @@ a_incidence_rate <- make_afun(
"person_years" = "xx.x",
"n_events" = "xx",
"rate" = "xx.xx",
"rate_ci" = "(xx.xx, xx.xx)"
"rate_ci" = "(xx.xx, xx.xx)",
"n_rate" = "xx (xx.x)"
)
)

Expand Down Expand Up @@ -142,7 +148,7 @@ estimate_incidence_rate <- function(lyt,
...,
show_labels = "hidden",
table_names = vars,
.stats = NULL,
.stats = c("person_years", "n_events", "rate", "rate_ci"),
.formats = NULL,
.labels = NULL,
.indent_mods = NULL) {
Expand Down
2 changes: 1 addition & 1 deletion R/utils_default_stats_formats_labels.R
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ tern_default_stats <- list(
count_patients_with_flags = c("n", "count", "count_fraction", "count_fraction_fixed_dp", "n_blq"),
count_values = c("n", "count", "count_fraction", "count_fraction_fixed_dp", "n_blq"),
coxph_pairwise = c("pvalue", "hr", "hr_ci", "n_tot", "n_tot_events"),
estimate_incidence_rate = c("person_years", "n_events", "rate", "rate_ci"),
estimate_incidence_rate = c("person_years", "n_events", "rate", "rate_ci", "n_rate"),
estimate_multinomial_response = c("n_prop", "prop_ci"),
estimate_odds_ratio = c("or_ci", "n_tot"),
estimate_proportion = c("n_prop", "prop_ci"),
Expand Down
3 changes: 2 additions & 1 deletion man/incidence_rate.Rd

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

17 changes: 17 additions & 0 deletions tests/testthat/_snaps/estimate_incidence_rate.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@
attr(,"label")
[1] "90% CI"
$n_rate
[1] 4.00000 44.15823
attr(,"label")
[1] "Number of adverse events observed (AE rate per 100 patient-years)"

# estimate_incidence_rate works as expected with healthy input

Expand All @@ -115,3 +120,15 @@
AE rate per 100 patient-years 26.20 57.23
90% CI (5.06, 135.73) (22.14, 147.94)

# estimate_incidence_rate `n_rate` statistic works as expected

Code
res
Output
A B
(N=3) (N=3)
—————————————————————————————————————————————————————————————————————————————————————
Number of adverse events observed 1 3
AE rate per 100 patient-years 2.18 4.77
Number of adverse events observed (AE rate per 100 patient-years) 1 (2.2) 3 (4.8)

24 changes: 24 additions & 0 deletions tests/testthat/test-estimate_incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,27 @@ testthat::test_that("estimate_incidence_rate works as expected with healthy inpu
res <- testthat::expect_silent(result)
testthat::expect_snapshot(res)
})

testthat::test_that("estimate_incidence_rate `n_rate` statistic works as expected", {
df <- data.frame(
USUBJID = as.character(seq(6)),
CNSR = c(0, 1, 1, 0, 0, 0),
AVAL = c(10.1, 20.4, 15.3, 20.8, 18.7, 23.4),
ARM = factor(c("A", "A", "A", "B", "B", "B"))
) %>%
dplyr::mutate(is_event = CNSR == 0) %>%
dplyr::mutate(n_events = as.integer(is_event))

result <- basic_table() %>%
split_cols_by("ARM") %>%
add_colcounts() %>%
estimate_incidence_rate(
vars = "AVAL",
n_events = "n_events",
.stats = c("n_events", "rate", "n_rate")
) %>%
build_table(df)

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

0 comments on commit 6b41d95

Please sign in to comment.