#' 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)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.