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

More intuitive parameter names in control_incidence_rate #975

Merged
merged 7 commits into from
Jun 20, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
3 changes: 2 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

### Miscellaneous
* Updated README to include installation instructions for CRAN.
* Began deprecation of `indent_mod` argument and replace it with the `.indent_mods` argument in `summarize_num_patients` and `analyze_num_patients`.
* Began deprecation of `indent_mod` argument and replaced it with the `.indent_mods` argument in `summarize_num_patients` and `analyze_num_patients`.
* Began deprecation of `time_unit_input` and `time_unit_output` arguments and replaced them with the `input_time_unit` and `n_pt_years_rate`, respectively, in `control_incidence_rate`.

# tern 0.8.2

Expand Down
35 changes: 26 additions & 9 deletions R/control_incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#' internally to specify details in `s_incidence_rate()`.
#'
#' @inheritParams argument_convention
#' @param time_unit_input (`string`)\cr `day`, `month`, or `year` (default)
#' indicating time unit for data input.
#' @param time_unit_output (`numeric`)\cr time unit for desired output (in person-years).
#' @param conf_type (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
#' for confidence interval type.
#' @param input_time_unit (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' indicating time unit for data input.
#' @param n_pt_years_rate (`numeric`)\cr number of patient-years to use when calculating AE rate.
#' @param time_unit_input `r lifecycle::badge("deprecated")` Please use the `input_time_unit` argument instead.
#' @param time_unit_output `r lifecycle::badge("deprecated")` Please use the `n_pt_years_rate` argument instead.
#'
#' @return A list of components with the same names as the arguments.
#'
Expand All @@ -22,17 +24,32 @@
#' @export
control_incidence_rate <- function(conf_level = 0.95,
conf_type = c("normal", "normal_log", "exact", "byar"),
time_unit_input = c("year", "day", "week", "month"),
time_unit_output = 1) {
input_time_unit = c("year", "day", "week", "month"),
n_pt_years_rate = 1,
time_unit_input = lifecycle::deprecated(),
time_unit_output = lifecycle::deprecated()) {
if (lifecycle::is_present(time_unit_input)) {
lifecycle::deprecate_warn(
"0.8.3", "control_incidence_rate(time_unit_input)", "control_incidence_rate(input_time_unit)"
)
input_time_unit <- time_unit_input
}
if (lifecycle::is_present(time_unit_output)) {
lifecycle::deprecate_warn(
"0.8.3", "control_incidence_rate(time_unit_output)", "control_incidence_rate(n_pt_years_rate)"
)
n_pt_years_rate <- time_unit_output
}

conf_type <- match.arg(conf_type)
time_unit_input <- match.arg(time_unit_input)
checkmate::assert_number(time_unit_output)
input_time_unit <- match.arg(input_time_unit)
checkmate::assert_number(n_pt_years_rate)
assert_proportion_value(conf_level)

list(
conf_level = conf_level,
conf_type = conf_type,
time_unit_input = time_unit_input,
time_unit_output = time_unit_output
input_time_unit = input_time_unit,
n_pt_years_rate = n_pt_years_rate
)
}
40 changes: 20 additions & 20 deletions R/incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
#' * `conf_level` (`proportion`)\cr confidence level for the estimated incidence rate.
#' * `conf_type` (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
#' for confidence interval type.
#' * `time_unit_input` (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' * `input_time_unit` (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' indicating time unit for data input.
#' * `time_unit_output` (`numeric`)\cr time unit for desired output (in person-years).
#' * `n_pt_years_rate` (`numeric`)\cr time unit for desired output (in person-years).
#' @param person_years (`numeric`)\cr total person-years at risk.
#' @param alpha (`numeric`)\cr two-sided alpha-level for confidence interval.
#' @param n_events (`integer`)\cr number of events observed.
Expand Down Expand Up @@ -52,8 +52,8 @@ NULL
#' .var = "AVAL",
#' n_events = "n_events",
#' control = control_incidence_rate(
#' time_unit_input = "month",
#' time_unit_output = 100
#' input_time_unit = "month",
#' n_pt_years_rate = 100
#' )
#' )
#' }
Expand Down Expand Up @@ -81,14 +81,14 @@ s_incidence_rate <- function(df,
checkmate::assert_integer(df[[n_events]], any.missing = FALSE)
}

time_unit_input <- control$time_unit_input
time_unit_output <- control$time_unit_output
input_time_unit <- control$input_time_unit
n_pt_years_rate <- control$n_pt_years_rate
conf_level <- control$conf_level
person_years <- sum(df[[.var]], na.rm = TRUE) * (
1 * (time_unit_input == "year") +
1 / 12 * (time_unit_input == "month") +
1 / 52.14 * (time_unit_input == "week") +
1 / 365.24 * (time_unit_input == "day")
1 * (input_time_unit == "year") +
1 / 12 * (input_time_unit == "month") +
1 / 52.14 * (input_time_unit == "week") +
1 / 365.24 * (input_time_unit == "day")
)
n_events <- sum(df[[n_events]], na.rm = TRUE)

Expand All @@ -100,7 +100,7 @@ s_incidence_rate <- function(df,
list(
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", time_unit_output, "patient-years")),
rate = formatters::with_label(result$rate, paste("AE rate per", n_pt_years_rate, "patient-years")),
rate_ci = formatters::with_label(result$rate_ci, f_conf_level(conf_level))
)
}
Expand All @@ -118,7 +118,7 @@ s_incidence_rate <- function(df,
#' df,
#' .var = "AVAL",
#' n_events = "n_events",
#' control = control_incidence_rate(time_unit_input = "month", time_unit_output = 100)
#' control = control_incidence_rate(input_time_unit = "month", n_pt_years_rate = 100)
#' )
#' }
#'
Expand Down Expand Up @@ -149,8 +149,8 @@ a_incidence_rate <- make_afun(
#' vars = "AVAL",
#' n_events = "n_events",
#' control = control_incidence_rate(
#' time_unit_input = "month",
#' time_unit_output = 100
#' input_time_unit = "month",
#' n_pt_years_rate = 100
#' )
#' ) %>%
#' build_table(df)
Expand Down Expand Up @@ -192,9 +192,9 @@ estimate_incidence_rate <- function(lyt,
#' * `conf_level`: (`proportion`)\cr confidence level for the estimated incidence rate.
#' * `conf_type`: (`string`)\cr `normal` (default), `normal_log`, `exact`, or `byar`
#' for confidence interval type.
#' * `time_unit_input`: (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' * `input_time_unit`: (`string`)\cr `day`, `week`, `month`, or `year` (default)
#' indicating time unit for data input.
#' * `time_unit_output`: (`numeric`)\cr time unit for desired output (in person-years).
#' * `n_pt_years_rate`: (`numeric`)\cr time unit for desired output (in person-years).
#' @param person_years (`numeric`)\cr total person-years at risk.
#' @param alpha (`numeric`)\cr two-sided alpha-level for confidence interval.
#' @param n_events (`integer`)\cr number of events observed.
Expand Down Expand Up @@ -311,7 +311,7 @@ h_incidence_rate_byar <- function(person_years,
#' control_incidence_rate(
#' conf_level = 0.9,
#' conf_type = "normal_log",
#' time_unit_output = 100
#' n_pt_years_rate = 100
#' )
#' )
#' }
Expand All @@ -328,9 +328,9 @@ h_incidence_rate <- function(person_years,
byar = h_incidence_rate_byar(person_years, n_events, alpha)
)

time_unit_output <- control$time_unit_output
n_pt_years_rate <- control$n_pt_years_rate
list(
rate = est$rate * time_unit_output,
rate_ci = est$rate_ci * time_unit_output
rate = est$rate * n_pt_years_rate,
rate_ci = est$rate_ci * n_pt_years_rate
)
}
14 changes: 10 additions & 4 deletions man/control_incidence_rate.Rd

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

6 changes: 3 additions & 3 deletions man/h_incidence_rate.Rd

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

14 changes: 7 additions & 7 deletions man/incidence_rate.Rd

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

18 changes: 9 additions & 9 deletions tests/testthat/test-estimate_incidence_rate.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ testthat::test_that("control_incidence_rate works with customized parameters", {
result <- control_incidence_rate(
conf_level = 0.9,
conf_type = "exact",
time_unit_input = "month",
time_unit_output = 100
input_time_unit = "month",
n_pt_years_rate = 100
)

res <- testthat::expect_silent(result)
Expand All @@ -13,8 +13,8 @@ testthat::test_that("control_incidence_rate works with customized parameters", {
testthat::test_that("control_incidence_rate fails with wrong input", {
testthat::expect_error(control_incidence_rate(conf_level = 1.1))
testthat::expect_error(control_incidence_rate(conf_type = "wald"))
testthat::expect_error(control_incidence_rate(time_unit_input = "decade"))
testthat::expect_error(control_incidence_rate(time_unit_output = "one"))
testthat::expect_error(control_incidence_rate(input_time_unit = "decade"))
testthat::expect_error(control_incidence_rate(n_pt_years_rate = "one"))
})

testthat::test_that("h_incidence_rate_normal works as expected with healthy input", {
Expand Down Expand Up @@ -49,7 +49,7 @@ testthat::test_that("h_incidence_rate works as expected with healthy input", {
result <- h_incidence_rate(
200,
2,
control_incidence_rate(conf_level = 0.9, conf_type = "normal_log", time_unit_output = 100)
control_incidence_rate(conf_level = 0.9, conf_type = "normal_log", n_pt_years_rate = 100)
)

res <- testthat::expect_silent(result)
Expand All @@ -72,8 +72,8 @@ testthat::test_that("s_incidence_rate works as expected with healthy input", {
control = control_incidence_rate(
conf_level = 0.9,
conf_type = "normal_log",
time_unit_input = "month",
time_unit_output = 100
input_time_unit = "month",
n_pt_years_rate = 100
)
)

Expand All @@ -100,8 +100,8 @@ testthat::test_that("estimate_incidence_rate works as expected with healthy inpu
control = control_incidence_rate(
conf_level = 0.9,
conf_type = "normal_log",
time_unit_input = "month",
time_unit_output = 100
input_time_unit = "month",
n_pt_years_rate = 100
)
) %>%
build_table(df)
Expand Down