# Acquire environmental variables
files <- list.files(path = file.path(system.file(package = "dismo"), "ex"),
pattern = "grd",
full.names = TRUE)
predictors <- terra::rast(files)
# Prepare background locations, by sampling also on areas with NA values
bg_coords <- terra::spatSample(predictors,
size = 9000,
method = "random",
xy = TRUE,
values = FALSE)
nrow(bg_coords)
# Thin the locations
# The function will remove the coordinates that have NA values for some
# predictors. Note that the function expects to have the coordinates in two
# columns named "x" and "y"
colnames(bg_coords)
thinned_bg <- thinData(bg_coords,
env = predictors)
nrow(thinned_bg)
# Here we sample only on areas without NA values and then we double the
# coordinates
bg_coords <- terra::spatSample(predictors,
size = 9000,
method = "random",
na.rm = TRUE,
xy = TRUE,
values = FALSE)
thinned_bg <- thinData(rbind(bg_coords, bg_coords),
env = predictors)
nrow(thinned_bg)
# In case of a dataframe containing more than two columns (e.g. a dataframe
# with the coordinates plus an additional column with the age of the species)
# and custom column names, use the function in this way
age <- sample(c(1, 2),
size = nrow(bg_coords),
replace = TRUE)
data <- cbind(age, bg_coords)
colnames(data) <- c("age", "X", "Y")
thinned_bg <- thinData(data,
env = predictors,
x = "X",
y = "Y")
head(data)
Run the code above in your browser using DataLab