This function provide several methods for sampling pseudo-absences, for instance totally random sampling method, with the options of using different environmental and or geographical constraints.
Usage
sample_pseudoabs(
data,
x,
y,
n,
method,
rlayer,
maskval = NULL,
calibarea = NULL,
sp_name = NULL
)
Arguments
- data
data.frame or tibble. Database with presences (or presence-absence, or presences-pseudo-absence) records, and coordinates
- x
character. Column name with spatial x coordinates
- y
character. Column name with spatial y coordinates
- n
integer. Number of pseudo-absences to be sampled
- method
character. Pseudo-absence allocation method. It is necessary to provide a vector for this argument. The methods implemented are:
random: Random allocation of pseudo-absences throughout the area used for model fitting. Usage method='random'.
env_const: Pseudo-absences are environmentally constrained to regions with lower suitability values predicted by a Bioclim model. For this method, it is necessary to provide a raster stack or brick object with environmental variables Usage method=c(method='env_const', env = somevar).
geo_const: Pseudo-absences are allocated far from occurrences based on a geographical buffer. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_const', width='50000').
geo_env_const: Pseudo-absences are constrained environmentally (based on Bioclim model) and distributed geographically far from occurrences based on a geographical buffer. For this method, a raster with environmental variables stored as SpatRaster object should be provided. A value of the buffer width in m must be provided if raster (used in rlayer) has a longitude/latitude CRS, or in map units in other cases. Usage method=c('geo_env_const', width='50000', env = somevar).
geo_env_km_const: Pseudo-absences are constrained using a three-level procedure; it is similar to the geo_env_const with an additional step which distributes the pseudo-absences in environmental space using k-means cluster analysis. For this method, it is necessary to provide a raster stack or brick object with environmental variables and a value of the buffer width in m if raster (used in rlayer) has a longitude/latitude CRS, or map units in other cases. Usage method=c('geo_env_km_const', width='50000', env = somevar).
- rlayer
SpatRaster. A raster layer used for sampling pseudo-absence A layer with the same resolution and extent that environmental variables that will be used for modeling is recommended. In the case use maskval argument, this raster layer must contain the values used to constrain sampling
- maskval
integer, character, or factor. Values of the raster layer used for constraining the pseudo-absence sampling
- calibarea
SpatVector A SpatVector which delimit the calibration area used for a given species (see
calib_area
function).- sp_name
character. Species name for which the output will be used. If this argument is used, the first output column will have the species name. Default NULL.
Examples
if (FALSE) { # \dontrun{
require(terra)
require(dplyr)
data("spp")
somevar <- system.file("external/somevar.tif", package = "flexsdm")
somevar <- terra::rast(somevar)
regions <- system.file("external/regions.tif", package = "flexsdm")
regions <- terra::rast(regions)
plot(regions)
single_spp <-
spp %>%
dplyr::filter(species == "sp3") %>%
dplyr::filter(pr_ab == 1) %>%
dplyr::select(-pr_ab)
# Pseudo-absences randomly sampled throughout study area
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = "random",
rlayer = regions,
maskval = NULL,
sp_name = "sp3"
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19) # presences
points(ps1[-1], col = "red", cex = 0.7, pch = 19) # absences
# Pseudo-absences randomly sampled within a regions where a species occurs
## Regions where this species occurrs
samp_here <- terra::extract(regions, single_spp[2:3])[, 2] %>%
unique() %>%
na.exclude()
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = "random",
rlayer = regions,
maskval = samp_here
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
points(ps1, col = "red", cex = 0.7, pch = 19)
# Pseudo-absences sampled with geographical constraint
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = c("geo_const", width = "30000"),
rlayer = regions,
maskval = samp_here
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
points(ps1, col = "red", cex = 0.7, pch = 19)
# Pseudo-absences sampled with environmental constraint
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = c("env_const", env = somevar),
rlayer = regions,
maskval = samp_here
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
points(ps1, col = "red", cex = 0.7, pch = 19)
# Pseudo-absences sampled with environmental and geographical constraint
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = c("geo_env_const", width = "50000", env = somevar),
rlayer = regions,
maskval = samp_here
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
points(ps1, col = "red", cex = 0.7, pch = 19)
# Pseudo-absences sampled with environmental and geographical constraint and with k-mean clustering
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = c("geo_env_km_const", width = "50000", env = somevar),
rlayer = regions,
maskval = samp_here
)
plot(regions, col = gray.colors(9))
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
points(ps1, col = "red", cex = 0.7, pch = 19)
# Sampling pseudo-absence using a calibration area
ca_ps1 <- calib_area(
data = single_spp,
x = "x",
y = "y",
method = c("buffer", width = 50000),
crs = crs(somevar)
)
plot(regions, col = gray.colors(9))
plot(ca_ps1, add = T)
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 50,
method = "random",
rlayer = regions,
maskval = NULL,
calibarea = ca_ps1
)
plot(regions, col = gray.colors(9))
plot(ca_ps1, add = T)
points(ps1, col = "red", cex = 0.7, pch = 19)
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 50,
method = "random",
rlayer = regions,
maskval = samp_here,
calibarea = ca_ps1
)
plot(regions, col = gray.colors(9))
plot(ca_ps1, add = T)
points(ps1, col = "red", cex = 0.7, pch = 19)
points(single_spp[-1], col = "blue", cex = 0.7, pch = 19)
} # }