sample_grid {ebirdst} | R Documentation |
Subsample points to deal with spatiotemporal bias in observations by defining
a grid in space and time, then sampling the given number of points from each
cell. sample_case_control()
additionally samples presence and absence
independently.
sample_grid(x, res, t_res, n = 1, replace = FALSE, jitter = TRUE)
sample_case_control(x, res, t_res, n = 1, replace = FALSE, jitter = TRUE)
x |
data frame or sf object; the points to subsample. If |
res |
numeric; the size in meters of the grid to sample from. This can be a 2 element vector indicating the x and y dimensions of the cells. |
t_res |
numeric; the temporal resolution for sampling expressed as a
proportion of the year. For example, |
n |
integer; the number of points to sample from each grid cell. |
replace |
logical; whether to sample with replacement. |
jitter |
logical; to avoid always using the same grid for sampling, the grid can be jittered so that the origin is different each time this function is called. |
Logical vector indicating which rows are selected.
## Not run:
# download example data
path <- ebirdst_download("example_data", tifs_only = FALSE)
# or get the path if you already have the data downloaded
path <- get_species_path("example_data")
# test data to sample
preds <- load_predictions(path, return_sf = TRUE)
# sample on a 100km, 1 month grid
s <- sample_grid(preds, res = 100000, t_res = 1 / 12)
preds_grid <- preds[s, ]
# case control sampling independently samples presence and absence
s <- sample_case_control(preds, res = 100000, t_res = 1 / 12)
preds_cc <- preds[s, ]
# grid sampling preserves the presence/absence ratio
table(preds$obs > 0) / nrow(preds)
table(preds_grid$obs > 0) / nrow(preds_grid)
# while case control sampling increases the prevelance of presences
table(preds_cc$obs > 0) / nrow(preds_cc)
# plot
library(sf)
p <- par(mar = c(0, 0, 0, 0))
plot(st_geometry(preds), col = "black", pch = 19, cex = 0.2)
plot(st_geometry(preds_cc), col = "red", pch = 19, cex = 0.5, add = TRUE)
## End(Not run)