问题描述
我有一个多边形的 shapefile 和分布在多边形上的另一个点。我想根据每个多边形包含的点为每个多边形创建一个核密度估计。不幸的是,我只能使用 MASS 包中的 kde2d 函数创建平方 KDE。我希望 KDE 被塑造成多边形。 有什么建议吗?
kde1 <- kde2d(poly$X,poly$Y,n=100,)
解决方法
您可以为此使用 spatstat
包。这是一个阅读的例子
在 sf
的 shapefile 中,生成随机点并运行内核密度
点强度估计(单位面积点数):
library(sf)
#> Linking to GEOS 3.8.0,GDAL 3.0.4,PROJ 6.3.1
nc <- st_read(system.file("shape/nc.shp",package="sf"))
#> Reading layer `nc' from data source `/usr/lib/R/site-library/sf/shape/nc.shp' using driver `ESRI Shapefile'
#> Simple feature collection with 100 features and 14 fields
#> geometry type: MULTIPOLYGON
#> dimension: XY
#> bbox: xmin: -84.32385 ymin: 33.88199 xmax: -75.45698 ymax: 36.58965
#> geographic CRS: NAD27
nc_flat <- st_transform(nc,crs = 26917)
W <- as.owin(nc_flat$geometry[1]) # First county of North Carolina data set in spatstat format
library(spatstat)
X <- runifpoint(100,win = W)
plot(X,"Random points")
D <- density(X)
plot(D,main = "KDE")
,
好的!我设法通过使用 spatstat 包中的“ppp”函数来使用我自己的点数。
C <- as.owin(polygon$geometry[n])
p<- ppp(points$X,points$Y,window = C)
D <- density(p)
[enter image description here][1]
[1]: https://i.stack.imgur.com/YZN0V.png