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

adding proportion CIs #9

Merged
merged 33 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
b9b940d
adding proportion CIs
ddsjoberg Jan 23, 2024
8659a56
spelling
ddsjoberg Jan 23, 2024
0e8e72c
style
ddsjoberg Jan 23, 2024
5a87753
styling
ddsjoberg Jan 23, 2024
8d3400f
Update .lintr
ddsjoberg Jan 23, 2024
779b7ac
style
ddsjoberg Jan 23, 2024
55b675c
Create ard_proportion_ci.md
ddsjoberg Jan 23, 2024
c3db199
Update test-ard_proportion_ci.R
ddsjoberg Jan 23, 2024
cb94568
Update test-ard_proportion_ci.R
ddsjoberg Jan 23, 2024
6e57aa3
snap updates
ddsjoberg Jan 23, 2024
abb2e1c
Update .lintr
ddsjoberg Jan 23, 2024
0c00842
Update test-ard_proportion_ci.R
ddsjoberg Jan 23, 2024
ac3ab98
prop updates
ddsjoberg Jan 23, 2024
d6daa55
style
ddsjoberg Jan 23, 2024
ec46e4b
Update .lintr
ddsjoberg Jan 23, 2024
2b12f64
Update .lintr
ddsjoberg Jan 24, 2024
2a34ab7
Update .lintr
ddsjoberg Jan 24, 2024
7f1be1b
Merge commit '0d67567c7957153935258b20a52f27b289e116ac'
ddsjoberg Jan 24, 2024
0aa88ca
Update ard_proportion_ci.R
ddsjoberg Jan 24, 2024
dfa9db2
Update DESCRIPTION
ddsjoberg Jan 24, 2024
9601d38
updates
ddsjoberg Jan 24, 2024
1cb0d83
doc updates
ddsjoberg Jan 24, 2024
733acdf
docs
ddsjoberg Jan 24, 2024
1a6789f
Update reexports.R
ddsjoberg Jan 24, 2024
df66aba
Merge branch 'main' into ard_proportion_ci
ddsjoberg Jan 24, 2024
e29bf18
Merge branch 'main' into ard_proportion_ci
ddsjoberg Jan 24, 2024
f6796e9
Merge commit 'd5458db56a033d468d8070ff9b4a8a30db095256'
ddsjoberg Feb 3, 2024
27ccfd2
Merge branch 'main' into ard_proportion_ci
ddsjoberg Feb 4, 2024
e07ac0c
Merge commit '7978fbd94631089352318c07a05398888e6beefa'
ddsjoberg Feb 9, 2024
a707f58
updates
ddsjoberg Feb 9, 2024
1a046fd
Merge branch 'main' into ard_proportion_ci
ddsjoberg Feb 13, 2024
78f986a
Merge branch 'main' into ard_proportion_ci
ddsjoberg Feb 13, 2024
369294e
Merge branch 'main' into ard_proportion_ci
ddsjoberg Feb 13, 2024
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
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ Imports:
cards (>= 0.0.0.9024),
cli (>= 3.6.1),
dplyr (>= 1.1.2),
glue (>= 1.6.2),
rlang (>= 1.1.1),
tidyr (>= 1.3.0)
Suggests:
broom (>= 1.0.5),
spelling,
testthat (>= 3.2.0)
testthat (>= 3.2.0),
withr
Remotes:
insightsengineering/cards
Config/Needs/website: insightsengineering/nesttemplate
Expand Down
7 changes: 7 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(ard_chisqtest)
export(ard_fishertest)
export(ard_paired_ttest)
export(ard_paired_wilcoxtest)
export(ard_proportion_ci)
export(ard_ttest)
export(ard_wilcoxtest)
export(contains)
Expand All @@ -16,6 +17,12 @@ export(last_col)
export(matches)
export(num_range)
export(one_of)
export(proportion_ci_agresti_coull)
export(proportion_ci_clopper_pearson)
export(proportion_ci_jeffreys)
export(proportion_ci_strat_wilson)
export(proportion_ci_wald)
export(proportion_ci_wilson)
export(starts_with)
export(where)
import(rlang)
Expand Down
82 changes: 82 additions & 0 deletions R/ard_proportion_ci.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#' ARD Proportion Confidence Intervals
#'
#' `r lifecycle::badge('experimental')`\cr
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is determining whether or not any of these are "experimental" ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, good question. I don't know! 🤷🏼 I think I marked it this way because I migrated these utilities from tern without full documentation of the methods, so wanted to hedge a bit.

#' Calculate confidence intervals for proportions.
#'
#' @inheritParams cards::ard_categorical
ddsjoberg marked this conversation as resolved.
Show resolved Hide resolved
#' @param variables ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' columns to include in summaries. Columns must be class `<logical>`
#' or `<numeric>` values coded as `c(0, 1)`.
#' @param by ([`tidy-select`][dplyr::dplyr_tidy_select])\cr
#' columns to stratify calculations by
#' @param conf.level (`numeric`)\cr
#' a scalar in `(0, 1)` indicating the confidence level.
#' Default is `0.95`
#' @param method (`string`)\cr
#' string indicating the type of confidence interval to calculate.
#' Must be one of `r formals(ard_proportion_ci)[["method"]] |> eval() |> shQuote()`.
#' See `?proportion_ci` for details.
#' @param strata,weights,max.iterations arguments passed to `proportion_ci_strat_wilson()`,
#' when `method='strat_wilson'`
#'
#' @return an ARD data frame
#' @export
#'
#' @examples
#' ard_proportion_ci(mtcars, variables = c(vs, am), method = "wilson")
ard_proportion_ci <- function(data, variables, by = dplyr::group_vars(data),
conf.level = 0.95,
strata,
weights = NULL,
max.iterations = 10,
method = c(
"waldcc", "wald", "clopper-pearson",
"wilson", "wilsoncc",
"strat_wilson", "strat_wilsoncc",
"agresti-coull", "jeffreys"
)) {
# process inputs -------------------------------------------------------------
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)
}

# calculate confidence intervals ---------------------------------------------
cards::ard_complex(
data = data,
variables = {{ variables }},
by = {{ by }},
statistics =
~ 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"
)
}
Loading