fix proportion ci calc and theme #2

Merged
jared merged 4 commits from feature-prop_int into master 2024-09-19 16:17:33 -04:00
9 changed files with 119 additions and 13 deletions
Showing only changes of commit f4a4ee85a7 - Show all commits
+1 -1
View File
@@ -23,4 +23,4 @@ Encoding: UTF-8
LazyData: true LazyData: true
Suggests: Suggests:
testthat testthat
RoxygenNote: 7.2.3 RoxygenNote: 7.3.2
+4
View File
@@ -2,6 +2,7 @@
export(add_logo) export(add_logo)
export(add_logo_ga) export(add_logo_ga)
export(clopper_pearson)
export(countCleanr) export(countCleanr)
export(countDots) export(countDots)
export(countNA) export(countNA)
@@ -31,6 +32,7 @@ export(safe_ratio)
export(simpleCap) export(simpleCap)
export(star_subs) export(star_subs)
export(theme_civilytics) export(theme_civilytics)
export(waldInterval)
export(z_gap_test) export(z_gap_test)
export(z_univariate) export(z_univariate)
import(ggplot2) import(ggplot2)
@@ -43,6 +45,8 @@ importFrom(grid,rasterGrob)
importFrom(gridExtra,arrangeGrob) importFrom(gridExtra,arrangeGrob)
importFrom(jpeg,readJPEG) importFrom(jpeg,readJPEG)
importFrom(png,readPNG) importFrom(png,readPNG)
importFrom(stats,qbeta)
importFrom(stats,qnorm)
importFrom(stats,runif) importFrom(stats,runif)
importFrom(stringdist,stringsim) importFrom(stringdist,stringsim)
importFrom(stringr,str_count) importFrom(stringr,str_count)
+10
View File
@@ -0,0 +1,10 @@
#' @keywords internal
#' @importFrom stats qbeta
#' @importFrom stats qnorm
"_PACKAGE"
## usethis namespace: start
## usethis namespace: end
NULL
+26 -11
View File
@@ -6,17 +6,15 @@
# https://andrewpwheeler.com/2020/11/30/confidence-intervals-around-proportions/ # https://andrewpwheeler.com/2020/11/30/confidence-intervals-around-proportions/
#' Get a simple Clopper Pearson interval #' Get a simple Clopper Pearson interval
#' #'
#' @param num #' @param num number of successes
#' @param den #' @param den number of trials
#' @param confint #' @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 #' @export
#' clopper_pearson <- function(num, den, conf.level = 0.95) {
#' @examples
clopper_pearson <- function(num, den, confint = 0.95) {
# Same results as binom.test in base R # Same results as binom.test in base R
quant <- (1 - confint) / 2 quant <- (1 - conf.level) / 2
low <- qbeta(quant, num, den-num+1) low <- qbeta(quant, num, den-num+1)
hi <- qbeta(1-quant, num+1, den-num) hi <- qbeta(1-quant, num+1, den-num)
obs <- num/den 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, # z_gap_test_v <- Vectorize(z_gap_test,
# SIMPLIFY = TRUE)# we only want to return a scalar # 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){ waldInterval <- function(x, n, conf.level = 0.95){
p <- x/n p <- x/n
sd <- sqrt(p*((1-p)/n)) sd <- sqrt(p*((1-p)/n))
@@ -60,11 +68,18 @@ waldInterval <- function(x, n, conf.level = 0.95){
ci <- p + z*sd ci <- p + z*sd
names(ci) <- c('lwr', 'upr') names(ci) <- c('lwr', 'upr')
return(ci) 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) { agresti_coull_interval <- function(num, den, conf.level) {
num <- num + 2 num <- num + 2
den <- den + 4 den <- den + 4
return(num/den)
} }
+21
View File
@@ -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
}
+11
View File
@@ -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}
+21
View File
@@ -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
}
+24
View File
@@ -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
}
+1 -1
View File
@@ -1,5 +1,5 @@
% Generated by roxygen2: do not edit by hand % 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} \name{z_univariate}
\alias{z_univariate} \alias{z_univariate}
\title{Calculate a univariate z score by comparing to a population} \title{Calculate a univariate z score by comparing to a population}