-
Notifications
You must be signed in to change notification settings - Fork 0
/
03.03.SES-calculation_merging.R
161 lines (139 loc) · 4.54 KB
/
03.03.SES-calculation_merging.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
#### recommended machine: 01####
#### packages ----
library("tidyverse")
library("tidyr")
library("data.table")
#### load data ----
load("02.data/header_sPlot3.0.RData")
load("02.data/DT_sPlot3.0.RData")
load("02.data/pruned.trees.traitsp.RData")
# add species richness for each plot, SR is based on species present in the phylogeny and TRY
Plots <- DT2 %>%
left_join(., DT2 %>% group_by(PlotObservationID) %>%
summarize_at(.vars = "Abundance", .funs = sum, na.rm = T) %>%
rename("sum.Abundance" = "Abundance"),
by = "PlotObservationID"
) %>%
mutate(Abu = Abundance / sum.Abundance) %>%
filter(Species %in% sPlot.in.Tree$Species) %>%
group_by(PlotObservationID) %>%
summarise_at(.vars = "Abu", .funs = sum, na.rm = T) %>%
filter(Abu >= .5) %>%
pull(PlotObservationID)
header <- header %>%
filter(PlotObservationID %in% Plots) %>%
left_join(
DT2 %>%
filter(Species %in% sPlot.in.Tree$Species) %>%
group_by(PlotObservationID) %>%
summarise(species.richness = n_distinct(Species)),
by = "PlotObservationID"
)
rm(DT2, PD.out0)
# the same for the sPlotOpen data
load("02.data/sPlotOpen.Rdata")
Plots.open <- DT2.oa %>%
left_join(., DT2.oa %>% group_by(PlotObservationID) %>%
summarize_at(.vars = "Original_abundance", .funs = sum, na.rm = T) %>%
rename("sum.Abundance" = "Original_abundance"),
by = "PlotObservationID"
) %>%
mutate(Abu = Original_abundance / sum.Abundance) %>%
filter(Species %in% sPlot.in.Tree$Species) %>%
group_by(PlotObservationID) %>%
summarise_at(.vars = "Abu", .funs = sum, na.rm = T) %>%
filter(Abu >= .5) %>%
pull(PlotObservationID)
header.oa <- header.oa %>%
filter(PlotObservationID %in% Plots.open) %>%
left_join(
DT2.oa %>%
filter(Species %in% sPlot.in.Tree$Species) %>%
group_by(PlotObservationID) %>%
summarise(species.richness = n_distinct(Species)),
by = "PlotObservationID"
)
rm(DT2.oa, CWM_CWV.oa, metadata.oa, reference.oa)
load("02.data/01.PD-values.RData")
PD.open <- readRDS("02.data/01a.PD-values-sPlotOpen.Rds")
load("02.data/01.FD-values.RData")
FD.open <- readRDS("02.data/01a.FD-values-sPlotOpen.Rds")
clim.var <- readRDS("02.data/01b.climate-data.RDS")
clim.open <- readRDS("02.data/01b.climate-data-sPlotOpen.RDS")
rm(FD.null.matrix, PD.null.matrix)
# merge data
df1 <- header %>%
left_join(., clim.var %>%
dplyr::select(-c("Longitude", "Latitude")),
by = "PlotObservationID"
) %>%
left_join(., PD.indices,
by = "PlotObservationID"
) %>%
left_join(., PD.null %>%
mutate(SR.null = as.numeric(SR.null)),
by = c("species.richness" = "SR.null")
) %>%
left_join(., FD.indices,
by = "PlotObservationID"
) %>%
left_join(., FD.null %>%
mutate(SR.null = as.numeric(SR.null)),
by = c("species.richness" = "SR.null")
)
rm(header)
df.open <- header.oa %>%
left_join(., clim.open %>%
dplyr::select(-c("Longitude", "Latitude")),
by = "PlotObservationID"
) %>%
left_join(., PD.open,
by = "PlotObservationID"
) %>%
left_join(., PD.null %>%
mutate(SR.null = as.numeric(SR.null)),
by = c("species.richness" = "SR.null")
) %>%
left_join(., FD.open,
by = "PlotObservationID"
) %>%
left_join(., FD.null %>%
mutate(SR.null = as.numeric(SR.null)),
by = c("species.richness" = "SR.null")
)
rm(header.oa)
#### indices ----
df1$SES.RQEP <- (df1$RaoD.phyl - df1$mean.RQE.phyl) / df1$sd.RQE.phyl
df1$SES.RQEF <- (df1$RQE.MULTI - df1$mean.RQE.MULTI) / df1$sd.RQE.MULTI
df1$SES.MPD <- (df1$MPD - df1$mean.MPD) / df1$sd.MPD
df1$SES.FDis <- (df1$FDis.MULTI - df1$mean.FDis.MULTI) / df1$sd.FDis.MULTI
df.open$SES.RQEP <- (df.open$RaoD.phyl - df.open$mean.RQE.phyl) / df.open$sd.RQE.phyl
df.open$SES.RQEF <- (df.open$RQE.MULTI - df.open$mean.RQE.MULTI) / df.open$sd.RQE.MULTI
df.open$SES.MPD <- (df.open$MPD - df.open$mean.MPD) / df.open$sd.MPD
df.open$SES.FDis <- (df.open$FDis.MULTI - df.open$mean.FDis.MULTI) / df.open$sd.FDis.MULTI
df1.1 <- df1 %>%
filter(
!is.na(df1$SES.RQEP),
!is.na(df1$SES.RQEF),
!is.na(df1$SES.MPD),
!is.na(df1$SES.FDis),
!is.na(Latitude),
!is.na(Longitude),
species.richness > 1
) %>%
filter(SES.RQEF < 5e+10) %>% # exclude outlier
as.data.frame()
df.open.1 <- df.open %>%
filter(
!is.na(df.open$SES.RQEP),
!is.na(df.open$SES.RQEF),
!is.na(df.open$SES.MPD),
!is.na(df.open$SES.FDis),
!is.na(Latitude),
!is.na(Longitude),
species.richness > 1
) %>%
filter(SES.RQEF < 5e+10) %>%
as.data.frame()
saveRDS(df1.1, "02.data/01c.sPlot.PD.FD.CD-data.RDS")
saveRDS(df.open.1, "02.data/01c.sPlotOpen.PD.FD.CD-data.RDS")