1

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). There is an example below (modified from: 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 (e.g. yellow points within black lines). And vice versa, I'd like a list of x coordinates falling within fhaty contour lines.

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)
Erin
  • 13
  • 3
  • 2
    You should provide some sort of [reproducible example](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample data that could be used to test possible solutions. It would be easier to help you and make it more clear what the desired result is. – MrFlick Sep 02 '16 at 20:43
  • You're right, thanks. I've updated it with a reproducible example. – Erin Sep 03 '16 at 15:58

1 Answers1

1

The output of kde can be transformed into a raster and then from there you can extract any contour using the rasterToPolygons function. 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.

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. The coordinates of these points may be extracted in an array using 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.

I hope this helps.

STEP 1: convert the outputs from kde to a raster object

# 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

#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

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

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

STEP 5: locate the points that intersect with one polygon or the other

#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)
S.Derville
  • 208
  • 1
  • 2
  • 6