Returns a function that performs preferential sampling on a spatial field. A weights layer is built from one or more covariates (as a SpatRaster), and cells are sampled with probability proportional to those weights, so samples can be biased towards particular parts of the covariate value range.

sample_preferential(
  covariate = NULL,
  strength = 1,
  fun = NULL,
  combine = c("prod", "mean"),
  range = NULL,
  replace = FALSE,
  ...
)

Arguments

covariate

Name(s) or index/indices of the raster layer(s) to use as the biasing covariate(s). Defaults to all layers of x.

strength

Numeric controlling the strength of the bias applied to the rescaled covariate values. strength is used as an exponent on the rescaled covariate values to produce a weights raster together with a small constant for numerical stability (e.g. (z + 1e-6)^strength). With the default strength = 1, the weights are proportional to the covariate values. With strength = 0, all cells have equal weight (uniform sampling). With strength > 1, higher covariate values are increasingly favored, and with strength < 0, lower covariate values are favored.

fun

Optional function applied to the combined, rescaled ([0, 1]) covariate raster to produce a weights raster. When supplied it overrides strength. Must accept and return a numeric vector (it is passed to terra::app()).

combine

How to combine multiple covariates into a single weight, either "prod" (product) or "mean". Ignored for a single covariate.

range

Optional named list giving c(min, max) value ranges used to restrict sampling to cells whose covariate values fall within the range, e.g. list(elevation = c(100, 500)). Cells outside the range are masked out.

replace

Logical; should cells be sampled with replacement?

...

Reserved for future use.

Value

A function that accepts x (SpatRaster) and size and returns an sf object.

Examples

rast_grid = terra::rast(
  ncols = 300, nrows = 100,
  xmin = 0, xmax = 300,
  ymin = 0, ymax = 100
)
terra::values(rast_grid) = runif(terra::ncell(rast_grid))

sam_field(rast_grid, 100, method = sample_preferential(strength = 2))
#> Simple feature collection with 100 features and 1 field
#> Geometry type: POINT
#> Dimension:     XY
#> Bounding box:  xmin: 1.5 ymin: 0.5 xmax: 296.5 ymax: 98.5
#> CRS:           NA
#> First 10 features:
#>        lyr.1           geometry
#> 1  0.4529907 POINT (200.5 74.5)
#> 2  0.4179270 POINT (219.5 93.5)
#> 3  0.9155951  POINT (130.5 0.5)
#> 4  0.9450059  POINT (147.5 6.5)
#> 5  0.9942481  POINT (246.5 4.5)
#> 6  0.9755176   POINT (70.5 5.5)
#> 7  0.7927128 POINT (296.5 65.5)
#> 8  0.6347594 POINT (125.5 38.5)
#> 9  0.9472884 POINT (260.5 32.5)
#> 10 0.9250780 POINT (174.5 37.5)