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
7 changed files with 167 additions and 29 deletions
Showing only changes of commit 962f8ea6b7 - Show all commits
+26 -26
View File
@@ -1,26 +1,26 @@
Package: civilytics Package: civilytics
Type: Package Type: Package
Title: Utilities Functions for Civilytics Title: Utilities Functions for Civilytics
Version: 0.1.0 Version: 0.2.0
Author: Jared E. Knowles <jared@civilytics.com> Author: Jared E. Knowles <jared@civilytics.com>
Maintainer: Jared E. Knowles <jared@civilytics.com> Maintainer: Jared E. Knowles <jared@civilytics.com>
Description: House R functions for Civilytics Consulting LLC Description: House R functions for Civilytics Consulting LLC
This package implements a variety of useful functions for creating and This package implements a variety of useful functions for creating and
branding analyses produced by Civilytics Consulting LLC. branding analyses produced by Civilytics Consulting LLC.
License: LGPL (>= 3) License: LGPL (>= 3)
Depends: Depends:
R (>= 2.15.1) R (>= 2.15.1)
Imports: Imports:
ggplot2, ggplot2,
jpeg, jpeg,
png, png,
stringr, stringr,
gridExtra, gridExtra,
tidycensus, tidycensus,
grid, grid,
stringdist stringdist
Encoding: UTF-8 Encoding: UTF-8
LazyData: true LazyData: true
Suggests: Suggests:
testthat testthat
RoxygenNote: 7.3.2 RoxygenNote: 7.3.2
Vendored
+3 -3
View File
@@ -24,11 +24,11 @@ pipeline {
stage('Check') { stage('Check') {
steps { steps {
sh ''' sh '''
R CMD check --no-manual civilytics_0.1.0.tar.gz R CMD check --no-manual civilytics_0.2.0.tar.gz
''' '''
sh ''' sh '''
R CMD INSTALL civilytics_0.1.0.tar.gz R CMD INSTALL civilytics_0.2.0.tar.gz
''' '''
} }
} }
@@ -50,7 +50,7 @@ pipeline {
steps { steps {
sh ''' sh '''
rm -rf civilytics_0.1.0.tar.gz civilytics.Rcheck rm -rf civilytics_0.2.0.tar.gz civilytics.Rcheck
''' '''
} }
} }
+3
View File
@@ -27,11 +27,14 @@ export(pretty_count)
export(pretty_per) export(pretty_per)
export(race_short_names) export(race_short_names)
export(random_round) export(random_round)
export(rnh)
export(round_to_nearest_half)
export(safe_max) export(safe_max)
export(safe_ratio) export(safe_ratio)
export(simpleCap) export(simpleCap)
export(star_subs) export(star_subs)
export(theme_civilytics) export(theme_civilytics)
export(trim_max)
export(waldInterval) export(waldInterval)
export(z_gap_test) export(z_gap_test)
export(z_univariate) export(z_univariate)
+69
View File
@@ -376,3 +376,72 @@ safe_ratio <- function(num, denom) {
y <- num / denom y <- num / denom
return(y) return(y)
} }
#' Take the maximum of a number after trimming values
#'
#' @param vec a numeric vector
#' @param n integer, the number of maximum values to trim before taking the maximum
#'
#' @return the highest value after removing the highest n values
#' @export
#'
#' @examples
#' trim_max(c(10, 10, 10, 9, 8, 7), n = 2)
#' trim_max(c(10, 10, 10, 9, 8, 7), n = 3)
#' trim_max(c(10, 10, 10, 9, 8, 7), n = 4)
trim_max <- function(vec, n) {
# Sort vector ascending
sorted_vec <- sort(vec)
end_point <- length(vec) - n
if (end_point <= 0) {
return(1)
}
# Exclude n largest (most extreme) values
filtered_vec <- sorted_vec[(1:(length(vec)-n))]
# Find the maximum value among excluded values if any exist
max_value <- max(filtered_vec, na.rm = TRUE)
return(max_value)
}
#' Round values to the nearest 0.5
#'
#' @param x a numeric vector to round
#'
#' @return a numeric vector with all elements rounded to 0, 0.5, or 1
#' @export
#'
#' @examples
#' round_to_nearest_half(0.9)
#' round_to_nearest_half(0.7)
#' round_to_nearest_half(0.4)
round_to_nearest_half <- function(x) {
if (x %% 1 == 0) { # If x is already an integer, no change needed
return(as.integer(x))
} else {
decimal_part <- x - floor(x)
if (decimal_part >= 0.25 & decimal_part < 0.75) {
rounded_x <- floor(x) + 0.5
} else {
rounded_x <- round(x, 0)
}
return(rounded_x)
}
}
#' Round values to the nearest 0.5
#'
#' @inheritParams round_to_nearest_half
#'
#' @return a numeric vector with all elements rounded to 0, 0.5, or 1
#' @export
#'
#' @examples
#' rnh(c(0.2, 0.3, 0.4, 0.8, 0.09, 0.9))
rnh <- function(x) {
tmp <- Vectorize(civilytics::round_to_nearest_half)
tmp(x)
}
+20
View File
@@ -0,0 +1,20 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{rnh}
\alias{rnh}
\title{Round values to the nearest 0.5}
\usage{
rnh(x)
}
\arguments{
\item{x}{a numeric vector to round}
}
\value{
a numeric vector with all elements rounded to 0, 0.5, or 1
}
\description{
Round values to the nearest 0.5
}
\examples{
rnh(c(0.2, 0.3, 0.4, 0.8, 0.09, 0.9))
}
+22
View File
@@ -0,0 +1,22 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{round_to_nearest_half}
\alias{round_to_nearest_half}
\title{Round values to the nearest 0.5}
\usage{
round_to_nearest_half(x)
}
\arguments{
\item{x}{a numeric vector to round}
}
\value{
a numeric vector with all elements rounded to 0, 0.5, or 1
}
\description{
Round values to the nearest 0.5
}
\examples{
round_to_nearest_half(0.9)
round_to_nearest_half(0.7)
round_to_nearest_half(0.4)
}
+24
View File
@@ -0,0 +1,24 @@
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/utils.R
\name{trim_max}
\alias{trim_max}
\title{Take the maximum of a number after trimming values}
\usage{
trim_max(vec, n)
}
\arguments{
\item{vec}{a numeric vector}
\item{n}{integer, the number of maximum values to trim before taking the maximum}
}
\value{
the highest value after removing the highest n values
}
\description{
Take the maximum of a number after trimming values
}
\examples{
trim_max(c(10, 10, 10, 9, 8, 7), n = 2)
trim_max(c(10, 10, 10, 9, 8, 7), n = 3)
trim_max(c(10, 10, 10, 9, 8, 7), n = 4)
}