R/universal_diversity4.R

Defines functions universal_diversity4

Documented in universal_diversity4

#' Universal Diversity 4
#'
#' This function can accept a value for population, and a value for q, and returns a universal diversity
#' measure for the population based on the value of q. There is also a check to see whether q has more
#' than one value, and if it does will stop and produce an error message.
#'
#' @param population - a set of population counts, containing the number of individuals of each species within the population
#' @param q a diversity measure
#'
#' @return diversity - a diversity index which is dependent on the value of q which has been supplied
#' @export
#'
#' @examples
universal_diversity4 <- function(population, q) {

  length.q <- length(q)
  if(length.q > 1) {
    stop("error - q has more than one value - q can only have one value")
  }
  species.proportions <- population / sum(population)
  non.zero.prop <- species.proportions[species.proportions > 0]

  if(q == 1) {
    diversity <- exp(-sum(non.zero.prop * log(non.zero.prop)))
  } else {
    if(q == Inf) {
      diversity <- 1/max(non.zero.prop)
    } else {
      qd.proportions <- non.zero.prop ^ q
      diversity <- sum(qd.proportions) ^ (1/(1-q))
    }
  }
  return(diversity)
}
EdieBishop/ProgInRBCIfunc documentation built on Dec. 23, 2019, 10:16 p.m.