forked from pwilliams0/Biogeography_and_global_diversity
-
Notifications
You must be signed in to change notification settings - Fork 0
/
2_FuncB_Bird_34.R
69 lines (58 loc) · 2.46 KB
/
2_FuncB_Bird_34.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
#==================================================================#
# Calculate functional beta diversity for birds, cell groups 3 & 4 #
#==================================================================#
library(tidyverse)
library(mFD)
# Split cells into 4 groups, every fourth cell
cell_nums <- unique(read.csv(file="Data/bird_cell_df.csv",
stringsAsFactors=FALSE)$cell_id)
group_1 <- cell_nums[c(TRUE, FALSE, FALSE, FALSE)]
group_2 <- cell_nums[c(FALSE, TRUE, FALSE, FALSE)]
group_3 <- cell_nums[c(FALSE, FALSE, TRUE, FALSE)]
group_4 <- cell_nums[c(FALSE, FALSE, FALSE, TRUE)]
# Select set of cells to calculate functional beta diversity
cell_list <- sort(c(group_3, group_4))
# Load functional traits PCoA
bird_func_PCoA <- readRDS("Data/bird_func_PCoA.RDS")
# Create community matrix
bird_comm_mat <- read.csv(file="Data/bird_cell_df.csv",
stringsAsFactors=FALSE) %>%
mutate(present = 1) %>%
# Remove cells with <= 4 species
group_by(cell_id) %>%
mutate(SR = n()) %>%
ungroup() %>%
dplyr::filter(SR > 4,
cell_id %in% cell_list) %>%
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()
bird_comm_mat <- bird_comm_mat[,order(colnames(bird_comm_mat))]
print("Done loading data")
# Calculate functional beta diversity
start <- Sys.time()
bird_fb <- mFD::beta.fd.multidim(
sp_faxes_coord = bird_func_PCoA[ , c("PC1", "PC2",
"PC3", "PC4")],
asb_sp_occ = bird_comm_mat,
check_input = TRUE,
beta_family = c("Sorensen"),
details_returned = TRUE)
end <- Sys.time()
time_diff <- end - start
print("Done calculating functional beta diversity 34")
print(time_diff)
saveRDS(bird_fb, "Data/bird_fb_34.RDS")
#write.table(as.matrix(bird_fb$pairasb_fbd_indices$sor_diss),
# file="Data/bird_fb_sor_34.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
write.table(as.matrix(bird_fb$pairasb_fbd_indices$sor_turn),
file="Data/bird_fb_turn_34.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
#write.table(as.matrix(bird_fb$pairasb_fbd_indices$sor_nest),
# file="Data/bird_fb_nest_34.txt", col.names=TRUE, row.name=TRUE, sep="\t", quote=FALSE)
print("Done with bird_34")
# [Runtime 45 hrs]
# [62.0 GB memory used]