diff --git a/DESCRIPTION b/DESCRIPTION index 63f2300..edd9d9e 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,4 +23,4 @@ Encoding: UTF-8 LazyData: true Suggests: testthat -RoxygenNote: 7.2.3 +RoxygenNote: 7.3.2 diff --git a/NAMESPACE b/NAMESPACE index 49aafd7..04ba561 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -2,6 +2,7 @@ export(add_logo) export(add_logo_ga) +export(clopper_pearson) export(countCleanr) export(countDots) export(countNA) @@ -31,6 +32,7 @@ export(safe_ratio) export(simpleCap) export(star_subs) export(theme_civilytics) +export(waldInterval) export(z_gap_test) export(z_univariate) import(ggplot2) @@ -43,6 +45,8 @@ importFrom(grid,rasterGrob) importFrom(gridExtra,arrangeGrob) importFrom(jpeg,readJPEG) importFrom(png,readPNG) +importFrom(stats,qbeta) +importFrom(stats,qnorm) importFrom(stats,runif) importFrom(stringdist,stringsim) importFrom(stringr,str_count) diff --git a/R/civilytics-package.R b/R/civilytics-package.R new file mode 100644 index 0000000..c1dcad6 --- /dev/null +++ b/R/civilytics-package.R @@ -0,0 +1,10 @@ +#' @keywords internal +#' @importFrom stats qbeta +#' @importFrom stats qnorm +"_PACKAGE" + +## usethis namespace: start +## usethis namespace: end +NULL + + diff --git a/R/prop_conf.R b/R/prop_conf.R index 6287f2d..5ac963d 100644 --- a/R/prop_conf.R +++ b/R/prop_conf.R @@ -6,17 +6,15 @@ # https://andrewpwheeler.com/2020/11/30/confidence-intervals-around-proportions/ #' Get a simple Clopper Pearson interval #' -#' @param num -#' @param den -#' @param confint +#' @param num number of successes +#' @param den number of trials +#' @param conf.level default 0.95, set the confidence interval to return #' -#' @return +#' @return three values forming the upper and lower bounds of the confidence region and the true value #' @export -#' -#' @examples -clopper_pearson <- function(num, den, confint = 0.95) { +clopper_pearson <- function(num, den, conf.level = 0.95) { # Same results as binom.test in base R - quant <- (1 - confint) / 2 + quant <- (1 - conf.level) / 2 low <- qbeta(quant, num, den-num+1) hi <- qbeta(1-quant, num+1, den-num) obs <- num/den @@ -24,7 +22,6 @@ clopper_pearson <- function(num, den, confint = 0.95) { } -# TODO consider making a vectorized version # z_gap_test_v <- Vectorize(z_gap_test, # SIMPLIFY = TRUE)# we only want to return a scalar @@ -53,6 +50,17 @@ z_univariate <- function(unit_prop, global_prop, unit_denom) { } +#' Calculate a Wald interval +#' +#' @param x the numerator, number of times the event occurs +#' @param n the denominator, the number of trials +#' @param conf.level default 0.95, set the confidence interval to return +#' +#' @return two values forming the upper and lower bounds of the confidence region +#' @export +#' +#' @examples +#' waldInterval(x = 20, n =40) #this will return 0.345 and 0.655 waldInterval <- function(x, n, conf.level = 0.95){ p <- x/n sd <- sqrt(p*((1-p)/n)) @@ -60,11 +68,18 @@ waldInterval <- function(x, n, conf.level = 0.95){ ci <- p + z*sd names(ci) <- c('lwr', 'upr') return(ci) -}#example -#waldInterval(x = 20, n =40) #this will return 0.345 and 0.655 +} +#' Calculate the Agresti-Coull interval +#' +#' @param num a number of successes +#' @param den a number of trials +#' @param conf.level default 0.95, set the confidence interval to return +#' +#' @return an interval agresti_coull_interval <- function(num, den, conf.level) { num <- num + 2 den <- den + 4 + return(num/den) } diff --git a/man/agresti_coull_interval.Rd b/man/agresti_coull_interval.Rd new file mode 100644 index 0000000..dfd54d5 --- /dev/null +++ b/man/agresti_coull_interval.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/prop_conf.R +\name{agresti_coull_interval} +\alias{agresti_coull_interval} +\title{Calculate the Agresti-Coull interval} +\usage{ +agresti_coull_interval(num, den, conf.level) +} +\arguments{ +\item{num}{a number of successes} + +\item{den}{a number of trials} + +\item{conf.level}{default 0.95, set the confidence interval to return} +} +\value{ +an interval +} +\description{ +Calculate the Agresti-Coull interval +} diff --git a/man/civilytics-package.Rd b/man/civilytics-package.Rd new file mode 100644 index 0000000..97f5880 --- /dev/null +++ b/man/civilytics-package.Rd @@ -0,0 +1,11 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/civilytics-package.R +\docType{package} +\name{civilytics-package} +\alias{civilytics} +\alias{civilytics-package} +\title{civilytics: Utilities Functions for Civilytics} +\description{ +House R functions for Civilytics Consulting LLC This package implements a variety of useful functions for creating and branding analyses produced by Civilytics Consulting LLC. +} +\keyword{internal} diff --git a/man/clopper_pearson.Rd b/man/clopper_pearson.Rd new file mode 100644 index 0000000..0fa5b52 --- /dev/null +++ b/man/clopper_pearson.Rd @@ -0,0 +1,21 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/prop_conf.R +\name{clopper_pearson} +\alias{clopper_pearson} +\title{Get a simple Clopper Pearson interval} +\usage{ +clopper_pearson(num, den, conf.level = 0.95) +} +\arguments{ +\item{num}{number of successes} + +\item{den}{number of trials} + +\item{conf.level}{default 0.95, set the confidence interval to return} +} +\value{ +three values forming the upper and lower bounds of the confidence region and the true value +} +\description{ +Get a simple Clopper Pearson interval +} diff --git a/man/waldInterval.Rd b/man/waldInterval.Rd new file mode 100644 index 0000000..4d3652d --- /dev/null +++ b/man/waldInterval.Rd @@ -0,0 +1,24 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/prop_conf.R +\name{waldInterval} +\alias{waldInterval} +\title{Calculate a Wald interval} +\usage{ +waldInterval(x, n, conf.level = 0.95) +} +\arguments{ +\item{x}{the numerator, number of times the event occurs} + +\item{n}{the denominator, the number of trials} + +\item{conf.level}{default 0.95, set the confidence interval to return} +} +\value{ +two values forming the upper and lower bounds of the confidence region +} +\description{ +Calculate a Wald interval +} +\examples{ +waldInterval(x = 20, n =40) #this will return 0.345 and 0.655 +} diff --git a/man/z_univariate.Rd b/man/z_univariate.Rd index 72a2a4b..e197127 100644 --- a/man/z_univariate.Rd +++ b/man/z_univariate.Rd @@ -1,5 +1,5 @@ % Generated by roxygen2: do not edit by hand -% Please edit documentation in R/utils.R +% Please edit documentation in R/prop_conf.R \name{z_univariate} \alias{z_univariate} \title{Calculate a univariate z score by comparing to a population}