-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinvasionSyndromesModels.Rmd
726 lines (617 loc) · 24 KB
/
invasionSyndromesModels.Rmd
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
---
title: "Invasion Syndrome Models"
author: "XXXXXX"
date: "`r format(Sys.time(), '%Y-%m-%d')`"
output:
html_document:
df_print: paged
toc: true
toc_depth: 3
toc_float: true
---
```{r, results= "hide"}
# software packages
library(parallel)
# data wrangling and analysis
library(tidyverse)
library(naniar)
library(randomForest)
library(caret)
library(ranger)
library(broom)
library(rsample)
library(ModelMetrics)
library(modEvA)
library(PresenceAbsence)
library(miceRanger, attach.required = TRUE)
```
# data management
# invasion history
Upload species recorded to have been unintetionally transported. For herps, the invasion histories are in the "./data/invasionHistory" folder
```{r}
invHis_accTrans <- read_csv(file.choose()) %>%
distinct(scientificName_harmonise, .keep_all = TRUE)
```
Download and resolve the taxonomies of herpetofauna species with established alien populations: https://onlinelibrary.wiley.com/doi/full/10.1111/ddi.12617
```{r}
invHis_est <- read_csv(file.choose()) %>%
rename("scientificName_harmonise" = "sciName_resolved")
```
# Invasion syndromes
The code below performs the following tasks:
1. Reads the imputed dataset.
2. Fine-tunes and evaluates random forest models separately for unintentionally transported species and species with established alien populations.
3. Writes evaluation scores, variable importance, and predictions to CSV files for further analysis.
4. loops this process 100 times.
```{r}
# Specify the path of the folder containing the imputed traits and macroecological patterns.
# This folder should be in the "./output/missRangeImp/" directory.
imp_path <- list.files("./output/miceRangeImp/", full.names = TRUE)
# Loop through each dataset for modeling invasion syndromes separately.
for (i in 1:100) {
message(paste0("Processing dataset ", i))
# Read the data for the current iteration and ensure correct data types for columns.
data <- read.csv(imp_path[i]) %>%
mutate(
Genus = as.factor(Genus),
Family = as.factor(Family),
Order = as.factor(Order),
habitat_mostFreq = as.factor(habitat_mostFreq),
Fos = as.logical(Fos),
Ter = as.logical(Ter),
Aqu = as.logical(Aqu),
Arb = as.logical(Arb),
Diu = as.logical(Diu),
Noc = as.logical(Noc),
Crepu = as.logical(Crepu),
Dir = as.logical(Dir),
Lar = as.logical(Lar),
Viv = as.logical(Viv),
invHis_trans = as.logical(scientificName_harmonise %in% pull(invHis_accTrans, scientificName_harmonise)),
invHis_est = as.logical(scientificName_harmonise %in% pull(invHis_est, scientificName_harmonise))
)
# Select dependent and optimal set of predictor variables.
data_t <- data %>%
select(
invHis_trans,
invHis_est,
scientificName_harmonise,
size = Body_size_mm,
clutchNumber = Reproductive_output_y,
area,
commonness = commonness_cleanedGBIF,
habitat_mostFreq,
habitatBreadth,
Levins_norm,
PropSim,
humanTolerance_occs_median,
humanTolerance_occs_min,
humanTolerance_occs_max,
airport = airport_passangers,
allPorts = allPorts_outflow
) %>%
distinct(scientificName_harmonise, .keep_all = TRUE) %>%
filter(across(.cols = everything(), .fns = ~!is.na(.x)))
######################################################
## Modelling invasion syndromes of unintentionally transported species
# Prepare data for modeling.
mod_data_trans <- data_t %>%
arrange(desc(invHis_trans)) %>%
select(-c(invHis_est, scientificName_harmonise))
# Fine-tune random forest model with cross-validation.
set.seed(i)
rf_trans_fineTune <- mod_data_trans %>%
vfold_cv(v = 5) %>%
mutate(
train = purrr::map(splits, ~training(.x)),
validate = purrr::map(splits, ~testing(.x))
) %>%
crossing(mtry = 1:10) %>%
mutate(
model = purrr::map2(train, mtry, ~ranger(
formula = invHis_trans ~ .,
data = .x,
mtry = .y,
importance = "impurity",
probability = TRUE,
replace = TRUE,
num.trees = 1000,
case.weights = c(
rep(1, as.numeric(table(.x$invHis_trans)["TRUE"])),
rep(
as.numeric(table(.x$invHis_trans)["TRUE"]) /
as.numeric(table(.x$invHis_trans)["FALSE"]),
as.numeric(table(.x$invHis_trans)["FALSE"])
)
)
))
) %>%
# Evaluate model performance
mutate(
validate_actual = purrr::map(validate, ~.x$invHis_trans),
validate_predict = purrr::map2(model, validate, ~predict(.x, .y)$predictions[, 1]),
validate_oob = purrr::map_dbl(model, ~.x$prediction.error),
validate_mae = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::mae(actual = .x, predicted = .y)
),
validate_auc = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::auc(actual = .x, predicted = .y)
),
validate_boyce = purrr::map2_dbl(
validate_actual,
validate_predict,
~Boyce(obs = .x, pred = .y, plot = FALSE)$B
),
validate_precision = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::precision(actual = .x, predicted = .y)
),
validate_recall = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::recall(actual = .x, predicted = .y)
),
validate_F1 = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::f1Score(actual = .x, predicted = .y)
)
) %>%
# Summarize model performance
select(
mtry,
validate_oob,
validate_mae,
validate_auc,
validate_boyce,
validate_precision,
validate_recall,
validate_F1
) %>%
group_by(mtry) %>%
summarise(across(
validate_oob:validate_F1,
list(mean = mean, sd = sd)
))
# Write the evaluation scores of the model with the best Boyce index to a CSV file.
write.csv(
rf_trans_fineTune %>% arrange(desc(validate_boyce_mean)) %>% slice(1),
paste0("./output/rf_fineTune/rf_finetune_trans_fineTune_", i, ".csv")
)
# Get the mtry value of the model with the best Boyce index.
rf_trans_fineTune_mtry <- rf_trans_fineTune %>%
arrange(desc(validate_boyce_mean)) %>%
slice(1) %>%
pull(mtry)
# Re-run the model using the best mtry value.
rf_trans <- ranger(
invHis_trans ~ .,
data = mod_data_trans,
importance = "impurity",
probability = TRUE,
mtry = rf_trans_fineTune_mtry
)
# Write variable importance to a CSV file.
data.frame(varImp = rf_trans$variable.importance) %>%
rownames_to_column("var") %>%
arrange(desc(varImp)) %>%
write_csv2(paste0("./output/rf_pred/rf_finetune_trans_varImp_", i, ".csv"))
# Prediction to species worldwide.
rf_pred_trans <- data_t %>%
cbind(data.frame(pred_trans = round(predict(rf_trans, data_t, type = "response")$predictions[, 1], 2))) %>%
select(pred_trans, invHis_trans, scientificName_harmonise)
# Calculate threshold for prediction.
trans_thresh <- optimal.thresholds(DATA = data.frame(
plot_ID = 1:length(rf_pred_trans[,"invHis_trans"]),
observed = rf_pred_trans[,"invHis_trans"],
fitted = rf_pred_trans[,"pred_trans"]
)) %>%
filter(Method == "Sens=Spec") %>%
pull(fitted)
######################################################
## Modelling invasion syndromes of species with established alien populations
# Prepare data for modeling.
mod_data_est <- data_t %>%
filter(invHis_trans == 1 | invHis_est == 1) %>%
arrange(desc(invHis_est)) %>%
select(-c(invHis_trans, scientificName_harmonise)) %>%
droplevels()
# Fine-tune random forest model with cross-validation.
set.seed(i)
rf_est_fineTune <- mod_data_est %>%
vfold_cv(v = 5) %>%
mutate(
train = purrr::map(splits, ~training(.x)),
validate = purrr::map(splits, ~testing(.x))
) %>%
crossing(mtry = 1:10) %>%
mutate(
model = purrr::map2(train, mtry, ~ranger(
formula = invHis_est ~ .,
data = .x,
mtry = .y,
importance = "impurity",
probability = TRUE,
replace = TRUE,
num.trees = 1000,
case.weights = c(
rep(1, as.numeric(table(.x$invHis_est)["TRUE"])),
rep(
as.numeric(table(.x$invHis_est)["TRUE"]) /
as.numeric(table(.x$invHis_est)["FALSE"]),
as.numeric(table(.x$invHis_est)["FALSE"])
)
)
))
) %>%
# Evaluate model performance
mutate(
validate_actual = purrr::map(validate, ~.x$invHis_est),
validate_predict = purrr::map2(model, validate, ~predict(.x, .y)$predictions[, 1]),
validate_oob = purrr::map_dbl(model, ~.x$prediction.error),
validate_mae = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::mae(actual = .x, predicted = .y)
),
validate_auc = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::auc(actual = .x, predicted = .y)
),
validate_boyce = purrr::map2_dbl(
validate_actual,
validate_predict,
~Boyce(obs = .x, pred = .y, plot = FALSE)$B
),
validate_precision = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::precision(actual = .x, predicted = .y)
),
validate_recall = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::recall(actual = .x, predicted = .y)
),
validate_F1 = purrr::map2_dbl(
validate_actual,
validate_predict,
~ModelMetrics::f1Score(actual = .x, predicted = .y)
)
) %>%
# Summarize model performance
select(
mtry,
validate_oob,
validate_mae,
validate_auc,
validate_boyce,
validate_precision,
validate_recall,
validate_F1
) %>%
group_by(mtry) %>%
summarise(across(
validate_oob:validate_F1,
list(mean = mean, sd = sd)
))
# Write the evaluation scores of the model with the best Boyce index to a CSV file.
write.csv(
rf_est_fineTune %>% arrange(desc(validate_boyce_mean)) %>% slice(1),
paste0("./output/rf_fineTune/rf_finetune_est_fineTune_", i, ".csv")
)
# Get the mtry value of the model with the best Boyce index.
rf_est_fineTune_mtry <- rf_est_fineTune %>%
arrange(desc(validate_boyce_mean)) %>%
slice(1) %>%
pull(mtry)
# Re-run the model using the best mtry value.
rf_est <- ranger(
invHis_est ~ .,
data = mod_data_est,
importance = "impurity",
probability = TRUE,
mtry = rf_est_fineTune_mtry
)
# Write variable importance to a CSV file.
data.frame(varImp = rf_est$variable.importance) %>%
rownames_to_column("var") %>%
arrange(desc(varImp)) %>%
write_csv2(paste0("./output/rf_pred/rf_finetune_est_varImp_", i, ".csv"))
# Prediction to species worldwide.
est_pred_data <- data_t %>%
filter(invHis_trans == 1 | invHis_est == 1)
rf_pred_est <- est_pred_data %>%
cbind(data.frame(pred_est = round(predict(rf_est, est_pred_data, type = "response")$predictions[, 1], 2))) %>%
relocate(pred_est) %>%
select(pred_est, invHis_est, scientificName_harmonise)
# Calculate threshold for prediction.
est_thresh <- optimal.thresholds(DATA = data.frame(
plot_ID = 1:length(rf_pred_est[,"invHis_est"]),
observed = rf_pred_est[,"invHis_est"],
fitted = rf_pred_est[,"pred_est"]
)) %>%
filter(Method == "Sens=Spec") %>%
pull(fitted)
# Apply threshold to prediction.
rf_pred_est <- data_t %>%
cbind(data.frame(pred_est = round(predict(rf_est, data_t, type = "response")$predictions[, 1], 2))) %>%
relocate(pred_est) %>%
select(pred_est, invHis_est, scientificName_harmonise)
## Compile predictions of both models.
rf_pred_trans %>%
mutate(pred_trans_thres = pred_trans >= trans_thresh) %>%
left_join(
rf_pred_est %>%
mutate(pred_est_thres = pred_est >= est_thresh),
by = c("scientificName_harmonise")
) %>%
relocate(
scientificName_harmonise,
.after = pred_est_thres
) %>%
write_csv2(paste0("./output/rf_pred/rf_finetune_pred_", i, ".csv"))
message(paste0("End processing dataset ", i, "\n"))
}
```
# Pooling results
```{r}
# This section finds the file paths of prediction files that contain the pattern "rf_finetune_pred"
# in the "./output/rf_pred" directory and stores them in the pred_filepath variable.
pred_filepath <- grep(list.files("./output/rf_pred", full.names = TRUE),
pattern = "rf_finetune_pred",
value = TRUE)
# Initialize an empty data frame to store the aggregated prediction summaries.
pred_summary <- data.frame()
# Iterate over each file path in pred_filepath.
for (i in 1:100) {
# Read each CSV file into a temporary data frame and append it to pred_summary.
pred_summary <- bind_rows(pred_summary, read.csv2(pred_filepath[i]))
}
# This section aggregates the predictions by species and computes summary statistics.
pred_summary <- pred_summary %>%
# Group the data by species names (scientificName and scientificName_harmonise).
group_by(scientificName_harmonise) %>%
summarise(pred_trans_thres = sum(pred_trans_thres),
pred_trans_mean = mean(pred_trans),
pred_trans_sd = sd(pred_trans),
pred_est_thres = sum(pred_est_thres),
pred_est_mean = mean(pred_est),
pred_est_sd = sd(pred_est)
# Summarize the predictions:
# - pred_trans_thres: total count of predictions meeting a threshold for unintentionally transported species
# - pred_trans_mean: mean prediction for unintentionally transported species
# - pred_trans_sd: standard deviation of predictions for unintentionally transported species
# - pred_est_thres: total count of predictions meeting a threshold for established alien populations
# - pred_est_mean: mean prediction for established alien populations
# - pred_est_sd: standard deviation of predictions for established alien populations
) %>%
# Join additional information about the species (invHis_trans:Order) from the data_t data frame.
left_join(
data_t %>%
select(invHis_trans:scientificName_harmonise),
by = "scientificName_harmonise"
)
# Write the aggregated prediction summary data frame to a CSV file.
pred_summary %>%
write_csv2("./output/rf_finetune_pred_summary.csv")
```
# variable importance
## invasion syndrome model of unintetionally transported and introduced species
```{r}
# Find file paths of variable importance files for predictions related to unintentionally transported species.
trans_imp_filepath <- grep(list.files("./output/rf_pred", full.names = TRUE),
pattern = "rf_finetune_trans_varImp_",
value = TRUE)
# Initialize an empty data frame to store the aggregated variable importance summaries.
trans_imp_summary <- data.frame()
# Iterate over each file path in trans_imp_filepath.
for (i in 1:100) {
# Read each CSV file into a temporary data frame and append it to trans_imp_summary.
trans_imp_summary <- bind_rows(trans_imp_summary, read.csv2(trans_imp_filepath[i]))
}
# Compute summary statistics for variable importance across all iterations.
trans_imp_summary <- trans_imp_summary %>%
# Group the data by variable names (var).
group_by(var) %>%
summarise(varImp_mean = mean(varImp),
varImp_sd = sd(varImp)
# Summarize the variable importance:
# - varImp_mean: mean variable importance
# - varImp_sd: standard deviation of variable importance
) %>%
# Arrange the summary data in descending order of mean variable importance.
arrange(desc(varImp_mean))
# Write the summarized variable importance data frame to a CSV file.
trans_imp_summary %>%
write_csv2("./output/finetune_trans_varimp_summary.csv")
```
## invasion syndrome model of species with established alien populations
```{r}
# Find file paths of variable importance files for predictions related to species with established alien populations.
est_imp_filepath <- grep(list.files("./output/rf_pred", full.names = TRUE),
pattern = "rf_finetune_est_varImp_",
value = TRUE)
# Initialize an empty data frame to store the aggregated variable importance summaries.
est_imp_summary <- data.frame()
# Iterate over each file path in est_imp_filepath.
for (i in 1:100) {
# Read each CSV file into a temporary data frame and append it to est_imp_summary.
est_imp_summary <- bind_rows(est_imp_summary, read.csv2(est_imp_filepath[i]))
}
# Compute summary statistics for variable importance across all iterations.
est_imp_summary <- est_imp_summary %>%
# Group the data by variable names (var).
group_by(var) %>%
summarise(varImp_mean = mean(varImp),
varImp_sd = sd(varImp)
# Summarize the variable importance:
# - varImp_mean: mean variable importance
# - varImp_sd: standard deviation of variable importance
) %>%
# Arrange the summary data in descending order of mean variable importance.
arrange(desc(varImp_mean))
# Write the summarized variable importance data frame to a CSV file.
est_imp_summary %>%
write_csv2("./output/finetune_est_varimp_summary.csv")
```
# model evaluation statistics
## invasion syndrome model of unintetionally transported and introduced species
```{r}
# Find file paths of evaluation files related to predictions for unintentionally transported species.
trans_eval_filepath <- grep(list.files("./output/rf_fineTune", full.names = TRUE),
pattern = "rf_finetune_trans_fineTune",
value = TRUE)
# Initialize an empty data frame to store the aggregated evaluation summaries.
trans_eval_summary <- data.frame()
# Iterate over each file path in trans_eval_filepath.
for (i in 1:100) {
# Read each CSV file into a temporary data frame and append it to trans_eval_summary.
trans_eval_summary <- bind_rows(trans_eval_summary,
read.csv2(trans_eval_filepath[i], sep = ","))
}
# Perform data wrangling and summarization on trans_eval_summary.
trans_eval_summary <- trans_eval_summary %>%
# Remove columns 'X' and 'mtry'.
select(-c(X, mtry)) %>%
# Reshape the data frame to long format.
pivot_longer(cols = everything(),
names_to = "metrics",
values_to = "val") %>%
# Convert 'val' column to numeric.
mutate(val = as.numeric(val)) %>%
# Filter rows containing "mean" in the 'metrics' column.
filter(grepl("mean", metrics)) %>%
# Group the data by 'metrics'.
group_by(metrics) %>%
# Calculate mean and standard deviation for each metric.
summarise(mean = mean(val),
sd = sd(val)) %>%
# Remove "_mean" from 'metrics'.
mutate(metrics = str_remove(metrics, "validate_")) %>%
mutate(metrics = str_remove(metrics, "_mean"))
# Write the summarized evaluation metrics to a CSV file.
trans_eval_summary %>%
write_csv2("./output/finetune_trans_eval_summary.csv")
```
## invasion syndrome model of species with established alien populations
```{r}
# Find file paths of evaluation files related to predictions for species with established alien populations.
est_eval_filepath <- grep(list.files("./output/rf_fineTune", full.names = TRUE),
pattern = "rf_finetune_est_fineTune",
value = TRUE)
# Initialize an empty data frame to store the aggregated evaluation summaries.
est_eval_summary <- data.frame()
# Iterate over each file path in est_eval_filepath.
for (i in 1:100) {
# Read each CSV file into a temporary data frame and append it to est_eval_summary.
est_eval_summary <- bind_rows(est_eval_summary,
read.csv2(est_eval_filepath[i], sep = ","))
}
# Perform data wrangling and summarization on est_eval_summary.
est_eval_summary <- est_eval_summary %>%
# Remove columns 'X' and 'mtry'.
select(-c(X, mtry)) %>%
# Reshape the data frame to long format.
pivot_longer(cols = everything(),
names_to = "metrics",
values_to = "val") %>%
# Convert 'val' column to numeric.
mutate(val = as.numeric(val)) %>%
# Filter rows containing "mean" in the 'metrics' column.
filter(grepl("mean", metrics)) %>%
# Group the data by 'metrics'.
group_by(metrics) %>%
# Calculate mean and standard deviation for each metric.
summarise(mean = mean(val),
sd = sd(val)) %>%
# Remove "_mean" from 'metrics'.
mutate(metrics = str_remove(metrics, "validate_")) %>%
mutate(metrics = str_remove(metrics, "_mean"))
# Write the summarized evaluation metrics to a CSV file.
est_eval_summary %>%
write_csv2("./output/finetune_est_eval_summary.csv")
```
# Computing Extrapolation uncertainty
```{r}
# Specify the path of the folder containing the imputed traits and macroecological patterns.
# This folder should be in the "./output/missRangeImp/" directory.
imp_path <- list.files("./output/miceRangeImp", full.names = TRUE)
# Initialize an empty data frame to store the results of MESS analysis.
amphibia_MESS <- data.frame()
# Iterate over each dataset in the folder.
for (i in 1:100) {
message(paste0("processing dataset ", i))
# Read the data from the CSV file in the current dataset.
data <- read.csv(imp_path[i]) %>%
# Convert certain columns to factors or logicals as required.
mutate(habitat_mostFreq = as.factor(habitat_mostFreq),
Fos = as.logical(Fos),
Ter = as.logical(Ter),
Aqu = as.logical(Aqu),
Arb = as.logical(Arb),
Diu = as.logical(Diu),
Noc = as.logical(Noc),
Crepu = as.logical(Crepu),
Dir = as.logical(Dir),
Lar = as.logical(Lar),
Viv = as.logical(Viv)) %>%
# Determine invasion history based on certain conditions.
mutate(invHis_trans = as.logical(scientificName_harmonise %in% pull(invHis_accTrans, scientificName_harmonise)),
invHis_est = as.logical(scientificName_harmonise %in% pull(invHis_est, scientificName_harmonise)))
# Select relevant columns for analysis.
data_t <- data %>%
select(
invHis_trans,
invHis_est,
scientificName_harmonise,
Diu,
Noc,
Crepu,
size = Body_size_mm,
clutchNumber = Reproductive_output_y,
commonness = commonness_cleanedGBIF,
habitat_mostFreq,
habitatBreadth,
Levins_norm,
PropSim,
humanTolerance_occs_median,
humanTolerance_occs_min,
humanTolerance_occs_max,
airport = airport_passangers,
allPorts = allPorts_outflow
) %>%
# Retain only unique records.
distinct(scientificName_harmonise, .keep_all = TRUE) %>%
# Remove rows with any NA values.
filter(across(.cols = everything(), .fns = ~!is.na(.x)))
# Filter data to include only species with established alien populations.
data_est_t <- data_t %>%
filter(invHis_trans == TRUE)
# Perform MESS analysis on the filtered data.
amphibia_MESS <- MESS(V = data_est_t %>%
dplyr::select(size, clutchNumber, commonness, habitatBreadth:allPorts),
P = data_t %>%
dplyr::select(size, clutchNumber, commonness, habitatBreadth:allPorts)) %>%
# Bind scientific names to the MESS results.
cbind(dplyr::select(data_t, scientificName_harmonise)) %>%
# Filter MESS results where total uncertainty is less than 0.
filter(TOTAL < 0) %>%
# Add a column to identify the imputation dataset.
mutate(impDataset = i) %>%
# Append the MESS results to the overall MESS data frame.
rbind(amphibia_MESS)
}
# Aggregate MESS results by scientific name and measure of divergence.
amphibia_MESS %>%
group_by(scientificName_harmonise, MoD) %>%
summarise(n = n()) %>%
# Write the summarized MESS results to a CSV file.
write_csv("./output/extrapolationUncertainty.csv")
```