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

enter image description here

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?
    – Spacedman
    Commented Feb 28, 2018 at 8:48
  • 1
    Specifically adehabitatHR and its home range estimation functions.
    – Spacedman
    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
    – Splash1199
    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
    – Splash1199
    Commented Mar 1, 2018 at 21:27

1 Answer 1


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:

enter image description here

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:

## [1] 20135.79   # to small
## [1] -24245.1   # too big

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

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

Then we can plot that:

enter image description here

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

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.