# 95% contour from kernel density estimates [closed]

I am looking to calculate the 95% volume contour from a kernel density estimate to estimate an animal's home range.

Normally, I work in R, but I calculated the "kernel density estimate" in ArcGIS so that I could incorporate barriers to movement. To do this, I used the "kernel interpolation with barriers tool". The output of this tool is a raster, where the value for each grid cell is the estimated number of records per km2, given the number and distribution of the actual records it was calculated from. values where the animal is not expected to occur have a value of 0 of less.

My question is, given the output of this tool, how can a calculate the 95% contour? Ideally I would like a solution in R. I know how to import the raster in R, but I don't know of any functions that can accomplish what I want.

I tried the function getVolumeUD from the move package, but got an error:

Error in (function (classes, fdef, mtable)  :
unable to find an inherited method for function ‘getVolumeUD’ for signature ‘"RasterLayer"’
• Can you show us a plot of the raster or tell us where to download it? Have you looked at the habitat modelling packages on CRAN? Commented Feb 28, 2018 at 8:48
• Specifically adehabitatHR and its home range estimation functions. Commented Feb 28, 2018 at 12:02
• The problem with adehabitatHR is that it can only calculate contours from estUD objects, which are created from the kernelUD function in the same package. I've edited by question with more information Commented Feb 28, 2018 at 16:01
• There is a function "raster.vol" for calculating raster volume contours in the spatialEco package. Commented Feb 28, 2018 at 19:59
• That's exactly what I needed, you should post that as an answer Commented Mar 1, 2018 at 21:27

So for some matrix, Z, you want to find the value k such that the sum of Z for Z > k equals 0.95 * sum(Z).

You can do this with uniroot on a function that returns the amount of a matrix above a threshold. This function returns an appropriate objective function for a raster:

cover = function(z,k=0.95){T=sum(values(z))*k;function(t){sum(z[][z[]>t])-T}}

Here's my sample raster, r:

So let's make an objective function for that raster:

c1 = cover(r)

Test it - for a small value it should be positive, for a large value negative, our solution is where the function is zero:

c1(1)
## [1] 20135.79   # to small
c1(10)
## [1] -24245.1   # too big

So we feed that to uniroot to get the zero point:

s = uniroot(c1,c(0,max(r[])))
s\$root
## [1] 5.776295

Then we can plot that:

And the green area is your 95% probability density contour.

• This is a really cool solution. Commented Feb 28, 2018 at 17:33