Skip to content

Commit

Permalink
Closes #49 add formulas/document the methods of CI interval calculati…
Browse files Browse the repository at this point in the history
…on in LaTeX (#63)

**What changes are proposed in this pull request?**
* Style this entry in a way that can be copied directly into `NEWS.md`.
(#<issue number>, @<username>)

Provide more detail here as needed.
write latex formulas of all the prop ci calculations


--------------------------------------------------------------------------------

Pre-review Checklist (if item does not apply, mark is as complete)
- [x] **All** GitHub Action workflows pass with a ✅
- [x] PR branch has pulled the most recent updates from master branch:
`usethis::pr_merge_main()`
- [x] If a bug was fixed, a unit test was added.
- [x] Code coverage is suitable for any new functions/features
(generally, 100% coverage for new code): `devtools::test_coverage()`
- [x] Request a reviewer

Reviewer Checklist (if item does not apply, mark is as complete)

- [x] If a bug was fixed, a unit test was added.
- [x] Run `pkgdown::build_site()`. Check the R console for errors, and
review the rendered website.
- [x] Code coverage is suitable for any new functions/features:
`devtools::test_coverage()`

When the branch is ready to be merged:
- [x] Update `NEWS.md` with the changes from this pull request under the
heading "`# cards (development version)`". If there is an issue
associated with the pull request, reference it in parentheses at the end
update (see `NEWS.md` for examples).
- [x] **All** GitHub Action workflows pass with a ✅
- [x] Approve Pull Request
- [x] Merge the PR. Please use "Squash and merge" or "Rebase and merge".

---------

Signed-off-by: Zelos Zhu <[email protected]>
Co-authored-by: Daniel Sjoberg <[email protected]>
  • Loading branch information
zdz2101 and ddsjoberg authored Mar 8, 2024
1 parent 59346d5 commit db79851
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
27 changes: 27 additions & 0 deletions R/proportion_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ NULL
#' @describeIn proportion_ci Calculates the Wald interval by following the usual textbook definition
#' for a single proportion confidence interval using the normal approximation.
#'
#' \deqn{\hat{p} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}(1 - \hat{p})}{n}}}
#'
#' @param correct (`logical`)\cr apply continuity correction.
#'
#' @export
Expand Down Expand Up @@ -62,6 +64,10 @@ proportion_ci_wald <- function(x, conf.level = 0.95, correct = FALSE) {
#' @describeIn proportion_ci Calculates the Wilson interval by calling [stats::prop.test()].
#' Also referred to as Wilson score interval.
#'
#' \deqn{\frac{\hat{p} +
#' \frac{z^2_{\alpha/2}}{2n} \pm z_{\alpha/2} \sqrt{\frac{\hat{p}(1 - \hat{p})}{n} +
#' \frac{z^2_{\alpha/2}}{4n^2}}}{1 + \frac{z^2_{\alpha/2}}{n}}}
#'
#' @export
proportion_ci_wilson <- function(x, conf.level = 0.95, correct = FALSE) {
cards::check_pkg_installed("broom", reference_pkg = "cardx")
Expand Down Expand Up @@ -91,6 +97,12 @@ proportion_ci_wilson <- function(x, conf.level = 0.95, correct = FALSE) {

#' @describeIn proportion_ci Calculates the Clopper-Pearson interval by calling [stats::binom.test()].
#' Also referred to as the `exact` method.
#'
#' \deqn{
#' \left( \frac{k}{n} \pm z_{\alpha/2} \sqrt{\frac{\frac{k}{n}(1-\frac{k}{n})}{n} +
#' \frac{z^2_{\alpha/2}}{4n^2}} \right)
#' / \left( 1 + \frac{z^2_{\alpha/2}}{n} \right)}
#'
#' @export
proportion_ci_clopper_pearson <- function(x, conf.level = 0.95) {
cards::check_pkg_installed("broom", reference_pkg = "cardx")
Expand All @@ -112,6 +124,12 @@ proportion_ci_clopper_pearson <- function(x, conf.level = 0.95) {

#' @describeIn proportion_ci Calculates the `Agresti-Coull` interval (created by `Alan Agresti` and `Brent Coull`) by
#' (for 95% CI) adding two successes and two failures to the data and then using the Wald formula to construct a CI.
#'
#' \deqn{
#' \left( \frac{\tilde{p} + z^2_{\alpha/2}/2}{n + z^2_{\alpha/2}} \pm
#' z_{\alpha/2} \sqrt{\frac{\tilde{p}(1 - \tilde{p})}{n} +
#' \frac{z^2_{\alpha/2}}{4n^2}} \right)}
#'
#' @export
proportion_ci_agresti_coull <- function(x, conf.level = 0.95) {
# check inputs ---------------------------------------------------------------
Expand Down Expand Up @@ -149,6 +167,10 @@ proportion_ci_agresti_coull <- function(x, conf.level = 0.95) {

#' @describeIn proportion_ci Calculates the Jeffreys interval, an equal-tailed interval based on the
#' non-informative Jeffreys prior for a binomial proportion.
#'
#' \deqn{\left( \text{Beta}\left(\frac{k}{2} + \frac{1}{2}, \frac{n - k}{2} + \frac{1}{2}\right)_\alpha,
#' \text{Beta}\left(\frac{k}{2} + \frac{1}{2}, \frac{n - k}{2} + \frac{1}{2}\right)_{1-\alpha} \right)}
#'
#' @export
proportion_ci_jeffreys <- function(x, conf.level = 0.95) {
# check inputs ---------------------------------------------------------------
Expand Down Expand Up @@ -190,6 +212,11 @@ proportion_ci_jeffreys <- function(x, conf.level = 0.95) {
#' Xin YA, Su XG. Stratified Wilson and Newcombe confidence intervals
#' for multiple binomial proportions. _Statistics in Biopharmaceutical Research_. 2010;2(3).
#'
#' \deqn{\frac{\hat{p}_j + \frac{z^2_{\alpha/2}}{2n_j} \pm
#' z_{\alpha/2} \sqrt{\frac{\hat{p}_j(1 - \hat{p}_j)}{n_j} +
#' \frac{z^2_{\alpha/2}}{4n_j^2}}}{1 + \frac{z^2_{\alpha/2}}{n_j}}}
#'
#'
#' @param strata (`factor`)\cr variable with one level per stratum and same length as `x`.
#' @param weights (`numeric` or `NULL`)\cr weights for each level of the strata. If `NULL`, they are
#' estimated using the iterative algorithm that
Expand Down
23 changes: 23 additions & 0 deletions man/proportion_ci.Rd

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

0 comments on commit db79851

Please sign in to comment.