R/descriptives.R

Defines functions sterr stdev variance

Documented in stdev sterr variance

#' Simple descriptive statistics
#'
#' Variance and standard deviation by populations
#'
#' @param x A numeric vector
#' @param method Either the "sample" or the "population" calculated
#' @param na.rm Logical, if `TRUE`, `NA` values are removed
#' @param standardized
#'
#' @name descriptives
#' @examples
#' x <- c(1:20)
#' stdev(x)
#' stdev(x, "p");
#' variance(c(x, NA), na.rm = TRUE)
#' sterr(c(x, NA), na.rm = TRUE)

variance <- function(x, method = c("sample", "population"), na.rm = TRUE) {
  stopifnot(is.numeric(x))
  method <- match.arg(method)
  switch(method,
         population = {
           n <- valid_length(x)
           (n - 1) / n * var(x, na.rm = na.rm)
         },
         sample = {
           var(x, na.rm = na.rm)
         })
}

#' @rdname descriptives
#' @export
stdev <- function(x, method = c("sample", "population"), na.rm = FALSE) {
  sqrt(variance(x, method = method, na.rm = na.rm))
}

#' @rdname descriptives
#' @export
sterr <- function(x, method = c("sample", "population"), na.rm = FALSE) {
  stdev(x, method = method, na.rm = na.rm) / sqrt(valid_length(x))
}
jmbarbone/qpm documentation built on July 25, 2020, 10:41 p.m.