forked from pwilliams0/Biogeography_and_global_diversity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1_SR_PD_FR_Mamm.R
146 lines (123 loc) · 4.6 KB
/
1_SR_PD_FR_Mamm.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#=========================================================#
# Calculate species richness (SR), phylogenetic diversity #
# (PD) and functional richness (FR) for mammals #
#=========================================================#
library(tidyverse)
# ----- SPECIES RICHNESS ----------
mamm_SR_cells <- read.csv("Data/mamm_cell_df.csv") %>%
group_by(cell_id) %>%
mutate(SR = n()) %>%
slice(1) %>%
dplyr::select(cell_id, SR)
# Save file
write.csv(mamm_SR_cells, "Data/mamm_SR_cells.csv")
# ----- PHYLOGENETIC DIVERSITY ----------
library(ape)
library(picante)
# Create community matrix
mamm_comm_mat <- read.csv(file="Data/mamm_cell_df.csv",
stringsAsFactors=FALSE) %>%
mutate(present = 1) %>%
dplyr::select(cell_id, names_IUCN, present) %>%
pivot_wider(names_from = names_IUCN,
values_from = present) %>%
replace(.,is.na(.),0) %>%
mutate(cell_id = paste("X",cell_id,sep="")) %>%
column_to_rownames("cell_id") %>%
as.matrix()
mamm_comm_mat <- mamm_comm_mat[,order(colnames(mamm_comm_mat))]
# Load phylogeny, prune for species in community matrix
# Use ultrametric consensus phylogeny derived from 1000 phylogenies
mamm_phylo <- picante::prune.sample(
mamm_comm_mat,
ape::read.tree("Data/Raw/Upham_mean_phylogeny_ultrametric.tree"))
# Calculate PD
start <- Sys.time()
PD_mamm <- picante::pd(mamm_comm_mat,
mamm_phylo,
include.root = TRUE)
Sys.time() - start
# Save file
mamm_PD_cells <- PD_mamm %>%
dplyr::select(PD) %>%
rownames_to_column("cell_id") %>%
mutate(cell_id = substring(cell_id, 2))
write.csv(mamm_PD_cells, "Data/mamm_PD_cells.csv")
# ----- FUNCTIONAL RICHNESS ----------
library(mFD)
# ---------- Make PCoA of traits ----------
# Load trait categories
traits_cat <- read.csv("Data/Raw/mamm_traits_cat.csv")
# Load trait data, one row per species
spp_traits <- read.csv("Data/mamm_cell_df.csv") %>%
group_by(names_IUCN) %>% slice(1) %>% ungroup() %>%
dplyr::select(names_IUCN, Diet.Inv, Diet.Vert,
Diet.Fish, Diet.Scav, Diet.Fruit,
Diet.Nect, Diet.Seed, Diet.Herb,
ForStrat, LogBodyMass) %>%
mutate(ForStrat = as.factor(ForStrat)) %>%
column_to_rownames("names_IUCN")
# Create distance matrix of species using functional traits
start <- Sys.time()
sp_dist_mamm <- mFD::funct.dist(
sp_tr = spp_traits,
tr_cat = traits_cat,
metric = "gower",
scale_euclid = "scale_center", # Standardize variables
ordinal_var = "classic",
weight_type = "equal",
stop_if_NA = TRUE)
Sys.time() - start # [6.0 minutes]
# Create PCoA using distance matrix
start <- Sys.time()
fspaces_quality_mamm <- mFD::quality.fspaces(
sp_dist = sp_dist_mamm,
maxdim_pcoa = 5,
deviation_weighting = c("absolute", "squared"),
fdendro = "average")
Sys.time() - start # [5.8 minutes]
# Save PCoA axes coordinates
sp_faxes_coord_mamm <- fspaces_quality_mamm$"details_fspaces"$"sp_pc_coord"
# Save files
saveRDS(fspaces_quality_mamm, "Data/mamm_func_qual.RDS")
saveRDS(sp_faxes_coord_mamm, "Data/mamm_func_PCoA.RDS")
# Check quality of PCoA
fspaces_quality_mamm$"quality_fspaces"
# Lowest mad score is 5 axes, but that gets to be very computationally demanding
# Use 4 axes going forward
# ---------- Calculate functional richness ----------
mamm_func_PCoA <- readRDS("Data/mamm_func_PCoA.RDS")
# Create community matrix
mamm_comm_mat <- read.csv(file="Data/mamm_cell_df.csv",
stringsAsFactors=FALSE) %>%
mutate(present = 1) %>%
# Remove cells with <= 4 species, need more species than axes
group_by(cell_id) %>%
mutate(SR = n()) %>%
ungroup() %>%
dplyr::filter(SR > 4) %>%
dplyr::select(cell_id, names_IUCN, present) %>%
pivot_wider(names_from = names_IUCN,
values_from = present) %>%
replace(.,is.na(.),0) %>%
mutate(cell_id = paste("X",cell_id,sep="")) %>%
column_to_rownames("cell_id") %>%
as.matrix()
mamm_comm_mat <- mamm_comm_mat[,order(colnames(mamm_comm_mat))]
# Calculate functional richness
start <- Sys.time()
mamm_FD_results <- mFD::alpha.fd.multidim(
sp_faxes_coord = mamm_func_PCoA[,c("PC1","PC2",
"PC3","PC4")],
asb_sp_w = mamm_comm_mat,
ind_vect = c("fric"),
scaling = TRUE, check_input = TRUE, verbose = TRUE,
details_returned = TRUE)$functional_diversity_indices
Sys.time() - start # [1.2 hours]
# Save file
mamm_FR_cells <- mamm_FD_results %>%
mutate(FR = fric) %>%
dplyr::select(FR) %>%
rownames_to_column("cell_id") %>%
mutate(cell_id = substring(cell_id, 2))
write.csv(mamm_FR_cells, "Data/mamm_FR_cells.csv")