-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFRUGINT_method_merge.R
More file actions
185 lines (138 loc) · 5.3 KB
/
FRUGINT_method_merge.R
File metadata and controls
185 lines (138 loc) · 5.3 KB
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
library(tidyverse)
library(readr)
library(tibble)
library(here)
# The workflow for integrating interaction data from multiple methods includes several steps:
# 1. Integrating datasets at different resolutions (record- and species-level) and times
# 2. Merging and standardizing by-method matrix (Grand Total Standardization)
#--------------------------------------------------------------
# GTS FUNCTION
# Standardize pairwise interaction number by the grand total
gts <- function(df) {
total_sum <- sum(df$total.int, na.rm = TRUE)
df %>%
mutate(gts = total.int / total_sum,
gts = ifelse(gts == 0, NA, round(gts, 8)))
}
# Standardize proportion data by the grand total
gts.prop <- function(df) {
df %>%
ungroup() %>%
mutate(gts = gts0 / sum(gts0)) %>%
mutate(gts = ifelse(gts == 0, NA, round(gts, 8)))
}
#--------------------------------------------------------------
## INPUT DATA
# INPUT DATA: RECORD LEVEL (1-5)
# 1.Cameras
CAM <- read_csv(here("FRUGINT/INPUT_DATA/CAM.csv"))
# 2.Barcoding-visits
BC_visit <- read_csv(here("FRUGINT/INPUT_DATA/BC_visit.csv"))
# 3.Barcoding-seeds
BC_seed <- read_csv(here("FRUGINT/INPUT_DATA/BC_seed.csv"))
# 4.Tracks
TR <- read_csv(here("FRUGINT/INPUT_DATA/TRACK.csv"))
# 5.Mist-netting (2024)
MN_24 <- read_csv(here("FRUGINT/INPUT_DATA/MN_2024.csv"))
# INPUT DATA: SPECIES LEVEL (6-7)
# 6.Mist-netting (1983)
MN_83 <- read_csv(here("FRUGINT/INPUT_DATA/MN_1983.csv"))
# 7.Direct observation data from two locations with different vegetation types
OBS_HR <- read_csv(here("FRUGINT/INPUT_DATA/OBS_HR.csv"))
OBS_JP <- read_csv(here("FRUGINT/INPUT_DATA/OBS_JP.csv"))
# INPUT DATA: FRUGIVORE DIET
bird_trait <- read_csv2(here("FRUGINT/TRAITS/FRUGINT_birdTraits.csv")) %>% select(1,6)
mamm_trait <- read_csv2(here("FRUGINT/TRAITS/FRUGINT_mammalTraits.csv")) %>% select(1,6)
# Add missing species
new_spp <- tibble(
Frug_species = c("Psammodromus algirus", "Sturnus vulgaris/unicolor"),
Diet_fruit_prop = c(0.1,0.8))
frug_diet <- bind_rows(bird_trait,mamm_trait,new_spp)
#--------------------------------------------------------------
## AGGREGATE DATA: From record to species level
# 1.Cameras and tracks (pooled)
CAM_TRACK <- bind_rows(CAM,TR) %>%
group_by(plantSp,animalSp) %>%
summarize(total.int = n()) %>%
mutate(method = "CAM",
method = if_else(plantSp == "Chamaerops humilis", "TRACK", method))
# 2.Barcoding
BC_visit <- BC_visit %>%
# Estimate probability of fruit consumption during a visit as fruit proportion in the diet
left_join(frug_diet, by = c("animalSp" = "Frug_species")) %>%
group_by(plantSp,animalSp) %>%
summarize(total.int = sum(Diet_fruit_prop))
BC_seed <- BC_seed %>%
group_by(plantSp,animalSp) %>%
summarize(total.int = n())
BC <- bind_rows(BC_visit, BC_seed) %>%
# Sum intensities across both datasets for each plant-animal pair
group_by(plantSp, animalSp) %>%
summarise(total.int = sum(total.int), .groups = "drop") %>%
mutate(method = "BC")
# 3.Mist-netting (2024)
MN_24 <- MN_24 %>%
group_by(vegetation, plantSp, animalSp) %>%
summarize(total.int = n(), .groups = "drop")
#--------------------------------------------------------------
## AGGREGATE DATA: different times and locations
# 1.Mist-netting
# GTS by time period and average
MN_83_gts <- gts(MN_83)
MN_24_gts <- gts(MN_24)
MN_gts <- bind_rows(MN_24_gts, MN_83_gts) %>%
group_by(plantSp, animalSp) %>%
summarize(gts = mean(gts, na.rm = TRUE),
total.int = sum(total.int), .groups = "drop") %>%
rename(gts0 = gts) %>%
mutate(method = "MIST-NET")
# 2.Observation
# GTS by vegetation and average
OBS_HR_gts <- gts(OBS_HR)
OBS_JP_gts <- gts(OBS_JP)
OBS_gts <- bind_rows(OBS_HR_gts, OBS_JP_gts) %>%
group_by(plantSp, animalSp) %>%
summarize(gts = mean(gts, na.rm = TRUE),
total.int = sum(total.int), .groups = "drop") %>%
rename(gts0 = gts) %>%
mutate(method = "OBS")
#--------------------------------------------------------------
## GRAND TOTAL STANDARDIZATION BY METHOD
# Standardization of interaction number
CAM_TRACK_gts <- CAM_TRACK %>%
gts() %>%
rename(PIE = gts)
BC_gts <- BC %>%
gts() %>%
rename(PIE = gts)
# Standardization of proportions
MN_gts <- MN_gts %>%
gts.prop() %>%
select(-gts0) %>%
rename(PIE = gts)
OBS_gts <- OBS_gts %>%
gts.prop() %>%
select(-gts0) %>%
rename(PIE = gts)
## Save file "FRUGINT_byMethod_gts.csv"
all_gts <- bind_rows(CAM_TRACK_gts,BC_gts,MN_gts,OBS_gts) %>%
select(plantSp, animalSp, method, total.int, PIE) %>%
# Taxonomic harmonization to AviList
mutate(animalSp = if_else(animalSp == "Corvus monedula", "Coloeus monedula", animalSp))
write_csv(all_gts, file = here("FRUGINT", "INTERACTIONS/FRUGINT_byMethod_gts.csv"))
#--------------------------------------------------------------
## FINAL MERGE BY MEAN
# Calculate the mean
final_matrix <- all_gts %>%
group_by(plantSp, animalSp) %>%
summarize(meanPIE = round(mean(PIE, na.rm = TRUE),8),
maxPIE = max(PIE, na.rm = TRUE),
n_methods = n_distinct(method),
.groups = "drop") %>%
arrange(desc(maxPIE)) %>%
mutate(meanPIE = meanPIE / sum(meanPIE),
maxPIE = maxPIE / sum(maxPIE))
n_distinct(final_matrix$plantSp)
n_distinct(final_matrix$animalSp)
# Save file "FRUGINT_finalMatrix.csv"
write_csv(final_matrix, file = here("FRUGINT", "INTERACTIONS/FRUGINT_finalMatrix.csv"))