Wrong result from calc_SPIE when there are only singletons
AlbanSagouis opened this issue · 2 comments
I'm using the latest version of mobr (c5fd569) and R 4.0.2.
Here are 2 communities with 30 species and only singletons. One community has 3 and the other has 5. calc_SPIE()
gives NA for the fist community which is the expected behaviour but not for the second one because the PIE value is not accurately = 1.
library(mobr)
x <- matrix(0, nrow = 2, ncol = 30)
x[1, 1:3] <- 1
x[2, 1:5] <- 1
calc_PIE(x)
[1] 1 1
calc_SPIE(x)
[1] NA 9.007199e+15
calc_SPIE(x)
should have given two NAs.
If we look into calc_SPIE()
:
PIE = calc_PIE(x, replace = FALSE)
SPIE = 1/(1 - PIE)
SPIE
[1] Inf 9.007199e+15
Not the wanted result because:
PIE == 1
[1] TRUE FALSE
Seems to be a rounding problem, confirmed here:
PIE = round(calc_PIE(x, replace = FALSE), 0)
SPIE = 1/(1 - PIE)
SPIE
[1] Inf Inf
I have no fix to commit to solve the issue.
Hope it helps,
Thank you @AlbanSagouis. It seems that the simplest solution here is just to round PIE to the 10th digit or something. I propose we add PIE = round(PIE, 10)
to the function calc_PIE. @T-Engel any concerns?
Thanks for spotting this, Alban. Yes, this is because of issues with the floating-point representation of numbers in R.
We used the "==" operator to check if PIE was 1 and that is not always safe as explained here.
I changed the code to use the "all.equal" notation instead. See here: fa996a8
I hope this has fixed it.