简体   繁体   English

使用R'ks'包提取重叠kdes中的数据点

[英]Extracting data points within overlapping kdes using R 'ks' package

I am using the ks package in R, and want to determine which location data points fall within areas of overlapping 2d kernel contours (I am comparing the UDs of two different species home ranges). 我正在R中使用ks程序包,并想确定哪些位置数据点落在重叠的2d内核轮廓区域内(我正在比较两个不同物种宿主范围的UD)。 There is an example below (modified from: http://www.rdocumentation.org/packages/ks/versions/1.5.3/topics/plot.kde ?). 下面有一个示例(从http://www.rdocumentation.org/packages/ks/versions/1.5.3/topics/plot.kde ?修改而来)。

What I am trying to generate is a list of all the y points that fall within the contours of fhatx (eg yellow points within black lines). 我试图生成的是落在fhatx轮廓内的所有y点的列表(例如,黑线内的黄色点)。 And vice versa, I'd like a list of x coordinates falling within fhaty contour lines. 反之亦然,我想要一列落在常见轮廓线内的x坐标。

library(ks)
x <- rmvnorm.mixt(n=100, mus=c(0,0), Sigmas=diag(2), props=1)
Hx <- Hpi(x)
fhatx <- kde(x=x, H=Hx) 
y <- rmvnorm.mixt(n=100, mus=c(0.5,0.5), Sigmas=0.5*diag(2), props=1)
Hy <- Hpi(y)
fhaty <- kde(x=y, H=Hy)
contourLevels(fhatx, cont=c(75, 50, 25))
contourSizes(fhatx, cont=25, approx=TRUE)
plot(fhatx, cont=c(50,95), drawpoints=TRUE)
plot(fhaty, cont=c(50,95), col=3, drawpoints=TRUE,col.pt="yellow", add=TRUE)

The output of kde can be transformed into a raster and then from there you can extract any contour using the rasterToPolygons function. 可以将kde的输出转换成栅格,然后从那里可以使用rasterToPolygons函数提取任何轮廓。 Once your points are converted to a format recognized by the sp package, you can look at any kind of intersection between the spatial objects using the gIntersection function. 一旦将点转换为sp包可识别的格式,就可以使用gIntersection函数gIntersection空间对象之间的任何交集。

You end up with two SpatialPoints objects x.inY and y.inX which contain the x points included within the 50% contour of fhaty and vice versa. 最后得到两个SpatialPoints对象x.inYy.inX ,其中包含x的点包含在fhaty的50%轮廓中,反之亦然。 The coordinates of these points may be extracted in an array using coordinates(...) . 这些点的坐标可以使用coordinates(...)在数组中提取。

This is probably not the most elegant solution but it should work fine if the array released by the kde function is not too big. 这可能不是最优雅的解决方案,但是如果kde函数释放的数组不太大,它应该可以正常工作。

I hope this helps. 我希望这有帮助。

STEP 1: convert the outputs from kde to a raster object 步骤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: rescale the rasters between 0 and 100, then convert all cells within the 50 contour to 1. Of course the contour may be changed to 95 or another value 步骤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

STEP 3: extract the polygons corresponding to the 50% contour 步骤3:提取与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)

STEP 4: convert your points to spatial objects in order to use the sp library 步骤4:将点转换为空间对象以使用sp库

x.points <- SpatialPoints(x)
y.points <- SpatialPoints(y)

STEP 5: locate the points that intersect with one polygon or the other 步骤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)

Plot 情节

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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