Difference between revisions of "Diversity index"

From Testiwiki
Jump to: navigation, 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)
 
}
 
}
 
##### qD4tapply is the same as the qD but using only a single parameter, #because that is what is required for tapply.
 
 
#qD4tapply <- function(pi, q = wikiq){
 
# return(qD(pi, q))
 
#}
 
  
 
###### 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))
# wj <- rep(wj, times = length(q))
 
 
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:wikiq|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
+
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, wikiq)
+
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



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

+ Show code

Examples

Example 1 to use function

Give your data in R format or leave empty for example data:

Is your data individual data or group abundancies?:

+ Show code

Example 2

Select your data:

Which q values you want to calculate.:
0
1
2
3

+ Show code

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>