Skip to content

Commit

Permalink
Return NA if a diversity measure is undefined
Browse files Browse the repository at this point in the history
  • Loading branch information
nfrerebeau committed Jan 1, 2025
1 parent b49fb6d commit 622bb88
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 13 deletions.
32 changes: 20 additions & 12 deletions R/diversity_alpha.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,12 @@ setMethod(
N_rare <- sum(x[x <= k]) # Number of individuals in the rare species

F1 <- sum(x == 1) # Number of singleton species
if (F1 == N_rare)
stop(tr_("ACE is undefined when all rare species are singletons."),
tr_("Consider using the bias-corrected Chao1 estimator instead."),
call. = FALSE)
if (F1 == N_rare) {
warning(tr_("ACE is undefined when all rare species are singletons."),
tr_("Consider using the bias-corrected Chao1 estimator instead."),
call. = FALSE)
return(NA_real_)
}

## Sample coverage estimate for rare species
## ie. proportion of all individuals in rare species that are not singletons
Expand Down Expand Up @@ -188,11 +190,13 @@ setMethod(
if (improved) {
f3 <- sum(x == 3) # Number of triple species
f4 <- sum(x == 4) # Number of quadruple species
if (f4 == 0)
stop(
if (f4 == 0) {
warning(
tr_("Improved Chao1 estimator is undefined when there is no quadruple species."),
call. = FALSE
)
return(NA_real_)
}

k <- f1 - ((N - 3) / (N - 1)) * ((f2 * f3) / (2 * f4))
D <- D + ((N - 3) / N) * (f3 / (4 * f4)) * max(k, 0)
Expand Down Expand Up @@ -231,11 +235,13 @@ setMethod(
if (improved) {
q3 <- sum(q == 3) # Number of triple species
q4 <- sum(q == 4) # Number of quadruple species
if (q4 == 0)
stop(
if (q4 == 0) {
warning(
tr_("Improved Chao2 estimator is undefined when there is no quadruple species."),
call. = FALSE
)
return(NA_real_)
}

k <- q1 - ((t - 3) / (t - 1)) * ((q2 * q3) / (2 * q4))
D <- D + ((t - 3) / (4 * t)) * (q3 / q4) * max(k, 0)
Expand Down Expand Up @@ -265,10 +271,12 @@ setMethod(
t <- sum(rowSums(x[, q <= k]) != 0)

q1 <- sum(q == 1) # Number of unique species in the assemblage
if (q1 == N_infr)
stop(tr_("ICE is undefined when all rare species are singletons."),
tr_("Consider using the bias-corrected Chao2 estimator instead."),
call. = FALSE)
if (q1 == N_infr) {
warning(tr_("ICE is undefined when all rare species are singletons."),
tr_("Consider using the bias-corrected Chao2 estimator instead."),
call. = FALSE)
return(NA_real_)
}

## Sample coverage estimate
## ie. proportion of all incidences of infrequent species that are not uniques
Expand Down
3 changes: 2 additions & 1 deletion inst/tinytest/test_index.R
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ expect_equal(round(index_menhinick(n), 3), 1.534) # 1.534
expect_equal(round(index_margalef(n), 3), 3.7) # 3.7
expect_equal(round(index_berger(n), 3), 0.206) # 0.2059
expect_equal(round(index_chao1(n, unbiased = TRUE), 3), 21.491) # 21.49
expect_error(index_chao1(n, unbiased = TRUE, improved = TRUE)) # 22.65 (?)
expect_warning(index_chao1(n, unbiased = TRUE, improved = TRUE)) # 22.65 (?)
expect_equal(suppressWarnings(index_chao1(n, unbiased = TRUE, improved = TRUE)), NA_real_) # 22.65 (?)
expect_equal(round(index_ace(n), 3), 22.087) # 22.09
expect_equal(round(index_squares(n), 3), 21.924) # 21.92

Expand Down

0 comments on commit 622bb88

Please sign in to comment.