#' Universal Diversity 3
#'
#' A function which calculates a diversity measure, dependent on the population and the value for
#' q passed to it. This function can be used to calculate the Diversity value for species richness,
#' Simpson diversity, Shannon Entropy and Berger-Parker Index.. It can be called by other functions
#' which pass a population and a value of q to it.
#'
#' @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 generic measure of diversity, dependent on the population and value of q entered
#' @export diversity
#'
#' @examples
universal_diversity3 <- function(population, q) {
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.