Skip to content

Commit

Permalink
Added vignette on data preparation #67
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin-Jung committed Aug 29, 2023
1 parent 4179be6 commit 1e8c768
Show file tree
Hide file tree
Showing 6 changed files with 350 additions and 22 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: ibis.iSDM
Type: Package
Title: Modelling framework for integrated biodiversity distribution scenarios
Version: 0.0.8
Version: 0.0.9
Authors@R:
c(person(given = "Martin",
family = "Jung",
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# ibis.iSDM 0.0.9 (current dev branch)
#### New features
* Added new vignette on available functions for data preparation #67

#### Minor improvements and bug fixes
* Small fix to `threshold()` now returning threshold values correctly.
Expand Down
30 changes: 15 additions & 15 deletions R/pseudoabsence.R
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
bias <- terra::resample(bias, background, method = "bilinear")
}
# Normalize if not already set
if(terra::global(bias, 'max', na.rm = TRUE) > 1 || terra::global(bias, 'min', na.rm = TRUE) < 0 ){
if(terra::global(bias, 'max', na.rm = TRUE)[,1] > 1 || terra::global(bias, 'min', na.rm = TRUE)[,1] < 0 ){
bias <- predictor_transform(bias, option = "norm")
}
} else { bias <- NULL }
Expand All @@ -276,11 +276,11 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
# Now sample from all cells not occupied
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg1[]==0)]
prob_bias <- bias[which(terra::values(bg1)[,1]==0)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg1[]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
abs <- sample(which(terra::values(bg1)[,1]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
abs <- sample(which(bg1[]==0), size = nrpoints, replace = TRUE)
abs <- sample(which(terra::values(bg1)[,1]==0), size = nrpoints, replace = TRUE)
}
} else if(method == "buffer"){
assertthat::assert_that(is.numeric(buffer_distance),msg = "Buffer distance parameter not numeric!")
Expand All @@ -301,7 +301,7 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
# Now sample from all cells not occupied
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg2[]==1)]
prob_bias <- bias[which(bg2[]==1)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg2[]==1), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
Expand All @@ -317,7 +317,7 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
bg2 <- terra::mask(bg1, mask = pol, inverse = !inside)
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg2[]==0)]
prob_bias <- bias[which(bg2[]==0)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
Expand All @@ -340,11 +340,11 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
bg2 <- terra::mask(bg1, mask = layer, inverse = !inside)
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg2[]==0)]
prob_bias <- bias[which(terra::values(bg2)[,1]==0)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE)
}
rm(bg2)
} else if(method == "zones"){
Expand Down Expand Up @@ -379,11 +379,11 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
bg2 <- terra::mask(bg1, mask = zones)
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg2[]==0)]
prob_bias <- bias[which(terra::values(bg2)[,1]==0)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE)
}
rm(bg2)
} else if(method == "target"){
Expand All @@ -398,11 +398,11 @@ add_pseudoabsence <- function(df, field_occurrence = "observed", template = NULL
bg2 <- terra::mask(bg1, mask = layer)
if(!is.null(bias)){
# Get probability values for cells where no sampling has been conducted
prob_bias <- bias[which(bg2[]==0)]
prob_bias <- bias[which(terra::values(bg2)[,1]==0)][,1]
if(any(is.na(prob_bias))) prob_bias[is.na(prob_bias)] <- 0
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE, prob = prob_bias)
} else {
abs <- sample(which(bg2[]==0), size = nrpoints, replace = TRUE)
abs <- sample(which(terra::values(bg2)[,1]==0), size = nrpoints, replace = TRUE)
}
rm(bg2)
} else {
Expand Down
13 changes: 9 additions & 4 deletions R/utils-spatial.R
Original file line number Diff line number Diff line change
Expand Up @@ -1278,7 +1278,9 @@ thin_observations <- function(df, background, env = NULL, method = "random", min
)
check_package("dplyr")
# Match method
method <- match.arg(method, choices = c("random", "spatial", "bias", "environmental", "zones"), several.ok = FALSE)
method <- match.arg(method,
choices = c("random", "spatial", "bias", "environmental", "zones"),
several.ok = FALSE)

# Label background with id
bg <- background
Expand All @@ -1300,7 +1302,8 @@ thin_observations <- function(df, background, env = NULL, method = "random", min
ras <- terra::rasterize(coords, bg) # Get the number of observations per grid cell

# Bounds for thining
totake <- c(lower = minpoints, upper = max( terra::global(ras, "min", na.rm = TRUE)[,1], minpoints))
totake <- c(lower = minpoints,
upper = max( terra::global(ras, "min", na.rm = TRUE)[,1], minpoints))

# -- #
if(method == "random"){
Expand Down Expand Up @@ -1374,7 +1377,8 @@ thin_observations <- function(df, background, env = NULL, method = "random", min
is.factor(zones))

if(!terra::compareGeom(bg, zones, stopOnError = FALSE)){
zones <- alignRasters(zones, bg, method = "near", func = terra::modal, cl = FALSE)
zones <- alignRasters(zones, bg, method = "near",
func = terra::modal, cl = FALSE)
}

# Output vector
Expand All @@ -1398,7 +1402,8 @@ thin_observations <- function(df, background, env = NULL, method = "random", min
# Environmental clustering

if(!terra::compareGeom(bg, env, stopOnError = FALSE)){
env <- alignRasters(env, bg, method = "near", func = terra::modal, cl = FALSE)
env <- alignRasters(env, bg, method = "near",
func = terra::modal, cl = FALSE)
}
# If there are any factors, explode
if(any(is.factor(env))){
Expand Down
2 changes: 1 addition & 1 deletion inst/CITATION
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ citEntry(
as.person("Maximilian H.K. Hesselbarth")
),
year = "2023",
version = "0.0.5",
version = "0.0.9",
textVersion = "Jung, M., Hesselbarth, H.K.M. (2023). An integrated species distribution modelling framework for heterogeneous biodiversity data. R package version 0.0.5")
Loading

0 comments on commit 1e8c768

Please sign in to comment.