Skip to content
Snippets Groups Projects
Commit a72dfbf8 authored by ye87zine's avatar ye87zine
Browse files

filtered duplicate occurrences, auto bandwidth in spatial kde

parent 2f676f27
No related branches found
No related tags found
1 merge request!1Presence absence sampling
......@@ -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")
......@@ -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({
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment