kde 的输出可以转换为栅格,然后您可以使用rasterToPolygons 函数从那里提取任何轮廓。将点转换为 sp 包可识别的格式后,您可以使用 gIntersection 函数查看空间对象之间的任何类型的交集。
您最终会得到两个 SpatialPoints 对象 x.inY 和 y.inX,它们包含包含在 fhaty 50% 轮廓内的 x 点,反之亦然。可以使用coordinates(...) 将这些点的坐标提取到一个数组中。
这可能不是最优雅的解决方案,但如果kde 函数释放的数组不太大,它应该可以正常工作。
我希望这会有所帮助。
第 1 步:将输出从 kde 转换为光栅对象
# for the x kde
arrayX <- expand.grid(list(fhatx$eval.points[[1]],fhatx$eval.points[[2]]))
arrayX$z <- as.vector(fhatx$estimate)
rasterX <- rasterFromXYZ(arrayX)
# for the y kde
arrayY <- expand.grid(list(fhaty$eval.points[[1]],fhaty$eval.points[[2]]))
arrayY$z <- as.vector(fhaty$estimate)
rasterY <- rasterFromXYZ(arrayY)
STEP 2:在 0 到 100 之间重新缩放栅格,然后将 50 等高线内的所有像元转换为 1。当然等高线可以更改为 95 或其他值
#for raster x
rasterX <- rasterX*100/rasterX@data@max
rasterX[rasterX[]<=50,] <- NA
rasterX[rasterX[]>50,] <- 1
#[enter image description here][1]for raster y
rasterY <- rasterY*100/rasterY@data@max
rasterY[rasterY[]<=50,] <- NA
rasterY[rasterY[]>50,] <- 1
第三步:提取50%轮廓对应的多边形
polyX50<-rasterToPolygons(rasterX, n=16, na.rm=T, digits=4, dissolve=T)
polyY50<-rasterToPolygons(rasterY, n=16, na.rm=T, digits=4, dissolve=T)
第 4 步:将点转换为空间对象以使用 sp 库
x.points <- SpatialPoints(x)
y.points <- SpatialPoints(y)
第 5 步:定位与一个多边形或另一个多边形相交的点
#x points falling in fhatx 50 contour
x.inY <- gIntersection(x.points, polyY50)
#y points falling in fhatx 50 contour
y.inX <- gIntersection(y.points, polyX50)
剧情
par(mfrow=c(1,2))
plot(fhatx, cont=c(50,95), col="red")
plot(fhaty, cont=c(50,95), col="green",add=TRUE)
plot(x.points, col="red", add=T)
plot(y.points, col="green", add=T)
plot(fhatx, cont=c(50,95), col="red")
plot(fhaty, cont=c(50,95), col="green",add=TRUE)
plot(x.inY, col="red", add=T)
plot(y.inX, col="green", add=T)