diff --git a/main/coverage-report/index.html b/main/coverage-report/index.html index 38f8ba32..948292eb 100644 --- a/main/coverage-report/index.html +++ b/main/coverage-report/index.html @@ -107,8 +107,8 @@
set_cli_abort_call()
check_not_missing(x)
check_binary(x)
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
check_class(x = correct, "logical")
check_scalar(correct)
x <- stats::na.omit(x)
n <- length(x)
p_hat <- mean(x)
z <- stats::qnorm((1 + conf.level) / 2)
q_hat <- 1 - p_hat
correction_factor <- ifelse(correct, 1 / (2 * n), 0)
err <- z * sqrt(p_hat * q_hat) / sqrt(n) + correction_factor
l_ci <- max(0, p_hat - err)
u_ci <- min(1, p_hat + err)
list(
N = n,
estimate = p_hat,
conf.low = l_ci,
conf.high = u_ci,
conf.level = conf.level,
method =
glue::glue("Wald Confidence Interval {ifelse(correct, 'with', 'without')} continuity correction")
set_cli_abort_call()
check_pkg_installed(pkg = "broom")
check_not_missing(x)
check_binary(x)
check_class(x = correct, "logical")
check_scalar(correct)
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
x <- stats::na.omit(x)
n <- length(x)
y <- stats::prop.test(x = sum(x), n = n, correct = correct, conf.level = conf.level)
list(N = n, conf.level = conf.level) |>
utils::modifyList(val = broom::tidy(y) |> as.list()) |>
utils::modifyList(
list(
method =
glue::glue("Wilson Confidence Interval {ifelse(correct, 'with', 'without')} continuity correction")
set_cli_abort_call()
check_pkg_installed(pkg = "broom")
check_not_missing(x)
check_binary(x)
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
x <- stats::na.omit(x)
n <- length(x)
y <- stats::binom.test(x = sum(x), n = n, conf.level = conf.level)
list(N = n, conf.level = conf.level) |>
utils::modifyList(val = broom::tidy(y) |> as.list()) |>
utils::modifyList(list(method = "Clopper-Pearson Confidence Interval"))
set_cli_abort_call()
check_not_missing(x)
check_binary(x)
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
x <- stats::na.omit(x)
n <- length(x)
x_sum <- sum(x)
z <- stats::qnorm((1 + conf.level) / 2)
x_sum_tilde <- x_sum + z^2 / 2
n_tilde <- n + z^2
p_tilde <- x_sum_tilde / n_tilde
q_tilde <- 1 - p_tilde
err <- z * sqrt(p_tilde * q_tilde) / sqrt(n_tilde)
l_ci <- max(0, p_tilde - err)
u_ci <- min(1, p_tilde + err)
list(
N = n,
estimate = mean(x),
conf.low = l_ci,
conf.high = u_ci,
conf.level = conf.level,
method = "Agresti-Coull Confidence Interval"
set_cli_abort_call()
check_not_missing(x)
check_binary(x)
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
x <- stats::na.omit(x)
n <- length(x)
x_sum <- sum(x)
alpha <- 1 - conf.level
l_ci <- ifelse(
x_sum == 0,
0,
stats::qbeta(alpha / 2, x_sum + 0.5, n - x_sum + 0.5)
u_ci <- ifelse(
x_sum == n,
1,
stats::qbeta(1 - alpha / 2, x_sum + 0.5, n - x_sum + 0.5)
list(
N = n,
estimate = mean(x),
conf.low = l_ci,
conf.high = u_ci,
conf.level = conf.level,
method = glue::glue("Jeffreys Interval")
set_cli_abort_call()
check_not_missing(x)
check_not_missing(strata)
check_binary(x)
check_class(correct, "logical")
check_scalar(correct)
check_class(strata, "factor")
check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE))
check_scalar(conf.level)
is_na <- is.na(x) | is.na(strata)
x <- x[!is_na]
strata <- strata[!is_na]
if (!inherits(x, "logical")) x <- as.logical(x)
if (all(x) || all(!x)) {
tbl <- table(factor(x, levels = c(FALSE, TRUE)), strata, useNA = "no")
n_strata <- length(unique(strata))
do_iter <- FALSE
if (is.null(weights)) {
weights <- rep(1 / n_strata, n_strata) # Initialization for iterative procedure
do_iter <- TRUE
if (!is_scalar_integerish(max.iterations) || max.iterations < 1) {
check_range(weights, range = c(0, 1), include_bounds = c(TRUE, TRUE))
sum_weights <- sum(weights) |>
round() |>
as.integer()
if (sum_weights != 1L || abs(sum_weights - sum(weights)) > sqrt(.Machine$double.eps)) {
xs <- tbl["TRUE", ]
ns <- colSums(tbl)
use_stratum <- (ns > 0)
ns <- ns[use_stratum]
xs <- xs[use_stratum]
ests <- xs / ns
vars <- ests * (1 - ests) / ns
strata_qnorm <- .strata_normal_quantile(vars, weights, conf.level)
weights_new <- if (do_iter) {
.update_weights_strat_wilson(vars, strata_qnorm, weights, ns, max.iterations, conf.level)$weights
strata_conf.level <- 2 * stats::pnorm(strata_qnorm) - 1
ci_by_strata <- Map(
function(x, n) {
suppressWarnings(stats::prop.test(x, n, correct = correct, conf.level = strata_conf.level)$conf.int)
x = xs,
n = ns
lower_by_strata <- sapply(ci_by_strata, "[", 1L)
upper_by_strata <- sapply(ci_by_strata, "[", 2L)
lower <- sum(weights_new * lower_by_strata)
upper <- sum(weights_new * upper_by_strata)
list(
N = length(x),
estimate = mean(x),
conf.low = lower,
conf.high = upper,
conf.level = conf.level,
weights = if (do_iter) weights_new else NULL,
method =
glue::glue("Stratified Wilson Confidence Interval {ifelse(correct, 'with', 'without')} continuity correction")
compact()
is.logical(x) || (is_integerish(x) && is_empty(setdiff(x, c(0, 1, NA))))
summands <- weights^2 * vars
sqrt(sum(summands)) / sum(sqrt(summands)) * stats::qnorm((1 + conf.level) / 2)
it <- 0
diff_v <- NULL
while (it < max.iterations) {
it <- it + 1
weights_new_t <- (1 + strata_qnorm^2 / n_per_strata)^2
weights_new_b <- (vars + strata_qnorm^2 / (4 * n_per_strata^2))
weights_new <- weights_new_t / weights_new_b
weights_new <- weights_new / sum(weights_new)
strata_qnorm <- .strata_normal_quantile(vars, weights_new, conf.level)
diff_v <- c(diff_v, sum(abs(weights_new - initial_weights)))
if (diff_v[length(diff_v)] < tol) break
initial_weights <- weights_new
if (it == max.iterations) {
list(
"n_it" = it,
"weights" = weights_new,
"diff_v" = diff_v
check_not_missing(data)
UseMethod("ard_categorical_ci")
set_cli_abort_call()
check_dots_empty()
check_pkg_installed(pkg = "broom")
cards::process_selectors(data, variables = {{ variables }}, by = {{ by }})
method <- arg_match(method)
if (method %in% c("strat_wilson", "strat_wilsoncc")) {
cards::process_selectors(data, strata = strata)
check_scalar(strata)
cards::process_formula_selectors(
data[variables],
value = value
check_not_missing(variables)
if (is_empty(variables)) {
map(
variables,
function(variable) {
levels <- .unique_values_sort(data, variable = variable, value = value[[variable]])
.calculate_ard_proportion(
data = .as_dummy(data, variable = variable, levels = levels, by = by, strata = strata),
variables = c(everything(), -all_of(c(by, strata))),
by = all_of(by),
method = method,
conf.level = conf.level,
strata = strata,
weights = weights,
max.iterations = max.iterations
dplyr::left_join(
dplyr::select(., "variable") |>
dplyr::distinct() |>
dplyr::mutate(variable_level = as.list(.env$levels)),
by = "variable"
dplyr::mutate(variable = .env$variable) |>
dplyr::relocate("variable_level", .after = "variable")
dplyr::bind_rows()
cards::ard_complex(
data = data,
variables = {{ variables }},
by = {{ by }},
statistic =
~ list(
prop_ci =
switch(method,
"waldcc" = \(x, ...) proportion_ci_wald(x, conf.level = conf.level, correct = TRUE),
"wald" = \(x, ...) proportion_ci_wald(x, conf.level = conf.level, correct = FALSE),
"wilsoncc" = \(x, ...) proportion_ci_wilson(x, conf.level = conf.level, correct = TRUE),
"wilson" = \(x, ...) proportion_ci_wilson(x, conf.level = conf.level, correct = FALSE),
"clopper-pearson" = \(x, ...) proportion_ci_clopper_pearson(x, conf.level = conf.level),
"agresti-coull" = \(x, ...) proportion_ci_agresti_coull(x, conf.level = conf.level),
"jeffreys" = \(x, ...) proportion_ci_jeffreys(x, conf.level = conf.level),
"strat_wilsoncc" = \(x, data, ...) {
proportion_ci_strat_wilson(x,
strata = data[[strata]], weights = weights,
max.iterations = max.iterations,
conf.level = conf.level, correct = TRUE
"strat_wilson" = \(x, data, ...) {
proportion_ci_strat_wilson(x,
strata = data[[strata]], weights = weights,
max.iterations = max.iterations,
conf.level = conf.level, correct = FALSE
dplyr::mutate(
context = "proportion_ci"
unique_levels <-
if (is.logical(data[[variable]])) c(TRUE, FALSE)
else if (is.factor(data[[variable]])) factor(levels(data[[variable]]), levels = levels(data[[variable]]))
else unique(data[[variable]]) |> sort()
if (!is_empty(value) && !value %in% unique_levels) {
if (!is_empty(value)) {
unique_levels <- value
unique_levels
map(levels, ~ data[[variable]] == .x) |>
set_names(paste0("this_is_not_a_column_name_anyone_would_choose_", variable, "_", levels, "...")) %>%
{dplyr::tibble(!!!.)} |> # styler: off
dplyr::bind_cols(data[c(by, strata)])