+ Show code- Hide code
true <- function(pi, q){
if(q != 1){qD <- (sum(pi^q))^(1 / (1 - q))} else
{qD <- exp(-sum(pi * log(pi)))}
return(qD)
}
true2 <- function(divj, q){
if(q != 1){qDa <- (sum(wj * divj^(1-q)))^1/(1 - q)} else
{qDa <- exp(sum(wj * log(divj)))}
return(qDa)
}
diversity <- function(amount = rep(1,length(species)), species = 1:length(amount)){
pi <- tapply(amount, species, sum)
pi <- pi/sum(pi)
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",
"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",
"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)),
1/true(pi,2), true(pi,2), 1-1/true(pi,2), max(pi)))
return(out)
}
diversity2 <- function(amount = rep(1,length(species)), species = 1:length(amount)){
out <- diversity(amount, species)[1:4, 3]
return(out)
}
library(OpasnetBaseUtils)
library(xtable)
data <- opasnet.csv("5/54/Tigre-data.csv", wiki = "opasnet_en", sep = ",")
data <- data[data$Species != 1 & data$Individuals != 0, ]
head(data)
out <- diversity(species = data$Species, amount = data$Individuals)
#print(xtable(out), type = 'html')
pi <- as.data.frame(as.table(tapply(data$Individuals, data[, c("Transect", "Species")], sum)))
pi[is.na(pi)] <- 0
head(pi)
pij <- pi
colnames(pij)[3] <- "pij"
pij$pij <- pi$Freq/sum(pi$Freq)
head(pij)
wj <- as.data.frame(as.table(tapply(pij$pij, pij$Transect, sum)))
head(wj)
colnames(wj) <- c("Transect", "Weight")
out <- merge(pij, wj)
out <- out[out$pij != 0, ]
out <- tapply(out$pij, out$Transect, diversity2)
out3 <- data.frame(Zero = 1:nrow(wj), One = NA, Two = NA, Infi = NA)
for(i in 1:nrow(wj)){
out3[i,] <- out[[i]][1:4]
}
q <- c(0, 1, 2,
out3 <-
#pi.j <-
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",
"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",
"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)),
1/true(pi,2), true(pi,2), 1-1/true(pi,2), max(pi)))
out
| |