Compute community-level FRic for all species, combine all PCA objects into a named list, export PCoA coordinates, species–IUCN table, TPD lists, and an eigenvalue summary table for supplementary material.
phenoBird <- read.csv("data/processed/phenoBirdsImputedREADY.csv", stringsAsFactors = FALSE)
colnames(phenoBird)[1] <- "scientificNameStd"
rownames(phenoBird) <- phenoBird[, 1]
# Taxonomy table
taxo <- phenoBird[, c(2, 3, 4)]
taxo$Genus <- vapply(strsplit(rownames(taxo), "_"), function(x) x[1], character(1L))
taxo$GenusSpecies <- rownames(taxo)
colnames(taxo) <- c("class", "order", "family", "genus", "genusspecies")
write.csv(taxo, "data/processed/Taxo_Birds.csv", row.names = TRUE)
PCA_file <- c("data/processed/PCA_Birds_MLD.rds","data/processed/PCA_Birds_M.rds",
"data/processed/PCA_Birds_D.rds","data/processed/PCA_Birds_L.rds")
PCA_file_OK <- sub("\\.rds$", "_OK.rds", PCA_file)
TPD_file <- c("data/processed/TPD_Birds_LMD.rds","data/processed/TPD_Birds_M.rds",
"data/processed/TPD_Birds_D.rds","data/processed/TPD_Birds_L.rds")
TPD_file_bio <- c("data/processed/Birds_TPDs_sdggs7_MLD.rds","data/processed/Birds_TPDs_sdggs7_M.rds",
"data/processed/Birds_TPDs_sdggs7_D.rds","data/processed/Birds_TPDs_sdggs7_L.rds")
for (i in seq_along(PCA_file)) {
PCA <- readRDS(PCA_file[i])
TPDsAux <- readRDS(TPD_file[i])
species <- rownames(PCA$PCoA$vectors)
species <- species[species %in% TPDsAux$data$species]
occurences <- matrix(1, ncol = length(species), nrow = 1, dimnames = list("ALL", species))
TPDc_occ <- TPD::TPDc(TPDsAux, occurences)
PCA$ALLFRic <- TPDRichness(TPDc_occ)$communities$FRichness
PCA$ALLDensity <- densityProfileTPD(TPDc_occ)
TPDs_sdggs7 <- readRDS(TPD_file_bio[i])
TPDc_occ_bio <- TPD::TPDc(TPDs_sdggs7, occurences)
PCA$ALLFRicBiogeo <- TPDRichness(TPDc_occ_bio)$communities$FRichness
saveRDS(PCA, file = PCA_file_OK[i])
}
type <- c("MLD", "M", "D", "L")
PCA_Birds <- setNames(lapply(PCA_file_OK, readRDS), type)
saveRDS(PCA_Birds, "data/processed/PCA_Birds.rds")
PCA <- readRDS("data/processed/PCA_Birds.rds")
coords <- list(locomotion = PCA$M$PCoA$vectors,
diet = PCA$D$PCoA$vectors,
reproduction = PCA$L$PCoA$vectors)
coords <- lapply(coords, function(x) {
colnames(x) <- paste0("PC", seq_len(ncol(x)))
x <- as.data.frame(x); x$species <- rownames(x); x
})
saveRDS(coords, "data/processed/pcoa_coords.rds")
species_df <- phenoBird[, c("scientificNameStd", "category")]
colnames(species_df) <- c("species", "iucn")
species_df <- na.omit(species_df[!species_df$iucn %in% c("DD","EW","EX","RE"), ])
saveRDS(species_df, "data/processed/species_table.rds")
tpd_lists <- list(locomotion = readRDS("data/processed/TPD_Birds_M.rds"),
diet = readRDS("data/processed/TPD_Birds_D.rds"),
reproduction = readRDS("data/processed/TPD_Birds_L.rds"))
saveRDS(tpd_lists, "data/processed/tpd_lists.rds")
table_eigen_clean <- do.call(rbind, lapply(names(PCA), function(name) {
tab <- PCA[[name]]$PCoA$values[, c(1, 2, 4)]
tab[, 2:3] <- 100 * tab[, 2:3]
tab <- round(tab, 3)
tab <- cbind(Space = name, Axis = rownames(tab), tab)
colnames(tab) <- c("TraitSpace","Axis","Eigenvalue","%Variance","%Cumulative")
tab
}))
dir.create("results/tables", showWarnings = FALSE, recursive = TRUE)
write.csv(table_eigen_clean, "results/tables/Annexe_Table_Eigenvalues.csv", row.names = FALSE)