R/hmean.r

Defines functions hmean

Documented in hmean

#' Harmonic Mean
#'
#' Calculates harmonic mean and its standard error.
#'
#' @param x A vector of positive numbers.
#' @param na.rm A logical (\code{TRUE} or \code{FALSE}) indicating whether
#'  to strip out missing values before computing.
#' @return A list with values \code{mean} and \code{se}.
#' @examples
#'   data(BCI_speed_data)
#'   agoutiData <- subset(BCI_speed_data, species=="agouti")
#'   hmean(agoutiData$speed)
#' @export
#'
hmean <- function(x, na.rm=TRUE){
  if(any(x <= 0))
    return(list(mean=NA, se=NA)) else{
      if(na.rm) x <- na.omit(x)
      mn <- 1/mean(1/x)
      se <- mn^2 * sqrt(var(1/x)/length(x))
      return(list(mean=mn, se=se))
    }
}

Try the sbd package in your browser

Any scripts or data that you put into this service are public.

sbd documentation built on June 22, 2024, 9:50 a.m.