Skip to content

Commit 242625e

Browse files
committed
When no individuals in a sample PIE and SPIE are set to zero in the function calc_PIE and calc_SPIE. Other code style changes and improved documentation.
1 parent 4114e96 commit 242625e

File tree

5 files changed

+89
-59
lines changed

5 files changed

+89
-59
lines changed

R/mobr_boxplots.R

Lines changed: 52 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -50,24 +50,28 @@ calc_chao1 = function(x) {
5050

5151
#' Calculate probability of interspecific encounter (PIE)
5252
#'
53-
#' \code{calc_PIE} returns the probability of interspecific encounter (PIE)
53+
#' \code{calc_PIE} returns the probability of interspecific encounter (PIE)
5454
#' which is also known as Simpson's evenness index and Gini-Simpson index.
5555
#'
56-
#' Per default, Hurlbert's (1971) sample-size corrected formula is used:
56+
#' By default, Hurlbert's (1971) sample-size corrected formula is used:
5757
#'
5858
#' \eqn{PIE = N /(N - 1) * (1 - sum(p_i^2))}
5959
#'
60-
#' where N is the total number of individuals and \eqn{p_i} is the relative abundance
61-
#' of species i. This formulation uses sampling without replacement (\code{replace = F} )
62-
#' For the uncorrected version (i.e. using sampling with replacement), set \code{replace = T}.
63-
#'
64-
#' In earlier versions of \code{mobr}, there was an additional argument (\code{ENS}) for
65-
#' the conversion into an effective number of species (i.e S_PIE). Now, \code{calc_SPIE} has become
66-
#' its own function and the (\code{ENS}) argument is no longer supported . Please, use \code{calc_SPIE} instead.
67-
#'
60+
#' where N is the total number of individuals and \eqn{p_i} is the relative
61+
#' abundance of species i. This formulation uses sampling without replacement
62+
#' (\code{replace = F} ) For sampling with replacement (i.e., the sample-size
63+
#' uncorrected version), set \code{replace = T}.
64+
#'
65+
#' In earlier versions of \code{mobr}, there was an additional argument
66+
#' (\code{ENS}) for the conversion into an effective number of species (i.e
67+
#' S_PIE). Now, \code{calc_SPIE} has become its own function and the
68+
#' (\code{ENS}) argument is no longer supported . Please, use \code{calc_SPIE}
69+
#' instead.
70+
#'
6871
#'
6972
#' @inheritParams rarefaction
70-
#' @param replace if TRUE, sampling with replacement is used. Otherwise, sampling without replacement (default).
73+
#' @param replace if TRUE, sampling with replacement is used. Otherwise,
74+
#' sampling without replacement (default).
7175
#'
7276
#' @author Dan McGlinn, Thore Engel
7377
#'
@@ -79,11 +83,13 @@ calc_chao1 = function(x) {
7983
#' @examples
8084
#' data(inv_comm)
8185
#' calc_PIE(inv_comm)
82-
#' calc_PIE(c(23,21,12,5,1,2,3), replace=TRUE)
83-
calc_PIE = function(x, replace=FALSE, ...) {
86+
#' calc_PIE(inv_comm, replace = TRUE)
87+
#' calc_PIE(c(23,21,12,5,1,2,3))
88+
#' calc_PIE(c(23,21,12,5,1,2,3), replace = TRUE)
89+
calc_PIE = function(x, replace = FALSE, ...) {
8490

8591
args = as.list(match.call())
86-
if ( any(names(args) == "ENS"))
92+
if (any(names(args) == "ENS"))
8793
stop("The ENS argumet was removed from this function. Please, use calc_SPIE() for the ENS transformation of PIE. ")
8894

8995
if ('mob_in' %in% class(x)) {
@@ -93,7 +99,7 @@ calc_PIE = function(x, replace=FALSE, ...) {
9399
if (any(x < 0, na.rm = TRUE))
94100
stop("input data must be non-negative")
95101

96-
if (any(x%%1!=0, na.rm = TRUE))
102+
if (any(x %% 1 != 0, na.rm = TRUE))
97103
stop("input data must be integers")
98104

99105
if (length(dim(x)) > 1) {
@@ -113,47 +119,58 @@ calc_PIE = function(x, replace=FALSE, ...) {
113119
}
114120

115121
# calculate PIE without replacement (for total >= 2)
116-
if(replace){
117-
PIE=1 - H
122+
if (replace) {
123+
PIE = 1 - H
118124
} else {
119125
PIE = total / (total - 1) * (1 - H)
120126
}
121-
if (!replace) PIE [total==1 ] <- NA
122-
if(any(is.na(PIE))) warning("NA was returned because because the sample just contains one individual.")
127+
# if sample had zero individuals set PIE to 0
128+
PIE[total == 0] = 0
129+
# if sample only contains 1 individual set PIE to NA
130+
if (!replace)
131+
PIE[total == 1] = NA
132+
if (any(is.na(PIE)))
133+
warning("NA was returned because because the sample just contains one individual.")
123134

124135
return(PIE)
125136
}
126137

127138
#' Calculate S_PIE
128-
#'
129-
#' S_PIE is the effective number transformation of the probability of interspecific encounter (PIE).
130-
#'
131-
#' Per default the sample size corrected version is returned (\code{replace = F}), which is the asymptotic
132-
#' estimator for the Hill number of diversity order q=2 (Chao et al, 2014). If \code{replace = T} the uncorrected
133-
#' hill number is returned. This is the same as vegan::diversity(x, index="invsimpson").
134-
#'
139+
#'
140+
#' S_PIE is the effective number of species transformation of the probability of
141+
#' interspecific encounter (PIE) which is equal to the number of equally common
142+
#' species that result in that value of PIE.
143+
#'
144+
#' By default the sample size corrected version is returned (\code{replace =
145+
#' F}), which is the asymptotic estimator for the Hill number of diversity order
146+
#' q=2 (Chao et al, 2014). If \code{replace = T} the uncorrected hill number is
147+
#' returned. This is the same as vegan::diversity(x, index="invsimpson").
148+
#'
135149
#'
136150
#' @inheritParams calc_PIE
137151
#' @return
138152
#' @export
139153
#'
140154
#' @references
141-
#' Chao, A., Gotelli, N. J., Hsieh, T. C., Sander, E. L., Ma, K. H., Colwell, R. K., & Ellison, A. M. (2014).
142-
#' Rarefaction and extrapolation with Hill numbers: A framework for sampling and estimation in species diversity studies.
143-
#' Ecological Monographs 84(1), 45-67.
155+
#' Chao, A., Gotelli, N. J., Hsieh, T. C., Sander, E. L., Ma, K. H., Colwell, R.
156+
#' K., & Ellison, A. M. (2014). Rarefaction and extrapolation with Hill numbers:
157+
#' A framework for sampling and estimation in species diversity studies.
158+
#' Ecological Monographs 84(1), 45-67.
144159
#'
145160
#' @examples
146161
#' data(inv_comm)
147162
#' calc_SPIE(inv_comm)
163+
#' calc_SPIE(inv_comm, replace = TRUE)
148164
#' calc_SPIE(c(23,21,12,5,1,2,3), replace=TRUE)
149-
calc_SPIE = function(x, replace=F){
150-
PIE=calc_PIE(x, replace = replace)
151-
SPIE=1 / (1 - PIE)
152-
SPIE[PIE==1]=NA
153-
if(any(PIE==1, na.rm = T)) warning("NA was returned because PIE = 1. This happens in samples where all species are singletons.")
165+
calc_SPIE = function(x, replace = F) {
166+
PIE = calc_PIE(x, replace = replace)
167+
SPIE = 1 / (1 - PIE)
168+
SPIE[PIE == 0] = 0
169+
SPIE[PIE == 1] = NA
170+
if (any(PIE == 1, na.rm = T))
171+
warning("NA was returned because PIE = 1. This happens in samples where all species are singletons.")
154172

155173
return(SPIE)
156-
157174
}
158175

159176

man/calc_PIE.Rd

Lines changed: 15 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_SPIE.Rd

Lines changed: 14 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/calc_div.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/plot_rarefaction.Rd

Lines changed: 7 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)