简体   繁体   English

将 kernel 密度估计提取到 R 中的新样本点

[英]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)

kde plot kde plot

The 'density' output is this: '密度' output 是这样的:

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在某些空间位置的值,那么您可以

  • make the query locations into a point pattern X , then extract the numerical values at those locations by values <- D[X] ;将查询位置变成点模式X ,然后通过values <- D[X]提取这些位置的数值; or或者
  • convert the pixel image to a function by f <- 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.pppat参数会更快、更准确:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM