[英]Extracting kernel density estimates to a new sample points in R
I have spatial referenced data (shapefiles) of 400 polygons and points that are spread over them.我有 400 个多边形的空间参考数据(shapefile)和分布在它们上面的点。 I wish to create a raster of kernel density estimate for each polygon based on the points it contains.
我希望根据它包含的点为每个多边形创建一个 kernel 密度估计的栅格。 After that, I want to create a sample of 100 random points for each polygon, where each point has a spatial reference (x and y coordinates) and a kde value.
之后,我想为每个多边形创建一个包含 100 个随机点的样本,其中每个点都有一个空间参考(x 和 y 坐标)和一个 kde 值。 All I managed to do until now was to create a kde image in the shape of the polygons using the 'ppp.density' function, but the output comes out different than the one described in the document and I don't know in what format it is and how to obtain the kde values to a sample of new points.
到目前为止,我所做的只是使用“ppp.density”function 创建一个多边形形状的 kde 图像,但是 output 与文档中描述的不同,我不知道是什么格式它是以及如何获取新点样本的 kde 值。
Any help would be much appreciated.任何帮助将非常感激。
buffer <- st_read(".../.././buffers.shp")
pbb<- st_read(".../.../.././pbb.shp")
library(spatstat)
for (p in 1:400) {
if(p %in% pbb$value) {
poly123<- pbb[pbb$value == p,]
C <- as.owin(buffer$geometry[p])
point<- ppp(poly123$X,poly123$Y, window = C)
d <- density(point, kernel = "gaussian")
plot(d)
You are using the function density.ppp
(not "ppp.density") from the spatstat
package.您正在使用来自
spatstat
package 的 function density.ppp
(不是“ppp.density”)。
If p
is a point pattern (class ppp
) then D <- density(p)
computes the unnormalised kernel density estimate.如果
p
是点模式(类ppp
),则D <- density(p)
计算未归一化的 kernel 密度估计。 See help(density.ppp)
for explanation and examples.有关解释和示例,请参阅
help(density.ppp)
。
The result D
is a pixel image (class im
) which can be printed, plotted and subsetted, etc. (In your question you have just printed the internal structure of the im
object.)结果
D
是可以打印、绘制和子集化等的像素图像(类im
)。(在您的问题中,您刚刚打印了im
object 的内部结构。)
After computing the pixel image D
, if you want to know the values of D
at some spatial locations, then you could在计算像素图像
D
之后,如果您想知道D
在某些空间位置的值,那么您可以
X
, then extract the numerical values at those locations by values <- D[X]
;X
,然后通过values <- D[X]
提取这些位置的数值; orf <- as.function(D)
then evaluate the function at desired coordinates x,y
by values <- f(x,y)
.f <- as.function(D)
将像素图像转换为 function 然后通过values <- f(x,y)
在所需坐标x,y
处评估 function 。 If you want to know the density values at the original data locations p
, then it is faster and more accurate to use the at
argument to density.ppp
:如果您想知道原始数据位置
p
的密度值,那么使用density.ppp
的at
参数会更快、更准确:
values <- density(p, at="points")
Warning: In your example (and the examples above), the smoothing bandwidth sigma
was not specified.警告:在您的示例(以及上面的示例)中,未指定平滑带宽
sigma
。 The default value of sigma
may be unsatisfactory for your data. sigma
的默认值可能对您的数据不满意。 See the help file and examples for density.ppp
.请参阅
density.ppp
的帮助文件和示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.