Skip to content

Commit 8147630

Browse files
authored
Merge pull request #273 from MoBiodiv/dev
Update of mobr from 2.0.0 to 3.0.0
2 parents 93b8127 + f1c836f commit 8147630

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2905
-1501
lines changed

DESCRIPTION

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
Package: mobr
22
Title: Measurement of Biodiversity
3-
Version: 2.0.2
3+
Version: 3.0.0
44
Authors@R: c(person("Daniel", "McGlinn", email = "[email protected]",
55
role = c("aut", "cre")),
66
person("Xiao", "Xiao", email = "[email protected]",
77
role = "aut"),
88
person("Brian", "McGill", email = "[email protected]",
99
role = "aut"),
10-
person("Felix", "May", email = "felix.may@idiv.de",
10+
person("Felix", "May", email = "felix.may@posteo.de",
1111
role = "aut"),
1212
person("Thore", "Engel", email = "[email protected]",
1313
role = "aut"),
@@ -28,8 +28,9 @@ Description: Functions for calculating metrics for the measurement biodiversity
2828
and its changes across scales, treatments, and gradients. The methods
2929
implemented in this package are described in:
3030
Chase, J.M., et al. (2018) <doi:10.1111/ele.13151>,
31-
McGlinn, D.J., et al. (2019) <doi:10.1111/2041-210X.13102>, and
32-
McGlinn, D.J., et al. (2021) <doi:10.1002/ecy.3233>.
31+
McGlinn, D.J., et al. (2019) <doi:10.1111/2041-210X.13102>,
32+
McGlinn, D.J., et al. (2020) <doi:10.1101/851717>, and
33+
McGlinn, D.J., et al. (2023) <doi:10.1101/2023.09.19.558467>.
3334
Depends:
3435
R (>= 3.0.2)
3536
Imports:
@@ -44,15 +45,18 @@ Imports:
4445
tibble,
4546
vctrs,
4647
rlang,
47-
geosphere
48+
geosphere,
49+
scam,
50+
sf
4851
Suggests:
4952
knitr,
5053
rmarkdown,
51-
testthat
54+
testthat,
55+
methods
5256
License: MIT + file LICENSE
5357
LazyData: true
58+
RoxygenNote: 7.3.1
5459
URL: https://github.com/MoBiodiv/mobr
5560
BugReports: https://github.com/MoBiodiv/mobr/issues
56-
RoxygenNote: 7.1.1
5761
Encoding: UTF-8
5862
VignetteBuilder: knitr

NAMESPACE

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,26 @@ S3method(plot,mob_out)
44
S3method(plot,mob_stats)
55
S3method(print,mob_in)
66
S3method(subset,mob_in)
7+
export(Chat)
78
export(avg_nn_dist)
9+
export(calc_C_target)
810
export(calc_PIE)
9-
export(calc_biodiv)
11+
export(calc_SPIE)
12+
export(calc_S_C)
13+
export(calc_beta_div)
1014
export(calc_chao1)
15+
export(calc_comm_div)
16+
export(calc_div)
1117
export(compare_samp_rarefaction)
1218
export(get_delta_stats)
1319
export(get_mob_stats)
1420
export(get_null_comm)
21+
export(invChat)
1522
export(kNCN_average)
1623
export(make_mob_in)
1724
export(plot_N)
1825
export(plot_abu)
26+
export(plot_comm_div)
1927
export(plot_rarefaction)
2028
export(rarefaction)
2129
import(dplyr)
@@ -45,6 +53,7 @@ importFrom(stats,coef)
4553
importFrom(stats,dist)
4654
importFrom(stats,lm)
4755
importFrom(stats,loess)
56+
importFrom(stats,optimize)
4857
importFrom(stats,pf)
4958
importFrom(stats,predict)
5059
importFrom(stats,quantile)

NEWS.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
## version 3.0.0
2+
---
3+
4+
Major update
5+
### new features
6+
- `calc_comm_div` now replaces `get_mob_stats`. The ancillary plotting function
7+
`plot.mob_stats` is now replaced with `plot_comm_div`. We hope to eventually
8+
add back in bootstrapping confidence intervals for the statistics but that is
9+
not currently supported. If you would like to know more about this design
10+
decision see the discussion here: https://github.com/MoBiodiv/mobr/issues/255.
11+
- `plot_rarefaction` is a bit more versatile with many new arguments
12+
that provide options for smoothing or averaging rarefaction curves
13+
when making comparisons within or between groups respectively.
14+
- The R package `beta_C` by Thore Engel has now been absorbed into
15+
the `mobr` package. Use function `calc_beta_div` or `calc_comm_div`
16+
with `beta` in the `scales` argument to compute coverage based beta
17+
diversity. Note the index in this case is `S_C` (i.e., richness S for a
18+
given level of coverage C)
19+
- a vignette was added to demonstrate how to make computations of beta
20+
diversity using `mobr` called `beta_div_demo`
21+
122
## version 2.0.2
223
---
324
### change for CRAN
@@ -19,7 +40,6 @@ others are compared to. This is similar to the implementation in the function
1940

2041

2142
## version 2.0.0
22-
2343
---
2444

2545
### new features
@@ -51,7 +71,6 @@ the most computationally efficient solutions
5171

5272

5373
## version 1.0.0
54-
5574
---
5675

5776
- provided core functionality of mob tools described in McGlinn et al.

R/beta_C.R

Lines changed: 251 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,251 @@
1+
#' Calculate expected sample coverage C_hat
2+
#'
3+
#' Returns expected sample coverage of a sample `x` for a smaller than observed
4+
#' sample size `m` (Chao & Jost, 2012). This code was copied from INEXT's internal
5+
#' function \code{iNEXT::Chat.Ind} (Hsieh et al 2016).
6+
#'
7+
#' @param x integer vector (species abundances)
8+
#' @param m integer a number of individuals that is smaller than observed total
9+
#' community abundance.
10+
#'
11+
#' @return a numeric value that is the expected coverage.
12+
#'
13+
#' @references
14+
#' Chao, A., and L. Jost. 2012. Coverage-based rarefaction and extrapolation:
15+
#' standardizing samples by completeness rather than size. Ecology 93:2533–2547.
16+
#'
17+
#' Anne Chao, Nicholas J. Gotelli, T. C. Hsieh, Elizabeth L. Sander, K. H. Ma,
18+
#' Robert K. Colwell, and Aaron M. Ellison 2014. Rarefaction and extrapolation
19+
#' with Hill numbers: a framework for sampling and estimation in species
20+
#' diversity studies. Ecological Monographs 84:45-67.
21+
#'
22+
#' T. C. Hsieh, K. H. Ma and Anne Chao. 2024.
23+
#' iNEXT: iNterpolation and EXTrapolation for
24+
#' species diversity. R package version 3.0.1
25+
#' URL: http://chao.stat.nthu.edu.tw/wordpress/software-download/.
26+
#'
27+
#'
28+
#' @export
29+
#'
30+
#' @examples
31+
#' data(inv_comm)
32+
#' # What is the expected coverage at a sample size of 50 at the gamma scale?
33+
#' Chat(colSums(inv_comm), 50)
34+
Chat <- function (x, m)
35+
{
36+
x <- x[x > 0]
37+
n <- sum(x)
38+
f1 <- sum(x == 1)
39+
f2 <- sum(x == 2)
40+
f0.hat <- ifelse(f2 == 0, (n - 1) / n * f1 * (f1 - 1) / 2, (n -
41+
1) / n * f1 ^
42+
2 / 2 / f2)
43+
A <- ifelse(f1 > 0, n * f0.hat / (n * f0.hat + f1), 1)
44+
Sub <- function(m) {
45+
if (m < n) {
46+
xx <- x[(n - x) >= m]
47+
out <- 1 - sum(xx / n * exp(
48+
lgamma(n - xx + 1) - lgamma(n -
49+
xx - m + 1) - lgamma(n) + lgamma(n - m)
50+
))
51+
}
52+
if (m == n)
53+
out <- 1 - f1 / n * A
54+
if (m > n)
55+
out <- 1 - f1 / n * A ^ (m - n + 1)
56+
out
57+
}
58+
sapply(m, Sub)
59+
}
60+
61+
#' Number of individuals corresponding to a desired coverage (inverse C_hat)
62+
#'
63+
#' If you wanted to resample a vector to a certain expected sample coverage, how
64+
#' many individuals would you have to draw? This is C_hat solved for the number
65+
#' of individuals. This code is a modification of INEXT's internal function
66+
#' `invChat.Ind` (Hsieh et al 2016).
67+
#'
68+
#' @param x integer vector (species abundances)
69+
#' @param C coverage value between 0 and 1
70+
#'
71+
#' @return a numeric value which is the number of individuals for a given
72+
#' level of coverage \code{C}.
73+
#' @references
74+
#' Chao, A., and L. Jost. 2012. Coverage-based rarefaction and extrapolation:
75+
#' standardizing samples by completeness rather than size. Ecology 93:2533–2547.
76+
#'
77+
#' Anne Chao, Nicholas J. Gotelli, T. C. Hsieh, Elizabeth L. Sander, K. H. Ma,
78+
#' Robert K. Colwell, and Aaron M. Ellison 2014. Rarefaction and extrapolation
79+
#' with Hill numbers: a framework for sampling and estimation in species
80+
#' diversity studies. Ecological Monographs 84:45-67.
81+
#'
82+
#' T. C. Hsieh, K. H. Ma and Anne Chao. 2024.
83+
#' iNEXT: iNterpolation and EXTrapolation for
84+
#' species diversity. R package version 3.0.1
85+
#' URL: http://chao.stat.nthu.edu.tw/wordpress/software-download/.
86+
#' @seealso \code{\link{calc_S_C}}
87+
#' @export
88+
#' @importFrom stats optimize
89+
#' @examples
90+
#' data(inv_comm)
91+
#' # What sample size corresponds to an expected sample coverage of 55%?
92+
#' invChat(colSums(inv_comm), 0.55)
93+
#'
94+
invChat <- function (x, C)
95+
{
96+
m <- NULL
97+
n <- sum(x)
98+
refC <- Chat(x, n)
99+
f <- function(m, C)
100+
abs(Chat(x, m) - C)
101+
# for interpolation
102+
if (refC > C) {
103+
opt <- stats::optimize(f,
104+
C = C,
105+
lower = 0,
106+
upper = sum(x))
107+
mm <- opt$minimum
108+
}
109+
# for extrapolation
110+
if (refC <= C) {
111+
f1 <- sum(x == 1)
112+
f2 <- sum(x == 2)
113+
if (f1 > 0 & f2 > 0) {
114+
A <- (n - 1) * f1 / ((n - 1) * f1 + 2 * f2)
115+
}
116+
if (f1 > 1 & f2 == 0) {
117+
A <- (n - 1) * (f1 - 1) / ((n - 1) * (f1 - 1) + 2)
118+
}
119+
if (f1 == 1 & f2 == 0) {
120+
A <- 1
121+
}
122+
if (f1 == 0 & f2 == 0) {
123+
A <- 1
124+
}
125+
mm <- (log(n / f1) + log(1 - C)) / log(A) - 1
126+
mm <- n + mm
127+
128+
}
129+
if (mm > 2 * n)
130+
warning(
131+
"The maximum size of the extrapolation exceeds double reference sample size, the results for q = 0 may be subject to large prediction bias."
132+
)
133+
return(mm)
134+
}
135+
136+
137+
#' Calculate species richness for a given coverage level.
138+
#'
139+
#' This function uses coverage-based rarefaction to compute species richness.
140+
#' Specifically, the metric is computed as the
141+
#'
142+
#' @param x a site by species matrix or a species abundance distribution
143+
#' @param C_target target coverage between 0 and 1 (default is NULL). If not
144+
#' provided then target coverage is computed by \code{\link{calc_C_target}}
145+
#' @param extrapolate logical. Defaults to TRUE in which case richness is
146+
#' extrapolated to sample sizes larger than observed in the dataset.
147+
#' @param interrupt logical. Should the function throw an error when \code{C_target}
148+
#' exceeds the maximum recommendable coverage?
149+
#'
150+
#' @returns numeric value which is the species richness at a specific level of
151+
#' coverage.
152+
#' @references
153+
#' Chao, A., and L. Jost. 2012. Coverage-based rarefaction and extrapolation:
154+
#' standardizing samples by completeness rather than size. Ecology 93:2533–2547.
155+
#'
156+
#' Anne Chao, Nicholas J. Gotelli, T. C. Hsieh, Elizabeth L. Sander, K. H. Ma,
157+
#' Robert K. Colwell, and Aaron M. Ellison 2014. Rarefaction and extrapolation
158+
#' with Hill numbers: a framework for sampling and estimation in species
159+
#' diversity studies. Ecological Monographs 84:45-67.
160+
#'
161+
#' T. C. Hsieh, K. H. Ma and Anne Chao. 2024.
162+
#' iNEXT: iNterpolation and EXTrapolation for
163+
#' species diversity. R package version 3.0.1
164+
#' URL: http://chao.stat.nthu.edu.tw/wordpress/software-download/.
165+
#'
166+
#' @seealso \code{\link{invChat}}
167+
#' @export
168+
#'
169+
#' @examples
170+
#' data(tank_comm)
171+
#' # What is species richness for a coverage value of 60%?
172+
#' calc_S_C(tank_comm, C_target = 0.6)
173+
calc_S_C <- function(x,
174+
C_target = NULL,
175+
extrapolate = TRUE,
176+
interrupt = TRUE) {
177+
x <- as.matrix(x)
178+
if (any(dim(x) == 1))
179+
sad <- as.numeric(x)
180+
else
181+
sad <- colSums(x)
182+
if (is.null(C_target))
183+
C_target <- calc_C_target(x)
184+
N <- round(invChat(sad, C_target))
185+
C_max = calc_C_target(x, factor = ifelse(extrapolate, 2, 1))
186+
if (C_target > C_max & interrupt) {
187+
if (extrapolate) {
188+
stop(
189+
paste0(
190+
"Coverage exceeds the maximum possible value recommendable for extrapolation (i.e. C_target = ",
191+
round(C_max, 4),
192+
"). Reduce the value of C_target."
193+
)
194+
)
195+
} else{
196+
stop(
197+
paste0(
198+
"Coverage exceeds the maximum possible value for interpolation (i.e. C_target = ",
199+
round(C_max, 4),
200+
"). Use extrapolation or reduce the value of C_target."
201+
)
202+
)
203+
}
204+
}
205+
if (N > 1) {
206+
S_C = rarefaction(x = sad,
207+
method = "IBR",
208+
effort = N,
209+
extrapolate = extrapolate,
210+
quiet_mode = TRUE)
211+
} else {
212+
S_C = NA
213+
}
214+
attr(S_C, "C") = C_target
215+
attr(S_C, "N") = N
216+
return(S_C)
217+
}
218+
219+
#' Calculate the recommended target coverage value for the computation of beta_C
220+
#'
221+
#' Returns the estimated gamma-scale coverage that corresponds to the largest
222+
#' allowable sample size (i.e. the smallest observed sample size at the alpha
223+
#' scale multiplied by an extrapolation factor). The default (factor = 2) allows
224+
#' for extrapolation up to 2 times the observed sample size of the smallest
225+
#' alpha sample. For factor= 1, only interpolation is applied. Factors larger
226+
#' than 2 are not recommended.
227+
#'
228+
#' @param x a site by species abundance matrix
229+
#' @param factor numeric. A multiplier for how much larger than total community
230+
#' abundance to extrapolate to. Defaults to 2.
231+
#'
232+
#' @return numeric value
233+
#' @export
234+
#'
235+
#' @examples
236+
#' data(tank_comm)
237+
#'
238+
#' # What is the largest possible C that I can use to calculate beta_C
239+
#' calc_C_target(tank_comm)
240+
calc_C_target <- function(x, factor = 2) {
241+
x <- as.matrix(x)
242+
if (any(dim(x) == 1)) {
243+
n <- factor * sum(x)
244+
C_target <- Chat(x, n)
245+
}
246+
else {
247+
n <- min(factor * rowSums(x))
248+
C_target <- Chat(colSums(x), n)
249+
}
250+
return(C_target)
251+
}

R/k-NCN_curve_functions.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
#' @importFrom stats runif
1414
#'
1515
#' @keywords internal
16+
#' @noRd
1617
centroid_accumulate = function(x, focal_sample = 1, n = NULL, coords = NULL, latlong = FALSE) {
1718

1819
if ("mob_in" %in% class(x)) {

R/mobr-package.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
#' abundance, species commonness, and spatial aggregation of conspecifics.
88
#'
99
#' @name mobr
10-
#' @docType package
11-
NULL
10+
"_PACKAGE"
11+

0 commit comments

Comments
 (0)