rspatial / terra

R package for spatial data handling https://rspatial.github.io/terra/reference/terra-package.html
GNU General Public License v3.0
543 stars 90 forks source link

In `spatSample`, should the default be `replace = TRUE` when `method = "weights"` ? #1565

Open frousseu opened 4 months ago

frousseu commented 4 months ago

Hi!

When using method = "weights" in spatSample, the default replace = FALSE has the consequence that non 0 cells will be filled if the size asked is larger than the number of non 0 cells, losing the proportionality between the weights and the density of points generated. I think it would make more sense to have replace = TRUE as the default when method = "weights". In fact, I have a hard time finding a use for replace = FALSE when the weight method is used. Below is an example with the first case showing a lack of proportionality and the second case showing what I think should be the default.

Thanks! François

library(terra)
library(sf)

set.seed(123)
r <- rast(res = 2, extent = c(0, 10, 0, 10))
v <- sample(c(0, 1, 10), ncell(r), replace = TRUE)
r <- setValues(r, v)
plot(r)

ss <- spatSample(r, size = 25, method = "weights", as.points = TRUE, replace = FALSE) |> 
  st_as_sf() |> 
  st_jitter(amount = 0.2) |> 
  st_geometry()

par(mfrow = c(1, 2))
plot(r)
plot(ss, add = TRUE)
length(ss)

ss <- spatSample(r, size = 25, method = "weights", as.points = TRUE, replace = TRUE) |> 
  st_as_sf() |> 
  st_jitter(amount = 0.2) |> 
  st_geometry()

plot(r)
plot(ss, add = TRUE)
length(ss)