View source: R/sample_pseudoabs.R
sample_pseudoabs | R Documentation |
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.
sample_pseudoabs(
data,
x,
y,
n,
method,
rlayer,
maskval = NULL,
calibarea = NULL,
sp_name = NULL
)
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:
|
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 |
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. |
A tibble object with x y coordinates of sampled pseudo-absence points
sample_background
and calib_area
.
## Not run:
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 K-means approach
set.seed(123)
ps1 <-
sample_pseudoabs(
data = single_spp,
x = "x",
y = "y",
n = nrow(single_spp) * 10,
method = c(method='kmeans', env = somevar),
rlayer = regions
)
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)
## End(Not run)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.