I have a shapefile of polygons and a shapefile of points that are distributed within the polygons. I've created kernel density estimate (KDE) maps for each polygon based on the points it contains using the density.ppp function from the spatstat
package.
I now wish to create different kde's with different pixel sizes in order to chose the one that best suits my model. I tried using the eps argument in the as.mask
function, but that only changed the pixel size of the window and not of the kernel map itself so the results did not change.
after going throw the entire manual of the density.ppp function, all I was able to find that looked related was the pixellate.ppp
function from the spatstat.geom
package, but i'm not sure how to use it with the density.ppp
.
Any suggestion how to change the pixel size of the kde's?
library(sf)
library(spatstat)
buffer <- st_read("gis/layers/buffers.shp")
pbb<- st_read("gis/layers/points_by_buffer.shp")
for (p in 1:10) {
if(p %in% pbb$field_id) {
poly123 <- pbb[pbb$field_id == p,]
C <- as.owin(buffer$geometry[p])
W<- as.mask(C, eps = 100)
point<- ppp(poly123$X,poly123$Y, window = W)
sigma <- bw.diggle(point)
d <- density.ppp(point, kernel = "gaussian", sigma=sigma, positive = TRUE, at="pixels", )
plot(d)
}
Three different things:
If you change the resolution (which you can do using the arguments eps
or dimyx
in the call to density.ppp
), the values of density will not change very much; you are just changing the spacing of the locations where the density is calculated.
If you change the bandwidth (which you can do by using the arguments sigma
and adjust
in the call to density.ppp
), the values of density will change substantially. When you increase the bandwidth, each data point is effectively spread over a greater distance, and the density estimate becomes smoother and flatter.
If you change the unit of length, everything will change by a scale factor. The values of density are "number of points per unit area". If the density value is 3, this means we expect to see an average of 3 points per square unit. Here the 'unit' is the unit of length in which the spatial coordinates are expressed. If the original point coordinates are expressed in metres, then the density values are 'number of points per square metre'. If you want to change this, there are two ways:
rescale.ppp
to do this: Xnew <- rescale(Xold, 1000)
. You can declare the name of the unit of length using the function unitname
(as in unitname(Xnew) <- "km"
), or using rescale.ppp
(as in Xnew <- rescale(Xold, 1000, "km")
). After rescaling the point pattern, you invoke density.ppp
(as in Dnew <- density(Xnew, ...)
with the bandwidth expressed in kilometres) and the resulting density values are expressed in points per square km.Dnew <- D * 1000^2
where D
is the original density image) and also rescaling the spatial coordinates of the image (by typing Dnew <- rescale(Dnew, 1000, "km")
). We don't recommend this because it is easy to mess up.See the spatstat book for further information.