Difference between revisions of "Diversity index"
From Testiwiki
(→Answer) |
m (→Answer) |
||
Line 18: | Line 18: | ||
data <- opasnet.csv("5/54/Tigre-data.csv", wiki = "opasnet_en", sep = ",") | data <- opasnet.csv("5/54/Tigre-data.csv", wiki = "opasnet_en", sep = ",") | ||
data <- data[data$Species != 1 & data$Individuals != 0, ] | data <- data[data$Species != 1 & data$Individuals != 0, ] | ||
− | head(data) | + | #head(data) |
species <- data$Species | species <- data$Species | ||
amount <- data$Individuals | amount <- data$Individuals | ||
Line 38: | Line 38: | ||
return(true(pi, c(0, 1, 2, 999))) | return(true(pi, c(0, 1, 2, 999))) | ||
} | } | ||
− | |||
− | |||
true2 <- function(divj, wj){ | true2 <- function(divj, wj){ | ||
− | q <- c | + | q <- rep(c(0, 1, 2, 999), each = length(wj)) |
wj <- rep(wj, times = 4) | wj <- rep(wj, times = 4) | ||
− | qDa <- ifelse(q == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q)))^1/(1 - q)) | + | #print(divj) |
+ | #print(wj) | ||
+ | #print(q) | ||
+ | qDa <- ifelse(q == 1, exp(sum(wj * log(divj))), (sum(wj * divj^(1-q)))^(1/(1 - q))) | ||
minqDj <- tapply(qDa, q, min) | minqDj <- tapply(qDa, q, min) | ||
qDa <- tapply(qDa, q, sum) | qDa <- tapply(qDa, q, sum) | ||
Line 56: | Line 57: | ||
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 | pij[is.na(pij)] <- 0 | ||
− | head(pij) | + | #head(pij) |
colnames(pij)[3] <- "pij" | colnames(pij)[3] <- "pij" | ||
Line 62: | Line 63: | ||
pij <- pij[pij$pij != 0, ] | pij <- pij[pij$pij != 0, ] | ||
− | head(pij) | + | #head(pij) |
wj <- as.data.frame(as.table(tapply(pij$pij, pij$Transect, sum))) | wj <- as.data.frame(as.table(tapply(pij$pij, pij$Transect, sum))) | ||
− | head(wj) | + | #head(wj) |
colnames(wj) <- c("Transect", "wj") | colnames(wj) <- c("Transect", "wj") | ||
out <- merge(pij, wj) | out <- merge(pij, wj) | ||
− | sum(out$pij) | + | #sum(out$pij) |
− | sum(tapply(out$wj, out$Transect, max)) | + | #sum(tapply(out$wj, out$Transect, max)) |
− | head(out) | + | #head(out) |
out <- tapply(out$pij, out$Transect, truelist) | out <- tapply(out$pij, out$Transect, truelist) | ||
− | # | + | #head(out) |
− | |||
− | head(out) | ||
qDj <- data.frame(Zero = 1:nrow(wj), One = NA, Two = NA, Infi = NA) | qDj <- data.frame(Zero = 1:nrow(wj), One = NA, Two = NA, Infi = NA) | ||
for(i in 1:nrow(wj)){ | for(i in 1:nrow(wj)){ | ||
qDj[i,] <- out[[i]][1:4] | qDj[i,] <- out[[i]][1:4] | ||
} | } | ||
+ | #qDj | ||
qDj <- c(qDj$Zero, qDj$One, qDj$Two, qDj$Infi) | qDj <- c(qDj$Zero, qDj$One, qDj$Two, qDj$Infi) | ||
+ | #qDj | ||
+ | #wj | ||
+ | qDa <- true2(qDj, wj$wj) | ||
− | + | 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", | |
− | |||
− | out <- data.frame(Name = c("True diversity with q=0", "True diversity with q=1", | ||
"True diversity with q=2", "True diversity with q=∞", "Richness", "Shannon index", | "True diversity with q=2", "True diversity with q=∞", "Richness", "Shannon index", | ||
"Simpson index", "Inverse Simpson index", "Gini-Simpson index", "Berger-Parker index"), | "Simpson index", "Inverse Simpson index", "Gini-Simpson index", "Berger-Parker index"), | ||
− | Symbol = c("0D", "1D", "2D", "∞D", "S", "H' or log(1D)", "λ or 1/(2D)", "1/λ or 2D", | + | 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)"), | "1-λ or 1-1/(2D)", "1/(∞D)"), | ||
− | Value = c(true(pi,0), true(pi,1), true(pi,2), 1/max(pi), length(pi), log(true(pi,1)), | + | 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))) | 1/true(pi,2), true(pi,2), 1-1/true(pi,2), max(pi))) | ||
#return(out) | #return(out) |
Revision as of 13:18, 8 January 2012
This page is a method.
The page identifier is Op_en5455 |
---|
Moderator:Jouni (see all) |
This page is a stub. You may improve it into a full page, and then a rating bar will appear here. |
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
Example to use function
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>