# Indicator Spcies Analysis を簡単に実行する関数 isa <- function(df, row.data=F){ # $stand, $species, $cover, $community table <- tapply(df$cover, list(df$stand, df$species), sum) # make compositional table table[is.na(table)] <- 0 table <- data.frame(table) # to be coerced to data.frame name.table <- colnames(table) # original names of species colnames(table) <- as.numeric(factor(colnames(table))) # numerical names of species names.table <- data.frame(species=name.table, numeric.sp=colnames(table)) com <- unique(subset(df, select=c(stand, community)))$community # unique stand and community com <- as.numeric(com) # to be coerced to numeric res <- duleg(table, com) # Species Indicator Analysis if(!row.data){ res <- data.frame(numeric.sp=names(res$maxcls), community=res$maxcls, ind.val=round(res$indcls,3), p.value=round(res$pval,4)) res <- merge(names.table, res) res <- res[order(res$community, res$ind.val, res$p.value, decreasing=T),][,-1] } return(res) } # 使用方法 library(akima) # load library library(labdsv) # load library # データ整理 data(bryceveg) # returns a vegetation dataframe df <- as.data.frame.table(as.matrix(bryceveg)) df <- subset(df, Freq!=0) colnames(df) <- c("stand","species","cover") df <- merge(df, data.frame(stand=unique(df$stand), community=sample(1:5, length(unique(df$stand)), replace=TRUE))) isa(df) # 整理した解析結果を出力 isa(df, row.data=T) # 生の解析結果を出力 df.2 <- read.table( "http://kobe.cool.ne.jp/matsut/df.txt", header=T) # 別のデータ isa(df.2) # 整理した解析結果を出力 isa(df.2, row.data=T) # 生の解析結果を出力