# Article: FUNCTIONAL GROUP-DEPENDENT EFFECTS OF BIRD COMMUNITIES ON A GRADIENT OF WEEVIL-CAUSED DAMAGE IN EUCALYPTUS PLANTATIONS # Analyses: Calculating functional diversity indices # Author: Ricardo Ceia # Date: 2021-11-04 rm(list = ls()) # Clear environment # Setting working directory #### path <- "C:/Associations between weevil damage and birds/Data" setwd(path) getwd() # Generating Gower dissimilarity from the trait table #### library(openxlsx) traits.raw <- read.xlsx("Dataset.xlsx",sheet=1,colNames=T,rowNames=T) # Importing trait data traits <- within(traits.raw, rm("functional_insectivores")) # Removing functional insectivores column for the dissimilarity calculation str(traits) # Checking the original column type # and then adjusting columns type to numerical, or categorical traits$phenology <- as.factor(traits$phenology) traits$habitat_specialization <- as.factor(traits$habitat_specialization) traits$adult_diet <- as.factor(traits$adult_diet) traits$foraging_guild <- as.factor(traits$foraging_guild) traits$body_mass <- as.numeric(traits$body_mass) traits[traits=="Unknown"] <- NA # Replace Unknown with NA str(traits) # Making sure all trait categories have changed as desired, and all cells still contain values. library("FD") gow <- as.dist(gowdis(traits)) # Computing Gower dissimilarity write.csv(as.matrix(gow), file=paste(path, 'FD_GowerDis.csv', sep='/')) # Un-comment to save gower dissimilarity matrix as a .csv file plot(hclust(gow, method = "ward.D")) # Creating a dendrogram pdf(paste(paste("FD_Dendrogram",Sys.Date(),".pdf"), sep='/'), height=7, width=11) # Saving the dendrogram as a figure dev.off() # Computing functional diversity indices #### abund <- read.xlsx("Dataset.xlsx",sheet=2,colNames=T,rowNames=T) # Importing abundance data str(abund) # Checking the original column type rownames(traits) == colnames(abund) # Checking if species names are the same traits and abund dim(traits) # The row total in traits should equal the column number in abund dim(abund) functdiv <- dbFD(traits, abund, asym.bin = NULL, corr = c("none"), calc.FRic = TRUE, m = "max", stand.FRic = FALSE, scale.RaoQ = FALSE, calc.FGR = FALSE, clust.type = "kmeans", km.inf.gr = 2, km.sup.gr = nrow(x) - 1, km.iter = 100, km.crit = c("calinski", "ssi"), calc.CWM = FALSE, calc.FDiv = TRUE, dist.bin = 2, print.pco = FALSE, messages = TRUE) # Calculating functional diversity indices write.csv(functdiv$FRic, paste(path, "FD_FRic.csv", sep="/")) # Saving FRic as a .csv file write.csv(functdiv$FEve, paste(path, "FD_FEve.csv", sep="/")) # Saving FEve as a .csv file write.csv(functdiv$FDiv, paste(path, "FD_FDiv.csv", sep="/")) # Saving FDiv as a .csv file write.csv(functdiv$FDis, paste(path, "FD_FDis.csv", sep="/")) # Saving FDis as a .csv file write.csv(functdiv$FRaoQ, paste(path, "FD_FRaoQ.csv", sep="/")) # Saving FRaoQ as a .csv file rm(list = ls()) # Clear environment dev.off() # Clear plots