From a72dfbf83cac22e6eabfc339807ea9a1e1f36f56 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?K=C3=B6nig?= <ye87zine@usr.idiv.de>
Date: Thu, 9 Jan 2025 12:41:59 +0100
Subject: [PATCH] filtered duplicate occurrences, auto bandwidth in spatial kde

---
 R/03_01_presence_preparation.R |  8 ++++++--
 R/03_02_absence_preparation.R  | 29 +++++++++++++++--------------
 2 files changed, 21 insertions(+), 16 deletions(-)

diff --git a/R/03_01_presence_preparation.R b/R/03_01_presence_preparation.R
index 9fb6ee3..8d3fce3 100644
--- a/R/03_01_presence_preparation.R
+++ b/R/03_01_presence_preparation.R
@@ -73,7 +73,11 @@ raster_data = terra::project(raster_data, proj_string)
 occs_final = st_transform(occs_final, proj_string)
 env_data = extract(raster_data, vect(occs_final), ID = F)
 
-# Save occurrences
+# Merge data + final processing
 occs_final = bind_cols(occs_final, env_data) %>% 
-  drop_na()
+  drop_na() %>% # Remove records with NA env vars
+  group_by(species) %>% 
+  distinct()    # Remove duplicate records
+
+# Save occurrences
 save(occs_final, file = "data/r_objects/occs_final.RData")
diff --git a/R/03_02_absence_preparation.R b/R/03_02_absence_preparation.R
index 951dded..2390512 100644
--- a/R/03_02_absence_preparation.R
+++ b/R/03_02_absence_preparation.R
@@ -85,8 +85,7 @@ model_data = furrr::future_walk(
     
     # Define spatial KDE
     ref = st_bbox(sampling_region)[c(1,3,2,4)]
-    min_extent = min(ref[2]-ref[1], ref[4]-ref[3])
-    spatial_kde = spatialEco::sf.kde(occs_spec, bw = min_extent/2, res = 10000, ref = ref, standardize = T, scale.factor = 1) %>% 
+    spatial_kde = spatialEco::sf.kde(occs_spec, res = 10000, ref = ref, standardize = T, scale.factor = 1) %>% 
       crop(sampling_region, mask = T, touches = F)
     
     # Define reverse niche
@@ -110,7 +109,7 @@ model_data = furrr::future_walk(
       if(length(sample_points) == 0){
         next
       }
-
+      
       samples_required = nrow(occs_spec) - nrow(abs_spec)
       if(length(sample_points) > samples_required){
         sample_points = sample(sample_points, samples_required)
@@ -136,17 +135,19 @@ model_data = furrr::future_walk(
       dplyr::mutate(present = 1) %>% 
       bind_rows(abs_spec) 
     
-    # ggplot() +
-    #   ggtitle(species) +
-    #   geom_sf(data = st_crop(sa_polygon, ref), fill = "#756445") +
-    #   tidyterra::geom_spatraster(data = abs_prob) +
-    #   scale_fill_gradient(low="darkblue", high="green", na.value=NA, name = "Absence sampling probability") +
-    #   geom_sf(data = range_spec, alpha = 0, color = "black") +
-    #   geom_sf(data = pa_spec, aes(color = as.factor(present)), size = 0.7) +
-    #   scale_color_discrete(name = "Presence") +
-    #   theme_minimal()
-    # 
-    # ggsave(paste0(species, ".pdf"), path = "plots/pa_sampling", device = "pdf", scale = 2)
+    ggplot() +
+      ggtitle(species) +
+      geom_sf(data = st_crop(sa_polygon, ref), fill = "#756445") +
+      tidyterra::geom_spatraster(data = abs_prob) +
+      scale_fill_gradient(low="darkblue", high="green", na.value=NA, name = "Absence sampling probability") +
+      geom_sf(data = range_spec, alpha = 0, color = "black") +
+      geom_sf(data = pa_spec, aes(color = as.factor(present)), size = 0.7) +
+      scale_color_discrete(name = "Presence") +
+      theme_minimal()
+    
+    try({
+      ggsave(paste0(species, ".pdf"), path = "plots/pa_sampling", device = "pdf", scale = 2)
+    })
     
     # Define cross-validation folds
     folds = tryCatch({
-- 
GitLab