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

update calls to epikit #28

Merged
merged 1 commit into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,":=")
Expand Down
26 changes: 14 additions & 12 deletions R/cfr.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")) {
Expand All @@ -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
}
Expand All @@ -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
}
Expand All @@ -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),
Expand Down Expand Up @@ -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.
Expand All @@ -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
}
Expand Down
18 changes: 9 additions & 9 deletions R/gt_wrappers.R
Original file line number Diff line number Diff line change
@@ -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
#'
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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
#'
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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.
#'
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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.
#'
Expand Down
35 changes: 19 additions & 16 deletions R/gt_wrappers_internal.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#'
Expand All @@ -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.
#'
Expand All @@ -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, ...) {

Expand All @@ -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")) %>%
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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")) %>%
Expand All @@ -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.
#'
Expand All @@ -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
#'
Expand Down Expand Up @@ -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) %>%
Expand Down Expand Up @@ -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.
#'
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) %>%
Expand Down Expand Up @@ -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.
#'
Expand All @@ -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.
#'
Expand Down Expand Up @@ -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
Expand Down
20 changes: 10 additions & 10 deletions man/gtsummary_wrappers.Rd

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

Loading