diff --git a/NAMESPACE b/NAMESPACE index 1f792a0dc..9d00c0ce2 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -39,6 +39,8 @@ export(proportion_ci_strat_wilson) export(proportion_ci_wald) export(proportion_ci_wilson) export(single_ci_mean) +export(single_ci_sd) +export(single_ci_var) export(starts_with) export(where) import(rlang) diff --git a/R/ard_single_ci.R b/R/ard_single_ci.R index 8d5244952..635d5feda 100644 --- a/R/ard_single_ci.R +++ b/R/ard_single_ci.R @@ -26,7 +26,7 @@ ard_single_ci <- function(data, variables, by = dplyr::group_vars(data), conf.level = 0.95, - method = c("mean_with_T", "mean_with_Z")) { + method = c("mean_with_T", "mean_with_Z", "sd", "var")) { # process inputs ------------------------------------------------------------- cards::process_selectors(data, variables = {{ variables }}, by = {{ by }}) method <- arg_match(method) @@ -41,7 +41,9 @@ ard_single_ci <- function(data, single_ci = switch(method, "mean_with_T" = \(x, ...) single_ci_mean(x, conf.level = conf.level, use_t = TRUE), - "mean_with_Z" = \(x, ...) single_ci_mean(x, conf.level = conf.level, use_t = FALSE) + "mean_with_Z" = \(x, ...) single_ci_mean(x, conf.level = conf.level, use_t = FALSE), + "sd" = \(x, ...) single_ci_sd(x, conf.level = conf.level), + "var"= \(x, ...) single_ci_sd(x, conf.level = conf.level) ) ) ) |> diff --git a/R/single_ci.R b/R/single_ci.R index 9306746fa..2f4cb756d 100644 --- a/R/single_ci.R +++ b/R/single_ci.R @@ -6,7 +6,7 @@ #' @param conf.level (`numeric`)\cr #' a scalar in `(0, 1)` indicating the confidence level. #' Default is `0.95` -#' @param use_t (`logicla`)\cr +#' @param use_t (`logical`)\cr #' a logical to indicated whether to use normal or t distribution. #' Default is t distribution. #' @return Confidence interval of a mean @@ -17,6 +17,8 @@ #' #' single_ci_mean(x, conf.level = 0.9, use_t = TRUE) #' single_ci_mean(x, conf.level = 0.9, use_t = FALSE) +#' single_ci_sd(x, conf.level = 0.9) +#' single_ci_var(x, conf.level = 0.9) NULL #' @describeIn single_ci Calculates the mean confidenence interval by following @@ -57,3 +59,68 @@ single_ci_mean <- function(x, conf.level = 0.95, use_t = TRUE) { glue::glue("Mean Confidence Interval {ifelse(use_t, 'with Student t', 'with Normal')} Distribution") ) } + + +#' @describeIn single_ci Calculates the standard deviation confidence interval by following +#' the usual textbook definition with the chi-sq distribution +#' +#' @export +single_ci_sd <- function(x, conf.level = 0.95) { + # check inputs --------------------------------------------------------------- + check_not_missing(x) + check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE)) + check_scalar_logical(use_t) + + x <- stats::na.omit(x) + + n <- length(x) + sd <- sd(x) + alpha <- 1 - conf.level + crit_value_left <- stats::qchisq(alpha / 2, df = n - 1) + crit_value_right <- stats::qchisq((1 - alpha) / 2, df = n - 1) + + l_ci <- sqrt((n - 1) * sd^2 / crit_value_left ) + u_ci <- sqrt((n - 1) * sd^2 / crit_value_right ) + + list( + N = n, + estimate = sd, + conf.low = l_ci, + conf.high = u_ci, + conf.level = conf.level, + method = + glue::glue("SD Confidence Interval using chi-sq distribution") + ) +} + +#' @describeIn single_ci Calculates the variance deviation confidence interval by following +#' the usual textbook definition with the chi-sq distribution +#' +#' @export +single_ci_var <- function(x, conf.level = 0.95) { + # check inputs --------------------------------------------------------------- + check_not_missing(x) + check_range(conf.level, range = c(0, 1), include_bounds = c(FALSE, FALSE)) + check_scalar_logical(use_t) + + x <- stats::na.omit(x) + + n <- length(x) + var <- var(x) + alpha <- 1 - conf.level + crit_value_left <- stats::qchisq(alpha / 2, df = n - 1) + crit_value_right <- stats::qchisq((1 - alpha) / 2, df = n - 1) + + l_ci <- (n - 1) * var / crit_value_left + u_ci <- (n - 1) * var / crit_value_right + + list( + N = n, + estimate = var, + conf.low = l_ci, + conf.high = u_ci, + conf.level = conf.level, + method = + glue::glue("Variance Confidence Interval using chi-sq distribution") + ) +} diff --git a/man/ard_single_ci.Rd b/man/ard_single_ci.Rd index ce9549ef9..b92548b9a 100644 --- a/man/ard_single_ci.Rd +++ b/man/ard_single_ci.Rd @@ -9,7 +9,7 @@ ard_single_ci( variables, by = dplyr::group_vars(data), conf.level = 0.95, - method = c("mean_with_T", "mean_with_Z") + method = c("mean_with_T", "mean_with_Z", "sd", "var") ) } \arguments{ diff --git a/man/single_ci.Rd b/man/single_ci.Rd index f5c2804c2..e9146cc1a 100644 --- a/man/single_ci.Rd +++ b/man/single_ci.Rd @@ -3,9 +3,15 @@ \name{single_ci} \alias{single_ci} \alias{single_ci_mean} +\alias{single_ci_sd} +\alias{single_ci_var} \title{Functions for Calculating Univariate Confidence Intervals} \usage{ single_ci_mean(x, conf.level = 0.95, use_t = TRUE) + +single_ci_sd(x, conf.level = 0.95) + +single_ci_var(x, conf.level = 0.95) } \arguments{ \item{x}{vector of numeric values} @@ -27,6 +33,12 @@ Functions to calculate different univariate/single statistic confidence interval \item \code{single_ci_mean()}: Calculates the mean confidenence interval by following the usual textbook definition with either the normal or t distribution +\item \code{single_ci_sd()}: Calculates the standard deviation confidence interval by following +the usual textbook definition with the chi-sq distribution + +\item \code{single_ci_var()}: Calculates the variance deviation confidence interval by following +the usual textbook definition with the chi-sq distribution + }} \examples{ \dontshow{if (cards::is_pkg_installed("broom", reference_pkg = "cardx")) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} @@ -34,5 +46,7 @@ x <- 1:10 single_ci_mean(x, conf.level = 0.9, use_t = TRUE) single_ci_mean(x, conf.level = 0.9, use_t = FALSE) +single_ci_sd(x, conf.level = 0.9) +single_ci_var(x, conf.level = 0.9) \dontshow{\}) # examplesIf} }