本函数主要用于计算otu丰度表的a多样性计算
#依赖包
library(vegan)
library(picante)
计算函数
alpha_diversity <- function(x, tree = NULL) {
observed_species <- estimateR(x)[1, ]
Chao1 <- estimateR(x)[2, ]
ACE <- estimateR(x)[4, ]
Shannon <- diversity(x, index = 'shannon',base = 2)
Simpson <- diversity(x, index = 'simpson') #注意,这里是Gini-Simpson 指数
goods_Coverage <- 1 - rowSums(x == 1) / rowSums(x)
#保留四位小数
Shannon <- sprintf("%0.4f", Shannon)
Simpson <- sprintf("%0.4f", Simpson)
goods_Coverage <- sprintf("%0.4f", goods_Coverage)
result <- data.frame(observed_species, ACE,Chao1, Shannon, Simpson, goods_Coverage)
if (!is.null(tree)) {
PD_whole_tree <- pd(x, tree, include.root = FALSE)[1]
names(PD_whole_tree) <- 'PD_whole_tree'
result <- cbind(result, PD_whole_tree)
result <- data.frame(observed_species, ACE,Chao1, Shannon, Simpson,
PD_whole_tree ,goods_Coverage)
}
result
}
使用方法
alpha <- alpha_diversity (otu)