rloopscomputational-geometryvolume

Errors occur when calculating hypervolumes on some, but not all, of my datasets in R using hypervolume package


Apologies for the vagueness of the question, but the error message was too long to include in the question. I am using the R library hypervolume to calculate hypervolumes of multiple species. I was recommended by a reviewer to subsample hypervolumes by X values (2, 3, 5, etc. depending on how many samples I have for each species), to try to control for increased niche hypervolume due to increased number of points (previously I was using cutoffs to calculate niche hypervolumes, e.g. hypervolumes for all species with more than 5 presence points). To do this, I have written the following code:

set.seed(42)
myfun <- function(x) {hypervolume(x, method = "box")}

DatAlaudaarvensisPoints <- replicate(99,DatAlaudaarvensis2[sample(nrow(DatAlaudaarvensis2), 3),], simplify=FALSE) |>
  lapply(myfun)
AnascreccaPoints <- replicate(99, Anascrecca2[sample(nrow(Anascrecca2), 3),], simplify=FALSE) |>
  lapply(myfun)

Repeating this for all of the species in question.

For some of the species it seems to work fine, but for most of them I get the following output:

Error during wrapup: Bandwidth must be non-zero.
Error: no more error handlers available (recursive errors?); invoking 'abort' restart
Browse[1]> 

Each of my species dataframes is structured as follows:

     bio01       bio12      bio05      bio06      bio16      bio17
-0.3765386  1.61372646  1.3483054 -0.6731469  1.9459743  0.6653154
-1.8453586 -1.49992668  1.1449311 -1.7417201 -0.2897975 -1.9108736
 0.6904630  0.08066363 -0.4607391  0.6716559 -0.2218050  0.5191682
 0.6904630  0.08066363 -0.4607391  0.6716559 -0.2218050  0.5191682
 0.6904630  0.08066363 -0.4607391  0.6716559 -0.2218050  0.5191682

Is the problem that I am trying to calculate hypervolumes with six environmental variables from too few points? Or that for some species there are not 99 unique combinations of three randomly selected points from which to calculate hypervolumes? In either case, is there a solution to the problem? I also am not sure what the Browse[1]> output means.


Solution

  • This error occurs in the hypervolume method if any calculated kde.bandwidth values are 0. The default bandwidth is estimated here as:

    (4/(ndim+2))^(1/(ndim+4)) * npoints^(-1/(ndim+4))*stdev
    

    About the only way for that expression to be 0 is if the standard deviation of a data column is 0. (Or to have 0 rows, but it doesn't sound like that's the issue.) The data you show has a lot of repeat values, which would make it pretty easy for a subsample to have 0 standard deviation thus causing the error.

    I don't know much about hypervolumes, but I think it makes sense that you need some variation in each dimension. Otherwise it's like asking for the volume of a square.