Diversity index: Difference between revisions
Jump to navigation
Jump to search
(→Answer) |
m (→Answer) |
||
Line 16: | Line 16: | ||
####### qD calculates true diversity for abundance pi with exponent q. | ####### qD calculates true diversity for abundance pi with exponent q. | ||
qD <- function(pi, q = q){ | qD <- function(pi, q = q.div){ # q.div is the q used by the diversity function. | ||
pi <- pi/sum(pi) | pi <- pi/sum(pi) | ||
out <- rep(NA, length(q)) | out <- rep(NA, length(q)) | ||
Line 29: | Line 29: | ||
return(out) | return(out) | ||
} | } | ||
###### qDa calculates alpha diversity using gamma diversities of each transect. | ###### qDa calculates alpha diversity using gamma diversities of each transect. | ||
qDa <- function(divj, wj = 1, q){ | qDa <- function(divj, wj = 1, q = q.div){ | ||
q. <- rep(q, each = length(wj)) | q. <- rep(q, each = length(wj)) | ||
out <- ifelse(q. == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q.)))^(1/(1 - q.))) | out <- ifelse(q. == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q.)))^(1/(1 - q.))) | ||
minqDj <- tapply(out, q., min) | minqDj <- tapply(out, q., min) | ||
Line 55: | Line 48: | ||
## species: an identifier for a species | ## species: an identifier for a species | ||
## transect: an identifier for a transect | ## transect: an identifier for a transect | ||
## q: exponent for the diversity calculation | ## q.div: exponent for the diversity calculation | ||
diversity <- function(amount = rep(1,length(species)), species = 1:length(amount), transect = 1, q = 0){ | diversity <- function(amount = rep(1,length(species)), species = 1:length(amount), transect = 1, q.div = 0){ | ||
pij <- as.data.frame(as.table(tapply(amount, data.frame(Transect = transect, Species = species), sum))) | pij <- as.data.frame(as.table(tapply(amount, data.frame(Transect = transect, Species = species), sum))) | ||
Line 73: | Line 66: | ||
out <- tapply(out$pij, out$Transect, qD) | out <- tapply(out$pij, out$Transect, qD) | ||
qDj <- rep(NA, nrow(wj)*length(q)) | qDj <- rep(NA, nrow(wj)*length(q.div)) | ||
for(i in 1:nrow(wj)){ | for(i in 1:nrow(wj)){ | ||
for(j in 1:length(q)){ | for(j in 1:length(q.div)){ | ||
qDj[i + (j - 1) * nrow(wj)] <- out[[i]][j] | qDj[i + (j - 1) * nrow(wj)] <- out[[i]][j] | ||
} | } | ||
} | } | ||
out <- qDa(qDj, wj$wj, q) | out <- qDa(qDj, wj$wj, q.div) | ||
out2 <- qD(pi, q) | out2 <- qD(pi, q.div) | ||
qt <- gsub("999", "∞", q) | qt <- gsub("999", "∞", q.div) | ||
outlabel <- c(paste("Alpha diversity with q=", qt, sep=""), paste("Gamma diversity with q=", qt, sep="")) | outlabel <- c(paste("Alpha diversity with q=", qt, sep=""), paste("Gamma diversity with q=", qt, sep="")) | ||
Line 121: | Line 114: | ||
<rcode include="page:OpasnetBaseUtils|name:generic|page:Diversity_index|name:answer" variables=" | <rcode include="page:OpasnetBaseUtils|name:generic|page:Diversity_index|name:answer" variables=" | ||
name:wikidata|description:Select your data|type:selection|options:'5/54/Tigre-data.csv';Tigre|default:'5/54/Tigre-data.csv'| | name:wikidata|description:Select your data|type:selection|options:'5/54/Tigre-data.csv';Tigre|default:'5/54/Tigre-data.csv'| | ||
name: | name:q.wiki|description:Which q values you want to calculate.|type:checkbox|options:0;0;1;1;2;2;3;3;999;∞|default:0;1;2;999 | ||
"> | "> | ||
library(OpasnetBaseUtils) | library(OpasnetBaseUtils) | ||
Line 131: | Line 124: | ||
amount <- data$Individuals | amount <- data$Individuals | ||
transect <- data$Transect | transect <- data$Transect | ||
q.wiki | |||
out <- diversity(amount, species, transect, | out <- diversity(amount, species, transect, q.wiki) | ||
print(xtable(out), type = 'html') | print(xtable(out), type = 'html') | ||
Revision as of 22:26, 8 January 2012
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page. |
Upload data
|
Question
How to calculate diversity indices?
Answer
Upload your data to Opasnet Base. Use the function diversity to calculate the most common indices.
Actual function diversity
Examples
Example 1 to use function
Example 2
The data should be given in R format as a list of values in parenthesis, beginning with c:
c(3,5,3,5,2,1,3,3,4,2) or equivalently c(0.1,0.2,0.4,0.1,0.2)
where the values are either
- identifiers of the species 1,2,3... in which the individuals belong (one entry per individual), or
- abundancies of species, i.e. proportions of individuals belonging to each species among the whole population (one entry per species).
Rationale
Diversity indices are thoroughly described in Wikipedia.
See also
References
Related files
<mfanonymousfilelist></mfanonymousfilelist>