R/utils-stats.R

Defines functions mean_se_corrected

Documented in mean_se_corrected

#' Corrected calculation of mean and standard error
#'
#' Adapted from \code{ggplot2::mean_se}. Corrects the denominator for the variance with \eqn{N - 1}.
#'
#' @param x Numeric vector.
#' @param mult Number of multiples of standard error.
#'
#' @return A dataframe with three columns:
#' \describe{
#'     \item{`y`}{ The mean.}
#'     \item{`ymin`}{ The mean minus the multiples of the standard error.}
#'     \item{`ymax`}{ The mean plus the multiples of the standard error.}
#' }
#' @export
#' @examples
#' x <- rnorm(100)
#' mean_se_corrected(x)
mean_se_corrected <- function(x, mult = 1) {
  x <- stats::na.omit(x)
  se <- mult * sqrt(stats::var(x) / (length(x) - 1))
  mean <- mean(x)
  as.data.frame(list(y = mean, ymin = mean - se, ymax = mean + se), n = 1)
}
yjunechoe/penngradlings documentation built on Sept. 6, 2024, 8:13 p.m.