p = c("sf", "sp", "terra", "spatstat.data")
if(any(!unlist(lapply(p, requireNamespace, quietly=TRUE)))) {
m = which(!unlist(lapply(p, requireNamespace, quietly=TRUE)))
message("Can't run examples, please install ", paste(p[m], collapse = " "))
} else {
invisible(lapply(p, require, character.only=TRUE))
data(meuse, package = "sp")
meuse <- st_as_sf(meuse, coords = c("x", "y"), crs = 28992,
agr = "constant")
# Using a raster mask
r <- rast(ext(meuse), resolution=40, crs=crs(meuse))
r[] <- rep(1,ncell(r))
pa <- pseudo.absence(meuse, n=100, window='hull', KDE=TRUE, ref = r,
sigma='Diggle', s=50)
col.br <- colorRampPalette(c('blue','yellow'))
plot(pa$kde, col=col.br(10))
plot(st_geometry(meuse), pch=20, cex=1, add=TRUE)
plot(st_geometry(pa$sample), col='red', pch=20, cex=1, add=TRUE)
legend('top', legend=c('Presence', 'Pseudo-absence'),
pch=c(20,20),col=c('black','red'), bg="white")
# With clustered data
data(bei, package = "spatstat.data")
trees <- st_as_sf(bei)
trees <- trees[-1,]
trees.abs <- pseudo.absence(trees, n=100, window='extent', KDE=TRUE)
col.br <- colorRampPalette(c('blue','yellow'))
plot(trees.abs$kde, col=col.br(10))
plot(st_geometry(trees), pch=20, cex=0.50, add=TRUE)
plot(st_geometry(trees.abs$sample), col='red', pch=20, cex=1, add=TRUE)
legend('top', legend=c('Presence', 'Pseudo-absence'),
pch=c(20,20),col=c('black','red'),bg="white")
}
Run the code above in your browser using DataLab