Skip to content

Commit

Permalink
add sd and var
Browse files Browse the repository at this point in the history
  • Loading branch information
zdz2101 committed Mar 13, 2024
1 parent 95a7548 commit 0672b6f
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 4 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
6 changes: 4 additions & 2 deletions R/ard_single_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
)
)
) |>
Expand Down
69 changes: 68 additions & 1 deletion R/single_ci.R
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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")
)
}
2 changes: 1 addition & 1 deletion man/ard_single_ci.Rd

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

14 changes: 14 additions & 0 deletions man/single_ci.Rd

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

0 comments on commit 0672b6f

Please sign in to comment.