Difference between revisions of "Diversity index"

From Testiwiki
Jump to: navigation, search
m (Answer)
(Answer: seems to work. A new example)
Line 12: Line 12:
 
'''Actual function ''diversity'''''
 
'''Actual function ''diversity'''''
  
<rcode name="answer">
+
<rcode name="answer"
 +
include="page:OpasnetBaseUtils|name:generic">
  
library(OpasnetBaseUtils)
+
####### qD calculates true diversity for abundance pi with exponent q.
library(xtable)
 
data <- opasnet.csv("5/54/Tigre-data.csv", wiki = "opasnet_en", sep = ",")
 
data <- data[data$Species != 1 & data$Individuals != 0, ]
 
#head(data)
 
species <- data$Species
 
amount <- data$Individuals
 
transect <- data$Transect
 
  
true <- function(pi, q){
+
qD <- function(pi, q){
pi <- pi/sum(pi)
+
pi <- pi/sum(pi)
qD <- rep(NA, length(q))
+
out <- rep(NA, length(q))
for(i in 1:length(q)){
+
for(i in 1:length(q)){
if(q[i] == 1)
+
if(q[i] == 1)
    {qD[i] <- exp(-sum(pi * log(pi)))} else
+
{out[i] <- exp(-sum(pi * log(pi)))} else
    if( q[i] == 999){
+
if( q[i] == 999){
          qD[i] <- 1/max(pi)} else
+
out[i] <- 1/max(pi)} else
    {qD[i] <- (sum(pi^q[i]))^(1 / (1 - q[i]))}}
+
{out[i] <- (sum(pi^q[i]))^(1 / (1 - q[i]))
return(qD)
+
}
 +
}
 +
return(out)
 
}
 
}
  
truelist <- function(pi){
+
##### qD4tapply is the same as the qD but using only a single parameter, because that is what is required for tapply.
return(true(pi, c(0, 1, 2, 999)))
+
 
 +
qD4tapply <- function(pi){
 +
return(qD(pi, q))
 
}
 
}
  
true2 <- function(divj, wj){
+
###### qDa calculates alpha diversity using gamma diversities of each transect.
q <- rep(c(0, 1, 2, 999), each = length(wj))
+
 
wj <- rep(wj, times = 4)
+
qDa <- function(divj, wj = 1, q){
#print(divj)
+
q. <- rep(q, each = length(wj))
#print(wj)
+
# wj <- rep(wj, times = length(q))
#print(q)
+
out <- ifelse(q. == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q.)))^(1/(1 - q.)))
    qDa <- ifelse(q == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q)))^(1/(1 - q)))
+
minqDj <- tapply(out, q., min)
minqDj <- tapply(qDa, q, min)
+
out <- tapply(out, q., sum)^(1-q)
qDa <- tapply(qDa, q, sum)
+
out[q == 999] <- minqDj[q == 999]
qDa <- qDa^(1-c(0, 1, 2, 3))
+
return(out)
qDa[4] <- minqDj[4]
 
return(qDa)
 
 
}
 
}
  
# diversity <- function(amount = rep(1,length(species)), species = 1:length(amount), transect = 1){
+
## There seems to be an error in qDa. What is it?
 +
 
 +
####### diversity is the function for the user. It calculates several diversity
 +
####### indices. Parameters:
 +
##      amount:  the number of individuals of a species in a transect.
 +
##      species:  an identifier for a species
 +
##      transect: an identifier for a transect
 +
##      q:        exponent for the diversity calculation
 +
 
 +
 
 +
diversity <- function(amount = rep(1,length(species)), species = 1:length(amount), transect = 1, q = 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)))
    pij[is.na(pij)] <- 0
+
colnames(pij)[3] <- "pij"
#head(pij)
+
pij <- dropall(pij[!is.na(pij$pij) & pij$pij != 0, ])
colnames(pij)[3] <- "pij"
+
pij$pij <- pij$pij/sum(pij$pij)
  
    pij$pij <- pij$pij/sum(pij$pij)
+
wj <- as.data.frame(as.table(tapply(pij$pij, pij$Transect, sum)))
pij <- pij[pij$pij != 0, ]
+
pi <- as.data.frame(as.table(tapply(pij$pij, pij$Species, sum)))[, 2]
  
#head(pij)
+
colnames(wj) <- c("Transect", "wj")
wj <- as.data.frame(as.table(tapply(pij$pij, pij$Transect, sum)))
+
out <- merge(pij, wj)
#head(wj)
 
colnames(wj) <- c("Transect", "wj")
 
out <- merge(pij, wj)
 
#sum(out$pij)
 
#sum(tapply(out$wj, out$Transect, max))
 
#head(out)
 
out <- tapply(out$pij, out$Transect, truelist)
 
  
#head(out)
+
out <- tapply(out$pij, out$Transect, qD4tapply)
qDj <- data.frame(Zero = 1:nrow(wj), One = NA, Two = NA, Infi = NA)
+
qDj <- rep(NA, nrow(wj)*length(q))
for(i in 1:nrow(wj)){
+
 
qDj[i,] <- out[[i]][1:4]
+
for(i in 1:nrow(wj)){
}
+
for(j in 1:length(q)){
#qDj
+
qDj[i + (j - 1) * nrow(wj)] <- out[[i]][j]
qDj <- c(qDj$Zero, qDj$One, qDj$Two, qDj$Infi)  
+
}
#qDj
+
}
#wj
+
 
qDa <- true2(qDj, wj$wj)
+
out <- qDa(qDj, wj$wj, q)
 +
out2 <- qD(pi, q)
 +
q <- gsub("999", "∞", q)
  
out <- data.frame(Name = c("Alpha diversity with q=0", "Alpha diversity with q=1", "Alpha diversity with q=2", "Alpha diversity with q=∞", "True diversity with q=0", "True diversity with q=1",  
+
outlabel <- c(paste("Alpha diversity with q=", q, sep=""), paste("Gamma diversity with q=", q, sep=""))
    "True diversity with q=2", "True diversity with q=∞", "Richness", "Shannon index",
+
outsymbol <- c(paste(q, "Da", sep=""), paste(q, "D", sep=""))
    "Simpson index", "Inverse Simpson index", "Gini-Simpson index", "Berger-Parker index"),
 
    Symbol = c("0Da", "1Da", "2Da", "∞Da", "0D", "1D", "2D", "∞D", "S", "H' or log(1D)", "λ or 1/(2D)", "1/λ or 2D",
 
    "1-λ or 1-1/(2D)", "1/(∞D)"),
 
    Value = c(qDa, true(pi,0), true(pi,1), true(pi,2), 1/max(pi), length(pi), log(true(pi,1)),
 
    1/true(pi,2), true(pi,2), 1-1/true(pi,2), max(pi)))
 
#return(out)
 
#}
 
  
print(xtable(out), type = 'html')
+
out <- data.frame(Name = c(outlabel, "Richness", "Shannon index",
 +
"Simpson index", "Inverse Simpson index", "Gini-Simpson index", "Berger-Parker index"),  
 +
Symbol = c(outsymbol, "S", "H' or log(1D)", "λ or 1/(2D)", "1/λ or 2D",
 +
"1-λ or 1-1/(2D)", "1/(∞D)"),
 +
Value = c(out, out2, length(pi), log(qD(pi,1)),
 +
1/qD(pi,2), qD(pi,2), 1-1/qD(pi,2), 1/qD(pi,999)))
 +
return(out)
 +
}
  
 
</rcode>
 
</rcode>
  
'''Example to use function
+
'''Example 1 to use function
  
 
<rcode include="page:Diversity_index|name:answer" variables="
 
<rcode include="page:Diversity_index|name:answer" variables="
Line 109: Line 110:
 
if(individual==TRUE) out <- diversity(species = data) else out <- diversity(amount = data)
 
if(individual==TRUE) out <- diversity(species = data) else out <- diversity(amount = data)
 
print(xtable(out), type = 'html')
 
print(xtable(out), type = 'html')
 +
</rcode>
 +
 +
'''Example 2
 +
 +
<rcode include="page:Diversity_index|name:answer|page:OpasnetBaseUtils|name:generic" variables="
 +
name:data|description:Select your data|type:selection|options:'5/54/Tigre-data.csv';Tigre|default:'5/54/Tigre-data.csv'|
 +
name:q|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(xtable)
 +
data <- opasnet.csv(data, wiki = "opasnet_en", sep = ",")
 +
data <- data[data$Species != 1, ]
 +
 +
species <- data$Species
 +
amount <- data$Individuals
 +
transect <- data$Transect
 +
 +
out <- diversity(amount, species, transect, q)
 +
print(xtable(out), type = 'html')
 +
 
</rcode>
 
</rcode>
  

Revision as of 16:43, 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

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>