From 73bc6aa1ec6429c6a9d4775bec2e5f8685bc39ed Mon Sep 17 00:00:00 2001 From: Alex Spina Date: Fri, 3 Feb 2023 18:28:57 +0000 Subject: [PATCH] update calls to epikit --- NAMESPACE | 1 + R/cfr.R | 26 ++++++++++++----------- R/gt_wrappers.R | 18 ++++++++-------- R/gt_wrappers_internal.R | 35 +++++++++++++++++-------------- man/gtsummary_wrappers.Rd | 20 +++++++++--------- tests/testthat/test-gt_wrappers.R | 16 +++++++------- 6 files changed, 61 insertions(+), 55 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 5b35b3a..dfbab89 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -25,6 +25,7 @@ importFrom(dplyr,select) importFrom(dplyr,summarise) importFrom(dplyr,summarise_all) importFrom(dplyr,ungroup) +importFrom(epikit,merge_ci_df) importFrom(rlang,"!!") importFrom(rlang,".data") importFrom(rlang,":=") diff --git a/R/cfr.R b/R/cfr.R index a2c8184..1391d07 100644 --- a/R/cfr.R +++ b/R/cfr.R @@ -27,13 +27,15 @@ #' the total value across all groups. #' #' @param digits if `mergeCI = TRUE`, this determines how many digits are printed -#' +#' #' @return a data frame with five columns that represent the numerator, #' denominator, rate, lower bound, and upper bound. #' #' - `attack_rate()`: cases, population, ar, lower, upper #' - `case_fatality_rate()`: deaths, population, cfr, lower, upper -#' +#' +#' @importFrom epikit merge_ci_df +#' #' @export #' #' @rdname attack_rate @@ -43,13 +45,13 @@ #' print(ar <- attack_rate(10, 50), digits = 4) # 20% attack rate #' #' # print them inline using `fmt_ci_df()` -#' fmt_ci_df(ar) +#' epikit::fmt_ci_df(ar) #' #' # Alternatively, if you want one column for the CI, use `mergeCI = TRUE` #' attack_rate(10, 50, mergeCI = TRUE, digits = 2) # 20% attack rate #' #' print(cfr <- case_fatality_rate(1, 100), digits = 2) # CFR of 1% -#' fmt_ci_df(cfr) +#' epikit::fmt_ci_df(cfr) #' #' # using a data frame #' if (require("outbreaks")) { @@ -68,7 +70,7 @@ attack_rate <- function(cases, population, conf_level = 0.95, res <- proportion(cases, population, multiplier = multiplier, conf_level = conf_level) colnames(res) <- c("cases", "population", "ar", "lower", "upper") if (mergeCI == TRUE) { - res <- merge_ci_df(res, digits = digits) + res <- epikit::merge_ci_df(res, digits = digits) } res } @@ -80,7 +82,7 @@ case_fatality_rate <- function(deaths, population, conf_level = 0.95, res <- proportion(deaths, population, multiplier = multiplier, conf_level = conf_level) colnames(res) <- c("deaths", "population", "cfr", "lower", "upper") if (mergeCI == TRUE) { - res <- merge_ci_df(res, digits = digits) + res <- epikit::merge_ci_df(res, digits = digits) } res } @@ -106,11 +108,11 @@ case_fatality_rate_df <- function(x, deaths, group = NULL, conf_level = 0.95, # This creates a list column for the case fatality rate based on the # calculated deaths and population before... so this means that # THE ORDER OF THE STATEMENTS MATTER - # + # # Wed Feb 19 09:25:26 2020 --------------------------------------------- - # This was modified to count the population for the non-missing cases, - # assuming the deaths columns would either be TRUE, FALSE, or NA for - # a death, recovery, or undetermined. + # This was modified to count the population for the non-missing cases, + # assuming the deaths columns would either be TRUE, FALSE, or NA for + # a death, recovery, or undetermined. res <- dplyr::summarise( x, !!quote(deaths) := sum(!!qdeath, na.rm = TRUE), @@ -142,7 +144,7 @@ case_fatality_rate_df <- function(x, deaths, group = NULL, conf_level = 0.95, !!qgroup := factor("Total"), deaths = tot$deaths, population = tot$population, - cfr = tot$cfr, + cfr = tot$cfr, .name_repair = "minimal" ) # merge CI gives different numbers of columns, this accounts for that. @@ -166,7 +168,7 @@ mortality_rate <- function(deaths, population, conf_level = 0.95, est_label <- paste0("mortality per ", scales::number(multiplier)) colnames(res) <- c("deaths", "population", est_label, "lower", "upper") if (mergeCI == TRUE) { - res <- merge_ci_df(res, digits = digits) + res <- epikit::merge_ci_df(res, digits = digits) } res } diff --git a/R/gt_wrappers.R b/R/gt_wrappers.R index 214c091..1afbe3e 100644 --- a/R/gt_wrappers.R +++ b/R/gt_wrappers.R @@ -1,7 +1,7 @@ -# gtsummary and epikit wrapper functions +# gtsummary wrapper functions -#' An attack rate wrapper function (using gtsummary and epikit packages)that takes +#' An attack rate wrapper function (using the gtsummary package)that takes #' a gtsummary object and returns a gtsummary object with attack rate (per given #' multiple) with 95% confidence interval #' @@ -16,7 +16,7 @@ #' function (e.g. illness). #' #'#'@param population the number of individuals in the population, passed to -#'`epikit::mortality_rate` +#'`epitabulate::mortality_rate` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -91,7 +91,7 @@ add_mr <- function(gts_object, return(gts_object) } -#' An attack rate wrapper function (using gtsummary and epikit packages)that takes +#' An attack rate wrapper function (using the gtsummary package)that takes #' a gtsummary object and returns a gtsummary object withattack rate (per given #' multiple) with 95% confidence interval #' @@ -104,12 +104,12 @@ add_mr <- function(gts_object, #' function (e.g. illness). #' #'#'@param population the number of individuals in the population, passed to -#'`epikit::attack_rate` +#'`epitabulate::attack_rate` #' #'@param multiplier The base by which to multiply the output: # '1: multiplier = 1: ratio between 0 and 1; # '2: multiplier = 100:proportion; -# '3: multiplier = 10^4: x per 10,000 people; passed to `epikit::attack_rate` +# '3: multiplier = 10^4: x per 10,000 people; passed to `epitabulate::attack_rate` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -185,7 +185,7 @@ add_ar <- function(gts_object, -#' An case fatality rate wrapper function (using gtsummary and epikit packages) +#' An case fatality rate wrapper function (using the gtsummary package) #' that takes a gtsummary object and returns a gtsummary object with number #' of deaths, case fatality rate, and 95% confidence interval. #' @@ -198,7 +198,7 @@ add_ar <- function(gts_object, #' (e.g. illness). #' #' @param deaths_var the name of a logical column in the data that indicates that the case died, -#' is passed as the first argument to `epikit::case_fatality_rate_df` +#' is passed as the first argument to `epitabulate::case_fatality_rate_df` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -463,7 +463,7 @@ add_crosstabs <- function( #' function (e.g. illness). #' #'#'@param population the number of individuals in the population, passed to -#'`epikit::mortality_rate` +#'`epitabulate::mortality_rate` #' #' @param ... additional params that may be passed from gtsummary functions. #' diff --git a/R/gt_wrappers_internal.R b/R/gt_wrappers_internal.R index 2a9beb3..480d75c 100644 --- a/R/gt_wrappers_internal.R +++ b/R/gt_wrappers_internal.R @@ -2,7 +2,7 @@ #' A case fatality rate wrapper function to be passed to the gtsummary::add_stat #' function, which returns a data frame with a single row to be used with -#' dichotomous data or overall data.Calls epikit::case_fatality_rate_df. +#' dichotomous data or overall data.Calls epitabulate::case_fatality_rate_df. #' #' @param data A data frame, passed by the gtsummary::add_stat function. #' @@ -13,7 +13,7 @@ #' (eov.g. illness). #' #' @param deaths_var the name of a logical column in the data that indicates that the case died, -#' is passed as the first argument to `epikit::case_fatality_rate_df` +#' is passed as the first argument to `epitabulate::case_fatality_rate_df` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -22,6 +22,9 @@ #' #' @rdname gtsummary_wrappers #' @import dplyr +#' @importFrom epikit merge_ci_df +#' +#' #' add_gt_cfr_stat_label <- function(data, variable, by, deaths_var, ...) { @@ -32,7 +35,7 @@ add_gt_cfr_stat_label <- function(data, variable, by, deaths_var, ...) { } stat_new <- data %>% - epikit::case_fatality_rate_df( + epitabulate::case_fatality_rate_df( deaths = data[[deaths_var]], mergeCI = TRUE) %>% dplyr::mutate(deaths = formatC(deaths, digits = 0, format = "f")) %>% @@ -47,7 +50,7 @@ add_gt_cfr_stat_label <- function(data, variable, by, deaths_var, ...) { #' A case fatality rate wrapper function to be passed to the gtsummary::add_stat function, #' which returns a data frame with multiple rows to be used when location is set -#' to "level" for multi-level categorical data. Calls epikit::case_fatality_rate_df. +#' to "level" for multi-level categorical data. Calls epitabulate::case_fatality_rate_df. #' #' @param data A data frame, passed by the gtsummary::add_stat function. #' @@ -81,14 +84,14 @@ add_gt_cfr_stat_level <- function(data, variable, by, deaths_var, ...) { dplyr::filter(variable %in% variable & !is.na(stat_0)) var_levels <- unique(var_dt$label) - # create rlang::enquo objects to use in epikit::case_fatality_rate_df + # create rlang::enquo objects to use in epitabulate::case_fatality_rate_df deaths_sym <- as.symbol(deaths_var) qdeaths <- rlang::enquo(deaths_sym) var_sym <- as.symbol(variable) qvariable <- rlang::enquo(var_sym) stat_new <- data %>% - epikit::case_fatality_rate_df(deaths = !!qdeaths, group = !!qvariable , mergeCI = TRUE) %>% + epitabulate::case_fatality_rate_df(deaths = !!qdeaths, group = !!qvariable , mergeCI = TRUE) %>% dplyr::filter(!!qvariable %in% var_levels) %>% dplyr::mutate(deaths = formatC(deaths, digits = 0, format = "f")) %>% dplyr::mutate(cfr = formatC(cfr, digits = 2, format = "f")) %>% @@ -102,7 +105,7 @@ add_gt_cfr_stat_level <- function(data, variable, by, deaths_var, ...) { #' An attack rate wrapper function to be passed to the gtsummary::add_stat #' function, which returns a data frame with a single row to be used with -#' dichotomous data or overall data. Calls epikit::attack_rate. +#' dichotomous data or overall data. Calls epitabulate::attack_rate. #' #' @param data A data frame, passed by the gtsummary::add_stat function. #' @@ -115,7 +118,7 @@ add_gt_cfr_stat_level <- function(data, variable, by, deaths_var, ...) { #'@param multiplier The base by which to multiply the output: # '1: multiplier = 1: ratio between 0 and 1; # '2: multiplier = 100:proportion; -# '3: multiplier = 10^4: x per 10,000 people; passed to `epikit::attack_rate` +# '3: multiplier = 10^4: x per 10,000 people; passed to `epitabulate::attack_rate` #' #'@param drop_total whether or not to include the population column; default TRUE #' @@ -152,7 +155,7 @@ add_gt_attack_rate_stat_label <- ar_label <- paste0("AR (per ", format(multiplier, big.mark=","), ")") cols_rename <- setNames("ar", ar_label) - ar <- epikit::attack_rate(cases = cases, + ar <- epitabulate::attack_rate(cases = cases, population = population, multiplier = multiplier) %>% epikit::merge_ci_df(e = 3) %>% @@ -180,7 +183,7 @@ add_gt_attack_rate_stat_label <- #' An attack rate wrapper function to be passed to the gtsummary::add_stat function, #' which returns a data frame with multiple rows to be used when location is set -#' to "level" for multi-level categorical data. Calls epikit::attack_rate. +#' to "level" for multi-level categorical data. Calls epitabulate::attack_rate. #' #' @param data A data frame, passed by the gtsummary::add_stat function. #' @@ -193,7 +196,7 @@ add_gt_attack_rate_stat_label <- #'@param multiplier The base by which to multiply the output: # '1: multiplier = 1: ratio between 0 and 1; # '2: multiplier = 100:proportion; -# '3: multiplier = 10^4: x per 10,000 people; passed to `epikit::attack_rate` +# '3: multiplier = 10^4: x per 10,000 people; passed to `epitabulate::attack_rate` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -245,7 +248,7 @@ add_gt_attack_rate_level <- ar_label <- paste0("AR (per ", format(multiplier, big.mark=","), ")") cols_rename <- setNames("ar", ar_label) - ar <- epikit::attack_rate(cases = counts$case_n, + ar <- epitabulate::attack_rate(cases = counts$case_n, population = population, multiplier = multiplier) %>% epikit::merge_ci_df(e = 3) %>% # merge the lower and upper CI into one column @@ -295,7 +298,7 @@ add_gt_mortality_rate_stat_label <- deaths <- sum(data[[deaths_var]]) mr_label <- paste0("MR (per ", format(multiplier, big.mark=","), ")") cols_rename <- setNames("mortality per 10 000", mr_label) - mr <- epikit::mortality_rate(deaths = deaths, + mr <- epitabulate::mortality_rate(deaths = deaths, population = population, multiplier = multiplier) %>% epikit::merge_ci_df(e = 3) %>% @@ -325,7 +328,7 @@ add_gt_mortality_rate_stat_label <- #' A mortality rate wrapper function to be passed to the gtsummary::add_stat function, #' which returns a data frame with multiple rows to be used when location is set -#' to "level" for multi-level categorical data. Calls epikit::attack_rate. +#' to "level" for multi-level categorical data. Calls epitabulate::attack_rate. #' #' @param data A data frame, passed by the gtsummary::add_stat function. #' @@ -338,7 +341,7 @@ add_gt_mortality_rate_stat_label <- #'@param multiplier The base by which to multiply the output: # '1: multiplier = 1: ratio between 0 and 1; # '2: multiplier = 100:proportion; -# '3: multiplier = 10^4: x per 10,000 people; passed to `epikit::attack_rate` +# '3: multiplier = 10^4: x per 10,000 people; passed to `epitabulate::attack_rate` #' #' @param ... additional params that may be passed from gtsummary functions. #' @@ -397,7 +400,7 @@ add_gt_mortality_rate_level <- function(data, mr_label <- paste0("MR (per ", format(multiplier, big.mark=","), ")") cols_rename <- setNames("mortality per 10 000", mr_label) - mr <- epikit::mortality_rate(deaths = counts$deaths_n, + mr <- epitabulate::mortality_rate(deaths = counts$deaths_n, population = population, multiplier = multiplier) %>% epikit::merge_ci_df(e = 3) %>% # merge the lower and upper CI into one column diff --git a/man/gtsummary_wrappers.Rd b/man/gtsummary_wrappers.Rd index c013e87..901e018 100644 --- a/man/gtsummary_wrappers.Rd +++ b/man/gtsummary_wrappers.Rd @@ -12,7 +12,7 @@ \alias{add_gt_attack_rate_stat_label} \alias{add_gt_attack_rate_level} \alias{add_gt_mortality_rate_level} -\title{An attack rate wrapper function (using gtsummary and epikit packages)that takes +\title{An attack rate wrapper function (using the gtsummary package)that takes a gtsummary object and returns a gtsummary object with attack rate (per given multiple) with 95\% confidence interval} \usage{ @@ -105,7 +105,7 @@ add_gt_mortality_rate_level( \item{gts_object}{A data frame, passed by the gtsummary::add_stat function} \item{deaths_var}{the name of a logical column in the data that indicates that the case died, -is passed as the first argument to \code{epikit::case_fatality_rate_df}} +is passed as the first argument to \code{epitabulate::case_fatality_rate_df}} \item{multiplier}{The base by which to multiply the output:} @@ -167,15 +167,15 @@ a single-row gtsummary object with attack rate results cases, population, attack rate, and 95\% confidence interval. } \description{ -An attack rate wrapper function (using gtsummary and epikit packages)that takes +An attack rate wrapper function (using the gtsummary package)that takes a gtsummary object and returns a gtsummary object with attack rate (per given multiple) with 95\% confidence interval -An attack rate wrapper function (using gtsummary and epikit packages)that takes +An attack rate wrapper function (using the gtsummary package)that takes a gtsummary object and returns a gtsummary object withattack rate (per given multiple) with 95\% confidence interval -An case fatality rate wrapper function (using gtsummary and epikit packages) +An case fatality rate wrapper function (using the gtsummary package) that takes a gtsummary object and returns a gtsummary object with number of deaths, case fatality rate, and 95\% confidence interval. @@ -190,21 +190,21 @@ dimensions (will add to this later.) A case fatality rate wrapper function to be passed to the gtsummary::add_stat function, which returns a data frame with a single row to be used with -dichotomous data or overall data.Calls epikit::case_fatality_rate_df. +dichotomous data or overall data.Calls epitabulate::case_fatality_rate_df. A case fatality rate wrapper function to be passed to the gtsummary::add_stat function, which returns a data frame with multiple rows to be used when location is set -to "level" for multi-level categorical data. Calls epikit::case_fatality_rate_df. +to "level" for multi-level categorical data. Calls epitabulate::case_fatality_rate_df. An attack rate wrapper function to be passed to the gtsummary::add_stat function, which returns a data frame with a single row to be used with -dichotomous data or overall data. Calls epikit::attack_rate. +dichotomous data or overall data. Calls epitabulate::attack_rate. An attack rate wrapper function to be passed to the gtsummary::add_stat function, which returns a data frame with multiple rows to be used when location is set -to "level" for multi-level categorical data. Calls epikit::attack_rate. +to "level" for multi-level categorical data. Calls epitabulate::attack_rate. A mortality rate wrapper function to be passed to the gtsummary::add_stat function, which returns a data frame with multiple rows to be used when location is set -to "level" for multi-level categorical data. Calls epikit::attack_rate. +to "level" for multi-level categorical data. Calls epitabulate::attack_rate. } diff --git a/tests/testthat/test-gt_wrappers.R b/tests/testthat/test-gt_wrappers.R index 79110bc..fe6ad5c 100644 --- a/tests/testthat/test-gt_wrappers.R +++ b/tests/testthat/test-gt_wrappers.R @@ -237,7 +237,7 @@ test_that("cfr calculation returns gtsummary object and correct results with cat # function: add_gt_case_fatality_rate expected_cfr <- linelist_cleaned %>% filter(patient_facility_type == "Inpatient") %>% - epikit::case_fatality_rate_df(deaths = DIED, group = gender, mergeCI = TRUE) %>% + epitabulate::case_fatality_rate_df(deaths = DIED, group = gender, mergeCI = TRUE) %>% mutate(cfr = formatC(cfr, 2, format = "f")) gt_cfr <- linelist_cleaned %>% @@ -280,7 +280,7 @@ test_that("cfr calculation returns gtsummary object and correct results with cat mutate(cfr = formatC(cfr, digits = 2, format = "f")) expected_cfr <- linelist_cleaned %>% filter(patient_facility_type == "Inpatient") %>% - epikit::case_fatality_rate_df(deaths = DIED, group = gender, mergeCI = TRUE) %>% + epitabulate::case_fatality_rate_df(deaths = DIED, group = gender, mergeCI = TRUE) %>% mutate(cfr = formatC(cfr, 2, format = "f")) gt_cfr <- linelist_cleaned %>% @@ -436,7 +436,7 @@ test_that("attack rate calculation returns gtsummary object and correct results mutate(ar = formatC(ar, 2, format = "f")) # attack rate for each group - expected_ar_lev <- epikit::attack_rate(cases$cases, cases$total, multiplier = 10000) %>% + expected_ar_lev <- epitabulate::attack_rate(cases$cases, cases$total, multiplier = 10000) %>% epikit::merge_ci_df(e = 3) %>% dplyr::mutate(cases = as.character(cases)) %>% mutate(ar = formatC(ar, 2, format = "f")) @@ -594,7 +594,7 @@ test_that("mortality rate calculation returns gtsummary object and correct resul dplyr::filter(DIED == TRUE) # attack rate for each group - expected_mr_lev <- epikit::mortality_rate(deaths$deaths, deaths$total, multiplier = 10000) %>% + expected_mr_lev <- epitabulate::mortality_rate(deaths$deaths, deaths$total, multiplier = 10000) %>% epikit::merge_ci_df(e = 3) %>% dplyr::mutate(deaths = as.character(deaths)) %>% dplyr::mutate(mr = `mortality per 10 000`) %>% @@ -603,7 +603,7 @@ test_that("mortality rate calculation returns gtsummary object and correct resul deaths_count <- sum(linelist_cleaned$DIED) total_pop <- nrow(linelist_cleaned) - expected_mr <- epikit::mortality_rate( deaths_count, total_pop, multiplier = 10000) %>% + expected_mr <- epitabulate::mortality_rate( deaths_count, total_pop, multiplier = 10000) %>% epikit::merge_ci_df(e = 3) @@ -652,7 +652,7 @@ test_that("mortality rate calculation returns gtsummary object and correct resul population_arg <- deaths$total * 15 # attack rate for each group - expected_mr_lev <- epikit::mortality_rate(deaths$deaths, population_arg, multiplier = 10000) %>% + expected_mr_lev <- epitabulate::mortality_rate(deaths$deaths, population_arg, multiplier = 10000) %>% epikit::merge_ci_df(e = 3) %>% dplyr::mutate(deaths = as.character(deaths)) %>% dplyr::mutate(mr = `mortality per 10 000`) %>% @@ -661,7 +661,7 @@ test_that("mortality rate calculation returns gtsummary object and correct resul deaths_count <- sum(linelist_cleaned$DIED) total_pop <- sum(population_arg) - expected_mr <- epikit::mortality_rate( deaths_count, total_pop, multiplier = 10000) %>% + expected_mr <- epitabulate::mortality_rate( deaths_count, total_pop, multiplier = 10000) %>% epikit::merge_ci_df(e = 3) @@ -718,7 +718,7 @@ test_that("attack rate calculation returns gtsummary object and correct results mutate(ar = formatC(ar, 2, format = "f")) # attack rate for each group - expected_ar_lev <- epikit::attack_rate( + expected_ar_lev <- epitabulate::attack_rate( cases$cases, population_arg, multiplier = 10000) %>%