data(finch.ind)
# \donttest{
comm <- t(table(ind.plot.finch,1:length(ind.plot.finch)))
comm.sp <- table(sp.finch, ind.plot.finch)
class(comm.sp) <- "matrix"
traits.finch.sp <- apply( apply(traits.finch, 2, scale ), 2,
function(x) tapply(x, sp.finch, mean, na.rm = TRUE))
mat.dist <- (as.matrix(dist(traits.finch.sp))^2)/2
res.rao <- RaoRel(sample = as.matrix(comm.sp), dfunc = mat.dist, dphyl = NULL,
weight = FALSE, Jost = FALSE, structure = NULL)
witRao <- res.rao$FD$Mean_Alpha #overall within species variance
betRao <- res.rao$FD$Beta_add #between species variance
totRao <- res.rao$FD$Gamma #the total variance
witRao+betRao
totRao
#Now let"s take the abundance to calculate Rao diversity.
res.rao.w <- RaoRel(sample = as.matrix(comm.sp), dfunc = mat.dist, dphyl = NULL,
weight = TRUE, Jost = FALSE, structure = NULL)
witRao.w <- res.rao.w$FD$Mean_Alpha #overall within species variance
betRao.w <- res.rao.w$FD$Beta_add #between species variance
totRao.w <- res.rao.w$FD$Gamma #the total variance
witRao.w
betRao.w
#Plot the results
barplot(cbind(c(witRao.w, betRao.w), c(witRao, betRao)),
names.arg = c("abundance" ,"presence"),
legend.text = c("within species", "between species"),
ylab = "Rao", ylim = c(0,10))
#We can do this analysis for each trait separately.
#First we need to replace (or exclude) NA values.
#For this example, we use the package mice to complete the data.
if (requireNamespace("mice", quietly = TRUE)) {
mice_imp <- mice::mice(traits.finch, printFlag = FALSE)
traits.finch.mice <- mice::complete(mice_imp)
traits.finch.mice.sp <- apply(apply(traits.finch.mice, 2, scale), 2,
function(x) tapply(x, sp.finch, mean, na.rm = TRUE))
trait.rao.w <- list()
witRao.w.bytrait <- c()
betRao.w.bytrait <- c()
for (t in 1:4) {
trait.rao.w[[t]] <- RaoRel(sample = as.matrix(comm.sp),
dfunc = (dist(traits.finch.mice.sp[, t])^2) / 2, dphyl = NULL,
weight = TRUE, Jost = FALSE, structure = NULL)
witRao.w.bytrait <- c(witRao.w.bytrait, trait.rao.w[[t]]$FD$Mean_Alpha)
betRao.w.bytrait <- c(betRao.w.bytrait, trait.rao.w[[t]]$FD$Beta_add)
}
barplot(t(cbind(witRao.w.bytrait, betRao.w.bytrait)),
names.arg = colnames(traits.finch),
legend.text = c("within species", "between species"),
ylab = "Rao", ylim = c(0, 1.5))
}
# }
Run the code above in your browser using DataLab