R/tukey_coef.R

Defines functions tukey_coef

Documented in tukey_coef

#' Tukey coefficient
#'
#' Calculates a Tukey's coefficient
#'
#' @param x A vector of numbers
#'
#' @importFrom stats quantile
#' @export
#' @examples
#' tukey_coef(0:20)
#' tukey_coef(stats::rchisq(100, 2))

tukey_coef <- function(x) {
  stopifnot(is.numeric(x))

  qq <- quantile(x, c(0.25, 0.75), names = FALSE, na.rm = TRUE)
  iqr <- diff(qq)
  vapply(x,
         function(x) {
           if(is.na(x)) {
             NA_real_
           } else if(x < qq[1]) {
             (x - qq[1]) / iqr
           } else if(x > qq[2]) {
             (x - qq[2]) / iqr
           } else {
             0.0
           }
         }, double(1),
         USE.NAMES = FALSE)
}
jmbarbone/qpm documentation built on July 25, 2020, 10:41 p.m.