| 1 |
# RESAMPLE |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Bootstrap ==================================================================== |
|
| 6 |
#' @export |
|
| 7 |
#' @rdname bootstrap |
|
| 8 |
#' @aliases bootstrap,DiversityIndex-method |
|
| 9 |
setMethod( |
|
| 10 |
f = "bootstrap", |
|
| 11 |
signature = c(object = "DiversityIndex"), |
|
| 12 |
definition = function(object, n = 1000, f = NULL, level = 0.95, |
|
| 13 |
interval = c("basic", "normal", "percentiles"),
|
|
| 14 |
seed = NULL, rare = FALSE) {
|
|
| 15 |
## Validation |
|
| 16 | ! |
interval <- match.arg(interval, several.ok = FALSE) |
| 17 | ||
| 18 |
## Seed |
|
| 19 | ! |
if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
|
| 20 | ! |
stats::runif(1) |
| 21 | ! |
if (is.null(seed)) {
|
| 22 | ! |
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
|
| 23 |
} else {
|
|
| 24 | ! |
R.seed <- get(".Random.seed", envir = .GlobalEnv)
|
| 25 | ! |
set.seed(seed) |
| 26 | ! |
RNGstate <- structure(seed, kind = as.list(RNGkind())) |
| 27 | ! |
on.exit(assign(".Random.seed", R.seed, envir = .GlobalEnv))
|
| 28 |
} |
|
| 29 | ||
| 30 | ! |
w <- object@data |
| 31 | ! |
m <- nrow(w) |
| 32 | ! |
method <- object@method |
| 33 | ! |
fun_index <- function(x) {
|
| 34 | ! |
do_index(x, method = method, evenness = is_evenness(object)) |
| 35 |
} |
|
| 36 | ! |
if (isTRUE(rare)) {
|
| 37 | ! |
fun_resample <- function(x, n) arkhe::resample_uniform(x, n, replace = TRUE) |
| 38 |
} else {
|
|
| 39 | ! |
fun_resample <- function(x, n) arkhe::resample_multinomial(x, n) |
| 40 |
} |
|
| 41 | ||
| 42 | ! |
results <- vector(mode = "list", length = m) |
| 43 | ! |
for (i in seq_len(m)) {
|
| 44 | ! |
hat <- fun_index(w[i, ]) |
| 45 | ! |
spl <- t(fun_resample(w[i, ], n = n)) |
| 46 | ! |
res <- apply(X = spl, MARGIN = 2, FUN = fun_index) |
| 47 | ! |
if (is.function(f)) {
|
| 48 | ! |
results[[i]] <- f(res) |
| 49 |
} else {
|
|
| 50 | ! |
results[[i]] <- summary_bootstrap(res, hat, level = level, interval = interval) |
| 51 |
} |
|
| 52 |
} |
|
| 53 | ! |
results <- do.call(rbind, results) |
| 54 | ! |
rownames(results) <- rownames(w) |
| 55 | ! |
results <- as.data.frame(results) |
| 56 | ||
| 57 | ! |
attr(results, "seed") <- RNGstate |
| 58 | ! |
results |
| 59 |
} |
|
| 60 |
) |
|
| 61 | ||
| 62 |
summary_bootstrap <- function(x, hat, level = 0.95, interval = "basic") {
|
|
| 63 | ! |
n <- length(x) |
| 64 | ! |
boot_mean <- mean(x) |
| 65 | ! |
boot_bias <- boot_mean - hat |
| 66 | ! |
boot_error <- stats::sd(x) |
| 67 | ||
| 68 | ! |
ci <- arkhe::confidence_bootstrap(x, level = level, t0 = hat, type = interval) |
| 69 | ! |
results <- c(hat, boot_mean, boot_bias, boot_error, ci) |
| 70 | ! |
names(results) <- c("original", "mean", "bias", "error", "lower", "upper")
|
| 71 | ! |
results |
| 72 |
} |
|
| 73 | ||
| 74 |
# Jackknife ==================================================================== |
|
| 75 |
#' @export |
|
| 76 |
#' @rdname jackknife |
|
| 77 |
#' @aliases jackknife,DiversityIndex-method |
|
| 78 |
setMethod( |
|
| 79 |
f = "jackknife", |
|
| 80 |
signature = c(object = "DiversityIndex"), |
|
| 81 |
definition = function(object, f = NULL) {
|
|
| 82 | ||
| 83 | 3x |
w <- object@data |
| 84 | 3x |
m <- nrow(w) |
| 85 | 3x |
method <- object@method |
| 86 | ||
| 87 | 3x |
results <- vector(mode = "list", length = m) |
| 88 | 3x |
for (i in seq_len(m)) {
|
| 89 | 15x |
results[[i]] <- arkhe::jackknife( |
| 90 | 15x |
object = w[i, ], |
| 91 | 15x |
do = do_index, |
| 92 | 15x |
method = method, |
| 93 | 15x |
evenness = methods::is(object, "EvennessIndex"), |
| 94 | 15x |
f = f |
| 95 |
) |
|
| 96 |
} |
|
| 97 | 3x |
results <- do.call(rbind, results) |
| 98 | 3x |
rownames(results) <- rownames(w) |
| 99 | 3x |
as.data.frame(results) |
| 100 |
} |
|
| 101 |
) |
|
| 102 | ||
| 103 |
# Simulate ===================================================================== |
|
| 104 |
#' @export |
|
| 105 |
#' @method simulate DiversityIndex |
|
| 106 |
simulate.DiversityIndex <- function(object, nsim = 1000, seed = NULL, step = 1, |
|
| 107 |
level = 0.80, interval = "percentiles", |
|
| 108 |
progress = getOption("tabula.progress"), ...) {
|
|
| 109 |
## Validation |
|
| 110 | ! |
interval <- match.arg(interval, several.ok = FALSE) |
| 111 | ||
| 112 |
## Seed |
|
| 113 | ! |
if (!exists(".Random.seed", envir = .GlobalEnv, inherits = FALSE))
|
| 114 | ! |
stats::runif(1) |
| 115 | ! |
if (is.null(seed)) {
|
| 116 | ! |
RNGstate <- get(".Random.seed", envir = .GlobalEnv)
|
| 117 |
} else {
|
|
| 118 | ! |
R.seed <- get(".Random.seed", envir = .GlobalEnv)
|
| 119 | ! |
set.seed(seed) |
| 120 | ! |
RNGstate <- structure(seed, kind = as.list(RNGkind())) |
| 121 | ! |
on.exit(assign(".Random.seed", R.seed, envir = .GlobalEnv))
|
| 122 |
} |
|
| 123 | ||
| 124 |
## Specify the probability for the classes |
|
| 125 | ! |
data <- object@data |
| 126 |
## Select method |
|
| 127 | ! |
method <- object@method |
| 128 | ||
| 129 |
## Sample size |
|
| 130 | ! |
size <- max(rowSums(data)) |
| 131 | ! |
sample_sizes <- seq(from = 1, to = size * 1.05, by = step) |
| 132 | ||
| 133 | ! |
m <- length(sample_sizes) |
| 134 | ! |
k <- seq_len(m) |
| 135 | ||
| 136 | ! |
results <- vector(mode = "list", length = m) |
| 137 | ! |
fun_index <- function(x) {
|
| 138 | ! |
do_index(x, method = method, evenness = is_evenness(object)) |
| 139 |
} |
|
| 140 | ||
| 141 | ! |
progress_bar <- interactive() && progress |
| 142 | ! |
if (progress_bar) pbar <- utils::txtProgressBar(max = m, style = 3) |
| 143 | ||
| 144 | ! |
for (i in k) {
|
| 145 | ! |
spl <- arkhe::resample_multinomial(colSums(data), n = nsim, size = sample_sizes[[i]]) |
| 146 | ! |
res <- apply(X = t(spl), MARGIN = 2, FUN = fun_index) |
| 147 | ! |
int <- conf(res, level = level, type = interval) |
| 148 | ! |
results[[i]] <- c(mean = mean(res), int) |
| 149 | ! |
if (progress_bar) utils::setTxtProgressBar(pbar, i) |
| 150 |
} |
|
| 151 | ||
| 152 | ! |
if (progress_bar) close(pbar) |
| 153 | ||
| 154 | ! |
results <- do.call(rbind, results) |
| 155 | ! |
results <- cbind(size = sample_sizes, results) |
| 156 | ||
| 157 | ! |
methods::initialize(object, simulation = results, seed = RNGstate) |
| 158 |
} |
|
| 159 | ||
| 160 |
#' @export |
|
| 161 |
#' @rdname simulate |
|
| 162 |
#' @aliases simulate,DiversityIndex-method |
|
| 163 |
setMethod("simulate", c(object = "DiversityIndex"), simulate.DiversityIndex)
|
|
| 164 | ||
| 165 |
conf <- function(x, level = 0.80, type = c("percentiles")) {
|
|
| 166 | ! |
if (type == "percentiles") {
|
| 167 |
## Confidence interval as described in Kintigh 1989 |
|
| 168 | ! |
k <- (1 - level) / 2 |
| 169 | ! |
conf <- stats::quantile(x, probs = c(k, 1 - k), names = FALSE) |
| 170 |
} |
|
| 171 | ||
| 172 | ! |
names(conf) <- c("lower", "upper")
|
| 173 | ! |
conf |
| 174 |
} |
| 1 |
# MATRIX PLOT |
|
| 2 | ||
| 3 |
# Plot ========================================================================= |
|
| 4 |
#' Matrix Plot |
|
| 5 |
#' |
|
| 6 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 7 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 8 |
#' individuals for each category, i.e. a contingency table). |
|
| 9 |
#' @param panel A [`function`] in the form `function(x, y, z, color, ...)` |
|
| 10 |
#' which gives the action to be carried out in each panel of the display. |
|
| 11 |
#' @param diag A [`logical`] scalar indicating whether the diagonal of the |
|
| 12 |
#' matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 13 |
#' @param upper A [`logical`] scalar indicating whether the upper triangle of |
|
| 14 |
#' the matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 15 |
#' @param lower A [`logical`] scalar indicating whether the lower triangle of |
|
| 16 |
#' the matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 17 |
#' @param freq A [`logical`] scalar indicating whether conditional proportions |
|
| 18 |
#' given `margins` should be used (i.e. entries of `object`, divided by the |
|
| 19 |
#' appropriate marginal sums). |
|
| 20 |
#' @param margin An [`integer`] vector giving the margins to split by: |
|
| 21 |
#' `1` indicates individuals/rows (the default), `2` indicates |
|
| 22 |
#' variables/columns. Only used if `freq` is `TRUE`. |
|
| 23 |
#' @param scale A [`logical`] scalar indicating whether data should be rescaled |
|
| 24 |
#' to \eqn{[-1,1]}. Only used if `freq` if `FALSE`.
|
|
| 25 |
#' @param drop_zero A [`logical`] scalar: should zeros be discarded? |
|
| 26 |
#' @param color A vector of colors or a [`function`] that when called with a |
|
| 27 |
#' single argument (an integer specifying the number of colors) returns a |
|
| 28 |
#' vector of colors. |
|
| 29 |
#' @param midpoint A [`numeric`] value specifying the data midpoint. |
|
| 30 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? It will |
|
| 31 |
#' omit labels where they would abut or overlap previously drawn labels. |
|
| 32 |
#' @param legend A [`logical`] scalar: should a legend be displayed? |
|
| 33 |
#' @param asp A length-one [`numeric`] vector, giving the aspect ratio |
|
| 34 |
#' \eqn{y/x}.
|
|
| 35 |
#' @param ... Further arguments to be passed to `panel`. |
|
| 36 |
#' @keywords internal |
|
| 37 |
plot_matrix <- function(object, panel, diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 38 |
freq = FALSE, margin = 1, scale = TRUE, drop_zero = TRUE, |
|
| 39 |
color = graphics::par("fg"), midpoint = NULL,
|
|
| 40 |
axes = TRUE, legend = TRUE, asp = 1, ...) {
|
|
| 41 |
## Validation |
|
| 42 | 21x |
if (is_incidence(object)) {
|
| 43 | 1x |
object[] <- as.numeric(object) |
| 44 | 1x |
legend <- FALSE |
| 45 |
} |
|
| 46 | ||
| 47 |
## Prepare data |
|
| 48 | 21x |
n <- nrow(object) |
| 49 | 21x |
m <- ncol(object) |
| 50 | 21x |
seq_row <- rev(seq_len(n)) |
| 51 | 21x |
seq_col <- seq_len(m) |
| 52 | 21x |
lab_row <- rownames(object) %||% seq_row |
| 53 | 21x |
lab_col <- colnames(object) %||% seq_col |
| 54 | ||
| 55 | 21x |
data <- prepare(object, diag = diag, upper = upper, lower = lower, |
| 56 | 21x |
freq = freq, margin = margin, scale = scale, |
| 57 | 21x |
drop_zero = drop_zero, |
| 58 | 21x |
palette = color, midpoint = midpoint) |
| 59 | ||
| 60 |
## Graphical parameters |
|
| 61 | 21x |
cex.axis <- graphics::par("cex.axis")
|
| 62 | 21x |
col.axis <- graphics::par("col.axis")
|
| 63 | 21x |
font.axis <- graphics::par("font.axis")
|
| 64 | ||
| 65 |
## Save and restore |
|
| 66 | 21x |
d <- inch2line("M", cex = cex.axis)
|
| 67 | 21x |
old_par <- graphics::par("mar", "plt")
|
| 68 | 21x |
on.exit(graphics::par(old_par)) |
| 69 | ||
| 70 | 21x |
mar_left <- inch2line(lab_row, cex = cex.axis) |
| 71 | 21x |
mar_top <- inch2line(lab_col, cex = cex.axis) |
| 72 | 21x |
mar_right <- if (legend) inch2line("999%", cex = cex.axis) else d
|
| 73 | 21x |
graphics::par(mar = c(d, mar_left, mar_top, mar_right)) |
| 74 | ||
| 75 |
## Open new window |
|
| 76 | 21x |
grDevices::dev.hold() |
| 77 | 21x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 78 | 21x |
graphics::plot.new() |
| 79 | ||
| 80 |
## Squish plotting area |
|
| 81 | 21x |
pin <- graphics::par("pin")
|
| 82 | 21x |
plt <- graphics::par("plt")
|
| 83 | ||
| 84 |
## Add horizontal space for the legend (5%) |
|
| 85 | 21x |
legend_width <- if (legend) max(1, m / 20) else 0 |
| 86 | ||
| 87 | 6x |
if (isTRUE(asp)) asp <- 1 |
| 88 | 21x |
if (!isFALSE(asp) && !is.na(asp)) {
|
| 89 | 15x |
aspect_ratio <- n / (m + legend_width) |
| 90 | 15x |
pin_y <- pin[1] * aspect_ratio * asp |
| 91 | ||
| 92 | 15x |
if (pin_y < pin[2]) {
|
| 93 |
## Squish vertically |
|
| 94 | 11x |
graphics::par(pin = c(pin[1], pin_y)) |
| 95 | 11x |
graphics::par(plt = c(plt[1:2], graphics::par('plt')[3:4]))
|
| 96 |
} else {
|
|
| 97 |
## Squish horizontally |
|
| 98 | 4x |
pin_x <- pin[2] / aspect_ratio / asp |
| 99 | 4x |
graphics::par(pin = c(pin_x, pin[2])) |
| 100 | 4x |
graphics::par(plt = c(graphics::par('plt')[1:2], plt[3:4]))
|
| 101 |
} |
|
| 102 |
} |
|
| 103 | ||
| 104 |
## Set plotting coordinates |
|
| 105 | 21x |
xlim <- c(0, m + legend_width) + 0.5 |
| 106 | 21x |
ylim <- c(0, n) + 0.5 |
| 107 | 21x |
graphics::plot.window(xlim = xlim, ylim = ylim, xaxs = "i", yaxs = "i", asp = asp) |
| 108 | ||
| 109 |
## Plot |
|
| 110 | 21x |
panel(x = data$x, y = data$y, z = data$scaled, color = data$color, ...) |
| 111 | ||
| 112 |
## Construct axis |
|
| 113 | 21x |
if (axes) {
|
| 114 | 21x |
graphics::axis(side = 2, at = seq_row, labels = lab_row, las = 2, |
| 115 | 21x |
lty = 0, cex.axis = cex.axis, col.axis = col.axis, |
| 116 | 21x |
font.axis = font.axis) |
| 117 | 21x |
graphics::axis(side = 3, at = seq_col, labels = lab_col, las = 2, |
| 118 | 21x |
lty = 0, cex.axis = cex.axis, col.axis = col.axis, |
| 119 | 21x |
font.axis = font.axis) |
| 120 |
} |
|
| 121 | ||
| 122 |
## Legend |
|
| 123 | 21x |
if (legend) {
|
| 124 | 13x |
lgd <- attr(data, "legend") |
| 125 | 13x |
legend_gradient(x = m, y = n, labels = lgd$labels, |
| 126 | 13x |
at = lgd$at, width = legend_width, col = lgd$colors) |
| 127 |
} |
|
| 128 |
} |
|
| 129 | ||
| 130 |
legend_gradient <- function(x, y, labels, at, width, col) {
|
|
| 131 | 13x |
legend_image <- grDevices::as.raster(col) |
| 132 | 13x |
legend_y <- (at - min(at)) * y / diff(range(at)) + 0.5 |
| 133 | ||
| 134 | 13x |
graphics::rasterImage(legend_image, xleft = x + 1, ybottom = max(legend_y), |
| 135 | 13x |
xright = x + 1 + width, ytop = min(legend_y)) |
| 136 | 13x |
graphics::segments(x0 = x + 1, y0 = legend_y, |
| 137 | 13x |
x1 = x + 1 + width, y1 = legend_y, |
| 138 | 13x |
col = "white") |
| 139 | 13x |
graphics::polygon(x = c(x, x + width, x + width, x) + 1, |
| 140 | 13x |
y = c(0.5, 0.5, max(legend_y), max(legend_y)), |
| 141 | 13x |
col = NA, border = "black") |
| 142 | 13x |
graphics::axis(side = 4, at = legend_y, labels = labels, las = 2) |
| 143 |
} |
|
| 144 | ||
| 145 |
# Panels ======================================================================= |
|
| 146 |
panel_bertin <- function(x, y, z, color, ..., space = 0.05) {
|
|
| 147 | 5x |
y_bottom <- y - 0.5 |
| 148 | 5x |
y_top <- y - 0.5 + z - space * 2 |
| 149 | 5x |
y_top[y_top < y_bottom] <- y_bottom[y_top < y_bottom] |
| 150 | 5x |
graphics::rect( |
| 151 | 5x |
xleft = x - 0.5 + space, |
| 152 | 5x |
ybottom = y_bottom, |
| 153 | 5x |
xright = x + 0.5 - space, |
| 154 | 5x |
ytop = y_top, |
| 155 | 5x |
col = color, |
| 156 | 5x |
border = "black" |
| 157 |
) |
|
| 158 |
} |
|
| 159 |
panel_matrigraph <- function(x, y, z, ..., reverse = FALSE) {
|
|
| 160 | 2x |
pvi <- data.frame(x = x, y = y, z = z) |
| 161 | 2x |
pvi$z <- pvi$z / 100 |
| 162 | 2x |
pvi_plus <- pvi[pvi$z > 1, ] |
| 163 | 2x |
pvi_plus$z <- pvi_plus$z - 1 |
| 164 | 2x |
pvi_plus$z[pvi_plus$z > 1] <- 1 |
| 165 | ||
| 166 | 2x |
if (reverse) {
|
| 167 | 1x |
pvi_minus <- pvi[pvi$z < 1, ] |
| 168 | 1x |
pvi_minus$z <- 1 - pvi_minus$z |
| 169 |
} else {
|
|
| 170 | 1x |
pvi_minus <- pvi |
| 171 | 1x |
pvi_minus$z[pvi_minus$z > 1] <- 1 |
| 172 |
} |
|
| 173 | ||
| 174 | 2x |
pvi_minus$z <- pvi_minus$z * 0.5 |
| 175 | 2x |
pvi_plus$z <- pvi_plus$z * 0.5 |
| 176 | ||
| 177 | 2x |
col_bkg <- if (reverse) "darkgrey" else "white" |
| 178 | 2x |
col_minus <- if (reverse) "white" else "darkgrey" |
| 179 | ||
| 180 | 2x |
graphics::rect(xleft = pvi$x - 0.5, ybottom = pvi$y - 0.5, |
| 181 | 2x |
xright = pvi$x + 0.5, ytop = pvi$y + 0.5, |
| 182 | 2x |
col = col_bkg, border = NA) |
| 183 | 2x |
graphics::rect(xleft = pvi_minus$x - pvi_minus$z, |
| 184 | 2x |
ybottom = pvi_minus$y - pvi_minus$z, |
| 185 | 2x |
xright = pvi_minus$x + pvi_minus$z, |
| 186 | 2x |
ytop = pvi_minus$y + pvi_minus$z, |
| 187 | 2x |
col = col_minus, border = NA) |
| 188 | 2x |
graphics::rect(xleft = pvi_plus$x - pvi_plus$z, |
| 189 | 2x |
ybottom = pvi_plus$y - pvi_plus$z, |
| 190 | 2x |
xright = pvi_plus$x + pvi_plus$z, |
| 191 | 2x |
ytop = pvi_plus$y + pvi_plus$z, |
| 192 | 2x |
col = "black", border = NA) |
| 193 |
} |
|
| 194 |
panel_tiles <- function(x, y, color, ...) {
|
|
| 195 | 7x |
width <- 0.5 |
| 196 | 7x |
graphics::rect( |
| 197 | 7x |
xleft = x - width, |
| 198 | 7x |
ybottom = y - width, |
| 199 | 7x |
xright = x + width, |
| 200 | 7x |
ytop = y + width, |
| 201 | 7x |
col = color, |
| 202 | 7x |
border = NA |
| 203 |
) |
|
| 204 |
} |
|
| 205 |
panel_spot <- function(x, y, z, color, type, ...) {
|
|
| 206 | 7x |
radius <- abs(z * 0.45) |
| 207 | 7x |
for (i in seq_along(x)) {
|
| 208 | 185x |
circle(x = x[i], y = y[i], radius = radius[i], |
| 209 | 185x |
col = color[i], border = color[i]) |
| 210 | 185x |
if (type == "ring") {
|
| 211 | 123x |
circle(x = x[i], y = y[i], radius = 0.45, |
| 212 | 123x |
col = NA, border = "black") |
| 213 |
} |
|
| 214 |
} |
|
| 215 |
} |
|
| 216 | ||
| 217 |
# Prepare ====================================================================== |
|
| 218 |
#' Prepare Data for Matrix Plot |
|
| 219 |
#' |
|
| 220 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 221 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 222 |
#' individuals for each category, i.e. a contingency table). |
|
| 223 |
#' @param diag A [`logical`] scalar indicating whether the diagonal of the |
|
| 224 |
#' matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 225 |
#' @param upper A [`logical`] scalar indicating whether the upper triangle of |
|
| 226 |
#' the matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 227 |
#' @param lower A [`logical`] scalar indicating whether the lower triangle of |
|
| 228 |
#' the matrix should be plotted. Only used if `object` is a symmetric matrix. |
|
| 229 |
#' @param freq A [`logical`] scalar indicating whether conditional proportions |
|
| 230 |
#' given `margins` should be used (i.e. entries of `object`, divided by the |
|
| 231 |
#' appropriate marginal sums). |
|
| 232 |
#' @param margin An [`integer`] vector giving the margins to split by: |
|
| 233 |
#' `1` indicates individuals/rows (the default), `2` indicates |
|
| 234 |
#' variables/columns. Only used if `freq` is `TRUE`. |
|
| 235 |
#' @param scale A [`logical`] scalar indicating whether data should be rescaled |
|
| 236 |
#' to \eqn{[-1,1]}. Only used if `freq` if `FALSE`.
|
|
| 237 |
#' @param drop_zero A [`logical`] scalar: should zeros be discarded? |
|
| 238 |
#' @param palette A vector of colors. |
|
| 239 |
#' @param midpoint A [`numeric`] value specifying the data midpoint. |
|
| 240 |
#' @param ... Currently not used. |
|
| 241 |
#' @return |
|
| 242 |
#' A long [`data.frame`]. |
|
| 243 |
#' \describe{
|
|
| 244 |
#' \item{`row`}{}
|
|
| 245 |
#' \item{`column`}{}
|
|
| 246 |
#' \item{`x`,`y`}{Tile center coordinates.}
|
|
| 247 |
#' \item{`z`}{Raw values.}
|
|
| 248 |
#' \item{`value`}{}
|
|
| 249 |
#' \item{`scaled`}{}
|
|
| 250 |
#' \item{`color`}{}
|
|
| 251 |
#' } |
|
| 252 |
#' @keywords internal |
|
| 253 |
#' @noRd |
|
| 254 |
prepare <- function(object, diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 255 |
freq = FALSE, margin = 1, scale = !freq, drop_zero = FALSE, |
|
| 256 |
palette = grDevices::hcl.colors(12, "YlOrBr", rev = TRUE), |
|
| 257 |
midpoint = NULL, ...) {
|
|
| 258 |
## Validation |
|
| 259 | 21x |
if (!arkhe::is_symmetric(object)) {
|
| 260 | 16x |
diag <- TRUE |
| 261 | 16x |
upper <- TRUE |
| 262 | 16x |
lower <- TRUE |
| 263 |
} |
|
| 264 | ||
| 265 |
## Coerce to matrix |
|
| 266 | 21x |
object <- as.matrix(object) |
| 267 | ||
| 268 |
## Relative frequencies |
|
| 269 | 21x |
val <- if (freq) prop.table(object, margin = margin) else object |
| 270 | ||
| 271 |
## Rescale to [-1;1] |
|
| 272 | 21x |
sca <- if (scale) val / max(abs(val), na.rm = TRUE) else val |
| 273 | ||
| 274 | 21x |
val <- as.vector(val) |
| 275 | 21x |
sca <- as.vector(sca) |
| 276 | 21x |
min_val <- min(val, na.rm = TRUE) |
| 277 | 21x |
max_val <- max(val, na.rm = TRUE) |
| 278 | ||
| 279 |
## Build a long table |
|
| 280 | 21x |
row <- row(object, as.factor = TRUE) |
| 281 | 21x |
col <- col(object, as.factor = TRUE) |
| 282 | 21x |
data <- data.frame( |
| 283 | 21x |
row = as.vector(row), |
| 284 | 21x |
column = as.vector(col), |
| 285 | 21x |
x = as.numeric(col), |
| 286 | 21x |
y = as.vector(nrow(object) - as.numeric(row) + 1), # Reverse y for plotting |
| 287 | 21x |
z = as.vector(object), |
| 288 | 21x |
value = val, |
| 289 | 21x |
scaled = sca |
| 290 |
) |
|
| 291 | ||
| 292 |
## Map colors |
|
| 293 | 21x |
breaks <- pretty(val, n = 5) |
| 294 | 21x |
domain <- range(c(breaks, min_val, max_val)) |
| 295 | 21x |
midpoint <- if (is.null(midpoint) & min_val < 0 & max_val > 0) 0 else midpoint |
| 296 | 21x |
pal <- khroma::palette_color_continuous(colors = palette, domain = domain, midpoint = midpoint) |
| 297 | 21x |
data$color <- if (length(palette) == length(val)) palette else pal(val) |
| 298 | ||
| 299 |
## Clean data |
|
| 300 | 3x |
if (!upper) data <- data[!upper.tri(object), ] |
| 301 | 2x |
if (!lower) data <- data[!lower.tri(object), ] |
| 302 | 5x |
if (!diag) data <- data[data$row != data$column, ] |
| 303 | 14x |
if (drop_zero) data <- data[data$value != 0, ] |
| 304 | ||
| 305 |
## Legend |
|
| 306 | 21x |
attr(data, "legend") <- list( |
| 307 | 21x |
labels = if (freq) label_percent(breaks) else breaks, |
| 308 | 21x |
at = breaks / max(abs(val), na.rm = TRUE), |
| 309 | 21x |
colors = pal(breaks) |
| 310 |
) |
|
| 311 | 21x |
data |
| 312 |
} |
| 1 |
# ALPHA DIVERSITY |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Observed ===================================================================== |
|
| 6 |
#' @export |
|
| 7 |
#' @rdname observed |
|
| 8 |
#' @aliases observed,numeric-method |
|
| 9 |
setMethod( |
|
| 10 |
f = "observed", |
|
| 11 |
signature = c(x = "numeric"), |
|
| 12 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 13 | 5x |
sum(x > 0, na.rm = na.rm) |
| 14 |
} |
|
| 15 |
) |
|
| 16 | ||
| 17 |
nobserved <- function(x, n, na.rm = FALSE) {
|
|
| 18 | ! |
sum(x == n, na.rm = na.rm) |
| 19 |
} |
|
| 20 | ||
| 21 |
#' @export |
|
| 22 |
#' @rdname observed |
|
| 23 |
#' @aliases singleton,numeric-method |
|
| 24 |
setMethod( |
|
| 25 |
f = "singleton", |
|
| 26 |
signature = c(x = "numeric"), |
|
| 27 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 28 | ! |
nobserved(x, n = 1, na.rm = na.rm) |
| 29 |
} |
|
| 30 |
) |
|
| 31 | ||
| 32 |
#' @export |
|
| 33 |
#' @rdname observed |
|
| 34 |
#' @aliases doubleton,numeric-method |
|
| 35 |
setMethod( |
|
| 36 |
f = "doubleton", |
|
| 37 |
signature = c(x = "numeric"), |
|
| 38 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 39 | ! |
nobserved(x, n = 2, na.rm = na.rm) |
| 40 |
} |
|
| 41 |
) |
|
| 42 | ||
| 43 |
# ACE ========================================================================== |
|
| 44 |
#' @export |
|
| 45 |
#' @rdname index_ace |
|
| 46 |
#' @aliases index_ace,numeric-method |
|
| 47 |
setMethod( |
|
| 48 |
f = "index_ace", |
|
| 49 |
signature = c(x = "numeric"), |
|
| 50 |
definition = function(x, k = 10, na.rm = FALSE, ...) {
|
|
| 51 |
## Validation |
|
| 52 | 8x |
x <- x[x > 0] # Remove unobserved species |
| 53 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 54 | ! |
if (anyNA(x)) return(NA) |
| 55 | ||
| 56 | 8x |
S_rare <- sum(x <= k) # Number of rare species |
| 57 | 8x |
S_abund <- sum(x > k) # Number of abundant species |
| 58 | 8x |
N_rare <- sum(x[x <= k]) # Number of individuals in the rare species |
| 59 | ||
| 60 | 8x |
F1 <- sum(x == 1) # Number of singleton species |
| 61 | 8x |
if (F1 == N_rare) {
|
| 62 | ! |
if (getOption("tabula.verbose")) {
|
| 63 | ! |
message( |
| 64 | ! |
tr_("ACE is undefined when all rare species are singletons."), "\n",
|
| 65 | ! |
tr_("Consider using the bias-corrected Chao1 estimator instead.")
|
| 66 |
) |
|
| 67 |
} |
|
| 68 | ! |
return(NA_real_) |
| 69 |
} |
|
| 70 | ||
| 71 |
## Sample coverage estimate for rare species |
|
| 72 |
## ie. proportion of all individuals in rare species that are not singletons |
|
| 73 | 8x |
C_rare <- 1 - (F1 / N_rare) |
| 74 | ||
| 75 |
# Coefficient of variation |
|
| 76 | 8x |
a <- sum(vapply( |
| 77 | 8x |
X = seq_len(k), |
| 78 | 80x |
FUN = function(i, x) { i * (i - 1) * sum(x == i) },
|
| 79 | 8x |
FUN.VALUE = double(1), x = x) |
| 80 |
) |
|
| 81 | 8x |
g2 <- max((S_rare / C_rare) * (a / (N_rare * (N_rare - 1))) - 1, 0) |
| 82 | ||
| 83 | 8x |
D <- S_abund + (S_rare / C_rare) + (F1 / C_rare) * g2 |
| 84 | 8x |
D |
| 85 |
} |
|
| 86 |
) |
|
| 87 | ||
| 88 |
# Berger-Parker ================================================================ |
|
| 89 |
#' @export |
|
| 90 |
#' @rdname index_berger |
|
| 91 |
#' @aliases index_berger,numeric-method |
|
| 92 |
setMethod( |
|
| 93 |
f = "index_berger", |
|
| 94 |
signature = c(x = "numeric"), |
|
| 95 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 96 |
## Validation |
|
| 97 | 12x |
x <- x[x > 0] # Remove unobserved species |
| 98 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 99 | ! |
if (anyNA(x)) return(NA) |
| 100 | ||
| 101 | 12x |
Nmax <- max(x) |
| 102 | 12x |
N <- sum(x) |
| 103 | 12x |
d <- Nmax / N |
| 104 | 12x |
d |
| 105 |
} |
|
| 106 |
) |
|
| 107 | ||
| 108 |
# Boone ======================================================================== |
|
| 109 |
#' @export |
|
| 110 |
#' @rdname index_boone |
|
| 111 |
#' @aliases index_boone,matrix-method |
|
| 112 |
setMethod( |
|
| 113 |
f = "index_boone", |
|
| 114 |
signature = c(x = "matrix"), |
|
| 115 |
definition = function(x, j = NULL, na.rm = FALSE, ...) {
|
|
| 116 |
## Validation |
|
| 117 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 118 | ! |
if (anyNA(x)) return(NA) |
| 119 | ||
| 120 | ! |
Y <- colSums(x) # Site-wide totals of each artifact class |
| 121 | ! |
if (is.null(j)) j <- which.max(Y) |
| 122 | ||
| 123 | ! |
W <- Y[j] / Y # Weighting factor |
| 124 | ! |
P <- W * Y / sum(W * Y) |
| 125 | ||
| 126 | ! |
W <- matrix(W, nrow = nrow(x), ncol = ncol(x), byrow = TRUE) |
| 127 | ! |
P <- matrix(P, nrow = nrow(x), ncol = ncol(x), byrow = TRUE) |
| 128 | ||
| 129 | ! |
Wx <- W * x # Weighted counts |
| 130 | ! |
px <- Wx / rowSums(Wx) # Weighted percentages |
| 131 | ||
| 132 | ! |
rowSums((px - P)^2) |
| 133 |
} |
|
| 134 |
) |
|
| 135 | ||
| 136 |
# Brillouin ==================================================================== |
|
| 137 |
#' @export |
|
| 138 |
#' @rdname index_brillouin |
|
| 139 |
#' @aliases index_brillouin,numeric-method |
|
| 140 |
setMethod( |
|
| 141 |
f = "index_brillouin", |
|
| 142 |
signature = c(x = "numeric"), |
|
| 143 |
definition = function(x, evenness = FALSE, na.rm = FALSE, ...) {
|
|
| 144 |
## Validation |
|
| 145 | 18x |
x <- x[x > 0] # Remove unobserved species |
| 146 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 147 | ! |
if (anyNA(x)) return(NA) |
| 148 | ||
| 149 | 18x |
N <- sum(x) |
| 150 | 18x |
bri <- (lfactorial(N) - sum(lfactorial(x))) / N |
| 151 | ||
| 152 | 18x |
if (evenness) {
|
| 153 | 6x |
N <- sum(x) |
| 154 | 6x |
S <- length(x) # richness = number of different species |
| 155 | 6x |
a <- trunc(N / S) |
| 156 | 6x |
r <- N - S * a |
| 157 | 6x |
c <- (S - r) * lfactorial(a) + r * lfactorial(a + 1) |
| 158 | 6x |
HBmax <- (1 / N) * (lfactorial(N) - c) |
| 159 | 6x |
bri <- bri / HBmax |
| 160 |
} |
|
| 161 | ||
| 162 | 18x |
bri |
| 163 |
} |
|
| 164 |
) |
|
| 165 | ||
| 166 |
# Chao1 ======================================================================== |
|
| 167 |
#' @export |
|
| 168 |
#' @rdname index_chao1 |
|
| 169 |
#' @aliases index_chao1,numeric-method |
|
| 170 |
setMethod( |
|
| 171 |
f = "index_chao1", |
|
| 172 |
signature = c(x = "numeric"), |
|
| 173 |
definition = function(x, unbiased = FALSE, improved = FALSE, na.rm = FALSE, ...) {
|
|
| 174 |
## Validation |
|
| 175 | 13x |
x <- x[x > 0] # Remove unobserved species |
| 176 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 177 | ! |
if (anyNA(x)) return(NA) |
| 178 | ||
| 179 | 13x |
S <- length(x) # Number of observed species |
| 180 | 13x |
N <- sum(x) # Number of individuals |
| 181 | 13x |
f1 <- sum(x == 1) # Number of singleton species |
| 182 | 13x |
f2 <- sum(x == 2) # Number of doubleton species |
| 183 | ||
| 184 | 13x |
if (unbiased) {
|
| 185 | 4x |
D <- S + (((N - 1) / N) * f1 * (f1 - 1)) / (2 * (f2 + 1)) |
| 186 |
} else {
|
|
| 187 | 9x |
if (f2 == 0) {
|
| 188 | ! |
D <- S + ((N - 1) / N) * f1 * ((f1 - 1) / 2) |
| 189 |
} else {
|
|
| 190 | 9x |
D <- S + ((N - 1) / N) * (f1^2 / (2 * f2)) |
| 191 |
} |
|
| 192 |
} |
|
| 193 | 13x |
if (improved) {
|
| 194 | 3x |
f3 <- sum(x == 3) # Number of triple species |
| 195 | 3x |
f4 <- sum(x == 4) # Number of quadruple species |
| 196 | 3x |
if (f4 == 0) {
|
| 197 | 1x |
if (getOption("tabula.verbose")) {
|
| 198 | ! |
message( |
| 199 | ! |
tr_("Improved Chao1 estimator is undefined when there is no quadruple species.")
|
| 200 |
) |
|
| 201 |
} |
|
| 202 | 1x |
return(NA_real_) |
| 203 |
} |
|
| 204 | ||
| 205 | 2x |
k <- f1 - ((N - 3) / (N - 1)) * ((f2 * f3) / (2 * f4)) |
| 206 | 2x |
D <- D + ((N - 3) / N) * (f3 / (4 * f4)) * max(k, 0) |
| 207 |
} |
|
| 208 | ||
| 209 | 12x |
D |
| 210 |
} |
|
| 211 |
) |
|
| 212 | ||
| 213 |
# Chao2 ======================================================================== |
|
| 214 |
#' @export |
|
| 215 |
#' @rdname index_chao2 |
|
| 216 |
#' @aliases index_chao2,matrix-method |
|
| 217 |
setMethod( |
|
| 218 |
f = "index_chao2", |
|
| 219 |
signature = c(x = "matrix"), |
|
| 220 |
definition = function(x, unbiased = FALSE, improved = FALSE, ...) {
|
|
| 221 | 6x |
x <- x > 0 # Convert to incidence |
| 222 | ||
| 223 | 6x |
q <- colSums(x) # Number of species in the assemblage |
| 224 | 6x |
q <- q[q > 0] # Remove unobserved species |
| 225 | 6x |
S <- length(q) # Number of observed species |
| 226 | 6x |
t <- nrow(x) # Total number of sampling units |
| 227 | 6x |
q1 <- sum(q == 1) # Number of unique species in the assemblage |
| 228 | 6x |
q2 <- sum(q == 2) # Number of duplicate species in the assemblage |
| 229 | ||
| 230 | 6x |
if (unbiased) {
|
| 231 | 2x |
D <- S + ((t - 1) / t) * q1 * ((q1 - 1) / (2 * (q2 + 1))) |
| 232 |
} else {
|
|
| 233 | 4x |
if (q2 == 0) {
|
| 234 | 1x |
D <- S + ((t - 1) / t) * q1 * ((q1 - 1) / 2) |
| 235 |
} else {
|
|
| 236 | 3x |
D <- S + ((t - 1) / t) * (q1^2 / (2 * q2)) |
| 237 |
} |
|
| 238 |
} |
|
| 239 | 6x |
if (improved) {
|
| 240 | 2x |
q3 <- sum(q == 3) # Number of triple species |
| 241 | 2x |
q4 <- sum(q == 4) # Number of quadruple species |
| 242 | 2x |
if (q4 == 0) {
|
| 243 | ! |
if (getOption("tabula.verbose")) {
|
| 244 | ! |
message( |
| 245 | ! |
tr_("Improved Chao2 estimator is undefined when there is no quadruple species.")
|
| 246 |
) |
|
| 247 |
} |
|
| 248 | ! |
return(NA_real_) |
| 249 |
} |
|
| 250 | ||
| 251 | 2x |
k <- q1 - ((t - 3) / (t - 1)) * ((q2 * q3) / (2 * q4)) |
| 252 | 2x |
D <- D + ((t - 3) / (4 * t)) * (q3 / q4) * max(k, 0) |
| 253 |
} |
|
| 254 | ||
| 255 | 6x |
D |
| 256 |
} |
|
| 257 |
) |
|
| 258 | ||
| 259 |
# ICE ========================================================================== |
|
| 260 |
#' @export |
|
| 261 |
#' @rdname index_ice |
|
| 262 |
#' @aliases index_ice,matrix-method |
|
| 263 |
setMethod( |
|
| 264 |
f = "index_ice", |
|
| 265 |
signature = c(x = "matrix"), |
|
| 266 |
definition = function(x, k = 10, ...) {
|
|
| 267 | 2x |
x <- x > 0 # Convert to incidence |
| 268 | ||
| 269 | 2x |
q <- colSums(x) # Number of species in the assemblage |
| 270 | 2x |
q <- q[q > 0] # Remove unobserved species |
| 271 | ||
| 272 | 2x |
S_infr <- sum(q <= k) # Number of infrequent species |
| 273 | 2x |
S_freq <- sum(q > k) # Number of frequent species |
| 274 | 2x |
N_infr <- sum(q[q <= k]) # Number of incidences in the infrequent species |
| 275 |
# Number of sampling units that include at least one infrequent species |
|
| 276 | 2x |
t <- sum(rowSums(x[, q <= k]) != 0) |
| 277 | ||
| 278 | 2x |
q1 <- sum(q == 1) # Number of unique species in the assemblage |
| 279 | 2x |
if (q1 == N_infr) {
|
| 280 | ! |
if (getOption("tabula.verbose")) {
|
| 281 | ! |
message( |
| 282 | ! |
tr_("ICE is undefined when all rare species are singletons."), "\n",
|
| 283 | ! |
tr_("Consider using the bias-corrected Chao2 estimator instead.")
|
| 284 |
) |
|
| 285 |
} |
|
| 286 | ! |
return(NA_real_) |
| 287 |
} |
|
| 288 | ||
| 289 |
## Sample coverage estimate |
|
| 290 |
## ie. proportion of all incidences of infrequent species that are not uniques |
|
| 291 | 2x |
C_infr <- 1 - (q1 / N_infr) |
| 292 | ||
| 293 |
## Coefficient of variation |
|
| 294 | 2x |
a <- sum(vapply( |
| 295 | 2x |
X = seq_len(k), |
| 296 | 20x |
FUN = function(x, q) { x * (x - 1) * sum(q == x) },
|
| 297 | 2x |
FUN.VALUE = double(1), q = q) |
| 298 |
) |
|
| 299 | 2x |
b <- sum(vapply( |
| 300 | 2x |
X = seq_len(k), |
| 301 | 20x |
FUN = function(x, q) { x * sum(q == x) },
|
| 302 | 2x |
FUN.VALUE = double(1), q = q) |
| 303 |
) |
|
| 304 | 2x |
c <- sum(vapply( |
| 305 | 2x |
X = seq_len(k), |
| 306 | 20x |
FUN = function(x, q) { x * sum(q == x) - 1 },
|
| 307 | 2x |
FUN.VALUE = double(1), q = q) |
| 308 |
) |
|
| 309 | 2x |
g2 <- max((S_infr / C_infr) * (t / (t - 1)) * (a / (b * c)) - 1, 0) |
| 310 | ||
| 311 | 2x |
D <- S_freq + S_infr / C_infr + q1 * g2 / C_infr |
| 312 | 2x |
D |
| 313 |
} |
|
| 314 |
) |
|
| 315 | ||
| 316 |
# Margalef ===================================================================== |
|
| 317 |
#' @export |
|
| 318 |
#' @rdname index_margalef |
|
| 319 |
#' @aliases index_margalef,numeric-method |
|
| 320 |
setMethod( |
|
| 321 |
f = "index_margalef", |
|
| 322 |
signature = c(x = "numeric"), |
|
| 323 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 324 |
## Validation |
|
| 325 | 12x |
x <- x[x > 0] # Remove unobserved species |
| 326 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 327 | ! |
if (anyNA(x)) return(NA) |
| 328 | ||
| 329 | 12x |
N <- sum(x) # Number of individuals |
| 330 | 12x |
S <- length(x) # Number of observed species |
| 331 | 12x |
D <- (S - 1) / log(N, base = exp(1)) |
| 332 | 12x |
D |
| 333 |
} |
|
| 334 |
) |
|
| 335 | ||
| 336 |
# McIntosh ===================================================================== |
|
| 337 |
#' @export |
|
| 338 |
#' @rdname index_mcintosh |
|
| 339 |
#' @aliases index_mcintosh,numeric-method |
|
| 340 |
setMethod( |
|
| 341 |
f = "index_mcintosh", |
|
| 342 |
signature = c(x = "numeric"), |
|
| 343 |
definition = function(x, evenness = FALSE, na.rm = FALSE, ...) {
|
|
| 344 |
## Validation |
|
| 345 | 12x |
x <- x[x > 0] # Remove unobserved species |
| 346 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 347 | ! |
if (anyNA(x)) return(NA) |
| 348 | ||
| 349 | 12x |
N <- sum(x) |
| 350 | 12x |
S <- length(x) # richness = number of different species |
| 351 | 12x |
U <- sqrt(sum(x^2)) |
| 352 | ||
| 353 | 12x |
if (evenness) {
|
| 354 | 6x |
mac <- (N - U) / (N - (N / sqrt(S))) |
| 355 |
} else {
|
|
| 356 | 6x |
mac <- (N - U) / (N - sqrt(N)) |
| 357 |
} |
|
| 358 | 12x |
mac |
| 359 |
} |
|
| 360 |
) |
|
| 361 | ||
| 362 |
# Menhinick ==================================================================== |
|
| 363 |
#' @export |
|
| 364 |
#' @rdname index_menhinick |
|
| 365 |
#' @aliases index_menhinick,numeric-method |
|
| 366 |
setMethod( |
|
| 367 |
f = "index_menhinick", |
|
| 368 |
signature = c(x = "numeric"), |
|
| 369 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 370 |
## Validation |
|
| 371 | 12x |
x <- x[x > 0] # Remove unobserved species |
| 372 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 373 | ! |
if (anyNA(x)) return(NA) |
| 374 | ||
| 375 | 12x |
N <- sum(x) # Number of individuals |
| 376 | 12x |
S <- length(x) # Number of observed species |
| 377 | 12x |
D <- S / sqrt(N) |
| 378 | 12x |
D |
| 379 |
} |
|
| 380 |
) |
|
| 381 | ||
| 382 |
# Shannon ====================================================================== |
|
| 383 |
#' @export |
|
| 384 |
#' @rdname index_shannon |
|
| 385 |
#' @aliases index_shannon,numeric-method |
|
| 386 |
setMethod( |
|
| 387 |
f = "index_shannon", |
|
| 388 |
signature = c(x = "numeric"), |
|
| 389 |
definition = function(x, evenness = FALSE, unbiased = FALSE, ACE = FALSE, |
|
| 390 |
base = exp(1), na.rm = FALSE, ...) {
|
|
| 391 |
## Validation |
|
| 392 | 487x |
x <- x[x > 0] # Remove unobserved species |
| 393 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 394 | ! |
if (anyNA(x)) return(NA) |
| 395 | ||
| 396 | 487x |
N <- sum(x) |
| 397 | 487x |
S <- length(x) # richness = number of different species |
| 398 | 487x |
p <- x / N |
| 399 | 487x |
Hmax <- log(p, base) |
| 400 | 487x |
Hmax[is.infinite(Hmax)] <- 0 |
| 401 | ||
| 402 | 487x |
H <- -sum(p * Hmax) |
| 403 | 487x |
if (unbiased) {
|
| 404 | ! |
if (ACE) S <- index_ace(x, ...) |
| 405 | 1x |
H <- H + (S - 1) / (2 * N) |
| 406 |
} |
|
| 407 | 236x |
if (evenness) H <- H / log(S) |
| 408 | 487x |
H |
| 409 |
} |
|
| 410 |
) |
|
| 411 | ||
| 412 |
# Simpson ====================================================================== |
|
| 413 |
#' @export |
|
| 414 |
#' @rdname index_simpson |
|
| 415 |
#' @aliases index_simpson,numeric-method |
|
| 416 |
setMethod( |
|
| 417 |
f = "index_simpson", |
|
| 418 |
signature = c(x = "numeric"), |
|
| 419 |
definition = function(x, evenness = FALSE, unbiased = FALSE, na.rm = FALSE, ...) {
|
|
| 420 |
## Validation |
|
| 421 | 21x |
x <- x[x > 0] # Remove unobserved species |
| 422 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 423 | ! |
if (anyNA(x)) return(NA) |
| 424 | ||
| 425 | 21x |
N <- sum(x) |
| 426 | 2x |
if (unbiased) D <- sum(x * (x - 1)) / (N * (N - 1)) |
| 427 | 19x |
else D <- sum((x / N)^2) |
| 428 | ||
| 429 | 21x |
if (evenness) {
|
| 430 | 5x |
D <- 1 / D |
| 431 | 5x |
S <- length(x) |
| 432 | 5x |
D <- D / S |
| 433 |
} |
|
| 434 | 21x |
D |
| 435 |
} |
|
| 436 |
) |
|
| 437 | ||
| 438 |
# Squares ====================================================================== |
|
| 439 |
#' @export |
|
| 440 |
#' @rdname index_squares |
|
| 441 |
#' @aliases index_squares,numeric-method |
|
| 442 |
setMethod( |
|
| 443 |
f = "index_squares", |
|
| 444 |
signature = c(x = "numeric"), |
|
| 445 |
definition = function(x, na.rm = FALSE, ...) {
|
|
| 446 |
## Validation |
|
| 447 | 6x |
x <- x[x > 0] # Remove unobserved species |
| 448 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 449 | ! |
if (anyNA(x)) return(NA) |
| 450 | ||
| 451 | 6x |
S <- length(x) # Number of observed species |
| 452 | 6x |
N <- sum(x) # Number of individuals |
| 453 | 6x |
f1 <- sum(x == 1) # Number of singleton species |
| 454 | ||
| 455 | 6x |
Ssq <- S + f1^2 * sum(x^2) / (N^2 - f1 * S) |
| 456 | 6x |
Ssq |
| 457 |
} |
|
| 458 |
) |
| 1 |
# PLOT DIVERSITY |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# DiversityIndex =============================================================== |
|
| 6 |
#' @export |
|
| 7 |
#' @method plot DiversityIndex |
|
| 8 |
plot.DiversityIndex <- function(x, log = "x", |
|
| 9 |
col.mean = "#DDAA33", col.interval = "#004488", |
|
| 10 |
lty.mean = "solid", lty.interval = "dashed", |
|
| 11 |
lwd.mean = 1, lwd.interval = 1, |
|
| 12 |
xlab = NULL, ylab = NULL, |
|
| 13 |
main = NULL, sub = NULL, |
|
| 14 |
ann = graphics::par("ann"),
|
|
| 15 |
axes = TRUE, frame.plot = axes, |
|
| 16 |
panel.first = NULL, panel.last = NULL, ...) {
|
|
| 17 |
## Prepare data |
|
| 18 | ! |
count <- as.data.frame(x) |
| 19 | ||
| 20 |
## Open new window |
|
| 21 | ! |
grDevices::dev.hold() |
| 22 | ! |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 23 | ! |
graphics::plot.new() |
| 24 | ||
| 25 |
## Set plotting coordinates |
|
| 26 | ! |
xlim <- range(count$size) |
| 27 | ! |
ylim <- range(count$index) |
| 28 | ! |
if (length(x@simulation) != 0) {
|
| 29 | ! |
xlim <- range(x@simulation[, "size"]) |
| 30 | ! |
ylim <- range(x@simulation[, c("lower", "upper")])
|
| 31 |
} |
|
| 32 | ! |
graphics::plot.window(xlim = xlim, ylim = ylim, log = log) |
| 33 | ||
| 34 |
## Evaluate pre-plot expressions |
|
| 35 | ! |
panel.first |
| 36 | ||
| 37 |
## Plot |
|
| 38 | ! |
graphics::points(x = count$size, y = count$index, ...) |
| 39 | ||
| 40 |
## Simulated assemblages |
|
| 41 | ! |
if (length(x@simulation) != 0) {
|
| 42 | ! |
refined <- x@simulation |
| 43 | ! |
graphics::lines(x = refined[, "size"], y = refined[, "mean"], |
| 44 | ! |
col = col.mean, lty = lty.mean, lwd = lwd.mean) |
| 45 | ! |
graphics::lines(x = refined[, "size"], y = refined[, "lower"], |
| 46 | ! |
col = col.interval, lty = lty.interval, lwd = lwd.interval) |
| 47 | ! |
graphics::lines(x = refined[, "size"], y = refined[, "upper"], |
| 48 | ! |
col = col.interval, lty = lty.interval, lwd = lwd.interval) |
| 49 |
} |
|
| 50 | ||
| 51 |
## Evaluate post-plot and pre-axis expressions |
|
| 52 | ! |
panel.last |
| 53 | ||
| 54 |
## Construct axis |
|
| 55 | ! |
if (axes) {
|
| 56 | ! |
graphics::axis(side = 1, las = 1) |
| 57 | ! |
graphics::axis(side = 2, las = 1) |
| 58 |
} |
|
| 59 | ||
| 60 |
## Plot frame |
|
| 61 | ! |
if (frame.plot) {
|
| 62 | ! |
graphics::box() |
| 63 |
} |
|
| 64 | ||
| 65 |
## Add annotation |
|
| 66 | ! |
if (ann) {
|
| 67 | ! |
y_lab <- switch ( |
| 68 | ! |
class(x), |
| 69 | ! |
HeterogeneityIndex = tr_("Heterogeneity"),
|
| 70 | ! |
EvennessIndex = tr_("Evenness"),
|
| 71 | ! |
RichnessIndex = tr_("Richness"),
|
| 72 | ! |
tr_("Diversity")
|
| 73 |
) |
|
| 74 | ! |
xlab <- xlab %||% tr_("Sample size")
|
| 75 | ! |
ylab <- ylab %||% sprintf("%s (%s)", y_lab, x@method)
|
| 76 | ! |
graphics::title(main = main, sub = sub, xlab = xlab, ylab = ylab) |
| 77 |
} |
|
| 78 | ||
| 79 | ! |
invisible(x) |
| 80 |
} |
|
| 81 | ||
| 82 |
#' @export |
|
| 83 |
#' @rdname plot.DiversityIndex |
|
| 84 |
#' @aliases plot,DiversityIndex,missing-method |
|
| 85 |
setMethod("plot", c(x = "DiversityIndex", y = "missing"), plot.DiversityIndex)
|
|
| 86 | ||
| 87 |
# SHE ========================================================================== |
|
| 88 |
#' @export |
|
| 89 |
#' @rdname she |
|
| 90 |
#' @aliases she,matrix-method |
|
| 91 |
setMethod( |
|
| 92 |
f = "she", |
|
| 93 |
signature = c(object = "matrix"), |
|
| 94 |
definition = function(object, unbiased = FALSE, |
|
| 95 |
xlab = NULL, ylab = NULL, |
|
| 96 |
main = NULL, sub = NULL, |
|
| 97 |
ann = graphics::par("ann"),
|
|
| 98 |
axes = TRUE, frame.plot = axes, |
|
| 99 |
panel.first = NULL, panel.last = NULL, |
|
| 100 |
legend = list(x = "right"), ...) {
|
|
| 101 |
## Prepare data |
|
| 102 | 1x |
data <- .she(object, unbiased = unbiased) |
| 103 | ||
| 104 |
## Open new window |
|
| 105 | 1x |
grDevices::dev.hold() |
| 106 | 1x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 107 | 1x |
graphics::plot.new() |
| 108 | ||
| 109 |
## Set plotting coordinates |
|
| 110 | 1x |
xlim <- range(data[, 4]) |
| 111 | 1x |
ylim <- range(data[, -4]) |
| 112 | 1x |
graphics::plot.window(xlim = xlim, ylim = ylim) |
| 113 | ||
| 114 |
## Evaluate pre-plot expressions |
|
| 115 | 1x |
panel.first |
| 116 | ||
| 117 |
## Plot |
|
| 118 | 1x |
col <- c("black", "red", "blue")
|
| 119 | 1x |
for (i in c(1, 2, 3)) {
|
| 120 | 3x |
graphics::lines(x = data[, 4], y = data[, i], col = col[i], lty = i) |
| 121 | 3x |
graphics::points(x = data[, 4], y = data[, i], col = col[i], pch = 16) |
| 122 |
} |
|
| 123 | ||
| 124 |
## Evaluate post-plot and pre-axis expressions |
|
| 125 | 1x |
panel.last |
| 126 | ||
| 127 |
## Construct axis |
|
| 128 | 1x |
if (axes) {
|
| 129 | 1x |
graphics::axis(side = 1, las = 1) |
| 130 | 1x |
graphics::axis(side = 2, las = 1) |
| 131 |
} |
|
| 132 | ||
| 133 |
## Plot frame |
|
| 134 | 1x |
if (frame.plot) {
|
| 135 | 1x |
graphics::box() |
| 136 |
} |
|
| 137 | ||
| 138 |
## Add annotation |
|
| 139 | 1x |
if (ann) {
|
| 140 | 1x |
xlab <- xlab %||% tr_("Sample size")
|
| 141 | 1x |
ylab <- ylab %||% tr_("Diversity")
|
| 142 | 1x |
graphics::title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) |
| 143 |
} |
|
| 144 | ||
| 145 |
## Legend |
|
| 146 | 1x |
if (is.list(legend) && length(legend) > 0) {
|
| 147 | 1x |
args <- list(legend = c("ln(S)", "H", "ln(E)"),
|
| 148 | 1x |
col = col, pch = 16, |
| 149 | 1x |
lty = c(1, 2, 3), bty = "n") |
| 150 | 1x |
args <- utils::modifyList(args, legend) |
| 151 | 1x |
do.call(graphics::legend, args = args) |
| 152 |
} |
|
| 153 | ||
| 154 | 1x |
invisible(object) |
| 155 |
} |
|
| 156 |
) |
|
| 157 | ||
| 158 |
#' @export |
|
| 159 |
#' @rdname she |
|
| 160 |
#' @aliases she,data.frame-method |
|
| 161 |
setMethod( |
|
| 162 |
f = "she", |
|
| 163 |
signature = c(object = "data.frame"), |
|
| 164 |
definition = function(object, unbiased = FALSE, |
|
| 165 |
xlab = NULL, ylab = NULL, |
|
| 166 |
main = NULL, sub = NULL, |
|
| 167 |
ann = graphics::par("ann"),
|
|
| 168 |
axes = TRUE, frame.plot = axes, |
|
| 169 |
panel.first = NULL, panel.last = NULL, |
|
| 170 |
legend = list(x = "right"), ...) {
|
|
| 171 | 1x |
object <- data.matrix(object) |
| 172 | 1x |
methods::callGeneric(object, unbiased = unbiased, |
| 173 | 1x |
xlab = xlab, ylab = ylab, |
| 174 | 1x |
main = main, sub = sub, ann = ann, |
| 175 | 1x |
axes = axes, frame.plot = frame.plot, |
| 176 | 1x |
panel.first = panel.first, panel.last = panel.last, |
| 177 | 1x |
legend = legend, ...) |
| 178 |
} |
|
| 179 |
) |
|
| 180 | ||
| 181 |
#' @param object A matrix. |
|
| 182 |
#' @noRd |
|
| 183 |
.she <- function(object, unbiased = FALSE, ...) {
|
|
| 184 | ||
| 185 | 1x |
n <- nrow(object) |
| 186 | 1x |
m <- ncol(object) |
| 187 | ||
| 188 | 1x |
SHE <- matrix(data = 0, nrow = n, ncol = 4) |
| 189 | 1x |
y <- numeric(m) |
| 190 | ||
| 191 | 1x |
for (i in seq_len(n)) {
|
| 192 | 5x |
x <- object[i, ] + y |
| 193 | 5x |
n <- sum(x) |
| 194 | ||
| 195 |
## Log species abundance |
|
| 196 | 5x |
S <- log(sum(x > 0)) # Observed species |
| 197 | ||
| 198 |
## Shannon index |
|
| 199 | 5x |
H <- index_shannon(x, evenness = FALSE, unbiased = unbiased) |
| 200 | ||
| 201 |
## log evenness |
|
| 202 | 5x |
E <- H - S |
| 203 | ||
| 204 | 5x |
SHE[i, ] <- c(S, H, E, n) |
| 205 | 5x |
y <- x |
| 206 |
} |
|
| 207 | ||
| 208 | 1x |
SHE |
| 209 |
} |
|
| 210 | ||
| 211 |
# Profile ====================================================================== |
|
| 212 |
#' @export |
|
| 213 |
#' @rdname profiles |
|
| 214 |
#' @aliases profiles,matrix-method |
|
| 215 |
setMethod( |
|
| 216 |
f = "profiles", |
|
| 217 |
signature = c(object = "matrix"), |
|
| 218 |
definition = function(object, alpha = seq(from = 0, to = 4, by = 0.04), |
|
| 219 |
color = NULL, symbol = FALSE, |
|
| 220 |
xlab = NULL, ylab = NULL, |
|
| 221 |
main = NULL, sub = NULL, |
|
| 222 |
ann = graphics::par("ann"),
|
|
| 223 |
axes = TRUE, frame.plot = axes, |
|
| 224 |
panel.first = NULL, panel.last = NULL, |
|
| 225 |
legend = list(x = "topright"), ...) {
|
|
| 226 |
## Prepare data |
|
| 227 | 1x |
alpha <- alpha[alpha != 1] |
| 228 | 1x |
data <- .profiles(object, alpha = alpha) |
| 229 | 1x |
lab <- rownames(object) |
| 230 | 1x |
n <- nrow(object) |
| 231 | ||
| 232 |
## Graphical parameters |
|
| 233 | 1x |
lwd <- list(...)$lwd %||% graphics::par("lwd")
|
| 234 | 1x |
if (length(lwd) == 1) lwd <- rep(lwd, length.out = n) |
| 235 | ||
| 236 | 1x |
lty <- list(...)$lty %||% graphics::par("lty")
|
| 237 | 1x |
if (length(lty) == 1) lty <- rep(lty, length.out = n) |
| 238 | ! |
if (!isFALSE(symbol)) lty <- khroma::palette_line(symbol)(lab) |
| 239 | ||
| 240 | 1x |
col <- list(...)$col %||% graphics::par("col")
|
| 241 | 1x |
if (length(col) == 1) col <- rep(col, length.out = n) |
| 242 | 1x |
if (!isFALSE(color)) col <- khroma::palette_color_discrete(color)(lab) |
| 243 | ||
| 244 |
## Open new window |
|
| 245 | 1x |
grDevices::dev.hold() |
| 246 | 1x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 247 | 1x |
graphics::plot.new() |
| 248 | ||
| 249 |
## Set plotting coordinates |
|
| 250 | 1x |
xlim <- range(alpha) |
| 251 | 1x |
ylim <- range(data, finite = TRUE) |
| 252 | 1x |
graphics::plot.window(xlim = xlim, ylim = ylim) |
| 253 | ||
| 254 |
## Evaluate pre-plot expressions |
|
| 255 | 1x |
panel.first |
| 256 | ||
| 257 |
## Plot |
|
| 258 | 1x |
for (i in seq_len(n)) {
|
| 259 | 5x |
graphics::lines(x = alpha, y = data[, i], col = col[i], |
| 260 | 5x |
lty = lty[i], lwd = lwd[i]) |
| 261 |
} |
|
| 262 | ||
| 263 |
## Evaluate post-plot and pre-axis expressions |
|
| 264 | 1x |
panel.last |
| 265 | ||
| 266 |
## Construct axis |
|
| 267 | 1x |
if (axes) {
|
| 268 | 1x |
graphics::axis(side = 1, las = 1) |
| 269 | 1x |
graphics::axis(side = 2, las = 1) |
| 270 |
} |
|
| 271 | ||
| 272 |
## Plot frame |
|
| 273 | 1x |
if (frame.plot) {
|
| 274 | 1x |
graphics::box() |
| 275 |
} |
|
| 276 | ||
| 277 |
## Add annotation |
|
| 278 | 1x |
if (ann) {
|
| 279 | 1x |
xlab <- xlab %||% "alpha" |
| 280 | 1x |
ylab <- ylab %||% tr_("Diversity")
|
| 281 | 1x |
graphics::title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) |
| 282 |
} |
|
| 283 | ||
| 284 |
## Legend |
|
| 285 | 1x |
if (is.list(legend) && length(legend) > 0) {
|
| 286 | 1x |
args <- list(legend = lab, col = col, lty = lty, lwd = lwd, bty = "n") |
| 287 | 1x |
args <- utils::modifyList(args, legend) |
| 288 | 1x |
do.call(graphics::legend, args = args) |
| 289 |
} |
|
| 290 | ||
| 291 | 1x |
invisible(object) |
| 292 |
} |
|
| 293 |
) |
|
| 294 | ||
| 295 |
#' @export |
|
| 296 |
#' @rdname profiles |
|
| 297 |
#' @aliases profiles,data.frame-method |
|
| 298 |
setMethod( |
|
| 299 |
f = "profiles", |
|
| 300 |
signature = c(object = "data.frame"), |
|
| 301 |
definition = function(object, alpha = seq(from = 0, to = 4, by = 0.04), |
|
| 302 |
color = NULL, symbol = FALSE, |
|
| 303 |
xlab = NULL, ylab = NULL, |
|
| 304 |
main = NULL, sub = NULL, |
|
| 305 |
ann = graphics::par("ann"),
|
|
| 306 |
axes = TRUE, frame.plot = axes, |
|
| 307 |
panel.first = NULL, panel.last = NULL, |
|
| 308 |
legend = list(x = "topright"), ...) {
|
|
| 309 | 1x |
object <- data.matrix(object) |
| 310 | 1x |
methods::callGeneric(object, alpha = seq(from = 0, to = 4, by = 0.04), |
| 311 | 1x |
color = color, symbol = symbol, |
| 312 | 1x |
xlab = xlab, ylab = ylab, |
| 313 | 1x |
main = main, sub = sub, ann = ann, |
| 314 | 1x |
axes = axes, frame.plot = frame.plot, |
| 315 | 1x |
panel.first = panel.first, panel.last = panel.last, |
| 316 | 1x |
legend = legend, ...) |
| 317 |
} |
|
| 318 |
) |
|
| 319 | ||
| 320 |
#' @param object A matrix. |
|
| 321 |
#' @noRd |
|
| 322 |
.profiles <- function(object, alpha = seq(from = 0, to = 4, by = 0.04), ...) {
|
|
| 323 | ||
| 324 | 1x |
n <- nrow(object) |
| 325 | 1x |
m <- length(alpha) |
| 326 | ||
| 327 | 1x |
prof <- matrix(data = 0, nrow = m, ncol = n) |
| 328 | ||
| 329 | 1x |
index_renyi <- function(z, na.rm = FALSE) {
|
| 330 | 5x |
z <- z[z > 0] # Remove unobserved species |
| 331 | ! |
if (na.rm) z <- stats::na.omit(z) # Remove NAs |
| 332 | ||
| 333 | 5x |
function(x) {
|
| 334 | 500x |
p <- z / sum(z) |
| 335 | 500x |
exp(log(sum(p^x)) / (1 - x)) |
| 336 |
} |
|
| 337 |
} |
|
| 338 | ||
| 339 | 1x |
for (i in seq_len(n)) {
|
| 340 | 5x |
x <- object[i, ] |
| 341 | 5x |
f <- index_renyi(x) |
| 342 | 5x |
prof[, i] <- vapply(X = alpha, FUN = f, FUN.VALUE = numeric(1), ...) |
| 343 |
} |
|
| 344 | ||
| 345 | 1x |
prof |
| 346 |
} |
| 1 |
# CO-OCCURRENCE |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname occurrence |
|
| 7 |
#' @aliases occurrence,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "occurrence", |
|
| 10 |
signature = c(object = "matrix"), |
|
| 11 |
definition = function(object, method = c("absolute", "relative", "binomial")) {
|
|
| 12 |
## Validation |
|
| 13 | 3x |
method <- match.arg(method, several.ok = FALSE) |
| 14 | ||
| 15 |
## Pairwise comparison |
|
| 16 | 3x |
p <- ncol(object) |
| 17 | 3x |
labels <- colnames(object) |
| 18 | ||
| 19 | 3x |
if (method == "absolute" || method == "relative") {
|
| 20 | 3x |
incid <- object > 0 |
| 21 | 3x |
fun <- function(x) sum(incid[, x[1]] + incid[, x[2]] == 2) |
| 22 |
} |
|
| 23 | 3x |
if (method == "binomial") {
|
| 24 | ! |
fun <- function(x) index_binomial(object[, x[1]], object[, x[2]]) |
| 25 |
} |
|
| 26 | ||
| 27 | 3x |
cbn <- utils::combn(p, m = 2, simplify = TRUE) |
| 28 | 3x |
index <- apply(X = cbn, MARGIN = 2, FUN = fun) |
| 29 | ||
| 30 |
## Matrix of results |
|
| 31 | 3x |
mtx <- matrix(data = 0, nrow = p, ncol = p, |
| 32 | 3x |
dimnames = list(labels, labels)) |
| 33 | 3x |
mtx[lower.tri(mtx, diag = FALSE)] <- index |
| 34 | 3x |
mtx <- t(mtx) |
| 35 | 3x |
mtx[lower.tri(mtx, diag = FALSE)] <- index |
| 36 | ||
| 37 | 3x |
if (method == "relative") {
|
| 38 | ! |
mtx <- mtx / nrow(object) |
| 39 |
} |
|
| 40 | ||
| 41 | 3x |
occ <- stats::as.dist(mtx) |
| 42 | 3x |
attr(occ, "total") <- nrow(object) |
| 43 | 3x |
occ |
| 44 |
} |
|
| 45 |
) |
|
| 46 | ||
| 47 |
#' @export |
|
| 48 |
#' @rdname occurrence |
|
| 49 |
#' @aliases occurrence,data.frame-method |
|
| 50 |
setMethod( |
|
| 51 |
f = "occurrence", |
|
| 52 |
signature = c(object = "data.frame"), |
|
| 53 |
definition = function(object, method = c("absolute", "relative", "binomial")) {
|
|
| 54 | 1x |
object <- data.matrix(object) |
| 55 | 1x |
methods::callGeneric(object, method = method) |
| 56 |
} |
|
| 57 |
) |
|
| 58 | ||
| 59 |
## Binomial co-occurrence ------------------------------------------------------ |
|
| 60 |
#' @export |
|
| 61 |
#' @rdname index_binomial |
|
| 62 |
#' @aliases index_binomial,numeric,numeric-method |
|
| 63 |
setMethod( |
|
| 64 |
f = "index_binomial", |
|
| 65 |
signature = c(x = "numeric", y = "numeric"), |
|
| 66 |
definition = function(x, y) {
|
|
| 67 |
## Validation |
|
| 68 | 1x |
arkhe::assert_length(y, length(x)) |
| 69 | ||
| 70 |
## Total number of assemblages |
|
| 71 | 1x |
N <- length(x) |
| 72 |
## Expected proportion of co-occurrences for artifact classes |
|
| 73 | 1x |
p <- sum(x > 0) * sum(y > 0) / N^2 |
| 74 |
## Number of observed co-occurence for artifact classes |
|
| 75 | 1x |
o <- sum((x > 0) + (y > 0) == 2) |
| 76 | 1x |
if (p == 1) {
|
| 77 |
## Avoid NaN generation |
|
| 78 |
## TODO: warning ? |
|
| 79 | ! |
Cbi <- 0 |
| 80 |
} else {
|
|
| 81 | 1x |
Cbi <- (o - N * p) / sqrt(N * p * (1 - p)) |
| 82 |
} |
|
| 83 | 1x |
Cbi |
| 84 |
} |
|
| 85 |
) |
| 1 |
# RAREFACTION |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname rarefaction |
|
| 7 |
#' @aliases rarefaction,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "rarefaction", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, sample = NULL, method = c("hurlbert", "baxter"),
|
|
| 12 |
step = 1) {
|
|
| 13 |
## Validation |
|
| 14 | 2x |
method <- match.arg(method, several.ok = FALSE) |
| 15 | ||
| 16 | 2x |
n <- nrow(object) |
| 17 | 2x |
if (is.null(sample)) {
|
| 18 | ! |
sample <- rowSums(object) |
| 19 |
} |
|
| 20 | 2x |
if (length(sample) == 1) {
|
| 21 | 2x |
sample <- rep(sample, n) |
| 22 |
} |
|
| 23 | 2x |
k <- seq(from = 1, to = max(sample), by = step) |
| 24 | ||
| 25 |
## Matrix of results |
|
| 26 | 2x |
z <- matrix(data = NA_real_, nrow = n, ncol = length(k)) |
| 27 | 2x |
row_names <- rownames(object) %||% paste0("S", seq_len(n)) # Fix names
|
| 28 | 2x |
dimnames(z) <- list(row_names, k) |
| 29 | ||
| 30 | 2x |
for (i in seq_len(n)) {
|
| 31 | 7x |
spl <- k[k <= sample[[i]]] |
| 32 | 7x |
rare <- vapply( |
| 33 | 7x |
X = spl, |
| 34 | 7x |
FUN = function(s, x, f) f(x, s), |
| 35 | 7x |
FUN.VALUE = numeric(1), |
| 36 | 7x |
x = object[i, ], |
| 37 | 7x |
f = get_index(method) # Select method |
| 38 |
) |
|
| 39 | 7x |
z[i, seq_along(rare)] <- rare |
| 40 |
} |
|
| 41 | ||
| 42 | 2x |
.RarefactionIndex( |
| 43 | 2x |
z, |
| 44 | 2x |
labels = row_names, |
| 45 | 2x |
size = as.integer(k), |
| 46 | 2x |
method = method |
| 47 |
) |
|
| 48 |
} |
|
| 49 |
) |
|
| 50 | ||
| 51 |
#' @export |
|
| 52 |
#' @rdname rarefaction |
|
| 53 |
#' @aliases rarefaction,data.frame-method |
|
| 54 |
setMethod( |
|
| 55 |
f = "rarefaction", |
|
| 56 |
signature = signature(object = "data.frame"), |
|
| 57 |
definition = function(object, sample = NULL, method = c("hurlbert", "baxter"),
|
|
| 58 |
step = 1) {
|
|
| 59 | 1x |
object <- data.matrix(object) |
| 60 | 1x |
methods::callGeneric(object, sample = sample, method = method, step = step) |
| 61 |
} |
|
| 62 |
) |
|
| 63 | ||
| 64 |
# Plot ========================================================================= |
|
| 65 |
#' @export |
|
| 66 |
#' @method plot RarefactionIndex |
|
| 67 |
plot.RarefactionIndex <- function(x, color = NULL, symbol = FALSE, |
|
| 68 |
xlab = NULL, ylab = NULL, |
|
| 69 |
main = NULL, sub = NULL, |
|
| 70 |
ann = graphics::par("ann"),
|
|
| 71 |
axes = TRUE, frame.plot = axes, |
|
| 72 |
panel.first = NULL, panel.last = NULL, |
|
| 73 |
legend = list(x = "topleft"), ...) {
|
|
| 74 |
## Prepare data |
|
| 75 | 2x |
n <- nrow(x) |
| 76 | 2x |
lab <- labels(x) |
| 77 | ||
| 78 |
## Graphical parameters |
|
| 79 | 2x |
lwd <- list(...)$lwd %||% graphics::par("lwd")
|
| 80 | 2x |
if (length(lwd) == 1) lwd <- rep(lwd, length.out = n) |
| 81 | ||
| 82 | 2x |
lty <- list(...)$lty %||% graphics::par("lty")
|
| 83 | 2x |
if (length(lty) == 1) lty <- rep(lty, length.out = n) |
| 84 | ! |
if (!isFALSE(symbol)) lty <- khroma::palette_line(symbol)(lab) |
| 85 | ||
| 86 | 2x |
col <- list(...)$col %||% graphics::par("col")
|
| 87 | 2x |
if (length(col) == 1) col <- rep(col, length.out = n) |
| 88 | 2x |
if (!isFALSE(color)) col <- khroma::palette_color_discrete(color)(lab) |
| 89 | ||
| 90 |
## Open new window |
|
| 91 | 2x |
grDevices::dev.hold() |
| 92 | 2x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 93 | 2x |
graphics::plot.new() |
| 94 | ||
| 95 |
## Set plotting coordinates |
|
| 96 | 2x |
xlim <- range(x@size) |
| 97 | 2x |
ylim <- range(x, na.rm = TRUE) |
| 98 | 2x |
graphics::plot.window(xlim = xlim, ylim = ylim) |
| 99 | ||
| 100 |
## Evaluate pre-plot expressions |
|
| 101 | 2x |
panel.first |
| 102 | ||
| 103 |
## Plot |
|
| 104 | 2x |
for (i in seq_len(n)) {
|
| 105 | 10x |
graphics::lines(x = x@size, y = x[i, ], col = col[i], |
| 106 | 10x |
lwd = lwd[i], lty = lty[i]) |
| 107 |
} |
|
| 108 | ||
| 109 |
## Evaluate post-plot and pre-axis expressions |
|
| 110 | 2x |
panel.last |
| 111 | ||
| 112 |
## Construct axis |
|
| 113 | 2x |
if (axes) {
|
| 114 | 2x |
graphics::axis(side = 1, las = 1) |
| 115 | 2x |
graphics::axis(side = 2, las = 1) |
| 116 |
} |
|
| 117 | ||
| 118 |
## Plot frame |
|
| 119 | 2x |
if (frame.plot) {
|
| 120 | 2x |
graphics::box() |
| 121 |
} |
|
| 122 | ||
| 123 |
## Add annotation |
|
| 124 | 2x |
if (ann) {
|
| 125 | 2x |
xlab <- xlab %||% tr_("Sample size")
|
| 126 | 2x |
ylab <- ylab %||% tr_("Expected species index")
|
| 127 | 2x |
graphics::title(main = main, sub = sub, xlab = xlab, ylab = ylab) |
| 128 |
} |
|
| 129 | ||
| 130 |
## Legend |
|
| 131 | 2x |
if (is.list(legend) && length(legend) > 0) {
|
| 132 | 1x |
args <- list(legend = lab, col = col, lty = lty, lwd = lwd, bty = "n") |
| 133 | 1x |
args <- utils::modifyList(args, legend) |
| 134 | 1x |
do.call(graphics::legend, args = args) |
| 135 |
} |
|
| 136 | ||
| 137 | 2x |
invisible(x) |
| 138 |
} |
|
| 139 | ||
| 140 |
#' @export |
|
| 141 |
#' @rdname plot.RarefactionIndex |
|
| 142 |
#' @aliases plot,RarefactionIndex,missing-method |
|
| 143 |
setMethod("plot", c(x = "RarefactionIndex", y = "missing"), plot.RarefactionIndex)
|
|
| 144 | ||
| 145 |
# Index ======================================================================== |
|
| 146 |
#' @export |
|
| 147 |
#' @rdname index_baxter |
|
| 148 |
#' @aliases index_baxter,numeric-method |
|
| 149 |
setMethod( |
|
| 150 |
f = "index_baxter", |
|
| 151 |
signature = signature(x = "numeric"), |
|
| 152 |
definition = function(x, sample, ...) {
|
|
| 153 | 116x |
x <- x[x > 0] |
| 154 | 116x |
N <- sum(x) |
| 155 | ||
| 156 | 116x |
E <- suppressWarnings(exp(ramanujan(N - x) + ramanujan(N - sample) - |
| 157 | 116x |
ramanujan(N - x - sample) - ramanujan(N))) |
| 158 | 116x |
sum(1 - E, na.rm = FALSE) |
| 159 |
} |
|
| 160 |
) |
|
| 161 | ||
| 162 |
#' @export |
|
| 163 |
#' @rdname index_hurlbert |
|
| 164 |
#' @aliases index_hurlbert,numeric-method |
|
| 165 |
setMethod( |
|
| 166 |
f = "index_hurlbert", |
|
| 167 |
signature = signature(x = "numeric"), |
|
| 168 |
definition = function(x, sample, ...) {
|
|
| 169 | 27x |
x <- x[x > 0] |
| 170 | 27x |
N <- sum(x) |
| 171 | ||
| 172 | 27x |
E <- vapply( |
| 173 | 27x |
X = x, |
| 174 | 27x |
FUN = function(x, N, sample) {
|
| 175 | 204x |
if (N - x > sample) {
|
| 176 | 185x |
combination(N - x, sample) / combination(N, sample) |
| 177 |
} else {
|
|
| 178 | 19x |
0 |
| 179 |
} |
|
| 180 |
}, |
|
| 181 | 27x |
FUN.VALUE = double(1), |
| 182 | 27x |
N, sample |
| 183 |
) |
|
| 184 | 27x |
sum(1 - E, na.rm = FALSE) |
| 185 |
} |
|
| 186 |
) |
| 1 |
# PLOT BERTIN |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_bertin |
|
| 7 |
#' @aliases plot_bertin,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_bertin", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, threshold = NULL, freq = FALSE, margin = 1, |
|
| 12 |
color = c("white", "black"), flip = TRUE,
|
|
| 13 |
axes = TRUE, ...) {
|
|
| 14 |
## Conditional proportions |
|
| 15 | 2x |
if (freq) object <- prop.table(object, margin = margin) |
| 16 | ||
| 17 |
## Compute threshold for each variable |
|
| 18 | 5x |
if (!freq && is.function(threshold)) {
|
| 19 | 1x |
thr <- apply(X = object, MARGIN = 2, FUN = threshold) |
| 20 | 1x |
thr <- matrix(thr, nrow = nrow(object), ncol = ncol(object), byrow = TRUE) |
| 21 | 1x |
thr <- ifelse(object > thr, color[length(color)], color[1L]) |
| 22 | 1x |
color <- as.vector(t(thr)) |
| 23 |
} |
|
| 24 | ||
| 25 |
## /!\ Bertin plot flips x and y axis /!\ |
|
| 26 | 5x |
data <- if (flip) t(object) else object |
| 27 | ||
| 28 | 5x |
plot_matrix(data, panel = panel_bertin, color = color, |
| 29 | 5x |
axes = axes, legend = FALSE, asp = NA) |
| 30 | ||
| 31 |
## Legend |
|
| 32 |
# TODO |
|
| 33 | ||
| 34 | 5x |
invisible(object) |
| 35 |
} |
|
| 36 |
) |
|
| 37 | ||
| 38 |
#' @export |
|
| 39 |
#' @rdname plot_bertin |
|
| 40 |
#' @aliases plot_bertin,data.frame-method |
|
| 41 |
setMethod( |
|
| 42 |
f = "plot_bertin", |
|
| 43 |
signature = signature(object = "data.frame"), |
|
| 44 |
definition = function(object, threshold = NULL, freq = FALSE, margin = 1, |
|
| 45 |
color = c("white", "black"), flip = TRUE,
|
|
| 46 |
axes = TRUE, ...) {
|
|
| 47 | 5x |
object <- data.matrix(object) |
| 48 | 5x |
methods::callGeneric(object, threshold = threshold, |
| 49 | 5x |
freq = freq, margin = margin, |
| 50 | 5x |
color = color, flip = flip, axes = axes) |
| 51 |
} |
|
| 52 |
) |
| 1 |
# BETA DIVERSITY |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Cody ========================================================================= |
|
| 6 |
#' @export |
|
| 7 |
#' @rdname index_cody |
|
| 8 |
#' @aliases index_cody,matrix-method |
|
| 9 |
setMethod( |
|
| 10 |
f = "index_cody", |
|
| 11 |
signature = signature(x = "matrix"), |
|
| 12 |
definition = function(x) {
|
|
| 13 | 2x |
x <- x > 0 # presence/absence |
| 14 | ||
| 15 | 2x |
m <- nrow(x) |
| 16 | 2x |
first_transect <- x[1, ] |
| 17 | 2x |
last_transect <- x[m, ] |
| 18 | ||
| 19 |
# Number of taxa gained along the transect |
|
| 20 | 2x |
absent <- x[, !first_transect] # Keep only taxa absent in the first transect |
| 21 | 2x |
gained <- sum(apply(X = absent, MARGIN = 2, FUN = cumsum)[m, ] > 0) |
| 22 |
# Number of taxa lost |
|
| 23 | 2x |
lost <- sum(!last_transect) |
| 24 | ||
| 25 | 2x |
beta <- (gained + lost) / 2 |
| 26 | 2x |
beta |
| 27 |
} |
|
| 28 |
) |
|
| 29 | ||
| 30 |
# Routledge ==================================================================== |
|
| 31 |
## Beta R ---------------------------------------------------------------------- |
|
| 32 |
#' @export |
|
| 33 |
#' @rdname index_routledge |
|
| 34 |
#' @aliases index_routledge1,matrix-method |
|
| 35 |
setMethod( |
|
| 36 |
f = "index_routledge1", |
|
| 37 |
signature = signature(x = "matrix"), |
|
| 38 |
definition = function(x) {
|
|
| 39 | 2x |
x <- x > 0 # presence/absence |
| 40 | ||
| 41 | 2x |
p <- ncol(x) |
| 42 | ||
| 43 |
# Total number of taxa recorded in the system |
|
| 44 | 2x |
S <- sum(colSums(x) > 0) |
| 45 |
# Works out which pairs of taxa occurs together in at least one sample |
|
| 46 | 2x |
combine <- as.matrix(occurrence(x)) > 0 |
| 47 | 2x |
r <- sum(combine) / 2 |
| 48 | ||
| 49 | 2x |
beta <- S^2 / (2 * r + S) - 1 |
| 50 | 2x |
return(beta) |
| 51 |
} |
|
| 52 |
) |
|
| 53 | ||
| 54 |
## Beta I ---------------------------------------------------------------------- |
|
| 55 |
#' @export |
|
| 56 |
#' @rdname index_routledge |
|
| 57 |
#' @aliases index_routledge2,matrix-method |
|
| 58 |
setMethod( |
|
| 59 |
f = "index_routledge2", |
|
| 60 |
signature = signature(x = "matrix"), |
|
| 61 |
definition = function(x) {
|
|
| 62 | 4x |
x <- x > 0 # presence/absence |
| 63 | ||
| 64 |
# Number of samples in which each taxa is present |
|
| 65 | 4x |
e <- colSums(x) |
| 66 |
# Taxa richness of each sample |
|
| 67 | 4x |
alpha <- rowSums(x) |
| 68 | 4x |
t <- sum(e) |
| 69 | ||
| 70 | 4x |
beta <- log(t) - (1 / t) * sum(e * log(e)) - (1 / t) * sum(alpha * log(alpha)) |
| 71 | 4x |
return(beta) |
| 72 |
} |
|
| 73 |
) |
|
| 74 | ||
| 75 |
## Beta E ---------------------------------------------------------------------- |
|
| 76 |
#' @export |
|
| 77 |
#' @rdname index_routledge |
|
| 78 |
#' @aliases index_routledge3,matrix-method |
|
| 79 |
setMethod( |
|
| 80 |
f = "index_routledge3", |
|
| 81 |
signature = signature(x = "matrix"), |
|
| 82 |
definition = function(x) {
|
|
| 83 | 2x |
I <- index_routledge2(x) |
| 84 | 2x |
E <- exp(I) |
| 85 | 2x |
return(E) |
| 86 |
} |
|
| 87 |
) |
|
| 88 | ||
| 89 |
# Whittaker ==================================================================== |
|
| 90 |
#' @export |
|
| 91 |
#' @rdname index_whittaker |
|
| 92 |
#' @aliases index_whittaker,matrix-method |
|
| 93 |
setMethod( |
|
| 94 |
f = "index_whittaker", |
|
| 95 |
signature = signature(x = "matrix"), |
|
| 96 |
definition = function(x) {
|
|
| 97 | 2x |
x <- x > 0 # presence/absence |
| 98 | ||
| 99 |
# Total number of taxa recorded in the system |
|
| 100 | 2x |
S <- sum(colSums(x) > 0) |
| 101 |
# Mean taxa richness |
|
| 102 | 2x |
alpha <- mean(rowSums(x)) |
| 103 | ||
| 104 | 2x |
W <- (S / alpha) - 1 |
| 105 | 2x |
W |
| 106 |
} |
|
| 107 |
) |
|
| 108 | ||
| 109 |
# Wilson ======================================================================= |
|
| 110 |
#' @export |
|
| 111 |
#' @rdname index_wilson |
|
| 112 |
#' @aliases index_wilson,matrix-method |
|
| 113 |
setMethod( |
|
| 114 |
f = "index_wilson", |
|
| 115 |
signature = signature(x = "matrix"), |
|
| 116 |
definition = function(x) {
|
|
| 117 | 2x |
x <- x > 0 # presence/absence |
| 118 | ||
| 119 | 2x |
m <- nrow(x) |
| 120 | 2x |
first_transect <- x[1, ] |
| 121 | 2x |
last_transect <- x[m, ] |
| 122 | ||
| 123 |
# Mean taxa richness |
|
| 124 | 2x |
alpha <- mean(rowSums(x)) |
| 125 |
# Number of taxa gained along the transect |
|
| 126 | 2x |
absent <- x[, !first_transect] # Keep only taxa absent in the first transect |
| 127 | 2x |
gained <- sum(apply(X = absent, MARGIN = 2, FUN = cumsum)[m, ] > 0) |
| 128 |
# Number of taxa lost |
|
| 129 | 2x |
lost <- sum(!last_transect) |
| 130 | ||
| 131 | 2x |
beta <- (gained + lost) / (2 * alpha) |
| 132 | 2x |
beta |
| 133 |
} |
|
| 134 |
) |
| 1 |
# SIMILARITY |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Similarity =================================================================== |
|
| 6 |
#' @export |
|
| 7 |
#' @rdname similarity |
|
| 8 |
#' @aliases similarity,matrix-method |
|
| 9 |
setMethod( |
|
| 10 |
f = "similarity", |
|
| 11 |
signature = c(object = "matrix"), |
|
| 12 |
definition = function(object, method = c("brainerd", "bray", "jaccard",
|
|
| 13 |
"morisita", "sorensen")) {
|
|
| 14 |
## Deprecated/defunct |
|
| 15 | 1x |
if (any(method == "binomial")) {
|
| 16 | ! |
stop("Use occurrence(x, method = \"bionomial\") instead.", call. = FALSE)
|
| 17 |
} |
|
| 18 | 1x |
method[method == "sorenson"] <- "sorensen" |
| 19 | ||
| 20 |
## Validation |
|
| 21 | 1x |
method <- match.arg(method, several.ok = FALSE) |
| 22 | ||
| 23 |
## Select method |
|
| 24 | 1x |
fun <- get_index(method) |
| 25 | ||
| 26 |
## Pairwise comparison |
|
| 27 |
## Sample/case comparisons |
|
| 28 | 1x |
m <- nrow(object) |
| 29 | 1x |
labels <- rownames(object) |
| 30 | 1x |
beta <- function(x, f) f(object[x[1], ], object[x[2], ]) |
| 31 | ||
| 32 | 1x |
cbn <- utils::combn(seq_len(m), m = 2, simplify = TRUE) |
| 33 | 1x |
index <- apply(X = cbn, MARGIN = 2, FUN = beta, f = fun) |
| 34 | ||
| 35 |
## Matrix of results |
|
| 36 | 1x |
diag_value <- ifelse(method == "brainerd", 200, 1) |
| 37 | 1x |
mtx <- matrix(data = diag_value, nrow = m, ncol = m, |
| 38 | 1x |
dimnames = list(labels, labels)) |
| 39 | 1x |
mtx[lower.tri(mtx, diag = FALSE)] <- index |
| 40 | 1x |
mtx <- t(mtx) |
| 41 | 1x |
mtx[lower.tri(mtx, diag = FALSE)] <- index |
| 42 | ||
| 43 | 1x |
sim <- stats::as.dist(mtx) |
| 44 | 1x |
attr(sim, "method") <- method |
| 45 | 1x |
sim |
| 46 |
} |
|
| 47 |
) |
|
| 48 | ||
| 49 |
#' @export |
|
| 50 |
#' @rdname similarity |
|
| 51 |
#' @aliases similarity,data.frame-method |
|
| 52 |
setMethod( |
|
| 53 |
f = "similarity", |
|
| 54 |
signature = c(object = "data.frame"), |
|
| 55 |
definition = function(object, method = c("brainerd", "bray", "jaccard",
|
|
| 56 |
"morisita", "sorensen")) {
|
|
| 57 | 1x |
object <- data.matrix(object) |
| 58 | 1x |
methods::callGeneric(object, method = method) |
| 59 |
} |
|
| 60 |
) |
|
| 61 | ||
| 62 |
# Index ======================================================================== |
|
| 63 |
## Qualitative index ----------------------------------------------------------- |
|
| 64 |
#' @export |
|
| 65 |
#' @rdname index_jaccard |
|
| 66 |
#' @aliases index_jaccard,character,character-method |
|
| 67 |
setMethod( |
|
| 68 |
f = "index_jaccard", |
|
| 69 |
signature = c(x = "character", y = "character"), |
|
| 70 |
definition = function(x, y) {
|
|
| 71 | 1x |
inter <- length(intersect(x, y)) |
| 72 | 1x |
union <- length(x) + length(y) - inter |
| 73 | 1x |
inter / union |
| 74 |
} |
|
| 75 |
) |
|
| 76 | ||
| 77 |
#' @export |
|
| 78 |
#' @rdname index_jaccard |
|
| 79 |
#' @aliases index_jaccard,logical,logical-method |
|
| 80 |
setMethod( |
|
| 81 |
f = "index_jaccard", |
|
| 82 |
signature = c(x = "logical", y = "logical"), |
|
| 83 |
definition = function(x, y) {
|
|
| 84 |
## Validation |
|
| 85 | 1x |
arkhe::assert_length(y, length(x)) |
| 86 | ||
| 87 | 1x |
a <- sum(x) |
| 88 | 1x |
b <- sum(y) |
| 89 | 1x |
j <- sum((x + y) == 2) |
| 90 | 1x |
Cj <- j / (a + b - j) |
| 91 | 1x |
Cj |
| 92 |
} |
|
| 93 |
) |
|
| 94 | ||
| 95 |
#' @export |
|
| 96 |
#' @rdname index_jaccard |
|
| 97 |
#' @aliases index_jaccard,numeric,numeric-method |
|
| 98 |
setMethod( |
|
| 99 |
f = "index_jaccard", |
|
| 100 |
signature = c(x = "numeric", y = "numeric"), |
|
| 101 |
definition = function(x, y) {
|
|
| 102 |
## presence/absence |
|
| 103 | 1x |
x <- x > 0 |
| 104 | 1x |
y <- y > 0 |
| 105 | 1x |
methods::callGeneric(x, y) |
| 106 |
} |
|
| 107 |
) |
|
| 108 | ||
| 109 |
#' @export |
|
| 110 |
#' @rdname index_sorensen |
|
| 111 |
#' @aliases index_sorensen,logical,logical-method |
|
| 112 |
setMethod( |
|
| 113 |
f = "index_sorensen", |
|
| 114 |
signature = c(x = "logical", y = "logical"), |
|
| 115 |
definition = function(x, y) {
|
|
| 116 |
## Validation |
|
| 117 | 1x |
arkhe::assert_length(y, length(x)) |
| 118 | ||
| 119 | 1x |
a <- sum(x) |
| 120 | 1x |
b <- sum(y) |
| 121 | 1x |
j <- sum((x + y) == 2) |
| 122 | 1x |
Cs <- 2 * j / (a + b) |
| 123 | 1x |
Cs |
| 124 |
} |
|
| 125 |
) |
|
| 126 | ||
| 127 |
#' @export |
|
| 128 |
#' @rdname index_sorensen |
|
| 129 |
#' @aliases index_sorensen,numeric,numeric-method |
|
| 130 |
setMethod( |
|
| 131 |
f = "index_sorensen", |
|
| 132 |
signature = c(x = "numeric", y = "numeric"), |
|
| 133 |
definition = function(x, y) {
|
|
| 134 |
## presence/absence |
|
| 135 | 1x |
x <- x > 0 |
| 136 | 1x |
y <- y > 0 |
| 137 | 1x |
methods::callGeneric(x, y) |
| 138 |
} |
|
| 139 |
) |
|
| 140 | ||
| 141 |
## Quantitative index ---------------------------------------------------------- |
|
| 142 |
#' @export |
|
| 143 |
#' @rdname index_brainerd |
|
| 144 |
#' @aliases index_brainerd,numeric,numeric-method |
|
| 145 |
setMethod( |
|
| 146 |
f = "index_brainerd", |
|
| 147 |
signature = c(x = "numeric", y = "numeric"), |
|
| 148 |
definition = function(x, y) {
|
|
| 149 |
## Validation |
|
| 150 | 37x |
arkhe::assert_length(y, length(x)) |
| 151 | ||
| 152 | 37x |
a <- x / sum(x) |
| 153 | 37x |
b <- y / sum(y) |
| 154 | 37x |
Cb <- 2 - sum(abs(a - b)) |
| 155 | 37x |
Cb * 100 |
| 156 |
} |
|
| 157 |
) |
|
| 158 | ||
| 159 |
#' @export |
|
| 160 |
#' @rdname index_bray |
|
| 161 |
#' @aliases index_bray,numeric,numeric-method |
|
| 162 |
setMethod( |
|
| 163 |
f = "index_bray", |
|
| 164 |
signature = c(x = "numeric", y = "numeric"), |
|
| 165 |
definition = function(x, y) {
|
|
| 166 |
## Validation |
|
| 167 | 1x |
arkhe::assert_length(y, length(x)) |
| 168 | ||
| 169 | 1x |
a <- sum(x) # Number of individuals in site A |
| 170 | 1x |
b <- sum(y) # Number of individuals in site B |
| 171 | ||
| 172 | 1x |
Cs <- 1 - sum(abs(y - x)) / sum(x + y) |
| 173 | 1x |
Cs |
| 174 |
} |
|
| 175 |
) |
|
| 176 | ||
| 177 |
#' @export |
|
| 178 |
#' @rdname index_morisita |
|
| 179 |
#' @aliases index_morisita,numeric,numeric-method |
|
| 180 |
setMethod( |
|
| 181 |
f = "index_morisita", |
|
| 182 |
signature = c(x = "numeric", y = "numeric"), |
|
| 183 |
definition = function(x, y) {
|
|
| 184 |
## Validation |
|
| 185 | 1x |
arkhe::assert_length(y, length(x)) |
| 186 | ||
| 187 | 1x |
a <- sum(x) # Number of individuals in site A |
| 188 | 1x |
b <- sum(y) # Number of individuals in site B |
| 189 | 1x |
da <- sum(x^2) / a^2 |
| 190 | 1x |
db <- sum(y^2) / b^2 |
| 191 | ||
| 192 | 1x |
Cm <- (2 * sum(x * y)) / ((da + db) * a * b) |
| 193 | 1x |
Cm |
| 194 |
} |
|
| 195 |
) |
| 1 |
# PLOT RANK |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_rank |
|
| 7 |
#' @aliases plot_rank,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_rank", |
|
| 10 |
signature = c(object = "matrix"), |
|
| 11 |
definition = function(object, log = NULL, |
|
| 12 |
color = NULL, symbol = FALSE, |
|
| 13 |
xlab = NULL, ylab = NULL, |
|
| 14 |
main = NULL, sub = NULL, |
|
| 15 |
ann = graphics::par("ann"),
|
|
| 16 |
axes = TRUE, frame.plot = axes, |
|
| 17 |
panel.first = NULL, panel.last = NULL, |
|
| 18 |
legend = list(x = "topright"), ...) {
|
|
| 19 |
## Prepare data |
|
| 20 | 2x |
n <- nrow(object) |
| 21 | 2x |
lab <- rownames(object) |
| 22 | 2x |
object <- object / rowSums(object) |
| 23 | 2x |
rk <- t(apply(X = object, MARGIN = 1, FUN = function(x) rank(-x))) |
| 24 | ||
| 25 |
## Graphical parameters |
|
| 26 | 2x |
lwd <- list(...)$lwd %||% graphics::par("lwd")
|
| 27 | 2x |
lty <- list(...)$lty %||% graphics::par("lty")
|
| 28 | 2x |
cex <- list(...)$cex %||% graphics::par("cex")
|
| 29 | 2x |
if (length(lwd) == 1) lwd <- rep(lwd, length.out = n) |
| 30 | 2x |
if (length(lty) == 1) lty <- rep(lty, length.out = n) |
| 31 | 2x |
if (length(cex) == 1) cex <- rep(cex, length.out = n) |
| 32 | 1x |
if (is.null(log)) log <- "" |
| 33 | ||
| 34 | 2x |
pch <- list(...)$pch %||% 16 |
| 35 | 2x |
if (length(pch) == 1) pch <- rep(pch, length.out = n) |
| 36 | ! |
if (!isFALSE(symbol)) pch <- khroma::palette_shape(symbol)(lab) |
| 37 | ||
| 38 | 2x |
col <- list(...)$col %||% graphics::par("col")
|
| 39 | 2x |
if (length(col) == 1) col <- rep(col, length.out = n) |
| 40 | 2x |
if (!isFALSE(color)) col <- khroma::palette_color_discrete(color)(lab) |
| 41 | ||
| 42 |
## Open new window |
|
| 43 | 2x |
grDevices::dev.hold() |
| 44 | 2x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 45 | 2x |
graphics::plot.new() |
| 46 | ||
| 47 |
## Set plotting coordinates |
|
| 48 | 2x |
xlim <- range(rk) |
| 49 | 2x |
ylim <- range(object) |
| 50 | 2x |
graphics::plot.window(xlim = xlim, ylim = ylim, log = log) |
| 51 | ||
| 52 |
## Evaluate pre-plot expressions |
|
| 53 | 2x |
panel.first |
| 54 | ||
| 55 |
## Plot |
|
| 56 | 2x |
for (i in seq_len(n)) {
|
| 57 | 10x |
z <- order(rk[i, ]) |
| 58 | 10x |
graphics::lines(x = rk[i, z], y = object[i, z], |
| 59 | 10x |
col = col[i], lty = lty[i], lwd = lwd[i]) |
| 60 | 10x |
graphics::points(x = rk[i, z], y = object[i, z], |
| 61 | 10x |
col = col[i], pch = pch[i], cex = cex[i]) |
| 62 |
} |
|
| 63 | ||
| 64 |
## Evaluate post-plot and pre-axis expressions |
|
| 65 | 2x |
panel.last |
| 66 | ||
| 67 |
## Construct axis |
|
| 68 | 2x |
if (axes) {
|
| 69 | 2x |
graphics::axis(side = 1, las = 1) |
| 70 | 2x |
graphics::axis(side = 2, las = 1) |
| 71 |
} |
|
| 72 | ||
| 73 |
## Plot frame |
|
| 74 | 2x |
if (frame.plot) {
|
| 75 | 2x |
graphics::box() |
| 76 |
} |
|
| 77 | ||
| 78 |
## Add annotation |
|
| 79 | 2x |
if (ann) {
|
| 80 | 2x |
xlab <- xlab %||% tr_("Rank")
|
| 81 | 2x |
ylab <- ylab %||% tr_("Frequency")
|
| 82 | 2x |
graphics::title(main = main, sub = sub, xlab = xlab, ylab = ylab, ...) |
| 83 |
} |
|
| 84 | ||
| 85 |
## Legend |
|
| 86 | 2x |
if (is.list(legend) && length(legend) > 0) {
|
| 87 | 1x |
args <- list(legend = lab, col = col, pch = pch, |
| 88 | 1x |
lty = lty, lwd = lwd, bty = "n") |
| 89 | 1x |
args <- utils::modifyList(args, legend) |
| 90 | 1x |
do.call(graphics::legend, args = args) |
| 91 |
} |
|
| 92 | ||
| 93 | 2x |
invisible(object) |
| 94 |
} |
|
| 95 |
) |
|
| 96 | ||
| 97 |
#' @export |
|
| 98 |
#' @rdname plot_rank |
|
| 99 |
#' @aliases plot_rank,data.frame-method |
|
| 100 |
setMethod( |
|
| 101 |
f = "plot_rank", |
|
| 102 |
signature = signature(object = "data.frame"), |
|
| 103 |
definition = function(object, log = NULL, |
|
| 104 |
xlab = NULL, ylab = NULL, |
|
| 105 |
main = NULL, sub = NULL, |
|
| 106 |
ann = graphics::par("ann"),
|
|
| 107 |
axes = TRUE, frame.plot = axes, |
|
| 108 |
panel.first = NULL, panel.last = NULL, |
|
| 109 |
legend = list(x = "topright"), ...) {
|
|
| 110 | 2x |
object <- data.matrix(object) |
| 111 | 2x |
methods::callGeneric(object, log = log, |
| 112 | 2x |
xlab = xlab, ylab = ylab, |
| 113 | 2x |
main = main, sub = sub, ann = ann, axes = axes, |
| 114 | 2x |
frame.plot = frame.plot, panel.first = panel.first, |
| 115 | 2x |
panel.last = panel.last, legend = legend, ...) |
| 116 |
} |
|
| 117 |
) |
| 1 |
# PLOT FORD |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_ford |
|
| 7 |
#' @aliases plot_ford,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_ford", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, weights = FALSE, EPPM = FALSE, |
|
| 12 |
fill = "darkgrey", border = NA, |
|
| 13 |
axes = TRUE, ...) {
|
|
| 14 |
## Prepare data |
|
| 15 | 3x |
n <- nrow(object) |
| 16 | 3x |
m <- ncol(object) |
| 17 | 3x |
seq_row <- rev(seq_len(n)) |
| 18 | 3x |
seq_col <- seq_len(m) |
| 19 | 3x |
lab_row <- rownames(object) %||% seq_row |
| 20 | 3x |
lab_col <- colnames(object) %||% seq_col |
| 21 | ||
| 22 | 3x |
padding_x <- 0.05 |
| 23 | 3x |
padding_y <- 0.5 - 0.01 |
| 24 | 3x |
data <- prepare_ford(object, padding = padding_x) |
| 25 | ||
| 26 |
## Graphical parameters |
|
| 27 | 3x |
cex.axis <- graphics::par("cex.axis")
|
| 28 | 3x |
col.axis <- graphics::par("col.axis")
|
| 29 | 3x |
font.axis <- graphics::par("font.axis")
|
| 30 | ||
| 31 |
## Save and restore |
|
| 32 | 3x |
d <- inch2line("M", cex = cex.axis)
|
| 33 | 3x |
mfrow <- graphics::par("mfrow")
|
| 34 | 3x |
mar <- graphics::par("mar")
|
| 35 | 3x |
mar[1] <- 3 |
| 36 | 3x |
mar[2] <- inch2line(lab_row, cex = cex.axis) + 0.1 |
| 37 | 3x |
mar[3] <- inch2line(lab_col, cex = cex.axis) + 0.1 |
| 38 | 3x |
mar[4] <- 0 |
| 39 | ||
| 40 | 3x |
old_par <- graphics::par(mfrow = mfrow, mar = mar) |
| 41 | 3x |
on.exit(graphics::par(old_par)) |
| 42 | ||
| 43 | 3x |
if (weights) {
|
| 44 | 1x |
graphics::layout(matrix(c(1, 2), nrow = 1), widths = c(m - 1, 1)) |
| 45 |
} |
|
| 46 | ||
| 47 |
## Open new window |
|
| 48 | 3x |
grDevices::dev.hold() |
| 49 | 3x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 50 | 3x |
graphics::plot.new() |
| 51 | ||
| 52 |
## Set plotting coordinates |
|
| 53 | 3x |
xlim <- range(data$x - data$value, data$x + data$value, na.rm = TRUE) |
| 54 | 3x |
ylim <- range(data$y) + c(-1, 1) * padding_y |
| 55 | 3x |
graphics::plot.window(xlim = xlim, ylim = ylim) |
| 56 | ||
| 57 |
## Plot |
|
| 58 | 3x |
graphics::rect( |
| 59 | 3x |
xleft = data$x - data$value, |
| 60 | 3x |
ybottom = data$y - padding_y, |
| 61 | 3x |
xright = data$x + data$value, |
| 62 | 3x |
ytop = data$y + padding_y, |
| 63 | 3x |
col = fill, |
| 64 | 3x |
border = border |
| 65 |
) |
|
| 66 | 3x |
if (EPPM) {
|
| 67 | 1x |
graphics::rect( |
| 68 | 1x |
xleft = data$x - data$eppm, |
| 69 | 1x |
ybottom = data$y - padding_y, |
| 70 | 1x |
xright = data$x + data$eppm, |
| 71 | 1x |
ytop = data$y + padding_y, |
| 72 | 1x |
col = "black", |
| 73 | 1x |
border = NA |
| 74 |
) |
|
| 75 |
} |
|
| 76 | ||
| 77 |
## Construct axis |
|
| 78 | 3x |
if (axes) {
|
| 79 | 3x |
graphics::axis(side = 2, at = seq_row, labels = lab_row, las = 2, |
| 80 | 3x |
lty = 0, cex.axis = cex.axis, col.axis = col.axis, |
| 81 | 3x |
font.axis = font.axis) |
| 82 | 3x |
graphics::axis(side = 3, at = unique(data$x), labels = lab_col, las = 2, |
| 83 | 3x |
lty = 0, cex.axis = cex.axis, col.axis = col.axis, |
| 84 | 3x |
font.axis = font.axis) |
| 85 | ||
| 86 | 3x |
x_axis <- data$x[which.max(data$value)] |
| 87 | 3x |
graphics::axis(side = 1, at = c(x_axis - 0.2, x_axis + 0.2), labels = FALSE) |
| 88 | 3x |
graphics::axis(side = 1, at = x_axis, labels = label_percent(0.2), |
| 89 | 3x |
tick = FALSE) |
| 90 |
} |
|
| 91 | ||
| 92 | 3x |
if (weights) {
|
| 93 |
## Graphical parameters |
|
| 94 | 1x |
mar[2] <- 0.1 |
| 95 | 1x |
mar[4] <- 0.1 |
| 96 | 1x |
graphics::par(mar = mar) |
| 97 | ||
| 98 |
## Open new window |
|
| 99 | 1x |
graphics::plot.new() |
| 100 | ||
| 101 |
## Set plotting coordinates |
|
| 102 | 1x |
total <- rowSums(object) |
| 103 | 1x |
graphics::plot.window(xlim = c(0, max(total) * 1.05), ylim = ylim, |
| 104 | 1x |
xaxs = "i") |
| 105 | ||
| 106 |
## Plot |
|
| 107 | 1x |
y <- length(total) - seq_along(total) + 1 |
| 108 | 1x |
graphics::rect( |
| 109 | 1x |
xleft = 0, |
| 110 | 1x |
ybottom = y - padding_y, |
| 111 | 1x |
xright = total, |
| 112 | 1x |
ytop = y + padding_y, |
| 113 | 1x |
col = fill, |
| 114 | 1x |
border = border |
| 115 |
) |
|
| 116 | ||
| 117 |
## Construct axis |
|
| 118 | 1x |
if (axes) {
|
| 119 | 1x |
graphics::segments(x0 = 0, y0 = 0, x1 = 0, y = n + 0.5, |
| 120 | 1x |
col = col.axis, lwd = 1) |
| 121 | 1x |
graphics::axis(side = 1) |
| 122 |
} |
|
| 123 |
} |
|
| 124 | ||
| 125 | 3x |
invisible(object) |
| 126 |
} |
|
| 127 |
) |
|
| 128 | ||
| 129 |
#' @export |
|
| 130 |
#' @rdname plot_ford |
|
| 131 |
#' @aliases plot_ford,data.frame-method |
|
| 132 |
setMethod( |
|
| 133 |
f = "plot_ford", |
|
| 134 |
signature = signature(object = "data.frame"), |
|
| 135 |
definition = function(object, weights = FALSE, EPPM = FALSE, |
|
| 136 |
fill = "darkgrey", border = NA, |
|
| 137 |
axes = TRUE, ...) {
|
|
| 138 | 2x |
object <- data.matrix(object) |
| 139 | 2x |
methods::callGeneric(object, weights = weights, EPPM = EPPM, fill = fill, |
| 140 | 2x |
border = border, axes = axes, ...) |
| 141 |
} |
|
| 142 |
) |
|
| 143 | ||
| 144 |
#' Prepare data for Ford plot |
|
| 145 |
#' @return A data.frame. |
|
| 146 |
#' @keywords internal |
|
| 147 |
#' @noRd |
|
| 148 |
prepare_ford <- function(x, padding = 0.05) {
|
|
| 149 |
## EPPM |
|
| 150 | 3x |
EPPM <- eppm(x) / 100 |
| 151 | ||
| 152 |
## Relative frequencies |
|
| 153 | 3x |
freq <- x / rowSums(x) |
| 154 | ||
| 155 |
## Prevent division by zero |
|
| 156 | 3x |
freq[is.nan(freq)] <- 0 |
| 157 | ||
| 158 |
## Adaptive spacing between columns |
|
| 159 | 3x |
col_max <- apply(X = freq, MARGIN = 2, FUN = max, na.rm = TRUE) |
| 160 | 3x |
roll_max <- roll_sum(col_max, n = 2) + padding * max(freq) |
| 161 | 3x |
cum_max <- c(0, cumsum(roll_max)) |
| 162 | ||
| 163 |
## Build a long table |
|
| 164 | 3x |
row <- row(x, as.factor = TRUE) |
| 165 | 3x |
col <- col(x, as.factor = TRUE) |
| 166 | 3x |
data <- data.frame( |
| 167 | 3x |
row = as.vector(row), |
| 168 | 3x |
column = as.vector(col), |
| 169 | 3x |
value = as.vector(freq), |
| 170 | 3x |
eppm = as.vector(EPPM) |
| 171 |
) |
|
| 172 | ||
| 173 | 3x |
m <- nrow(freq) |
| 174 | 3x |
data$x <- rep(cum_max, each = m) + 1 |
| 175 | 3x |
data$y <- as.vector(m + 1 - as.numeric(row)) # Reverse levels order |
| 176 | ||
| 177 | 3x |
return(data) |
| 178 |
} |
| 1 |
# MATRIGRAPH |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname matrigraph |
|
| 7 |
#' @aliases pvi,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "pvi", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object) {
|
|
| 12 | 3x |
pvi <- object * 100 / expected(object) |
| 13 | 3x |
dimnames(pvi) <- dimnames(object) |
| 14 | 3x |
pvi |
| 15 |
} |
|
| 16 |
) |
|
| 17 | ||
| 18 |
#' @export |
|
| 19 |
#' @rdname matrigraph |
|
| 20 |
#' @aliases pvi,data.frame-method |
|
| 21 |
setMethod( |
|
| 22 |
f = "pvi", |
|
| 23 |
signature = signature(object = "data.frame"), |
|
| 24 |
definition = function(object) {
|
|
| 25 | 1x |
object <- data.matrix(object) |
| 26 | 1x |
methods::callGeneric(object) |
| 27 |
} |
|
| 28 |
) |
|
| 29 | ||
| 30 |
#' @export |
|
| 31 |
#' @rdname matrigraph |
|
| 32 |
#' @aliases matrigraph,matrix-method |
|
| 33 |
setMethod( |
|
| 34 |
f = "matrigraph", |
|
| 35 |
signature = signature(object = "matrix"), |
|
| 36 |
definition = function(object, reverse = FALSE, axes = TRUE, ...) {
|
|
| 37 | 2x |
plot_matrix(pvi(object), panel = panel_matrigraph, scale = FALSE, |
| 38 | 2x |
axes = axes, legend = FALSE, asp = 1, reverse = reverse) |
| 39 | 2x |
invisible(object) |
| 40 |
} |
|
| 41 |
) |
|
| 42 | ||
| 43 |
#' @export |
|
| 44 |
#' @rdname matrigraph |
|
| 45 |
#' @aliases matrigraph,data.frame-method |
|
| 46 |
setMethod( |
|
| 47 |
f = "matrigraph", |
|
| 48 |
signature = signature(object = "data.frame"), |
|
| 49 |
definition = function(object, reverse = FALSE, ...) {
|
|
| 50 | 2x |
object <- data.matrix(object) |
| 51 | 2x |
methods::callGeneric(object, reverse = reverse, ...) |
| 52 |
} |
|
| 53 |
) |
| 1 |
# DIVERSITY |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Index ======================================================================== |
|
| 6 |
index_observed <- function(x, ...) {
|
|
| 7 | 235x |
sum(x > 0, ...) # Number of observed species |
| 8 |
} |
|
| 9 |
get_index <- function(x) {
|
|
| 10 | 706x |
match.fun(sprintf("index_%s", x))
|
| 11 |
} |
|
| 12 |
do_index <- function(x, method, ...) {
|
|
| 13 | 675x |
f <- get_index(method) |
| 14 | 675x |
f(x, ...) |
| 15 |
} |
|
| 16 |
is_evenness <- function(x) {
|
|
| 17 | ! |
methods::is(x, "EvennessIndex") |
| 18 |
} |
|
| 19 | ||
| 20 |
#' Compute a Diversity Index |
|
| 21 |
#' |
|
| 22 |
#' @param x A [`numeric`] [`matrix`]. |
|
| 23 |
#' @param method A [`character`] string specifying the measure to be computed. |
|
| 24 |
#' @param by_row A [`logical`] scalar: should `method` be computed for each row? |
|
| 25 |
#' @param ... Further parameters to be passed to `method`. |
|
| 26 |
#' @return A [DiversityIndex-class] object. |
|
| 27 |
#' @author N. Frerebeau |
|
| 28 |
#' @keywords internal |
|
| 29 |
#' @noRd |
|
| 30 |
index_diversity <- function(x, method, ..., by_row = TRUE) {
|
|
| 31 | 17x |
fun <- get_index(method) |
| 32 | 17x |
if (by_row) {
|
| 33 | 15x |
idx <- apply(X = x, MARGIN = 1, FUN = fun, ...) |
| 34 |
} else {
|
|
| 35 | 2x |
idx <- fun(x, ...) |
| 36 |
} |
|
| 37 | ||
| 38 |
## Fix names |
|
| 39 | 17x |
row_names <- rownames(x) %||% paste0("S", seq_along(idx))
|
| 40 | ||
| 41 | 17x |
.DiversityIndex( |
| 42 | 17x |
idx, |
| 43 | 17x |
labels = row_names, |
| 44 | 17x |
size = as.integer(rowSums(x)), |
| 45 | 17x |
data = x, |
| 46 | 17x |
method = method |
| 47 |
) |
|
| 48 |
} |
|
| 49 | ||
| 50 |
# Diversity ==================================================================== |
|
| 51 |
#' @export |
|
| 52 |
#' @rdname diversity |
|
| 53 |
#' @aliases diversity,matrix-method |
|
| 54 |
setMethod( |
|
| 55 |
f = "diversity", |
|
| 56 |
signature = c(object = "matrix"), |
|
| 57 |
definition = function(object, ..., evenness = FALSE, unbiased = FALSE) {
|
|
| 58 | ||
| 59 | 1x |
index <- t(apply( |
| 60 | 1x |
X = object, |
| 61 | 1x |
MARGIN = 1, |
| 62 | 1x |
FUN = function(x, evenness, unbiased) {
|
| 63 | 5x |
c( |
| 64 | 5x |
size = sum(x), |
| 65 | 5x |
observed = observed(x), |
| 66 |
## Heterogeneity |
|
| 67 | 5x |
shannon = index_shannon(x, evenness = evenness, unbiased = unbiased), |
| 68 | 5x |
brillouin = index_brillouin(x, evenness = evenness), |
| 69 |
## Dominance |
|
| 70 | 5x |
simpson = index_simpson(x, evenness = evenness, unbiased = unbiased), |
| 71 | 5x |
berger = index_berger(x), |
| 72 |
## Richness |
|
| 73 | 5x |
menhinick = index_menhinick(x), |
| 74 | 5x |
margalef = index_margalef(x), |
| 75 | 5x |
chao1 = index_chao1(x, unbiased = unbiased), |
| 76 | 5x |
ace = index_ace(x), |
| 77 | 5x |
squares = index_squares(x) |
| 78 |
) |
|
| 79 |
}, |
|
| 80 | 1x |
evenness = evenness, |
| 81 | 1x |
unbiased = unbiased |
| 82 |
)) |
|
| 83 | 1x |
rownames(index) <- rownames(object) |
| 84 | 1x |
as.data.frame(index) |
| 85 |
} |
|
| 86 |
) |
|
| 87 | ||
| 88 |
#' @export |
|
| 89 |
#' @rdname diversity |
|
| 90 |
#' @aliases diversity,data.frame-method |
|
| 91 |
setMethod( |
|
| 92 |
f = "diversity", |
|
| 93 |
signature = c(object = "data.frame"), |
|
| 94 |
definition = function(object, ..., evenness = FALSE, unbiased = FALSE) {
|
|
| 95 | 1x |
object <- data.matrix(object) |
| 96 | 1x |
methods::callGeneric(object, ..., evenness = evenness, unbiased = unbiased) |
| 97 |
} |
|
| 98 |
) |
|
| 99 | ||
| 100 |
# Heterogeneity ================================================================ |
|
| 101 |
#' @export |
|
| 102 |
#' @rdname heterogeneity |
|
| 103 |
#' @aliases heterogeneity,matrix-method |
|
| 104 |
setMethod( |
|
| 105 |
f = "heterogeneity", |
|
| 106 |
signature = c(object = "matrix"), |
|
| 107 |
definition = function(object, ..., |
|
| 108 |
method = c("shannon", "simpson", "berger",
|
|
| 109 |
"boone", "brillouin", "mcintosh")) {
|
|
| 110 | 6x |
method <- match.arg(method, several.ok = FALSE) |
| 111 | 6x |
by_row <- method != "boone" |
| 112 | 6x |
index <- index_diversity(object, method, ..., evenness = FALSE, |
| 113 | 6x |
by_row = by_row) |
| 114 | 6x |
.HeterogeneityIndex(index) |
| 115 |
} |
|
| 116 |
) |
|
| 117 | ||
| 118 |
#' @export |
|
| 119 |
#' @rdname heterogeneity |
|
| 120 |
#' @aliases heterogeneity,data.frame-method |
|
| 121 |
setMethod( |
|
| 122 |
f = "heterogeneity", |
|
| 123 |
signature = c(object = "data.frame"), |
|
| 124 |
definition = function(object, ..., |
|
| 125 |
method = c("shannon", "simpson", "berger",
|
|
| 126 |
"boone", "brillouin", "mcintosh")) {
|
|
| 127 | 6x |
object <- data.matrix(object) |
| 128 | 6x |
methods::callGeneric(object, ..., method = method) |
| 129 |
} |
|
| 130 |
) |
|
| 131 | ||
| 132 |
# Evenness ===================================================================== |
|
| 133 |
#' @export |
|
| 134 |
#' @rdname evenness |
|
| 135 |
#' @aliases evenness,matrix-method |
|
| 136 |
setMethod( |
|
| 137 |
f = "evenness", |
|
| 138 |
signature = c(object = "matrix"), |
|
| 139 |
definition = function(object, ..., |
|
| 140 |
method = c("shannon", "simpson", "brillouin",
|
|
| 141 |
"mcintosh")) {
|
|
| 142 | 5x |
method <- match.arg(method, several.ok = FALSE) |
| 143 | 5x |
index <- index_diversity(object, method, ..., evenness = TRUE) |
| 144 | 5x |
.EvennessIndex(index) |
| 145 |
} |
|
| 146 |
) |
|
| 147 | ||
| 148 |
#' @export |
|
| 149 |
#' @rdname evenness |
|
| 150 |
#' @aliases evenness,data.frame-method |
|
| 151 |
setMethod( |
|
| 152 |
f = "evenness", |
|
| 153 |
signature = c(object = "data.frame"), |
|
| 154 |
definition = function(object, ..., |
|
| 155 |
method = c("shannon", "simpson", "brillouin",
|
|
| 156 |
"mcintosh")) {
|
|
| 157 | 5x |
object <- data.matrix(object) |
| 158 | 5x |
methods::callGeneric(object, ..., method = method) |
| 159 |
} |
|
| 160 |
) |
|
| 161 | ||
| 162 |
# Richness ===================================================================== |
|
| 163 |
#' @export |
|
| 164 |
#' @rdname richness |
|
| 165 |
#' @aliases richness,matrix-method |
|
| 166 |
setMethod( |
|
| 167 |
f = "richness", |
|
| 168 |
signature = c(object = "matrix"), |
|
| 169 |
definition = function(object, ..., method = c("observed", "margalef", "menhinick")) {
|
|
| 170 |
## Backward compatibility |
|
| 171 | ! |
if (method == "count") method <- "observed" |
| 172 | ||
| 173 | 4x |
method <- match.arg(method, several.ok = FALSE) |
| 174 | 4x |
index <- index_diversity(object, method, ...) |
| 175 | 4x |
.RichnessIndex(index) |
| 176 |
} |
|
| 177 |
) |
|
| 178 | ||
| 179 |
#' @export |
|
| 180 |
#' @rdname richness |
|
| 181 |
#' @aliases richness,data.frame-method |
|
| 182 |
setMethod( |
|
| 183 |
f = "richness", |
|
| 184 |
signature = c(object = "data.frame"), |
|
| 185 |
definition = function(object, ..., method = c("observed", "margalef", "menhinick")) {
|
|
| 186 | 4x |
object <- data.matrix(object) |
| 187 | 4x |
methods::callGeneric(object, ..., method = method) |
| 188 |
} |
|
| 189 |
) |
|
| 190 | ||
| 191 |
# Composition ================================================================== |
|
| 192 |
#' @export |
|
| 193 |
#' @rdname richness |
|
| 194 |
#' @aliases composition,matrix-method |
|
| 195 |
setMethod( |
|
| 196 |
f = "composition", |
|
| 197 |
signature = c(object = "matrix"), |
|
| 198 |
definition = function(object, ..., |
|
| 199 |
method = c("chao1", "ace", "squares", "chao2", "ice")) {
|
|
| 200 | 2x |
method <- match.arg(method, several.ok = FALSE) |
| 201 | 2x |
by_row <- any(method == c("chao1", "ace", "squares"))
|
| 202 | 2x |
index <- index_diversity(object, method, ..., by_row = by_row) |
| 203 | 2x |
.CompositionIndex(index) |
| 204 |
} |
|
| 205 |
) |
|
| 206 | ||
| 207 |
#' @export |
|
| 208 |
#' @rdname richness |
|
| 209 |
#' @aliases composition,data.frame-method |
|
| 210 |
setMethod( |
|
| 211 |
f = "composition", |
|
| 212 |
signature = c(object = "data.frame"), |
|
| 213 |
definition = function(object, ..., |
|
| 214 |
method = c("chao1", "ace", "squares", "chao2", "ice")) {
|
|
| 215 | ! |
object <- data.matrix(object) |
| 216 | ! |
methods::callGeneric(object, ..., method = method) |
| 217 |
} |
|
| 218 |
) |
|
| 219 | ||
| 220 |
# Turnover ===================================================================== |
|
| 221 |
#' @export |
|
| 222 |
#' @rdname turnover |
|
| 223 |
#' @aliases turnover,matrix-method |
|
| 224 |
setMethod( |
|
| 225 |
f = "turnover", |
|
| 226 |
signature = c(object = "matrix"), |
|
| 227 |
definition = function(object, ..., |
|
| 228 |
method = c("whittaker", "cody", "routledge1",
|
|
| 229 |
"routledge2", "routledge3", "wilson")) {
|
|
| 230 | 6x |
method <- match.arg(method, several.ok = FALSE) |
| 231 | 6x |
fun <- get_index(method) |
| 232 | 6x |
fun(object) |
| 233 |
} |
|
| 234 |
) |
|
| 235 | ||
| 236 |
#' @export |
|
| 237 |
#' @rdname turnover |
|
| 238 |
#' @aliases turnover,data.frame-method |
|
| 239 |
setMethod( |
|
| 240 |
f = "turnover", |
|
| 241 |
signature = c(object = "data.frame"), |
|
| 242 |
definition = function(object, ..., |
|
| 243 |
method = c("whittaker", "cody", "routledge1",
|
|
| 244 |
"routledge2", "routledge3", "wilson")) {
|
|
| 245 | ! |
object <- data.matrix(object) |
| 246 | ! |
methods::callGeneric(object, ..., method = method) |
| 247 |
} |
|
| 248 |
) |
| 1 |
# PLOT DICE-LERASS |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_diceleraas |
|
| 7 |
#' @aliases plot_diceleraas,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_diceleraas", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, |
|
| 12 |
main = NULL, sub = NULL, |
|
| 13 |
ann = graphics::par("ann"),
|
|
| 14 |
axes = TRUE, frame.plot = FALSE, |
|
| 15 |
panel.first = NULL, panel.last = NULL, ...) {
|
|
| 16 |
## Prepare data |
|
| 17 | 1x |
object <- t(object) |
| 18 | 1x |
object[object == 0] <- NA |
| 19 | 1x |
n <- nrow(object) |
| 20 | 1x |
id <- seq_len(n) |
| 21 | 1x |
lab <- rownames(object) %||% id |
| 22 | ||
| 23 |
## Mean |
|
| 24 | 1x |
moy <- rowMeans(object, na.rm = TRUE) |
| 25 |
## Standard deviation |
|
| 26 | 1x |
ec <- apply(X = object, MARGIN = 1, FUN = stats::sd, na.rm = TRUE) |
| 27 |
## Standard error |
|
| 28 | 1x |
se <- ec / sqrt(rowSums(!is.na(object))) |
| 29 |
## Range |
|
| 30 | 1x |
ran <- t(apply(X = object, MARGIN = 1, FUN = range, na.rm = TRUE)) |
| 31 | 1x |
colnames(ran) <- c("min", "max")
|
| 32 | ||
| 33 | 1x |
data <- data.frame(y = rev(id), mean = moy, sd = ec, se = se * 2, ran) |
| 34 | ||
| 35 |
## Graphical parameters |
|
| 36 | 1x |
cex.axis <- list(...)$cex.axis %||% graphics::par("cex.axis")
|
| 37 | 1x |
col.axis <- list(...)$col.axis %||% graphics::par("col.axis")
|
| 38 | 1x |
font.axis <- list(...)$font.axis %||% graphics::par("font.axis")
|
| 39 | 1x |
lwd <- list(...)$lwd %||% graphics::par("lwd")
|
| 40 | 1x |
lty <- list(...)$lty %||% graphics::par("lty")
|
| 41 | 1x |
col <- list(...)$col %||% c("black")
|
| 42 | 1x |
if (length(lwd) < n) lwd <- rep(lwd, length.out = n) |
| 43 | 1x |
if (length(lty) < n) lty <- rep(lty, length.out = n) |
| 44 | 1x |
if (length(col) < n) col <- rep(col, length.out = n) |
| 45 | ||
| 46 |
## Save and restore |
|
| 47 | 1x |
mar <- graphics::par("mar")
|
| 48 | 1x |
mar[2] <- inch2line(lab, cex = cex.axis) + 0.5 |
| 49 | 1x |
old_par <- graphics::par(mar = mar) |
| 50 | 1x |
on.exit(graphics::par(old_par)) |
| 51 | ||
| 52 |
## Open new window |
|
| 53 | 1x |
grDevices::dev.hold() |
| 54 | 1x |
on.exit(grDevices::dev.flush(), add = TRUE) |
| 55 | 1x |
graphics::plot.new() |
| 56 | ||
| 57 |
## Set plotting coordinates |
|
| 58 | 1x |
xlim <- range(data$min, data$max, data$mean + data$sd, |
| 59 | 1x |
data$mean - data$sd, na.rm = TRUE) |
| 60 | 1x |
ylim <- c(1, n + 1) |
| 61 | 1x |
graphics::plot.window(xlim = xlim, ylim = ylim) |
| 62 | ||
| 63 |
## Evaluate pre-plot expressions |
|
| 64 | 1x |
panel.first |
| 65 | ||
| 66 |
## Plot |
|
| 67 | 1x |
for (i in id) {
|
| 68 | 5x |
tmp <- data[i, ] |
| 69 | 5x |
graphics::polygon( |
| 70 | 5x |
x = tmp$sd * c(-1, -1, 1, 1) + tmp$mean, |
| 71 | 5x |
y = tmp$y + (1 / 3) * c(0, 1, 1, 0), |
| 72 | 5x |
col = "white", border = NA |
| 73 |
) |
|
| 74 | 5x |
graphics::lines( |
| 75 | 5x |
x = tmp$sd * c(-1, -1, 1, 1) + tmp$mean, |
| 76 | 5x |
y = tmp$y + (1 / 3) * c(0, 1, 1, 0), |
| 77 | 5x |
col = col[i], lty = lty[i] |
| 78 |
) |
|
| 79 | 5x |
graphics::polygon( |
| 80 | 5x |
x = tmp$se * c(-1, -1, 1, 1) + tmp$mean, |
| 81 | 5x |
y = tmp$y + (1 / 3) * c(0, 1, 1, 0), |
| 82 | 5x |
col = col[i], border = NA |
| 83 |
) |
|
| 84 | 5x |
graphics::segments( |
| 85 | 5x |
x0 = tmp$mean, y0 = tmp$y, |
| 86 | 5x |
x1 = tmp$mean, y1 = tmp$y + 0.5, |
| 87 | 5x |
col = col[i], lty = lty[i] |
| 88 |
) |
|
| 89 | 5x |
graphics::segments( |
| 90 | 5x |
x0 = tmp$min, y0 = tmp$y, |
| 91 | 5x |
x1 = tmp$max, y1 = tmp$y, |
| 92 | 5x |
col = col[i], lty = lty[i] |
| 93 |
) |
|
| 94 |
} |
|
| 95 | ||
| 96 |
## Evaluate post-plot and pre-axis expressions |
|
| 97 | 1x |
panel.last |
| 98 | ||
| 99 |
## Construct axis |
|
| 100 | 1x |
if (axes) {
|
| 101 | 1x |
graphics::axis(side = 1, las = 1) |
| 102 | 1x |
graphics::mtext(rev(lab), side = 2, at = id, las = 1, padj = 0, line = 0, |
| 103 | 1x |
cex = cex.axis, col.axis = col.axis, font = font.axis) |
| 104 |
} |
|
| 105 | ||
| 106 |
## Plot frame |
|
| 107 | 1x |
if (frame.plot) {
|
| 108 | ! |
graphics::box() |
| 109 |
} |
|
| 110 | ||
| 111 |
## Add annotation |
|
| 112 | 1x |
if (ann) {
|
| 113 | 1x |
graphics::title(main = main, sub = sub, |
| 114 | 1x |
xlab = tr_("Absolute frequency"), ylab = NULL, ...)
|
| 115 |
} |
|
| 116 | ||
| 117 | 1x |
invisible(object) |
| 118 |
} |
|
| 119 |
) |
|
| 120 | ||
| 121 |
#' @export |
|
| 122 |
#' @rdname plot_diceleraas |
|
| 123 |
#' @aliases plot_diceleraas,data.frame-method |
|
| 124 |
setMethod( |
|
| 125 |
f = "plot_diceleraas", |
|
| 126 |
signature = signature(object = "data.frame"), |
|
| 127 |
definition = function(object, |
|
| 128 |
main = NULL, sub = NULL, |
|
| 129 |
ann = graphics::par("ann"),
|
|
| 130 |
axes = TRUE, frame.plot = FALSE, |
|
| 131 |
panel.first = NULL, panel.last = NULL, ...) {
|
|
| 132 | 1x |
object <- data.matrix(object) |
| 133 | 1x |
methods::callGeneric(object, |
| 134 | 1x |
main = main, sub = sub, ann = ann, |
| 135 | 1x |
axes = axes, frame.plot = frame.plot, |
| 136 | 1x |
panel.first = panel.first, panel.last = panel.last, ...) |
| 137 |
} |
|
| 138 |
) |
| 1 |
# HELPERS |
|
| 2 | ||
| 3 |
## https://michaelchirico.github.io/potools/articles/developers.html |
|
| 4 |
tr_ <- function(...) {
|
|
| 5 | 12x |
enc2utf8(gettext(paste0(...), domain = "R-tabula")) |
| 6 |
} |
|
| 7 | ||
| 8 |
is_incidence <- function(x) {
|
|
| 9 | 1x |
if (is.logical(x)) return(TRUE) |
| 10 | 20x |
x <- as.numeric(x) |
| 11 | 20x |
all(x == 0 | x == 1, na.rm = TRUE) |
| 12 |
} |
|
| 13 | ||
| 14 |
#' Rolling Sum |
|
| 15 |
#' |
|
| 16 |
#' @param x A [`numeric`] vector. |
|
| 17 |
#' @param n An [`integer`] giving the rolling window size. |
|
| 18 |
#' @return A [`numeric`] vector. |
|
| 19 |
#' @keywords internal |
|
| 20 |
#' @noRd |
|
| 21 |
roll_sum <- function(x, n = 2) {
|
|
| 22 | 3x |
utils::tail(cumsum(x) - cumsum(c(rep(0, n), utils::head(x, -n))), -n + 1) |
| 23 |
} |
|
| 24 | ||
| 25 |
#' Plotting Dimensions of Character Strings |
|
| 26 |
#' |
|
| 27 |
#' Convert string length in inch to number of (margin) lines. |
|
| 28 |
#' @param x A [`character`] vector of string whose length is to be calculated. |
|
| 29 |
#' @param ... Further parameter to be passed to [graphics::strwidth()]`, such as |
|
| 30 |
#' `cex`. |
|
| 31 |
#' @return |
|
| 32 |
#' A [`numeric`] vector (maximum string width in units of margin lines). |
|
| 33 |
#' @note For internal use only. |
|
| 34 |
#' @family graphic tools |
|
| 35 |
#' @keywords internal |
|
| 36 |
#' @noRd |
|
| 37 |
inch2line <- function(x, ...) {
|
|
| 38 | 86x |
(max(graphics::strwidth(x, units = "inch", ...)) / |
| 39 | 86x |
graphics::par("cin")[2] + graphics::par("mgp")[2]) * graphics::par("cex")
|
| 40 |
} |
|
| 41 | ||
| 42 |
#' Circle |
|
| 43 |
#' |
|
| 44 |
#' Draws a circle. |
|
| 45 |
#' @param x,y A length-one [`numeric`] vector giving the coordinates of the |
|
| 46 |
#' center of the circle. |
|
| 47 |
#' @param radius A length-one [`numeric`] vector giving the radius of the |
|
| 48 |
#' circle. |
|
| 49 |
#' @param n A length-on [`integer`] vector specifying the number of vertices to |
|
| 50 |
#' draw the circle. |
|
| 51 |
#' @param ... Further parameters to be passed to [graphics::polygon()]. |
|
| 52 |
#' @return |
|
| 53 |
#' `circle()` is called it for its side-effects: it results in a graphic |
|
| 54 |
#' being displayed. |
|
| 55 |
#' @example inst/examples/ex-circle.R |
|
| 56 |
#' @author N. Frerebeau |
|
| 57 |
#' @keywords internal |
|
| 58 |
#' @noRd |
|
| 59 |
circle <- function(x, y, radius, ..., n = 100) {
|
|
| 60 | 308x |
angle.inc <- 2 * pi / n |
| 61 | 308x |
angles <- seq(0, 2 * pi - angle.inc, by = angle.inc) |
| 62 | ||
| 63 | 308x |
xv <- cos(angles) * radius + x |
| 64 | 308x |
yv <- sin(angles) * radius + y |
| 65 | 308x |
graphics::polygon(xv, yv, ...) |
| 66 |
} |
|
| 67 | ||
| 68 |
#' Label Percentages |
|
| 69 |
#' |
|
| 70 |
#' @param x A [`numeric`] vector. |
|
| 71 |
#' @param digits An [`integer`] indicating the number of decimal places. |
|
| 72 |
#' If `NULL` (the default), breaks will have the minimum number of digits |
|
| 73 |
#' needed to show the difference between adjacent values. |
|
| 74 |
#' @param trim A [`logical`] scalar. If `FALSE` (the default), values are |
|
| 75 |
#' right-justified to a common width (see [base::format()]). |
|
| 76 |
#' @return A [`character`] vector. |
|
| 77 |
#' @keywords internal |
|
| 78 |
#' @noRd |
|
| 79 |
label_percent <- function(x, digits = NULL, trim = FALSE) {
|
|
| 80 | 7x |
i <- !is.na(x) |
| 81 | 7x |
y <- x[i] |
| 82 | 7x |
y <- abs(y) * 100 |
| 83 | 7x |
y <- format(y, trim = trim, digits = digits) |
| 84 | 7x |
y <- paste0(y, "%") |
| 85 | 7x |
x[i] <- y |
| 86 | 7x |
x |
| 87 |
} |
| 1 |
# GENERIC METHODS |
|
| 2 |
#' @include AllClasses.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Import S4 generics =========================================================== |
|
| 6 |
#' @importMethodsFrom arkhe jackknife |
|
| 7 |
#' @importMethodsFrom arkhe bootstrap |
|
| 8 |
NULL |
|
| 9 | ||
| 10 |
# Extract ====================================================================== |
|
| 11 |
## Mutators -------------------------------------------------------------------- |
|
| 12 |
#' Get or Set Parts of an Object |
|
| 13 |
#' |
|
| 14 |
#' Getters and setters to extract or replace parts of an object. |
|
| 15 |
#' @param object,x An \R object from which to get or set element(s). |
|
| 16 |
#' @param ... Currently not used. |
|
| 17 |
# @param value A possible value for the element(s) of `object` (see below). |
|
| 18 |
#' @return |
|
| 19 |
#' * `labels()` returns a suitable set of labels from an object for use in |
|
| 20 |
#' printing or plotting. |
|
| 21 |
# @example inst/examples/ex-mutator.R |
|
| 22 |
#' @author N. Frerebeau |
|
| 23 |
#' @docType methods |
|
| 24 |
#' @family mutators |
|
| 25 |
#' @name mutators |
|
| 26 |
#' @rdname mutators |
|
| 27 |
#' @aliases get set |
|
| 28 |
NULL |
|
| 29 | ||
| 30 |
#' @rdname mutators |
|
| 31 |
#' @aliases get_method-method |
|
| 32 |
setGeneric( |
|
| 33 |
name = "get_method", |
|
| 34 | 14x |
def = function(x) standardGeneric("get_method")
|
| 35 |
) |
|
| 36 | ||
| 37 |
## Coerce ---------------------------------------------------------------------- |
|
| 38 |
#' Coerce to a Data Frame |
|
| 39 |
#' |
|
| 40 |
#' @param x An object. |
|
| 41 |
#' @param row.names,optional Currently not used. |
|
| 42 |
#' @param ... Currently not used. |
|
| 43 |
#' @return |
|
| 44 |
#' A [`data.frame`]. |
|
| 45 |
#' @author N. Frerebeau |
|
| 46 |
#' @docType methods |
|
| 47 |
#' @family mutators |
|
| 48 |
#' @name data.frame |
|
| 49 |
#' @rdname data.frame |
|
| 50 |
NULL |
|
| 51 | ||
| 52 |
# Resample ===================================================================== |
|
| 53 |
#' Bootstrap Estimation |
|
| 54 |
#' |
|
| 55 |
#' @param object An \R object (typically a [DiversityIndex-class] object). |
|
| 56 |
#' @param n A non-negative [`integer`] giving the number of bootstrap |
|
| 57 |
#' replications. |
|
| 58 |
#' @param f A [`function`] that takes a single numeric vector (the bootstrap |
|
| 59 |
#' estimates) as argument. |
|
| 60 |
#' @param level A length-one [`numeric`] vector giving the confidence level. |
|
| 61 |
#' Must be a single number between \eqn{0} and \eqn{1}. Only used if `f` is
|
|
| 62 |
#' `NULL`. |
|
| 63 |
#' @param interval A [`character`] string giving the type of confidence |
|
| 64 |
#' interval to be returned. It must be one "`basic`" (the default), "`normal`" |
|
| 65 |
#' or "`percentiles`" (see [arkhe::confidence_bootstrap()]). Any unambiguous |
|
| 66 |
#' substring can be given. Only used if `f` is `NULL`. |
|
| 67 |
#' @param seed An object specifying if and how the random number generator |
|
| 68 |
#' should be initialized (see [stats::simulate()]). |
|
| 69 |
#' @param rare A [`logical`] scalar: should the sample be drawn from an |
|
| 70 |
#' uniform distribution with replacement instead of a multinomial distribution? |
|
| 71 |
#' @details |
|
| 72 |
#' `n` random samples are drawn, each with the same sample size as in the |
|
| 73 |
#' original sample and with class probabilities proportional to the original |
|
| 74 |
#' abundances. |
|
| 75 |
#' |
|
| 76 |
#' Note that the mean of the bootstrapped samples will often be much lower than |
|
| 77 |
#' the observed value. Bootstrapping results must be interpreted with great |
|
| 78 |
#' care. |
|
| 79 |
#' @return |
|
| 80 |
#' If `f` is `NULL` (the default), `bootstrap()` returns a `numeric` `matrix` |
|
| 81 |
#' with the following columns: |
|
| 82 |
#' \describe{
|
|
| 83 |
#' \item{`original`}{The observed value.}
|
|
| 84 |
#' \item{`mean`}{The bootstrap estimate of mean.}
|
|
| 85 |
#' \item{`bias`}{The bootstrap estimate of bias.}
|
|
| 86 |
#' \item{`error`}{The bootstrap estimate of standard error.}
|
|
| 87 |
#' \item{`lower`}{The lower limit of the bootstrap confidence interval at `level`.}
|
|
| 88 |
#' \item{`upper`}{The upper limit of the bootstrap confidence interval at `level`.}
|
|
| 89 |
#' } |
|
| 90 |
#' |
|
| 91 |
#' If `f` is a `function`, `bootstrap()` returns the result of `f` applied to |
|
| 92 |
#' the values computed from the `n` replications. |
|
| 93 |
#' @example inst/examples/ex-bootstrap.R |
|
| 94 |
#' @author N. Frerebeau |
|
| 95 |
#' @docType methods |
|
| 96 |
#' @family resampling methods |
|
| 97 |
#' @name bootstrap |
|
| 98 |
#' @rdname bootstrap |
|
| 99 |
NULL |
|
| 100 | ||
| 101 |
#' Jackknife Estimation |
|
| 102 |
#' |
|
| 103 |
#' @param object An \R object (typically a [DiversityIndex-class] object). |
|
| 104 |
#' @param f A [`function`] that takes a single numeric vector (the leave-one-out |
|
| 105 |
#' values) as argument. |
|
| 106 |
#' @return |
|
| 107 |
#' If `f` is `NULL` (the default), `jackknife()` returns a `numeric` `matrix` |
|
| 108 |
#' with the following columns: |
|
| 109 |
#' \describe{
|
|
| 110 |
#' \item{`original`}{The observed value.}
|
|
| 111 |
#' \item{`mean`}{The jackknife estimate of mean.}
|
|
| 112 |
#' \item{`bias`}{The jackknife estimate of bias.}
|
|
| 113 |
#' \item{`error`}{The jackknife estimate of standard error.}
|
|
| 114 |
#' } |
|
| 115 |
#' |
|
| 116 |
#' If `f` is a `function`, `jackknife()` returns the result of `f` applied to |
|
| 117 |
#' the leave-one-out values. |
|
| 118 |
#' @example inst/examples/ex-jackknife.R |
|
| 119 |
#' @author N. Frerebeau |
|
| 120 |
#' @docType methods |
|
| 121 |
#' @family resampling methods |
|
| 122 |
#' @name jackknife |
|
| 123 |
#' @rdname jackknife |
|
| 124 |
NULL |
|
| 125 | ||
| 126 |
# Diversity ==================================================================== |
|
| 127 |
#' Alpha Diversity |
|
| 128 |
#' |
|
| 129 |
#' Computes multiple alpha diversity indices. |
|
| 130 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 131 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 132 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 133 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 134 |
#' @param evenness A [`logical`] scalar: should an evenness measure be computed |
|
| 135 |
#' instead of an heterogeneity/dominance index? Only available for `shannon`, |
|
| 136 |
#' `simpson` and `brillouin` indices. |
|
| 137 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 138 |
#' used? Only available for `shannon`, `simpson` and `chao1` indices. |
|
| 139 |
#' @param ... Currently not used. |
|
| 140 |
#' @details |
|
| 141 |
#' Alpha diversity refers to diversity at the local level, assessed within a |
|
| 142 |
#' delimited system. It is the diversity within a uniform habitat of fixed size. |
|
| 143 |
#' |
|
| 144 |
#' *Diversity* measurement assumes that all individuals in a specific |
|
| 145 |
#' taxa are equivalent and that all types are equally different from each |
|
| 146 |
#' other (Peet 1974). A measure of diversity can be achieved by using indices |
|
| 147 |
#' built on the relative abundance of taxa. These indices (sometimes referred |
|
| 148 |
#' to as non-parametric indices) benefit from not making assumptions about the |
|
| 149 |
#' underlying distribution of taxa abundance: they only take relative |
|
| 150 |
#' abundances of the species that are present and species richness into |
|
| 151 |
#' account. Peet (1974) refers to them as indices of |
|
| 152 |
#' *[heterogeneity][heterogeneity()]*. |
|
| 153 |
#' |
|
| 154 |
#' Diversity indices focus on one aspect of the taxa abundance and emphasize |
|
| 155 |
#' either *[richness][richness()]* (weighting towards uncommon taxa) or |
|
| 156 |
#' *dominance* (weighting towards abundant taxa; Magurran 1988). |
|
| 157 |
#' |
|
| 158 |
#' *[Evenness][evenness()]* is a measure of how evenly individuals are |
|
| 159 |
#' distributed across the sample. |
|
| 160 |
#' @return |
|
| 161 |
#' A [`data.frame`] with the following columns: |
|
| 162 |
#' \describe{
|
|
| 163 |
#' \item{`size`}{Sample size.}
|
|
| 164 |
#' \item{`observed`}{Number of observed taxa/types.}
|
|
| 165 |
#' \item{`shannon`}{[Shannon-Wiener diversity index][index_shannon()].}
|
|
| 166 |
#' \item{`brillouin`}{[Brillouin diversity index][index_brillouin()].}
|
|
| 167 |
#' \item{`simpson`}{[Simpson dominance index][index_simpson()].}
|
|
| 168 |
#' \item{`berger`}{[Berger-Parker dominance index][index_berger()].}
|
|
| 169 |
#' \item{`menhinick`}{[Menhinick richness index][index_menhinick()].}
|
|
| 170 |
#' \item{`margalef`}{[Margalef richness index][index_margalef()].}
|
|
| 171 |
#' \item{`chao1`}{[Chao1 estimator][index_chao1()].}
|
|
| 172 |
#' \item{`ace`}{[Abundance-based Coverage Estimator][index_ace()].}
|
|
| 173 |
#' \item{`squares`}{[Squares estimator][index_squares()].}
|
|
| 174 |
#' } |
|
| 175 |
#' @note |
|
| 176 |
#' The `berger` and `simpson` methods return a *dominance* index, not the |
|
| 177 |
#' reciprocal or inverse form usually adopted, so that an increase in the value |
|
| 178 |
#' of the index accompanies a decrease in diversity. |
|
| 179 |
#' @references |
|
| 180 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 181 |
#' Princeton, NJ: Princeton University Press. |
|
| 182 |
#' \doi{10.1007/978-94-015-7358-0}.
|
|
| 183 |
#' |
|
| 184 |
#' Peet, R. K. (1974). The Measurement of Species Diversity. *Annual Review of |
|
| 185 |
#' Ecology and Systematics*, 5(1), 285-307. |
|
| 186 |
#' \doi{10.1146/annurev.es.05.110174.001441}.
|
|
| 187 |
#' @example inst/examples/ex-diversity.R |
|
| 188 |
#' @author N. Frerebeau |
|
| 189 |
#' @family diversity measures |
|
| 190 |
#' @docType methods |
|
| 191 |
#' @aliases diversity-method |
|
| 192 |
setGeneric( |
|
| 193 |
name = "diversity", |
|
| 194 |
def = function(object, ...) standardGeneric("diversity"),
|
|
| 195 |
valueClass = "data.frame" |
|
| 196 |
) |
|
| 197 | ||
| 198 |
## Heterogeneity --------------------------------------------------------------- |
|
| 199 |
#' Heterogeneity |
|
| 200 |
#' |
|
| 201 |
#' Computes an heterogeneity or a dominance index. |
|
| 202 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 203 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 204 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 205 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 206 |
#' @param method A [`character`] string specifying the index to be computed |
|
| 207 |
#' (see details). Any unambiguous substring can be given. |
|
| 208 |
#' @param ... Further arguments to be passed to internal methods (see below). |
|
| 209 |
#' @details |
|
| 210 |
#' The following heterogeneity index are available (see Magurran 1988 for |
|
| 211 |
#' details): |
|
| 212 |
#' \describe{
|
|
| 213 |
#' \item{`berger`}{[Berger-Parker dominance index][index_berger()].}
|
|
| 214 |
#' \item{`boone`}{[Boone heterogeneity measure][index_boone()].}
|
|
| 215 |
#' \item{`brillouin`}{[Brillouin diversity index][index_brillouin()].}
|
|
| 216 |
#' \item{`mcintosh`}{[McIntosh dominance index][index_mcintosh()].}
|
|
| 217 |
#' \item{`shannon`}{[Shannon-Wiener diversity index][index_shannon()].}
|
|
| 218 |
#' \item{`simpson`}{[Simpson dominance index][index_simpson()].}
|
|
| 219 |
#' } |
|
| 220 |
#' |
|
| 221 |
#' The `berger`, `mcintosh` and `simpson` methods return a *dominance* index, |
|
| 222 |
#' not the reciprocal or inverse form usually adopted, so that an increase in |
|
| 223 |
#' the value of the index accompanies a decrease in diversity. |
|
| 224 |
#' @return |
|
| 225 |
#' An [HeterogeneityIndex-class] object. |
|
| 226 |
#' @seealso [index_berger()], [index_boone()], [index_brillouin()], |
|
| 227 |
#' [index_mcintosh()], [index_shannon()], [index_simpson()] |
|
| 228 |
#' @references |
|
| 229 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 230 |
#' Princeton, NJ: Princeton University Press. |
|
| 231 |
#' \doi{10.1007/978-94-015-7358-0}.
|
|
| 232 |
#' @example inst/examples/ex-diversity.R |
|
| 233 |
#' @author N. Frerebeau |
|
| 234 |
#' @family diversity measures |
|
| 235 |
#' @docType methods |
|
| 236 |
#' @aliases heterogeneity-method |
|
| 237 |
setGeneric( |
|
| 238 |
name = "heterogeneity", |
|
| 239 |
def = function(object, ...) standardGeneric("heterogeneity"),
|
|
| 240 |
valueClass = "HeterogeneityIndex" |
|
| 241 |
) |
|
| 242 | ||
| 243 |
#' Evenness |
|
| 244 |
#' |
|
| 245 |
#' Computes an evenness measure. |
|
| 246 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 247 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 248 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 249 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 250 |
#' @param method A [`character`] string specifying the index to be computed |
|
| 251 |
#' (see details). Any unambiguous substring can be given. |
|
| 252 |
#' @param ... Further arguments to be passed to internal methods (see below). |
|
| 253 |
#' @details |
|
| 254 |
#' *Evenness* is a measure of how evenly individuals are distributed across the |
|
| 255 |
#' sample. |
|
| 256 |
#' |
|
| 257 |
#' The following evenness measures are available (see Magurran 1988 for |
|
| 258 |
#' details): |
|
| 259 |
#' \describe{
|
|
| 260 |
#' \item{`brillouin`}{[Brillouin diversity index][index_brillouin()].}
|
|
| 261 |
#' \item{`mcintosh`}{[McIntosh dominance index][index_mcintosh()].}
|
|
| 262 |
#' \item{`shannon`}{[Shannon-Wiener diversity index][index_shannon()].}
|
|
| 263 |
#' \item{`simpson`}{[Simpson dominance index][index_simpson()].}
|
|
| 264 |
#' } |
|
| 265 |
#' @return |
|
| 266 |
#' An [EvennessIndex-class] object. |
|
| 267 |
#' @seealso [index_brillouin()], [index_mcintosh()], [index_shannon()], |
|
| 268 |
#' [index_simpson()] |
|
| 269 |
#' @references |
|
| 270 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 271 |
#' Princeton, NJ: Princeton University Press. |
|
| 272 |
#' \doi{10.1007/978-94-015-7358-0}.
|
|
| 273 |
#' @example inst/examples/ex-diversity.R |
|
| 274 |
#' @author N. Frerebeau |
|
| 275 |
#' @family diversity measures |
|
| 276 |
#' @docType methods |
|
| 277 |
#' @aliases evenness-method |
|
| 278 |
setGeneric( |
|
| 279 |
name = "evenness", |
|
| 280 |
def = function(object, ...) standardGeneric("evenness"),
|
|
| 281 |
valueClass = "EvennessIndex" |
|
| 282 |
) |
|
| 283 | ||
| 284 |
#' Berger-Parker Dominance Index |
|
| 285 |
#' |
|
| 286 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 287 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 288 |
#' removed? |
|
| 289 |
#' @param ... Currently not used. |
|
| 290 |
#' @details |
|
| 291 |
#' The Berger-Parker index expresses the proportional importance of the most |
|
| 292 |
#' abundant type. This metric is highly biased by sample size and richness, |
|
| 293 |
#' moreover it does not make use of all the information available from sample. |
|
| 294 |
#' |
|
| 295 |
#' This is a *dominance* index, so that an increase in the value of the index |
|
| 296 |
#' accompanies a decrease in diversity. |
|
| 297 |
#' @return |
|
| 298 |
#' A [`numeric`] vector. |
|
| 299 |
#' @references |
|
| 300 |
#' Berger, W. H. & Parker, F. L. (1970). Diversity of Planktonic Foraminifera |
|
| 301 |
#' in Deep-Sea Sediments. *Science*, 168(3937), 1345-1347. |
|
| 302 |
#' \doi{10.1126/science.168.3937.1345}.
|
|
| 303 |
#' @author N. Frerebeau |
|
| 304 |
#' @family alpha diversity measures |
|
| 305 |
#' @docType methods |
|
| 306 |
#' @aliases index_berger-method |
|
| 307 |
setGeneric( |
|
| 308 |
name = "index_berger", |
|
| 309 | 12x |
def = function(x, ...) standardGeneric("index_berger")
|
| 310 |
) |
|
| 311 | ||
| 312 |
#' Boone Heterogeneity Measure |
|
| 313 |
#' |
|
| 314 |
#' @param x A \eqn{m \times p}{m x p} `numeric` [`matrix`] of count data
|
|
| 315 |
#' (absolute frequencies, i.e. a contingency table). |
|
| 316 |
#' @param j An [`integer`] giving the index of the reference type/taxa. |
|
| 317 |
#' If `NULL` (the default), the most frequent type/taxa in any assemblage will |
|
| 318 |
#' be used. |
|
| 319 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 320 |
#' removed? |
|
| 321 |
#' @param ... Currently not used. |
|
| 322 |
#' @return |
|
| 323 |
#' A [`numeric`] vector. |
|
| 324 |
#' @references |
|
| 325 |
#' Boone, J. L. (1987). Defining and Measuring Midden Catchment. *American |
|
| 326 |
#' Antiquity*, 52(2), 336-45. \doi{10.2307/281785}.
|
|
| 327 |
#' |
|
| 328 |
#' Kintigh, K. W. (1989). Sample Size, Significance, and Measures of |
|
| 329 |
#' Diversity. In Leonard, R. D. and Jones, G. T., *Quantifying Diversity |
|
| 330 |
#' in Archaeology*. New Directions in Archaeology. Cambridge: |
|
| 331 |
#' Cambridge University Press, p. 25-36. |
|
| 332 |
#' @author N. Frerebeau |
|
| 333 |
#' @family alpha diversity measures |
|
| 334 |
#' @docType methods |
|
| 335 |
#' @aliases index_boone-method |
|
| 336 |
setGeneric( |
|
| 337 |
name = "index_boone", |
|
| 338 | ! |
def = function(x, ...) standardGeneric("index_boone")
|
| 339 |
) |
|
| 340 | ||
| 341 |
#' Brillouin Diversity Index. |
|
| 342 |
#' |
|
| 343 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 344 |
#' @param evenness A [`numeric`] scalar: should evenness be computed? |
|
| 345 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 346 |
#' removed? |
|
| 347 |
#' @param ... Currently not used. |
|
| 348 |
#' @details |
|
| 349 |
#' The Brillouin index describes a known collection: it does not assume random |
|
| 350 |
#' sampling in an infinite population. Pielou (1975) and Laxton (1978) argues |
|
| 351 |
#' for the use of the Brillouin index in all circumstances, especially in |
|
| 352 |
#' preference to the Shannon index. |
|
| 353 |
#' @note |
|
| 354 |
#' Ramanujan approximation is used for \eqn{x!} computation if \eqn{x > 170}.
|
|
| 355 |
#' @return |
|
| 356 |
#' A [`numeric`] vector. |
|
| 357 |
#' @references |
|
| 358 |
#' Brillouin, L. (1956). *Science and information theory*. New York: |
|
| 359 |
#' Academic Press. |
|
| 360 |
#' |
|
| 361 |
#' Laxton, R. R. (1978). The measure of diversity. *Journal of Theoretical |
|
| 362 |
#' Biology*, 70(1), 51-67. |
|
| 363 |
#' \doi{10.1016/0022-5193(78)90302-8}.
|
|
| 364 |
#' |
|
| 365 |
#' Pielou, E. C. (1975). *Ecological Diversity*. New York: Wiley. |
|
| 366 |
#' \doi{10.4319/lo.1977.22.1.0174b}
|
|
| 367 |
#' @author N. Frerebeau |
|
| 368 |
#' @family alpha diversity measures |
|
| 369 |
#' @docType methods |
|
| 370 |
#' @aliases index_brillouin-method |
|
| 371 |
setGeneric( |
|
| 372 |
name = "index_brillouin", |
|
| 373 | 18x |
def = function(x, ...) standardGeneric("index_brillouin")
|
| 374 |
) |
|
| 375 | ||
| 376 |
#' McIntosh Dominance Index. |
|
| 377 |
#' |
|
| 378 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 379 |
#' @param evenness A [`numeric`] scalar: should evenness be computed? |
|
| 380 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 381 |
#' removed? |
|
| 382 |
#' @param ... Currently not used. |
|
| 383 |
#' @details |
|
| 384 |
#' The McIntosh index expresses the heterogeneity of a sample in geometric |
|
| 385 |
#' terms. It describes the sample as a point of a \eqn{S}-dimensional
|
|
| 386 |
#' hypervolume and uses the Euclidean distance of this point from the origin. |
|
| 387 |
#' |
|
| 388 |
#' This is a *dominance* index, so that an increase in the value of the index |
|
| 389 |
#' accompanies a decrease in diversity. |
|
| 390 |
#' @return |
|
| 391 |
#' A [`numeric`] vector. |
|
| 392 |
#' @references |
|
| 393 |
#' McIntosh, R. P. (1967). An Index of Diversity and the Relation of Certain |
|
| 394 |
#' Concepts to Diversity. *Ecology*, 48(3), 392-404. |
|
| 395 |
#' \doi{10.2307/1932674}.
|
|
| 396 |
#' @author N. Frerebeau |
|
| 397 |
#' @family alpha diversity measures |
|
| 398 |
#' @docType methods |
|
| 399 |
#' @aliases index_mcintosh-method |
|
| 400 |
setGeneric( |
|
| 401 |
name = "index_mcintosh", |
|
| 402 | 12x |
def = function(x, ...) standardGeneric("index_mcintosh")
|
| 403 |
) |
|
| 404 | ||
| 405 |
#' Shannon-Wiener Diversity Index |
|
| 406 |
#' |
|
| 407 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 408 |
#' @param evenness A [`numeric`] scalar: should evenness be computed? |
|
| 409 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 410 |
#' used? |
|
| 411 |
#' @param ACE A [`logical`] scalar: should the ACE species richness estimator be |
|
| 412 |
#' used in the bias correction? |
|
| 413 |
#' @param base A positive [`numeric`] value specifying the base with respect to |
|
| 414 |
#' which logarithms are computed. |
|
| 415 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 416 |
#' removed? |
|
| 417 |
#' @param ... Currently not used. |
|
| 418 |
#' @details |
|
| 419 |
#' The Shannon index assumes that individuals are randomly sampled from an |
|
| 420 |
#' infinite population and that all taxa are represented in the sample (it |
|
| 421 |
#' does not reflect the sample size). The main source of error arises from the |
|
| 422 |
#' failure to include all taxa in the sample: this error increases as the |
|
| 423 |
#' proportion of species discovered in the sample declines (Peet 1974, |
|
| 424 |
#' Magurran 1988). The maximum likelihood estimator (MLE) is used for the |
|
| 425 |
#' relative abundance, this is known to be negatively biased by sample size. |
|
| 426 |
#' @return |
|
| 427 |
#' A [`numeric`] vector. |
|
| 428 |
#' @references |
|
| 429 |
#' Peet, R. K. (1974). The Measurement of Species Diversity. *Annual Review of |
|
| 430 |
#' Ecology and Systematics*, 5(1), 285-307. |
|
| 431 |
#' \doi{10.1146/annurev.es.05.110174.001441}.
|
|
| 432 |
#' |
|
| 433 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 434 |
#' Princeton, NJ: Princeton University Press. |
|
| 435 |
#' \doi{10.1007/978-94-015-7358-0}.
|
|
| 436 |
#' |
|
| 437 |
#' Shannon, C. E. (1948). A Mathematical Theory of Communication. *The |
|
| 438 |
#' Bell System Technical Journal*, 27, 379-423. |
|
| 439 |
#' \doi{10.1002/j.1538-7305.1948.tb01338.x}.
|
|
| 440 |
#' @author N. Frerebeau |
|
| 441 |
#' @family alpha diversity measures |
|
| 442 |
#' @docType methods |
|
| 443 |
#' @aliases index_shannon-method |
|
| 444 |
setGeneric( |
|
| 445 |
name = "index_shannon", |
|
| 446 | 487x |
def = function(x, ...) standardGeneric("index_shannon")
|
| 447 |
) |
|
| 448 | ||
| 449 |
#' Simpson Dominance Index |
|
| 450 |
#' |
|
| 451 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 452 |
#' @param evenness A [`numeric`] scalar: should evenness be computed? |
|
| 453 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 454 |
#' used? |
|
| 455 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 456 |
#' removed? |
|
| 457 |
#' @param ... Currently not used. |
|
| 458 |
#' @details |
|
| 459 |
#' The Simpson index expresses the probability that two individuals randomly |
|
| 460 |
#' picked from a finite sample belong to two different types. It can be |
|
| 461 |
#' interpreted as the weighted mean of the proportional abundances. This |
|
| 462 |
#' metric is a true probability value, it ranges from \eqn{0} (all taxa are
|
|
| 463 |
#' equally present) to \eqn{1} (one taxon dominates the community completely).
|
|
| 464 |
#' |
|
| 465 |
#' This is a *dominance* index, so that an increase in the value of the index |
|
| 466 |
#' accompanies a decrease in diversity. |
|
| 467 |
#' @return |
|
| 468 |
#' A [`numeric`] vector. |
|
| 469 |
#' @references |
|
| 470 |
#' Simpson, E. H. (1949). Measurement of Diversity. *Nature*, 163(4148), |
|
| 471 |
#' 688-688. \doi{10.1038/163688a0}.
|
|
| 472 |
#' @author N. Frerebeau |
|
| 473 |
#' @family alpha diversity measures |
|
| 474 |
#' @docType methods |
|
| 475 |
#' @aliases index_simpson-method |
|
| 476 |
setGeneric( |
|
| 477 |
name = "index_simpson", |
|
| 478 | 21x |
def = function(x, ...) standardGeneric("index_simpson")
|
| 479 |
) |
|
| 480 | ||
| 481 |
## Richness -------------------------------------------------------------------- |
|
| 482 |
#' Richness |
|
| 483 |
#' |
|
| 484 |
#' @description |
|
| 485 |
#' * `richness()` computes sample richness. |
|
| 486 |
#' * `composition()` computes asymptotic species richness. |
|
| 487 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 488 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 489 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 490 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 491 |
#' @param method A [`character`] string or vector of strings specifying the |
|
| 492 |
#' index to be computed (see details). Any unambiguous substring can be given. |
|
| 493 |
#' @param ... Further arguments to be passed to internal methods (see below). |
|
| 494 |
#' @section Details: |
|
| 495 |
#' The number of observed taxa, provides an instantly comprehensible |
|
| 496 |
#' expression of diversity. While the number of taxa within a sample |
|
| 497 |
#' is easy to ascertain, as a term, it makes little sense: some taxa |
|
| 498 |
#' may not have been seen, or there may not be a fixed number of taxa |
|
| 499 |
#' (e.g. in an open system; Peet 1974). As an alternative, *richness* |
|
| 500 |
#' (\eqn{S}) can be used for the concept of taxa number (McIntosh 1967).
|
|
| 501 |
#' |
|
| 502 |
#' It is not always possible to ensure that all sample sizes are equal |
|
| 503 |
#' and the number of different taxa increases with sample size and |
|
| 504 |
#' sampling effort (Magurran 1988). Then, *[rarefaction][rarefaction()]* |
|
| 505 |
#' (\eqn{E(S)}) is the number of taxa expected if all samples were of a
|
|
| 506 |
#' standard size (i.e. taxa per fixed number of individuals). |
|
| 507 |
#' Rarefaction assumes that imbalances between taxa are due to sampling and |
|
| 508 |
#' not to differences in actual abundances. |
|
| 509 |
#' @section Richness Measures: |
|
| 510 |
#' The following richness measures are available for count data: |
|
| 511 |
#' \describe{
|
|
| 512 |
#' \item{`observed`}{Number of observed taxa/types.}
|
|
| 513 |
#' \item{`margalef`}{[Margalef richness index][index_margalef()].}
|
|
| 514 |
#' \item{`menhinick`}{[Menhinick richness index][index_menhinick()].}
|
|
| 515 |
#' } |
|
| 516 |
#' |
|
| 517 |
#' @section Asymptotic Species Richness: |
|
| 518 |
#' The following measures are available for count data: |
|
| 519 |
#' \describe{
|
|
| 520 |
#' \item{`ace`}{[Abundance-based Coverage Estimator][index_ace()].}
|
|
| 521 |
#' \item{`chao1`}{(improved/unbiased) [Chao1 estimator][index_chao1()].}
|
|
| 522 |
#' \item{`squares`}{[Squares estimator][index_squares()].}
|
|
| 523 |
#' } |
|
| 524 |
#' |
|
| 525 |
#' The following measures are available for replicated incidence data: |
|
| 526 |
#' \describe{
|
|
| 527 |
#' \item{`ice`}{[Incidence-based Coverage Estimator][index_ice()].}
|
|
| 528 |
#' \item{`chao2`}{(improved/unbiased) [Chao2 estimator][index_chao2()].}
|
|
| 529 |
#' } |
|
| 530 |
#' @return |
|
| 531 |
#' * `richness()` returns a [RichnessIndex-class] object. |
|
| 532 |
#' * `composition()` returns a [CompositionIndex-class] object. |
|
| 533 |
#' @seealso [index_margalef()], [index_menhinick()], [index_ace()], |
|
| 534 |
#' [index_chao1()], [index_squares()], [index_ice()], [index_chao2()] |
|
| 535 |
#' @references |
|
| 536 |
#' Kintigh, K. W. (1989). Sample Size, Significance, and Measures of |
|
| 537 |
#' Diversity. In Leonard, R. D. and Jones, G. T., *Quantifying Diversity |
|
| 538 |
#' in Archaeology*. New Directions in Archaeology. Cambridge: |
|
| 539 |
#' Cambridge University Press, p. 25-36. |
|
| 540 |
#' |
|
| 541 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 542 |
#' Princeton, NJ: Princeton University Press. \doi{10.1007/978-94-015-7358-0}.
|
|
| 543 |
#' |
|
| 544 |
#' Magurran, A E. & Brian J. McGill (2011). *Biological Diversity: |
|
| 545 |
#' Frontiers in Measurement and Assessment*. Oxford: Oxford University Press. |
|
| 546 |
#' |
|
| 547 |
#' McIntosh, R. P. (1967). An Index of Diversity and the Relation of Certain |
|
| 548 |
#' Concepts to Diversity. *Ecology*, 48(3), 392-404. \doi{10.2307/1932674}.
|
|
| 549 |
#' |
|
| 550 |
#' Peet, R. K. (1974). The Measurement of Species Diversity. *Annual Review of |
|
| 551 |
#' Ecology and Systematics*, 5(1), 285-307. |
|
| 552 |
#' \doi{10.1146/annurev.es.05.110174.001441}.
|
|
| 553 |
#' @seealso [`plot()`][plot.DiversityIndex] |
|
| 554 |
#' @example inst/examples/ex-richness.R |
|
| 555 |
#' @author N. Frerebeau |
|
| 556 |
#' @family diversity measures |
|
| 557 |
#' @docType methods |
|
| 558 |
#' @aliases richness-method |
|
| 559 |
setGeneric( |
|
| 560 |
name = "richness", |
|
| 561 |
def = function(object, ...) standardGeneric("richness"),
|
|
| 562 |
valueClass = "RichnessIndex" |
|
| 563 |
) |
|
| 564 | ||
| 565 |
#' @rdname richness |
|
| 566 |
#' @aliases composition-method |
|
| 567 |
setGeneric( |
|
| 568 |
name = "composition", |
|
| 569 |
def = function(object, ...) standardGeneric("composition"),
|
|
| 570 |
valueClass = "CompositionIndex" |
|
| 571 |
) |
|
| 572 | ||
| 573 |
#' Number of Observed Species |
|
| 574 |
#' |
|
| 575 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 576 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 577 |
#' removed? |
|
| 578 |
#' @param ... Currently not used. |
|
| 579 |
#' @return |
|
| 580 |
#' A [`numeric`] vector. |
|
| 581 |
#' @family alpha diversity measures |
|
| 582 |
#' @docType methods |
|
| 583 |
#' @aliases observed-method |
|
| 584 |
setGeneric( |
|
| 585 |
name = "observed", |
|
| 586 | 5x |
def = function(x, ...) standardGeneric("observed")
|
| 587 |
) |
|
| 588 | ||
| 589 |
#' @rdname observed |
|
| 590 |
#' @aliases singleton-method |
|
| 591 |
setGeneric( |
|
| 592 |
name = "singleton", |
|
| 593 | ! |
def = function(x, ...) standardGeneric("singleton")
|
| 594 |
) |
|
| 595 | ||
| 596 |
#' @rdname observed |
|
| 597 |
#' @aliases doubleton-method |
|
| 598 |
setGeneric( |
|
| 599 |
name = "doubleton", |
|
| 600 | ! |
def = function(x, ...) standardGeneric("doubleton")
|
| 601 |
) |
|
| 602 | ||
| 603 |
#' Abundance-based Coverage Estimator |
|
| 604 |
#' |
|
| 605 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 606 |
#' @param k A length-one [`numeric`] vector giving the threshold between |
|
| 607 |
#' rare/infrequent and abundant/frequent species. |
|
| 608 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 609 |
#' removed? |
|
| 610 |
#' @param ... Currently not used. |
|
| 611 |
#' @return |
|
| 612 |
#' A [`numeric`] vector. |
|
| 613 |
#' @references |
|
| 614 |
#' Chao, A. & Lee, S.-M. (1992). Estimating the Number of Classes via Sample |
|
| 615 |
#' Coverage. *Journal of the American Statistical Association*, 87(417), |
|
| 616 |
#' 210-217. \doi{10.1080/01621459.1992.10475194}.
|
|
| 617 |
#' @author N. Frerebeau |
|
| 618 |
#' @family alpha diversity measures |
|
| 619 |
#' @docType methods |
|
| 620 |
#' @aliases index_ace-method |
|
| 621 |
setGeneric( |
|
| 622 |
name = "index_ace", |
|
| 623 | 8x |
def = function(x, ...) standardGeneric("index_ace")
|
| 624 |
) |
|
| 625 | ||
| 626 |
#' Chao1 Estimator |
|
| 627 |
#' |
|
| 628 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 629 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 630 |
#' used? |
|
| 631 |
#' @param improved A [`logical`] scalar: should the improved estimator be used? |
|
| 632 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 633 |
#' removed? |
|
| 634 |
#' @param ... Currently not used. |
|
| 635 |
#' @return |
|
| 636 |
#' A [`numeric`] vector. |
|
| 637 |
#' @references |
|
| 638 |
#' Chao, A. (1984). Nonparametric Estimation of the Number of Classes in a |
|
| 639 |
#' Population. *Scandinavian Journal of Statistics*, 11(4), 265-270. |
|
| 640 |
#' |
|
| 641 |
#' Chiu, C.-H., Wang, Y.-T., Walther, B. A. & Chao, A. (2014). An improved |
|
| 642 |
#' nonparametric lower bound of species richness via a modified good-turing |
|
| 643 |
#' frequency formula. *Biometrics*, 70(3), 671-682. |
|
| 644 |
#' \doi{10.1111/biom.12200}.
|
|
| 645 |
#' @author N. Frerebeau |
|
| 646 |
#' @family alpha diversity measures |
|
| 647 |
#' @docType methods |
|
| 648 |
#' @aliases index_chao1-method |
|
| 649 |
setGeneric( |
|
| 650 |
name = "index_chao1", |
|
| 651 | 13x |
def = function(x, ...) standardGeneric("index_chao1")
|
| 652 |
) |
|
| 653 | ||
| 654 |
#' Chao2 Estimator |
|
| 655 |
#' |
|
| 656 |
#' @param x A \eqn{m \times p}{m x p} [`matrix`] of presence/absence data
|
|
| 657 |
#' (incidence). |
|
| 658 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 659 |
#' used? |
|
| 660 |
#' @param improved A [`logical`] scalar: should the improved estimator be used? |
|
| 661 |
#' @param ... Currently not used. |
|
| 662 |
#' @return |
|
| 663 |
#' A [`numeric`] vector. |
|
| 664 |
#' @references |
|
| 665 |
#' Chao, A. (1987). Estimating the Population Size for Capture-Recapture Data |
|
| 666 |
#' with Unequal Catchability. *Biometrics* 43(4), 783-791. |
|
| 667 |
#' |
|
| 668 |
#' Chiu, C.-H., Wang, Y.-T., Walther, B. A. & Chao, A. (2014). An improved |
|
| 669 |
#' nonparametric lower bound of species richness via a modified good-turing |
|
| 670 |
#' frequency formula. *Biometrics*, 70(3), 671-682. |
|
| 671 |
#' \doi{10.2307/2531532}.
|
|
| 672 |
#' @author N. Frerebeau |
|
| 673 |
#' @family alpha diversity measures |
|
| 674 |
#' @docType methods |
|
| 675 |
#' @aliases index_chao2-method |
|
| 676 |
setGeneric( |
|
| 677 |
name = "index_chao2", |
|
| 678 | 6x |
def = function(x, ...) standardGeneric("index_chao2")
|
| 679 |
) |
|
| 680 | ||
| 681 |
#' Incidence-based Coverage Estimator |
|
| 682 |
#' |
|
| 683 |
#' @param x A \eqn{m \times p}{m x p} [`matrix`] of presence/absence data
|
|
| 684 |
#' (incidence). |
|
| 685 |
#' @param k A length-one [`numeric`] vector giving the threshold between |
|
| 686 |
#' rare/infrequent and abundant/frequent species. |
|
| 687 |
#' @param ... Currently not used. |
|
| 688 |
#' @return |
|
| 689 |
#' A [`numeric`] vector. |
|
| 690 |
#' @references |
|
| 691 |
#' Chao, A. & Chiu, C.-H. (2016). Species Richness: Estimation and Comparison. |
|
| 692 |
#' *In* Balakrishnan, N., Colton, T., Everitt, B., Piegorsch, B., Ruggeri, |
|
| 693 |
#' F. & Teugels, J. L. (Eds.), *Wiley StatsRef: Statistics Reference Online*. |
|
| 694 |
#' Chichester, UK: John Wiley & Sons, Ltd., 1-26. |
|
| 695 |
#' \doi{10.1002/9781118445112.stat03432.pub2}
|
|
| 696 |
#' @author N. Frerebeau |
|
| 697 |
#' @family alpha diversity measures |
|
| 698 |
#' @docType methods |
|
| 699 |
#' @aliases index_ice-method |
|
| 700 |
setGeneric( |
|
| 701 |
name = "index_ice", |
|
| 702 | 2x |
def = function(x, ...) standardGeneric("index_ice")
|
| 703 |
) |
|
| 704 | ||
| 705 |
#' Margalef Richness Index |
|
| 706 |
#' |
|
| 707 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 708 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 709 |
#' removed? |
|
| 710 |
#' @param ... Currently not used. |
|
| 711 |
#' @return |
|
| 712 |
#' A [`numeric`] vector. |
|
| 713 |
#' @references |
|
| 714 |
#' Margalef, R. (1958). Information Theory in Ecology. *General Systems*, |
|
| 715 |
#' 3, 36-71. |
|
| 716 |
#' @author N. Frerebeau |
|
| 717 |
#' @family alpha diversity measures |
|
| 718 |
#' @docType methods |
|
| 719 |
#' @aliases index_margalef-method |
|
| 720 |
setGeneric( |
|
| 721 |
name = "index_margalef", |
|
| 722 | 12x |
def = function(x, ...) standardGeneric("index_margalef")
|
| 723 |
) |
|
| 724 | ||
| 725 |
#' Menhinick Richness Index |
|
| 726 |
#' |
|
| 727 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 728 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 729 |
#' removed? |
|
| 730 |
#' @param ... Currently not used. |
|
| 731 |
#' @return |
|
| 732 |
#' A [`numeric`] vector. |
|
| 733 |
#' @references |
|
| 734 |
#' Menhinick, E. F. (1964). A Comparison of Some Species-Individuals Diversity |
|
| 735 |
#' Indices Applied to Samples of Field Insects. *Ecology*, 45(4), 859-861. |
|
| 736 |
#' \doi{10.2307/1934933}.
|
|
| 737 |
#' @author N. Frerebeau |
|
| 738 |
#' @family alpha diversity measures |
|
| 739 |
#' @docType methods |
|
| 740 |
#' @aliases index_menhinick-method |
|
| 741 |
setGeneric( |
|
| 742 |
name = "index_menhinick", |
|
| 743 | 12x |
def = function(x, ...) standardGeneric("index_menhinick")
|
| 744 |
) |
|
| 745 | ||
| 746 |
#' Squares Estimator |
|
| 747 |
#' |
|
| 748 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 749 |
#' @param na.rm A [`numeric`] scalar: should missing values (including `NaN`) be |
|
| 750 |
#' removed? |
|
| 751 |
#' @param ... Currently not used. |
|
| 752 |
#' @return |
|
| 753 |
#' A [`numeric`] vector. |
|
| 754 |
#' @references |
|
| 755 |
#' Alroy, J. (2018). Limits to Species Richness in Terrestrial Communities. |
|
| 756 |
#' *Ecology Letters*, 21(12), 1781-1789. \doi{10.1111/ele.13152}.
|
|
| 757 |
#' @author N. Frerebeau |
|
| 758 |
#' @family alpha diversity measures |
|
| 759 |
#' @docType methods |
|
| 760 |
#' @aliases index_squares-method |
|
| 761 |
setGeneric( |
|
| 762 |
name = "index_squares", |
|
| 763 | 6x |
def = function(x, ...) standardGeneric("index_squares")
|
| 764 |
) |
|
| 765 | ||
| 766 |
## Rarefaction ----------------------------------------------------------------- |
|
| 767 |
#' Rarefaction |
|
| 768 |
#' |
|
| 769 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 770 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 771 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 772 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 773 |
#' @param sample A length-one [`numeric`] vector giving the sub-sample size. |
|
| 774 |
#' The size of sample should be smaller than total community size. |
|
| 775 |
#' @param method A [`character`] string or vector of strings specifying the |
|
| 776 |
#' index to be computed (see details). Any unambiguous substring can be given. |
|
| 777 |
#' @param step An [`integer`] giving the increment of the sample size. |
|
| 778 |
#' @param ... Currently not used. |
|
| 779 |
#' @inheritSection richness Details |
|
| 780 |
#' @section Rarefaction Measures: |
|
| 781 |
#' The following rarefaction measures are available for count data: |
|
| 782 |
#' \describe{
|
|
| 783 |
#' \item{`baxter`}{[Baxter's rarefaction][index_baxter()].}
|
|
| 784 |
#' \item{`hurlbert`}{[Hurlbert's unbiased estimate][index_hurlbert()] of
|
|
| 785 |
#' Sander's rarefaction.} |
|
| 786 |
#' } |
|
| 787 |
#' @return |
|
| 788 |
#' A [RarefactionIndex-class] object. |
|
| 789 |
#' @example inst/examples/ex-rarefaction.R |
|
| 790 |
#' @seealso [index_baxter()], [index_hurlbert()], [`plot()`][plot.RarefactionIndex] |
|
| 791 |
#' @author N. Frerebeau |
|
| 792 |
#' @family diversity measures |
|
| 793 |
#' @docType methods |
|
| 794 |
#' @aliases rarefaction-method |
|
| 795 |
setGeneric( |
|
| 796 |
name = "rarefaction", |
|
| 797 | 3x |
def = function(object, ...) standardGeneric("rarefaction")
|
| 798 |
) |
|
| 799 | ||
| 800 |
#' Baxter's Rarefaction |
|
| 801 |
#' |
|
| 802 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 803 |
#' @param sample A length-one [`numeric`] vector giving the sub-sample size. |
|
| 804 |
#' The size of sample should be smaller than total community size. |
|
| 805 |
#' @param ... Currently not used. |
|
| 806 |
#' @return |
|
| 807 |
#' A [`numeric`] vector. |
|
| 808 |
#' @references |
|
| 809 |
#' Baxter, M. J. (2001). Methodological Issues in the Study of Assemblage |
|
| 810 |
#' Diversity. *American Antiquity*, 66(4), 715-725. \doi{10.2307/2694184}.
|
|
| 811 |
#' @author N. Frerebeau |
|
| 812 |
#' @family alpha diversity measures |
|
| 813 |
#' @docType methods |
|
| 814 |
#' @aliases index_baxter-method |
|
| 815 |
setGeneric( |
|
| 816 |
name = "index_baxter", |
|
| 817 | 116x |
def = function(x, ...) standardGeneric("index_baxter")
|
| 818 |
) |
|
| 819 | ||
| 820 |
#' Hurlbert's Rarefaction |
|
| 821 |
#' |
|
| 822 |
#' Hurlbert's unbiased estimate of Sander's rarefaction. |
|
| 823 |
#' @param x A [`numeric`] vector of count data (absolute frequencies). |
|
| 824 |
#' @param sample A length-one [`numeric`] vector giving the sub-sample size. |
|
| 825 |
#' The size of sample should be smaller than total community size. |
|
| 826 |
#' @param ... Currently not used. |
|
| 827 |
#' @return |
|
| 828 |
#' A [`numeric`] vector. |
|
| 829 |
#' @references |
|
| 830 |
#' Hurlbert, S. H. (1971). The Nonconcept of Species Diversity: A Critique and |
|
| 831 |
#' Alternative Parameters. *Ecology*, 52(4), 577-586. |
|
| 832 |
#' \doi{10.2307/1934145}.
|
|
| 833 |
#' |
|
| 834 |
#' Sander, H. L. (1968). Marine Benthic Diversity: A Comparative Study. |
|
| 835 |
#' *The American Naturalist*, 102(925), 243-282. |
|
| 836 |
#' @author N. Frerebeau |
|
| 837 |
#' @family alpha diversity measures |
|
| 838 |
#' @docType methods |
|
| 839 |
#' @aliases index_hurlbert-method |
|
| 840 |
setGeneric( |
|
| 841 |
name = "index_hurlbert", |
|
| 842 | 27x |
def = function(x, ...) standardGeneric("index_hurlbert")
|
| 843 |
) |
|
| 844 | ||
| 845 |
## Similarity ------------------------------------------------------------------ |
|
| 846 |
#' Similarity |
|
| 847 |
#' |
|
| 848 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 849 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 850 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 851 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 852 |
#' @param method A [`character`] string specifying the method to be |
|
| 853 |
#' used (see details). Any unambiguous substring can be given. |
|
| 854 |
#' @param ... Currently not used. |
|
| 855 |
#' @details |
|
| 856 |
#' \eqn{\beta}-diversity can be measured by addressing *similarity*
|
|
| 857 |
#' between pairs of samples/cases. |
|
| 858 |
#' |
|
| 859 |
#' `bray`, `jaccard`, `morisita` and `sorensen` indices provide a scale of |
|
| 860 |
#' similarity from \eqn{0}-\eqn{1} where \eqn{1} is perfect similarity and
|
|
| 861 |
#' \eqn{0} is no similarity.
|
|
| 862 |
#' `brainerd` is scaled between \eqn{0} and \eqn{200}.
|
|
| 863 |
#' |
|
| 864 |
#' \describe{
|
|
| 865 |
#' \item{`brainerd`}{[Brainerd-Robinson quantitative index][index_brainerd()].}
|
|
| 866 |
#' \item{`bray`}{[Bray-Curtis similarity (a.k.a. Dice-Sorensen quantitative index)][index_bray()].}
|
|
| 867 |
#' \item{`jaccard`}{[Jaccard qualitative index][index_jaccard()].}
|
|
| 868 |
#' \item{`morisita`}{[Morisita-Horn quantitative index][index_morisita()].}
|
|
| 869 |
#' \item{`sorensen`}{[Dice-Sorensen qualitative index][index_sorensen()].}
|
|
| 870 |
#' } |
|
| 871 |
#' |
|
| 872 |
#' For `jaccard` and `sorensen`, data are standardized on a presence/absence |
|
| 873 |
#' scale (\eqn{0}/\eqn{1}) beforehand.
|
|
| 874 |
#' @return |
|
| 875 |
#' A [stats::dist] object. |
|
| 876 |
#' @seealso [index_binomial()], [index_brainerd()], [index_bray()], |
|
| 877 |
#' [index_jaccard()], [index_morisita()], [index_sorensen()] |
|
| 878 |
#' @references |
|
| 879 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 880 |
#' Princeton, NJ: Princeton University Press. \doi{10.1007/978-94-015-7358-0}.
|
|
| 881 |
#' @example inst/examples/ex-similarity.R |
|
| 882 |
#' @author N. Frerebeau |
|
| 883 |
#' @family diversity measures |
|
| 884 |
#' @docType methods |
|
| 885 |
#' @aliases similarity-method |
|
| 886 |
setGeneric( |
|
| 887 |
name = "similarity", |
|
| 888 | 2x |
def = function(object, ...) standardGeneric("similarity")
|
| 889 |
) |
|
| 890 | ||
| 891 |
#' Jaccard Index |
|
| 892 |
#' |
|
| 893 |
#' @param x,y A [`numeric`] vector. |
|
| 894 |
#' @param ... Currently not used. |
|
| 895 |
#' @details |
|
| 896 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 897 |
#' beforehand. |
|
| 898 |
#' @return |
|
| 899 |
#' A [`numeric`] vector. |
|
| 900 |
#' @references |
|
| 901 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 902 |
#' Princeton, NJ: Princeton University Press. \doi{10.1007/978-94-015-7358-0}.
|
|
| 903 |
#' @author N. Frerebeau |
|
| 904 |
#' @family beta diversity measures |
|
| 905 |
#' @docType methods |
|
| 906 |
#' @aliases index_jaccard-method |
|
| 907 |
setGeneric( |
|
| 908 |
name = "index_jaccard", |
|
| 909 | 3x |
def = function(x, y, ...) standardGeneric("index_jaccard")
|
| 910 |
) |
|
| 911 | ||
| 912 |
#' Dice-Sorensen Qualitative Index |
|
| 913 |
#' |
|
| 914 |
#' @param x,y A [`numeric`] vector. |
|
| 915 |
#' @param ... Currently not used. |
|
| 916 |
#' @details |
|
| 917 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 918 |
#' beforehand. |
|
| 919 |
#' @return |
|
| 920 |
#' A [`numeric`] vector. |
|
| 921 |
#' @references |
|
| 922 |
#' Dice, L. R. (1945). Measures of the Amount of Ecologic Association Between |
|
| 923 |
#' Species. *Ecology*, 26(3): 297-302. \doi{10.2307/1932409}
|
|
| 924 |
#' |
|
| 925 |
#' Sorensen, T. (1948). A Method of Establishing Groups of Equal Amplitude in |
|
| 926 |
#' Plant Sociology Based on Similarity of Species Content and Its Application |
|
| 927 |
#' to Analyses of the Vegetation on Danish Commons. *Kongelige Danske |
|
| 928 |
#' Videnskabernes Selskab*, 5(4): 1-34. |
|
| 929 |
#' @author N. Frerebeau |
|
| 930 |
#' @family beta diversity measures |
|
| 931 |
#' @docType methods |
|
| 932 |
#' @aliases index_sorensen-method |
|
| 933 |
setGeneric( |
|
| 934 |
name = "index_sorensen", |
|
| 935 | 2x |
def = function(x, y, ...) standardGeneric("index_sorensen")
|
| 936 |
) |
|
| 937 | ||
| 938 |
#' Bray-Curtis Similarity |
|
| 939 |
#' |
|
| 940 |
#' Bray and Curtis modified version of the Dice-Sorensen index. |
|
| 941 |
#' @param x,y A [`numeric`] vector. |
|
| 942 |
#' @param ... Currently not used. |
|
| 943 |
#' @return |
|
| 944 |
#' A [`numeric`] vector. |
|
| 945 |
#' @references |
|
| 946 |
#' Bray, J. R. & Curtis, J. T. (1957). An Ordination of the Upland Forest |
|
| 947 |
#' Communities of Southern Wisconsin. *Ecological Monographs*, 27(4), |
|
| 948 |
#' 325-349. \doi{10.2307/1942268}.
|
|
| 949 |
#' @author N. Frerebeau |
|
| 950 |
#' @family beta diversity measures |
|
| 951 |
#' @docType methods |
|
| 952 |
#' @aliases index_bray-method |
|
| 953 |
setGeneric( |
|
| 954 |
name = "index_bray", |
|
| 955 | 1x |
def = function(x, y, ...) standardGeneric("index_bray")
|
| 956 |
) |
|
| 957 | ||
| 958 |
#' Morisita-Horn Quantitative Index |
|
| 959 |
#' |
|
| 960 |
#' Horn modified version of the Morisita overlap index. |
|
| 961 |
#' @param x,y A [`numeric`] vector. |
|
| 962 |
#' @param ... Currently not used. |
|
| 963 |
#' @return |
|
| 964 |
#' A [`numeric`] vector. |
|
| 965 |
#' @references |
|
| 966 |
#' Horn, H. S. (1966). Measurement of "Overlap" in Comparative Ecological |
|
| 967 |
#' Studies. *The American Naturalist*, 100(914): 419-424. \doi{10.1086/282436}.
|
|
| 968 |
#' |
|
| 969 |
#' Mosrisita, M. (1959). Measuring of interspecific association and similarity |
|
| 970 |
#' between communities. *Memoirs of the Faculty of Science, Kyushu University*, |
|
| 971 |
#' Series E, 3:65-80. |
|
| 972 |
#' @author N. Frerebeau |
|
| 973 |
#' @family beta diversity measures |
|
| 974 |
#' @docType methods |
|
| 975 |
#' @aliases index_morisita-method |
|
| 976 |
setGeneric( |
|
| 977 |
name = "index_morisita", |
|
| 978 | 1x |
def = function(x, y, ...) standardGeneric("index_morisita")
|
| 979 |
) |
|
| 980 | ||
| 981 |
#' Brainerd-Robinson Quantitative Index |
|
| 982 |
#' |
|
| 983 |
#' @param x,y A [`numeric`] vector. |
|
| 984 |
#' @param ... Currently not used. |
|
| 985 |
#' @details |
|
| 986 |
#' A city-block metric of similarity between pairs of samples/cases. |
|
| 987 |
#' @return |
|
| 988 |
#' A [`numeric`] vector. |
|
| 989 |
#' @references |
|
| 990 |
#' Brainerd, G. W. (1951). The Place of Chronological Ordering in |
|
| 991 |
#' Archaeological Analysis. *American Antiquity*, 16(04), 301-313. |
|
| 992 |
#' \doi{10.2307/276979}.
|
|
| 993 |
#' |
|
| 994 |
#' Robinson, W. S. (1951). A Method for Chronologically Ordering Archaeological |
|
| 995 |
#' Deposits. *American Antiquity*, 16(04), 293-301. \doi{10.2307/276978}.
|
|
| 996 |
#' @author N. Frerebeau |
|
| 997 |
#' @family beta diversity measures |
|
| 998 |
#' @docType methods |
|
| 999 |
#' @aliases index_brainerd-method |
|
| 1000 |
setGeneric( |
|
| 1001 |
name = "index_brainerd", |
|
| 1002 | 37x |
def = function(x, y, ...) standardGeneric("index_brainerd")
|
| 1003 |
) |
|
| 1004 | ||
| 1005 |
## Turnover -------------------------------------------------------------------- |
|
| 1006 |
#' Turnover |
|
| 1007 |
#' |
|
| 1008 |
#' Returns the degree of turnover in taxa composition along a gradient or |
|
| 1009 |
#' transect. |
|
| 1010 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1011 |
#' [`data.frame`] of count data or incidence data. A [`data.frame`] |
|
| 1012 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1013 |
#' @param method A [`character`] string specifying the method to be |
|
| 1014 |
#' used (see details). Any unambiguous substring can be given. |
|
| 1015 |
#' @param ... Further arguments to be passed to internal methods. |
|
| 1016 |
#' @details |
|
| 1017 |
#' The following methods can be used to ascertain the degree of *turnover* |
|
| 1018 |
#' in taxa composition along a gradient (\eqn{\beta}-diversity) on qualitative
|
|
| 1019 |
#' (presence/absence) data: |
|
| 1020 |
#' |
|
| 1021 |
#' \describe{
|
|
| 1022 |
#' \item{`cody`}{[Cody measure][index_cody()].}
|
|
| 1023 |
#' \item{`routledge1`}{[Routledge first measure][index_routledge].}
|
|
| 1024 |
#' \item{`routledge2`}{[Routledge second measure][index_routledge].}
|
|
| 1025 |
#' \item{`routledge3`}{[Routledge third measure][index_routledge] (exponential
|
|
| 1026 |
#' form of the second measure).} |
|
| 1027 |
#' \item{`whittaker`}{[Whittaker measure][index_whittaker()].}
|
|
| 1028 |
#' \item{`wilson`}{[Wilson measure][index_wilson()].}
|
|
| 1029 |
#' } |
|
| 1030 |
#' |
|
| 1031 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1032 |
#' follows the progression along the gradient/transect. |
|
| 1033 |
#' |
|
| 1034 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 1035 |
#' beforehand. |
|
| 1036 |
#' @return |
|
| 1037 |
#' A [`numeric`] vector. |
|
| 1038 |
#' @seealso [index_cody()], [index_routledge1()], [index_routledge2()], |
|
| 1039 |
#' [index_routledge3()], [index_whittaker()], [index_wilson()] |
|
| 1040 |
#' @example inst/examples/ex-turnover.R |
|
| 1041 |
#' @author N. Frerebeau |
|
| 1042 |
#' @family diversity measures |
|
| 1043 |
#' @docType methods |
|
| 1044 |
#' @aliases turnover-method |
|
| 1045 |
setGeneric( |
|
| 1046 |
name = "turnover", |
|
| 1047 | 6x |
def = function(object, ...) standardGeneric("turnover")
|
| 1048 |
) |
|
| 1049 | ||
| 1050 |
#' Cody Measure |
|
| 1051 |
#' |
|
| 1052 |
#' @param x A \eqn{m \times p}{m x p} `numeric` [`matrix`] of count data
|
|
| 1053 |
#' (absolute frequencies, i.e. a contingency table). |
|
| 1054 |
#' @param ... Currently not used. |
|
| 1055 |
#' @details |
|
| 1056 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1057 |
#' follows the progression along the gradient/transect. |
|
| 1058 |
#' |
|
| 1059 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 1060 |
#' beforehand. |
|
| 1061 |
#' @return |
|
| 1062 |
#' A [`numeric`] vector. |
|
| 1063 |
#' @references |
|
| 1064 |
#' Cody, M. L. (1975). Towards a theory of continental species diversity: Bird |
|
| 1065 |
#' distributions over Mediterranean habitat gradients. *In* M. L. Cody & |
|
| 1066 |
#' J. M. Diamond (Eds.), *Ecology and Evolution of Communities*. |
|
| 1067 |
#' Cambridge, MA: Harvard University Press, p. 214-257. |
|
| 1068 |
#' @author N. Frerebeau |
|
| 1069 |
#' @family beta diversity measures |
|
| 1070 |
#' @docType methods |
|
| 1071 |
#' @aliases index_cody-method |
|
| 1072 |
setGeneric( |
|
| 1073 |
name = "index_cody", |
|
| 1074 | 2x |
def = function(x, ...) standardGeneric("index_cody")
|
| 1075 |
) |
|
| 1076 | ||
| 1077 |
#' Routledge Measures |
|
| 1078 |
#' |
|
| 1079 |
#' @param x A \eqn{m \times p}{m x p} `numeric` [`matrix`] of count data
|
|
| 1080 |
#' (absolute frequencies, i.e. a contingency table). |
|
| 1081 |
#' @param ... Currently not used. |
|
| 1082 |
#' @details |
|
| 1083 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1084 |
#' follows the progression along the gradient/transect. |
|
| 1085 |
#' |
|
| 1086 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 1087 |
#' beforehand. |
|
| 1088 |
#' @return |
|
| 1089 |
#' A [`numeric`] vector. |
|
| 1090 |
#' @references |
|
| 1091 |
#' Routledge, R. D. (1977). On Whittaker's Components of Diversity. |
|
| 1092 |
#' *Ecology*, 58(5), 1120-1127. \doi{10.2307/1936932}.
|
|
| 1093 |
#' @author N. Frerebeau |
|
| 1094 |
#' @family beta diversity measures |
|
| 1095 |
#' @docType methods |
|
| 1096 |
#' @name index_routledge |
|
| 1097 |
#' @rdname index_routledge |
|
| 1098 |
NULL |
|
| 1099 | ||
| 1100 |
#' @rdname index_routledge |
|
| 1101 |
#' @aliases index_routledge1-method |
|
| 1102 |
setGeneric( |
|
| 1103 |
name = "index_routledge1", |
|
| 1104 | 2x |
def = function(x, ...) standardGeneric("index_routledge1")
|
| 1105 |
) |
|
| 1106 | ||
| 1107 |
#' @rdname index_routledge |
|
| 1108 |
#' @aliases index_routledge2-method |
|
| 1109 |
setGeneric( |
|
| 1110 |
name = "index_routledge2", |
|
| 1111 | 4x |
def = function(x, ...) standardGeneric("index_routledge2")
|
| 1112 |
) |
|
| 1113 | ||
| 1114 |
#' @rdname index_routledge |
|
| 1115 |
#' @aliases index_routledge3-method |
|
| 1116 |
setGeneric( |
|
| 1117 |
name = "index_routledge3", |
|
| 1118 | 2x |
def = function(x, ...) standardGeneric("index_routledge3")
|
| 1119 |
) |
|
| 1120 | ||
| 1121 |
#' Whittaker Measure |
|
| 1122 |
#' |
|
| 1123 |
#' @param x A \eqn{m \times p}{m x p} `numeric` [`matrix`] of count data
|
|
| 1124 |
#' (absolute frequencies, i.e. a contingency table). |
|
| 1125 |
#' @param ... Currently not used. |
|
| 1126 |
#' @details |
|
| 1127 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1128 |
#' follows the progression along the gradient/transect. |
|
| 1129 |
#' |
|
| 1130 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 1131 |
#' beforehand. |
|
| 1132 |
#' @return |
|
| 1133 |
#' A [`numeric`] vector. |
|
| 1134 |
#' @references |
|
| 1135 |
#' Whittaker, R. H. (1960). Vegetation of the Siskiyou Mountains, Oregon and |
|
| 1136 |
#' California. *Ecological Monographs*, 30(3), 279-338. |
|
| 1137 |
#' \doi{10.2307/1943563}.
|
|
| 1138 |
#' @author N. Frerebeau |
|
| 1139 |
#' @family beta diversity measures |
|
| 1140 |
#' @docType methods |
|
| 1141 |
#' @aliases index_whittaker-method |
|
| 1142 |
setGeneric( |
|
| 1143 |
name = "index_whittaker", |
|
| 1144 | 2x |
def = function(x, ...) standardGeneric("index_whittaker")
|
| 1145 |
) |
|
| 1146 | ||
| 1147 |
#' Wilson Measure |
|
| 1148 |
#' |
|
| 1149 |
#' @param x A \eqn{m \times p}{m x p} `numeric` [`matrix`] of count data
|
|
| 1150 |
#' (absolute frequencies, i.e. a contingency table). |
|
| 1151 |
#' @param ... Currently not used. |
|
| 1152 |
#' @details |
|
| 1153 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1154 |
#' follows the progression along the gradient/transect. |
|
| 1155 |
#' |
|
| 1156 |
#' Data are standardized on a presence/absence scale (\eqn{0}/\eqn{1})
|
|
| 1157 |
#' beforehand. |
|
| 1158 |
#' @return |
|
| 1159 |
#' A [`numeric`] vector. |
|
| 1160 |
#' @references |
|
| 1161 |
#' Wilson, M. V., & Shmida, A. (1984). Measuring Beta Diversity with |
|
| 1162 |
#' Presence-Absence Data. *The Journal of Ecology*, 72(3), 1055-1064. |
|
| 1163 |
#' \doi{10.2307/2259551}.
|
|
| 1164 |
#' @author N. Frerebeau |
|
| 1165 |
#' @family beta diversity measures |
|
| 1166 |
#' @docType methods |
|
| 1167 |
#' @aliases index_wilson-method |
|
| 1168 |
setGeneric( |
|
| 1169 |
name = "index_wilson", |
|
| 1170 | 2x |
def = function(x, ...) standardGeneric("index_wilson")
|
| 1171 |
) |
|
| 1172 | ||
| 1173 |
## Co-Occurrence --------------------------------------------------------------- |
|
| 1174 |
#' Co-Occurrence |
|
| 1175 |
#' |
|
| 1176 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1177 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1178 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1179 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1180 |
#' @param method A [`character`] string specifying the method to be |
|
| 1181 |
#' used. It must be one of "`absolute`", "`relative`" or "`binomial`" |
|
| 1182 |
#' (see details). Any unambiguous substring can be given. |
|
| 1183 |
#' @param ... Currently not used. |
|
| 1184 |
#' @details |
|
| 1185 |
#' \describe{
|
|
| 1186 |
#' \item{`absolute`}{Count how many times each pairs of types occur together
|
|
| 1187 |
#' in at least one sample (absolute frequencies).} |
|
| 1188 |
#' \item{`relative`}{Count how many times each pairs of types occur together
|
|
| 1189 |
#' in at least one sample (relative frequencies).} |
|
| 1190 |
#' \item{`binomial`}{[Binomial co-occurrence assessment][index_binomial()].}
|
|
| 1191 |
#' } |
|
| 1192 |
#' @return |
|
| 1193 |
#' A [stats::dist] object. |
|
| 1194 |
#' @example inst/examples/ex-occurrence.R |
|
| 1195 |
#' @author N. Frerebeau |
|
| 1196 |
#' @family diversity measures |
|
| 1197 |
#' @docType methods |
|
| 1198 |
#' @aliases occurrence-method |
|
| 1199 |
setGeneric( |
|
| 1200 |
name = "occurrence", |
|
| 1201 | 4x |
def = function(object, ...) standardGeneric("occurrence")
|
| 1202 |
) |
|
| 1203 | ||
| 1204 |
#' Binomial Co-Occurrence Assessment |
|
| 1205 |
#' |
|
| 1206 |
#' @param x,y A [`numeric`] vector. |
|
| 1207 |
#' @param ... Currently not used. |
|
| 1208 |
#' @details |
|
| 1209 |
#' This assesses the degree of co-occurrence between taxa/types within a |
|
| 1210 |
#' dataset. The strongest associations are shown by large positive numbers, |
|
| 1211 |
#' the strongest segregations by large negative numbers. |
|
| 1212 |
#' |
|
| 1213 |
#' The Binomial co-occurrence assessment approximates a Z-score. |
|
| 1214 |
#' @return |
|
| 1215 |
#' A [`numeric`] vector. |
|
| 1216 |
#' @references |
|
| 1217 |
#' Kintigh, K. (2006). Ceramic Dating and Type Associations. In J. Hantman and |
|
| 1218 |
#' R. Most (eds.), *Managing Archaeological Data: Essays in Honor of |
|
| 1219 |
#' Sylvia W. Gaines*. Anthropological Research Paper, 57. Tempe, AZ: Arizona |
|
| 1220 |
#' State University, p. 17-26. |
|
| 1221 |
#' @author N. Frerebeau |
|
| 1222 |
#' @family beta diversity measures |
|
| 1223 |
#' @docType methods |
|
| 1224 |
#' @aliases index_binomial-method |
|
| 1225 |
setGeneric( |
|
| 1226 |
name = "index_binomial", |
|
| 1227 | 1x |
def = function(x, y, ...) standardGeneric("index_binomial")
|
| 1228 |
) |
|
| 1229 | ||
| 1230 |
## Simulate -------------------------------------------------------------------- |
|
| 1231 |
#' Measure Diversity by Comparing to Simulated Assemblages |
|
| 1232 |
#' |
|
| 1233 |
#' @param object A [DiversityIndex-class] object. |
|
| 1234 |
#' @param nsim A non-negative [`integer`] specifying the number of simulations. |
|
| 1235 |
#' @param seed An object specifying if and how the random number generator |
|
| 1236 |
#' should be initialized (see [stats::simulate()]). |
|
| 1237 |
#' @param interval A [`character`] string giving the type of confidence |
|
| 1238 |
#' interval to be returned. Currently, only "`percentiles`" is supported |
|
| 1239 |
#' (sample quantiles, as described in Kintigh 1984).. |
|
| 1240 |
#' @param level A length-one [`numeric`] vector giving the confidence level. |
|
| 1241 |
#' @param step An [`integer`] giving the increment of the sample size. |
|
| 1242 |
#' @param progress A [`logical`] scalar: should a progress bar be displayed? |
|
| 1243 |
#' @param ... Currently not used. |
|
| 1244 |
#' @return |
|
| 1245 |
#' Returns a [DiversityIndex-class] object. |
|
| 1246 |
#' @references |
|
| 1247 |
#' Baxter, M. J. (2001). Methodological Issues in the Study of Assemblage |
|
| 1248 |
#' Diversity. *American Antiquity*, 66(4), 715-725. \doi{10.2307/2694184}.
|
|
| 1249 |
#' |
|
| 1250 |
#' Kintigh, K. W. (1984). Measuring Archaeological Diversity by Comparison |
|
| 1251 |
#' with Simulated Assemblages. *American Antiquity*, 49(1), 44-54. |
|
| 1252 |
#' \doi{10.2307/280511}.
|
|
| 1253 |
#' @seealso [bootstrap()], [jackknife()] |
|
| 1254 |
#' @example inst/examples/ex-simulate.R |
|
| 1255 |
#' @author N. Frerebeau |
|
| 1256 |
#' @family diversity measures |
|
| 1257 |
#' @docType methods |
|
| 1258 |
#' @name simulate |
|
| 1259 |
#' @rdname simulate |
|
| 1260 |
NULL |
|
| 1261 | ||
| 1262 |
## Diversity Test -------------------------------------------------------------- |
|
| 1263 |
#' Diversity Test |
|
| 1264 |
#' |
|
| 1265 |
#' Compares Shannon/Simpson diversity between samples. |
|
| 1266 |
#' @param x,y A [`numeric`] vector, a \eqn{m \times p}{m x p} [`matrix`] or
|
|
| 1267 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1268 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1269 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1270 |
#' @param adjust A [`character`] string specifying the method for |
|
| 1271 |
#' adjusting \eqn{p} values (see [stats::p.adjust()]).
|
|
| 1272 |
#' @param ... Further arguments to be passed to internal methods. |
|
| 1273 |
#' @return |
|
| 1274 |
#' If `x` and `y` are `numeric` vectors, returns a [`list`] containing the |
|
| 1275 |
#' following components: |
|
| 1276 |
#' \describe{
|
|
| 1277 |
#' \item{`statistic`}{The value of the t-statistic.}
|
|
| 1278 |
#' \item{`parameter`}{The degrees of freedom for the t-statistic.}
|
|
| 1279 |
#' \item{`p.value`}{The p-value for the test.}
|
|
| 1280 |
#' } |
|
| 1281 |
#' |
|
| 1282 |
#' If `x` is a `matrix` or a `data.frame`, returns a table of adjusted p-values |
|
| 1283 |
#' in lower triangular form. |
|
| 1284 |
#' @example inst/examples/ex-test.R |
|
| 1285 |
#' @author N. Frerebeau |
|
| 1286 |
#' @references |
|
| 1287 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 1288 |
#' Princeton, NJ: Princeton University Press. \doi{10.1007/978-94-015-7358-0}.
|
|
| 1289 |
#' @family statistics |
|
| 1290 |
#' @docType methods |
|
| 1291 |
#' @name test |
|
| 1292 |
#' @rdname test |
|
| 1293 |
NULL |
|
| 1294 | ||
| 1295 |
#' @rdname test |
|
| 1296 |
#' @aliases test_shannon-method |
|
| 1297 |
setGeneric( |
|
| 1298 |
name = "test_shannon", |
|
| 1299 | 3x |
def = function(x, y, ...) standardGeneric("test_shannon")
|
| 1300 |
) |
|
| 1301 | ||
| 1302 |
#' @rdname test |
|
| 1303 |
#' @aliases test_simpson-method |
|
| 1304 |
setGeneric( |
|
| 1305 |
name = "test_simpson", |
|
| 1306 | 3x |
def = function(x, y, ...) standardGeneric("test_simpson")
|
| 1307 |
) |
|
| 1308 | ||
| 1309 |
# Plot ========================================================================= |
|
| 1310 |
## Diversity ------------------------------------------------------------------- |
|
| 1311 |
#' Diversity Plot |
|
| 1312 |
#' |
|
| 1313 |
#' @param x A [DiversityIndex-class] object to be plotted. |
|
| 1314 |
#' @param log A [`character`] string indicating which axes should be in log |
|
| 1315 |
#' scale. Defaults to `x`. |
|
| 1316 |
#' @param col.mean,col.interval A [`character`] string specifying the |
|
| 1317 |
#' color of the lines. |
|
| 1318 |
#' @param lty.mean,lty.interval A [`character`] string or [`numeric`] |
|
| 1319 |
#' value specifying the line types. |
|
| 1320 |
#' @param lwd.mean,lwd.interval A non-negative [`numeric`] value specifying |
|
| 1321 |
#' the line widths. |
|
| 1322 |
#' @param xlab,ylab A [`character`] vector giving the x and y axis labels. |
|
| 1323 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1324 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1325 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1326 |
#' y and z axis labels) appear on the plot? |
|
| 1327 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1328 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1329 |
#' plot? |
|
| 1330 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1331 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1332 |
#' background grids. |
|
| 1333 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1334 |
#' place but before the axes, title and box are added. |
|
| 1335 |
#' @param ... Further [graphical parameters][graphics::par] to be passed to |
|
| 1336 |
#' [graphics::points()], particularly, `cex`, `col` and `pch`. |
|
| 1337 |
#' @return |
|
| 1338 |
#' `plot()` is called for its side-effects: it results in a graphic being |
|
| 1339 |
#' displayed (invisibly returns `x`). |
|
| 1340 |
#' @example inst/examples/ex-simulate.R |
|
| 1341 |
#' @author N. Frerebeau |
|
| 1342 |
#' @family diversity measures |
|
| 1343 |
#' @docType methods |
|
| 1344 |
#' @name plot.DiversityIndex |
|
| 1345 |
#' @rdname plot.DiversityIndex |
|
| 1346 |
NULL |
|
| 1347 | ||
| 1348 |
#' Rarefaction Plot |
|
| 1349 |
#' |
|
| 1350 |
#' @param x A [RarefactionIndex-class] object to be plotted. |
|
| 1351 |
#' @param color A vector of colors (will be mapped to the rownames of `object`). |
|
| 1352 |
#' If `color` is a named a named vector, then the colors will be associated |
|
| 1353 |
#' with the rownames of `object`. Ignored if set to `FALSE`. |
|
| 1354 |
#' @param symbol A specification for the line type (will be mapped to |
|
| 1355 |
#' the names of `x`). If `symbol` is a named a named vector, then the |
|
| 1356 |
#' line types will be associated with the names of `x`. |
|
| 1357 |
#' Ignored if set to `FALSE`. |
|
| 1358 |
#' @param xlab,ylab A [`character`] vector giving the x and y axis labels. |
|
| 1359 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1360 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1361 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1362 |
#' y and z axis labels) appear on the plot? |
|
| 1363 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1364 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1365 |
#' plot? |
|
| 1366 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1367 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1368 |
#' background grids. |
|
| 1369 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1370 |
#' place but before the axes, title and box are added. |
|
| 1371 |
#' @param legend A [`list`] of additional arguments to be passed to |
|
| 1372 |
#' [graphics::legend()]; names of the list are used as argument names. |
|
| 1373 |
#' If `NULL`, no legend is displayed. |
|
| 1374 |
#' @param ... Further [graphical parameters][graphics::par] to be passed to |
|
| 1375 |
#' [graphics::lines()]. |
|
| 1376 |
#' @return |
|
| 1377 |
#' `plot()` is called for its side-effects: it results in a graphic being |
|
| 1378 |
#' displayed (invisibly returns `x`). |
|
| 1379 |
#' @example inst/examples/ex-rarefaction.R |
|
| 1380 |
#' @author N. Frerebeau |
|
| 1381 |
#' @family diversity measures |
|
| 1382 |
#' @docType methods |
|
| 1383 |
#' @name plot.RarefactionIndex |
|
| 1384 |
#' @rdname plot.RarefactionIndex |
|
| 1385 |
NULL |
|
| 1386 | ||
| 1387 |
#' SHE Analysis |
|
| 1388 |
#' |
|
| 1389 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1390 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1391 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1392 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1393 |
#' @param unbiased A [`logical`] scalar: should the bias-corrected estimator be |
|
| 1394 |
#' used (see [index_shannon()])? |
|
| 1395 |
#' @param xlab,ylab A [`character`] vector giving the x and y axis labels. |
|
| 1396 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1397 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1398 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1399 |
#' y and z axis labels) appear on the plot? |
|
| 1400 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1401 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1402 |
#' plot? |
|
| 1403 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1404 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1405 |
#' background grids. |
|
| 1406 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1407 |
#' place but before the axes, title and box are added. |
|
| 1408 |
#' @param legend A [`list`] of additional arguments to be passed to |
|
| 1409 |
#' [graphics::legend()]; names of the list are used as argument names. |
|
| 1410 |
#' If `NULL`, no legend is displayed. |
|
| 1411 |
#' @param ... Further [graphical parameters][graphics::par] to be passed to |
|
| 1412 |
#' [graphics::lines()] and [graphics::points()]. |
|
| 1413 |
#' @return |
|
| 1414 |
#' `she()` is called for its side-effects: it results in a graphic being |
|
| 1415 |
#' displayed (invisibly returns `object`). |
|
| 1416 |
#' @details |
|
| 1417 |
#' If samples are taken along a gradient or stratigraphic section, breaks in |
|
| 1418 |
#' the curve may be used to infer discontinuities. |
|
| 1419 |
#' |
|
| 1420 |
#' This assumes that the order of the matrix rows (from \eqn{1} to \eqn{n})
|
|
| 1421 |
#' follows the progression along the gradient/transect. |
|
| 1422 |
#' @references |
|
| 1423 |
#' Buzas, M. A. & Hayek, L.-A. C. (1998). SHE analysis for biofacies |
|
| 1424 |
#' identification. *Journal of Foraminiferal Research*, 1998, 28(3), 233-239. |
|
| 1425 |
#' |
|
| 1426 |
#' Hayek, L.-A. C. & Buzas, M. A. (2010). *Surveying Natural Populations: |
|
| 1427 |
#' Quantitative Tools for Assessing Biodiversity*. Second edition. |
|
| 1428 |
#' New York: Columbia University Press. |
|
| 1429 |
#' @example inst/examples/ex-she.R |
|
| 1430 |
#' @author N. Frerebeau |
|
| 1431 |
#' @family diversity measures |
|
| 1432 |
#' @docType methods |
|
| 1433 |
#' @aliases she-method |
|
| 1434 |
setGeneric( |
|
| 1435 |
name = "she", |
|
| 1436 | 2x |
def = function(object, ...) standardGeneric("she")
|
| 1437 |
) |
|
| 1438 | ||
| 1439 |
#' Diversity Profiles |
|
| 1440 |
#' |
|
| 1441 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1442 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1443 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1444 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1445 |
#' @param alpha A [`numeric`] vector giving the values of the alpha parameter. |
|
| 1446 |
#' @param color A vector of colors (will be mapped to the rownames of `object`). |
|
| 1447 |
#' If `color` is a named a named vector, then the colors will be associated |
|
| 1448 |
#' with the rownames of `object`. Ignored if set to `FALSE`. |
|
| 1449 |
#' @param symbol A specification for the line type (will be mapped to |
|
| 1450 |
#' the rownames of `object`). If `symbol` is a named a named vector, then the |
|
| 1451 |
#' line types will be associated with the rownames of `object`. |
|
| 1452 |
#' Ignored if set to `FALSE`. |
|
| 1453 |
#' @param xlab,ylab A [`character`] vector giving the x and y axis labels. |
|
| 1454 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1455 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1456 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1457 |
#' y and z axis labels) appear on the plot? |
|
| 1458 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1459 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1460 |
#' plot? |
|
| 1461 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1462 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1463 |
#' background grids. |
|
| 1464 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1465 |
#' place but before the axes, title and box are added. |
|
| 1466 |
#' @param legend A [`list`] of additional arguments to be passed to |
|
| 1467 |
#' [graphics::legend()]; names of the list are used as argument names. |
|
| 1468 |
#' If `NULL`, no legend is displayed. |
|
| 1469 |
#' @param ... Further [graphical parameters][graphics::par] to be passed to |
|
| 1470 |
#' [graphics::lines()] |
|
| 1471 |
#' @details |
|
| 1472 |
#' If the profiles cross, the diversities are non-comparable across samples. |
|
| 1473 |
#' @return |
|
| 1474 |
#' `profiles()` is called for its side-effects: it results in a graphic being |
|
| 1475 |
#' displayed (invisibly returns `object`). |
|
| 1476 |
#' @references |
|
| 1477 |
#' Tóthmérész, B. (1995). Comparison of Different Methods for Diversity |
|
| 1478 |
#' Ordering. *Journal of Vegetation Science*, 6(2), 283-290. |
|
| 1479 |
#' \doi{10.2307/3236223}.
|
|
| 1480 |
#' @example inst/examples/ex-profiles.R |
|
| 1481 |
#' @author N. Frerebeau |
|
| 1482 |
#' @family diversity measures |
|
| 1483 |
#' @docType methods |
|
| 1484 |
#' @aliases profiles-method |
|
| 1485 |
setGeneric( |
|
| 1486 |
name = "profiles", |
|
| 1487 | 2x |
def = function(object, ...) standardGeneric("profiles")
|
| 1488 |
) |
|
| 1489 | ||
| 1490 |
## Matrix plot ----------------------------------------------------------------- |
|
| 1491 |
### Spot Plot ------------------------------------------------------------------ |
|
| 1492 |
#' Spot Plot |
|
| 1493 |
#' |
|
| 1494 |
#' Plots a spot matrix. |
|
| 1495 |
#' @inheritParams plot_matrix |
|
| 1496 |
#' @param type A [`character`] string specifying the graph to be plotted. |
|
| 1497 |
#' It must be one of "`ring`" (the default) or "`plain`". Any unambiguous |
|
| 1498 |
#' substring can be given. |
|
| 1499 |
#' @param ... Currently not used. |
|
| 1500 |
#' @details |
|
| 1501 |
#' The spot matrix can be considered as a variant of the |
|
| 1502 |
#' [Bertin diagram][plot_bertin()] where the data are first transformed to |
|
| 1503 |
#' relative frequencies. |
|
| 1504 |
#' @return |
|
| 1505 |
#' `plot_spot()` is called for its side-effects: it results in a graphic |
|
| 1506 |
#' being displayed (invisibly returns `object`). |
|
| 1507 |
#' @note |
|
| 1508 |
#' Adapted from Dan Gopstein's original |
|
| 1509 |
#' [idea](https://dan.gop/articles/spot-matrix/). |
|
| 1510 |
#' @example inst/examples/ex-plot_spot.R |
|
| 1511 |
#' @author N. Frerebeau |
|
| 1512 |
#' @family plot methods |
|
| 1513 |
#' @docType methods |
|
| 1514 |
#' @aliases plot_spot-method |
|
| 1515 |
setGeneric( |
|
| 1516 |
name = "plot_spot", |
|
| 1517 | 14x |
def = function(object, ...) standardGeneric("plot_spot")
|
| 1518 |
) |
|
| 1519 | ||
| 1520 |
### Heatmap -------------------------------------------------------------------- |
|
| 1521 |
#' Heatmap |
|
| 1522 |
#' |
|
| 1523 |
#' Plots a heatmap. |
|
| 1524 |
#' @inheritParams plot_matrix |
|
| 1525 |
#' @param fixed_ratio A [`logical`] scalar: should a fixed aspect ratio (1) be |
|
| 1526 |
#' used? |
|
| 1527 |
#' @param ... Currently not used. |
|
| 1528 |
#' @return |
|
| 1529 |
#' `plot_heatmap()` is called for its side-effects: it results in a graphic |
|
| 1530 |
#' being displayed (invisibly returns `object`). |
|
| 1531 |
#' @example inst/examples/ex-plot_heatmap.R |
|
| 1532 |
#' @author N. Frerebeau |
|
| 1533 |
#' @family plot methods |
|
| 1534 |
#' @docType methods |
|
| 1535 |
#' @aliases plot_heatmap-method |
|
| 1536 |
setGeneric( |
|
| 1537 |
name = "plot_heatmap", |
|
| 1538 | 13x |
def = function(object, ...) standardGeneric("plot_heatmap")
|
| 1539 |
) |
|
| 1540 | ||
| 1541 |
### Matrigraph ----------------------------------------------------------------- |
|
| 1542 |
#' Matrigraph |
|
| 1543 |
#' |
|
| 1544 |
#' @description |
|
| 1545 |
#' * `matrigraph()` produces a heatmap highlighting the deviations from |
|
| 1546 |
#' independence. |
|
| 1547 |
#' * `pvi()` computes for each cell of a numeric matrix the percentage to the |
|
| 1548 |
#' column theoretical independence value. |
|
| 1549 |
#' @inheritParams plot_matrix |
|
| 1550 |
#' @param reverse A [`logical`] scalar: should negative deviations be centered |
|
| 1551 |
#' (see details)? |
|
| 1552 |
#' @param ... Currently not used. |
|
| 1553 |
#' @details |
|
| 1554 |
#' PVI (in french "pourcentages de valeur d'indépendance") is calculated for |
|
| 1555 |
#' each cell as the percentage to the column theoretical independence value: |
|
| 1556 |
#' PVI greater than \eqn{1} represent positive deviations from the
|
|
| 1557 |
#' independence, whereas PVI smaller than \eqn{1} represent negative
|
|
| 1558 |
#' deviations (Desachy 2004). |
|
| 1559 |
#' |
|
| 1560 |
#' The PVI matrix allows to explore deviations from independence (an |
|
| 1561 |
#' intuitive approach to \eqn{\chi^2}{Chi-squared}), in such a way that a
|
|
| 1562 |
#' high-contrast matrix has quite significant deviations, |
|
| 1563 |
#' with a low risk of being due to randomness (Desachy 2004). |
|
| 1564 |
#' |
|
| 1565 |
#' `matrigraph()` displays the deviations from independence: |
|
| 1566 |
#' |
|
| 1567 |
#' * If the PVI is equal to \eqn{1} (statistical independence), the cell of the
|
|
| 1568 |
#' matrix is filled in grey. |
|
| 1569 |
#' * If the PVI is less than \eqn{1} (negative deviation from independence),
|
|
| 1570 |
#' the size of the grey square is proportional to the PVI (the white margin |
|
| 1571 |
#' thus represents the fraction of negative deviation). |
|
| 1572 |
#' * If the PVI is greater than \eqn{1} (positive deviation), a black
|
|
| 1573 |
#' square representing the fraction of positive deviations is |
|
| 1574 |
#' superimposed. For large positive deviations (PVI greater than \eqn{2}),
|
|
| 1575 |
#' the cell in filled in black. |
|
| 1576 |
#' |
|
| 1577 |
#' If `reverse` is `TRUE`, the fraction of negative deviations is displayed |
|
| 1578 |
#' as a white square. |
|
| 1579 |
#' @references |
|
| 1580 |
#' Desachy, B. (2004). Le sériographe EPPM: un outil informatisé de sériation |
|
| 1581 |
#' graphique pour tableaux de comptages. *Revue archéologique de Picardie*, |
|
| 1582 |
#' 3(1), 39-56. \doi{10.3406/pica.2004.2396}.
|
|
| 1583 |
#' @return |
|
| 1584 |
#' * `matrigraph()` is called for its side-effects: it results in a graphic |
|
| 1585 |
#' being displayed (invisibly returns `object`). |
|
| 1586 |
#' * `pvi()` returns a [`numeric`] [`matrix`]. |
|
| 1587 |
#' @example inst/examples/ex-matrigraph.R |
|
| 1588 |
#' @author N. Frerebeau |
|
| 1589 |
#' @seealso [plot_heatmap()] |
|
| 1590 |
#' @family plot methods |
|
| 1591 |
#' @docType methods |
|
| 1592 |
#' @aliases matrigraph-method |
|
| 1593 |
setGeneric( |
|
| 1594 |
name = "matrigraph", |
|
| 1595 | 4x |
def = function(object, ...) standardGeneric("matrigraph")
|
| 1596 |
) |
|
| 1597 | ||
| 1598 |
#' @rdname matrigraph |
|
| 1599 |
#' @aliases pvi-method |
|
| 1600 |
setGeneric( |
|
| 1601 |
name = "pvi", |
|
| 1602 | 4x |
def = function(object, ...) standardGeneric("pvi")
|
| 1603 |
) |
|
| 1604 | ||
| 1605 |
## Bar Plot -------------------------------------------------------------------- |
|
| 1606 |
### Bertin --------------------------------------------------------------------- |
|
| 1607 |
#' Bertin Diagram |
|
| 1608 |
#' |
|
| 1609 |
#' Plots a Bertin diagram. |
|
| 1610 |
#' @inheritParams plot_matrix |
|
| 1611 |
#' @param threshold A [`function`] that takes a numeric vector as argument and |
|
| 1612 |
#' returns a numeric threshold value (see below). If `NULL` (the default), no |
|
| 1613 |
#' threshold is computed. Only used if `freq` is `FALSE`. |
|
| 1614 |
#' @param flip A [`logical`] scalar: should `x` and `y` axis be flipped? |
|
| 1615 |
#' Defaults to `TRUE`. |
|
| 1616 |
#' @param ... Currently not used. |
|
| 1617 |
#' @details |
|
| 1618 |
#' As de Falguerolles *et al.* (1997) points out: |
|
| 1619 |
#' "In abstract terms, a Bertin matrix is a matrix of displays. \[...\] To fix |
|
| 1620 |
#' ideas, think of a data matrix, variable by case, with real valued variables. |
|
| 1621 |
#' For each variable, draw a bar chart of variable value by case. High-light |
|
| 1622 |
#' all bars representing a value above some sample threshold for that |
|
| 1623 |
#' variable." |
|
| 1624 |
#' @return |
|
| 1625 |
#' `plot_bertin()` is called for its side-effects: it results in a graphic |
|
| 1626 |
#' being displayed (invisibly returns `object`). |
|
| 1627 |
#' @references |
|
| 1628 |
#' Bertin, J. (1977). *La graphique et le traitement graphique de |
|
| 1629 |
#' l'information*. Paris: Flammarion. Nouvelle Bibliothèque Scientifique. |
|
| 1630 |
#' |
|
| 1631 |
#' de Falguerolles, A., Friedrich, F. & Sawitzki, G. (1997). A Tribute to J. |
|
| 1632 |
#' Bertin's Graphical Data Analysis. In W. Badilla & F. Faulbaum (eds.), |
|
| 1633 |
#' *SoftStat '97: Advances in Statistical Software 6*. Stuttgart: Lucius |
|
| 1634 |
#' & Lucius, p. 11-20. |
|
| 1635 |
#' @example inst/examples/ex-plot_bertin.R |
|
| 1636 |
#' @author N. Frerebeau |
|
| 1637 |
#' @family plot methods |
|
| 1638 |
#' @docType methods |
|
| 1639 |
#' @aliases plot_bertin-method Bertin |
|
| 1640 |
setGeneric( |
|
| 1641 |
name = "plot_bertin", |
|
| 1642 | 10x |
def = function(object, ...) standardGeneric("plot_bertin")
|
| 1643 |
) |
|
| 1644 | ||
| 1645 |
### Ford ----------------------------------------------------------------------- |
|
| 1646 |
#' Ford Diagram |
|
| 1647 |
#' |
|
| 1648 |
#' Plots a Ford (battleship curve) diagram. |
|
| 1649 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1650 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1651 |
#' individuals for each category, i.e. a contingency table). |
|
| 1652 |
#' @param weights A [`logical`] scalar: should the row sums be displayed? |
|
| 1653 |
#' @param EPPM A [`logical`] scalar: should the EPPM be drawn? |
|
| 1654 |
#' See `seriograph()`. |
|
| 1655 |
#' @param fill The color for filling the bars. |
|
| 1656 |
#' @param border The color to draw the borders. |
|
| 1657 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? It will |
|
| 1658 |
#' omit labels where they would abut or overlap previously drawn labels. |
|
| 1659 |
#' @param ... Currently not used. |
|
| 1660 |
#' @return |
|
| 1661 |
#' `plot_ford()` is called for its side-effects: it results in a graphic |
|
| 1662 |
#' being displayed (invisibly returns `object`). |
|
| 1663 |
#' @references |
|
| 1664 |
#' Ford, J. A. (1962). *A quantitative method for deriving cultural |
|
| 1665 |
#' chronology*. Washington, DC: Pan American Union. Technical manual 1. |
|
| 1666 |
#' @example inst/examples/ex-plot_ford.R |
|
| 1667 |
#' @author N. Frerebeau |
|
| 1668 |
#' @family plot methods |
|
| 1669 |
#' @docType methods |
|
| 1670 |
#' @aliases plot_ford-method Ford |
|
| 1671 |
setGeneric( |
|
| 1672 |
name = "plot_ford", |
|
| 1673 | 5x |
def = function(object, ...) standardGeneric("plot_ford")
|
| 1674 |
) |
|
| 1675 | ||
| 1676 |
### Seriograph ----------------------------------------------------------------- |
|
| 1677 |
#' Seriograph |
|
| 1678 |
#' |
|
| 1679 |
#' @description |
|
| 1680 |
#' * `seriograph()` produces a Ford diagram highlighting the relationships |
|
| 1681 |
#' between rows and columns. |
|
| 1682 |
#' * `eppm()` computes for each cell of a numeric matrix the positive |
|
| 1683 |
#' difference from the column mean percentage. |
|
| 1684 |
#' @inheritParams plot_ford |
|
| 1685 |
#' @details |
|
| 1686 |
#' The positive difference from the column mean percentage (in french "écart |
|
| 1687 |
#' positif au pourcentage moyen", EPPM) represents a deviation from the |
|
| 1688 |
#' situation of statistical independence. As independence can be interpreted as |
|
| 1689 |
#' the absence of relationships between types and the chronological order of |
|
| 1690 |
#' the assemblages, EPPM is a useful tool to explore significance |
|
| 1691 |
#' of relationship between rows and columns related to seriation (Desachy |
|
| 1692 |
#' 2004). |
|
| 1693 |
#' |
|
| 1694 |
#' `seriograph()` superimposes the frequencies (grey) and EPPM values (black) |
|
| 1695 |
#' for each row-column pair in a Ford diagram. |
|
| 1696 |
#' @references |
|
| 1697 |
#' Desachy, B. (2004). Le sériographe EPPM: un outil informatisé de sériation |
|
| 1698 |
#' graphique pour tableaux de comptages. *Revue archéologique de Picardie*, |
|
| 1699 |
#' 3(1), 39-56. \doi{10.3406/pica.2004.2396}.
|
|
| 1700 |
#' @return |
|
| 1701 |
#' * `seriograph()` is called for its side-effects: it results in a graphic |
|
| 1702 |
#' being displayed (invisibly returns `object`). |
|
| 1703 |
#' * `eppm()` returns a [`numeric`] [`matrix`]. |
|
| 1704 |
#' @example inst/examples/ex-seriograph.R |
|
| 1705 |
#' @author N. Frerebeau |
|
| 1706 |
#' @seealso [plot_ford()] |
|
| 1707 |
#' @family plot methods |
|
| 1708 |
#' @docType methods |
|
| 1709 |
#' @aliases seriograph-method |
|
| 1710 |
setGeneric( |
|
| 1711 |
name = "seriograph", |
|
| 1712 | 2x |
def = function(object, ...) standardGeneric("seriograph")
|
| 1713 |
) |
|
| 1714 | ||
| 1715 |
#' @rdname seriograph |
|
| 1716 |
#' @aliases eppm-method |
|
| 1717 |
setGeneric( |
|
| 1718 |
name = "eppm", |
|
| 1719 | 5x |
def = function(object, ...) standardGeneric("eppm")
|
| 1720 |
) |
|
| 1721 | ||
| 1722 |
### Dice-Leraas ---------------------------------------------------------------- |
|
| 1723 |
#' Dice-Leraas Diagram |
|
| 1724 |
#' |
|
| 1725 |
#' Plots a Dice-Leraas diagram. |
|
| 1726 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1727 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1728 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1729 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1730 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1731 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1732 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1733 |
#' y and z axis labels) appear on the plot? |
|
| 1734 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1735 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1736 |
#' plot? |
|
| 1737 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1738 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1739 |
#' background grids. |
|
| 1740 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1741 |
#' place but before the axes, title and box are added. |
|
| 1742 |
#' @param ... Further [graphical parameters][graphics::par]. |
|
| 1743 |
#' @details |
|
| 1744 |
#' In a Dice-Leraas diagram, the horizontal line represents the range of data |
|
| 1745 |
#' (min-max) and the small vertical line indicates the mean. The black |
|
| 1746 |
#' rectangle is twice the standard error on the mean, while the white rectangle |
|
| 1747 |
#' is one standard deviation on either side of the mean. |
|
| 1748 |
#' @references |
|
| 1749 |
#' Dice, L. R., & Leraas, H. J. (1936). A Graphic Method for Comparing Several |
|
| 1750 |
#' Sets of Measurements. *Contributions from the Laboratory of Vertebrate |
|
| 1751 |
#' Genetics*, 3: 1-3. |
|
| 1752 |
#' |
|
| 1753 |
#' Hubbs, C. L., & C. Hubbs (1953). An Improved Graphical Analysis and |
|
| 1754 |
#' Comparison of Series of Samples. *Systematic Biology*, 2(2): 49-56. |
|
| 1755 |
#' \doi{10.2307/sysbio/2.2.49}.
|
|
| 1756 |
#' |
|
| 1757 |
#' Simpson, G. G., Roe, A., & Lewontin, R. C. *Quantitative Zoology*. |
|
| 1758 |
#' New York: Harcourt, Brace and Company, 1960. |
|
| 1759 |
#' @return |
|
| 1760 |
#' `plot_diceleraas()` is called for its side-effects: it results in a |
|
| 1761 |
#' graphic being displayed (invisibly returns `object`). |
|
| 1762 |
#' @example inst/examples/ex-diceleraas.R |
|
| 1763 |
#' @author N. Frerebeau |
|
| 1764 |
#' @family plot methods |
|
| 1765 |
#' @docType methods |
|
| 1766 |
#' @aliases plot_diceleraas-method |
|
| 1767 |
setGeneric( |
|
| 1768 |
name = "plot_diceleraas", |
|
| 1769 | 2x |
def = function(object, ...) standardGeneric("plot_diceleraas")
|
| 1770 |
) |
|
| 1771 | ||
| 1772 |
## Line Plot ------------------------------------------------------------------- |
|
| 1773 |
#' Rank Plot |
|
| 1774 |
#' |
|
| 1775 |
#' Plots a rank *vs* relative abundance diagram. |
|
| 1776 |
#' @param object A \eqn{m \times p}{m x p} `numeric` [`matrix`] or
|
|
| 1777 |
#' [`data.frame`] of count data (absolute frequencies giving the number of |
|
| 1778 |
#' individuals for each category, i.e. a contingency table). A [`data.frame`] |
|
| 1779 |
#' will be coerced to a `numeric` `matrix` via [data.matrix()]. |
|
| 1780 |
#' @param color A vector of colors (will be mapped to the rownames of `object`). |
|
| 1781 |
#' If `color` is a named a named vector, then the colors will be associated |
|
| 1782 |
#' with the rownames of `object`. Ignored if set to `FALSE`. |
|
| 1783 |
#' @param symbol A specification for the line type (will be mapped to |
|
| 1784 |
#' the rownames of `object`). If `symbol` is a named a named vector, then the |
|
| 1785 |
#' line types will be associated with the rownames of `object`. |
|
| 1786 |
#' Ignored if set to `FALSE`. |
|
| 1787 |
#' @param log A [`character`] string which contains "`x`" if the x axis is to be |
|
| 1788 |
#' logarithmic, "`y`" if the y axis is to be logarithmic and "`xy`" or "`yx`" |
|
| 1789 |
#' if both axes are to be logarithmic (base 10). |
|
| 1790 |
# @param facet A [`logical`] scalar: should a matrix of panels defined by |
|
| 1791 |
# case/sample be drawn? |
|
| 1792 |
#' @param xlab,ylab A [`character`] vector giving the x and y axis labels. |
|
| 1793 |
#' @param main A [`character`] string giving a main title for the plot. |
|
| 1794 |
#' @param sub A [`character`] string giving a subtitle for the plot. |
|
| 1795 |
#' @param ann A [`logical`] scalar: should the default annotation (title and x, |
|
| 1796 |
#' y and z axis labels) appear on the plot? |
|
| 1797 |
#' @param axes A [`logical`] scalar: should axes be drawn on the plot? |
|
| 1798 |
#' @param frame.plot A [`logical`] scalar: should a box be drawn around the |
|
| 1799 |
#' plot? |
|
| 1800 |
#' @param panel.first An an `expression` to be evaluated after the plot axes are |
|
| 1801 |
#' set up but before any plotting takes place. This can be useful for drawing |
|
| 1802 |
#' background grids. |
|
| 1803 |
#' @param panel.last An `expression` to be evaluated after plotting has taken |
|
| 1804 |
#' place but before the axes, title and box are added. |
|
| 1805 |
#' @param legend A [`list`] of additional arguments to be passed to |
|
| 1806 |
#' [graphics::legend()]; names of the list are used as argument names. |
|
| 1807 |
#' If `NULL`, no legend is displayed. |
|
| 1808 |
#' @param ... Further [graphical parameters][graphics::par]. |
|
| 1809 |
#' @return |
|
| 1810 |
#' `plot_rank()` is called for its side-effects: it results in a graphic |
|
| 1811 |
#' being displayed (invisibly returns `object`). |
|
| 1812 |
#' @references |
|
| 1813 |
#' Magurran, A. E. (1988). *Ecological Diversity and its Measurement*. |
|
| 1814 |
#' Princeton, NJ: Princeton University Press. \doi{10.1007/978-94-015-7358-0}.
|
|
| 1815 |
#' @example inst/examples/ex-plot_rank.R |
|
| 1816 |
#' @author N. Frerebeau |
|
| 1817 |
#' @family plot methods |
|
| 1818 |
#' @docType methods |
|
| 1819 |
#' @aliases plot_rank-method |
|
| 1820 |
setGeneric( |
|
| 1821 |
name = "plot_rank", |
|
| 1822 | 4x |
def = function(object, ...) standardGeneric("plot_rank")
|
| 1823 |
) |
| 1 |
# STATISTICS |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
expected <- function(x) {
|
|
| 6 | 7x |
m <- nrow(x) |
| 7 | 7x |
p <- ncol(x) |
| 8 | ||
| 9 | 7x |
column_total <- matrix(colSums(x), nrow = m, ncol = p, byrow = TRUE) |
| 10 | 7x |
row_total <- matrix(rowSums(x), nrow = m, ncol = p, byrow = FALSE) |
| 11 | 7x |
grand_total <- sum(x) |
| 12 | ||
| 13 | 7x |
column_total * row_total / grand_total |
| 14 |
} |
|
| 15 | ||
| 16 |
#' Binomial Coefficient |
|
| 17 |
#' |
|
| 18 |
#' Computes the number of `k`-combinations from a given set of `n` elements |
|
| 19 |
#' ("`n` choose `k`").
|
|
| 20 |
#' @param n A length-one [`numeric`] vector. |
|
| 21 |
#' @param k A length-one [`numeric`] vector. |
|
| 22 |
#' @details |
|
| 23 |
#' Ramanujan approximation is used for \eqn{x!} computation if \eqn{x > 170}.
|
|
| 24 |
#' @return A length-one [`numeric`] vector. |
|
| 25 |
#' @author N. Frerebeau |
|
| 26 |
#' @keywords internal |
|
| 27 |
#' @noRd |
|
| 28 |
combination <- function(n, k) {
|
|
| 29 |
# Validation |
|
| 30 | 373x |
arkhe::assert_scalar(n, "numeric") |
| 31 | 373x |
arkhe::assert_scalar(k, "numeric") |
| 32 | ||
| 33 | 372x |
if (n > 170 | k > 170) {
|
| 34 |
## Ramanujan approximation of x! |
|
| 35 | 1x |
c <- exp(ramanujan(n) - ramanujan(k) - ramanujan(n - k)) |
| 36 |
} else {
|
|
| 37 | 371x |
c <- factorial(n) / (factorial(k) * factorial(n - k)) |
| 38 |
} |
|
| 39 | 372x |
c |
| 40 |
} |
|
| 41 | ||
| 42 |
#' Ramanujan Factorial Approximation |
|
| 43 |
#' |
|
| 44 |
#' @param x A [`numeric`] vector. |
|
| 45 |
#' @return A [`numeric`] vector. |
|
| 46 |
#' @examples |
|
| 47 |
#' factorial(50) |
|
| 48 |
#' exp(ramanujan(50)) |
|
| 49 |
#' @references |
|
| 50 |
#' Ramanujan Aiyangar, S. (1988). *The lost notebook and other unpublished |
|
| 51 |
#' papers*. Berlin: Springer-Verlag. |
|
| 52 |
#' @author N. Frerebeau |
|
| 53 |
#' @keywords internal |
|
| 54 |
#' @noRd |
|
| 55 |
ramanujan <- function(x){
|
|
| 56 | 467x |
x * log(x) - x + log(x * (1 + 4 * x * (1 + 2 * x))) / 6 + log(pi) / 2 |
| 57 |
} |
| 1 |
# PLOT SPOT |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_spot |
|
| 7 |
#' @aliases plot_spot,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_spot", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, type = c("ring", "plain"),
|
|
| 12 |
color = NULL, |
|
| 13 |
diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 14 |
freq = FALSE, margin = 1, |
|
| 15 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 16 |
## Validation |
|
| 17 | 7x |
type <- match.arg(type, several.ok = FALSE) |
| 18 | ||
| 19 |
## Backward compatibility |
|
| 20 | 7x |
col <- list(...)$col |
| 21 | ! |
if (!is.null(col) && is.null(color)) color <- col |
| 22 | ||
| 23 | 7x |
plot_matrix(object, panel = panel_spot, color = color, |
| 24 | 7x |
diag = diag, upper = upper, lower = lower, |
| 25 | 7x |
freq = freq, margin = margin, drop_zero = TRUE, |
| 26 | 7x |
axes = axes, legend = legend, type = type) |
| 27 | ||
| 28 | 7x |
invisible(object) |
| 29 |
} |
|
| 30 |
) |
|
| 31 | ||
| 32 |
#' @export |
|
| 33 |
#' @rdname plot_spot |
|
| 34 |
#' @aliases plot_spot,data.frame-method |
|
| 35 |
setMethod( |
|
| 36 |
f = "plot_spot", |
|
| 37 |
signature = signature(object = "data.frame"), |
|
| 38 |
definition = function(object, type = c("ring", "plain"),
|
|
| 39 |
color = NULL, |
|
| 40 |
diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 41 |
freq = FALSE, margin = 1, |
|
| 42 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 43 | 4x |
object <- data.matrix(object) |
| 44 | 4x |
methods::callGeneric(object, type = type, color = color, |
| 45 | 4x |
diag = diag, upper = upper, lower = lower, |
| 46 | 4x |
freq = freq, margin = margin, |
| 47 | 4x |
axes = axes, legend = legend, ...) |
| 48 |
} |
|
| 49 |
) |
|
| 50 | ||
| 51 |
#' @export |
|
| 52 |
#' @rdname plot_spot |
|
| 53 |
#' @aliases plot_spot,dist-method |
|
| 54 |
setMethod( |
|
| 55 |
f = "plot_spot", |
|
| 56 |
signature = signature(object = "dist"), |
|
| 57 |
definition = function(object, type = c("ring", "plain"),
|
|
| 58 |
color = NULL, |
|
| 59 |
diag = FALSE, upper = FALSE, lower = !upper, |
|
| 60 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 61 |
# index_name <- attr(object, "method") |
|
| 62 | 3x |
object <- as.matrix(object) |
| 63 | 3x |
methods::callGeneric(object, type = type, color = color, |
| 64 | 3x |
diag = diag, upper = upper, lower = lower, |
| 65 | 3x |
axes = axes, legend = legend) |
| 66 |
} |
|
| 67 |
) |
| 1 |
# PLOT HEATMAP |
|
| 2 |
#' @include AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname plot_heatmap |
|
| 7 |
#' @aliases plot_heatmap,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "plot_heatmap", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object, color = NULL, |
|
| 12 |
diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 13 |
freq = FALSE, margin = 1, fixed_ratio = TRUE, |
|
| 14 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 15 |
## Backward compatibility |
|
| 16 | 7x |
col <- list(...)$col |
| 17 | ! |
if (!is.null(col) && is.null(color)) color <- col |
| 18 | ||
| 19 | 7x |
plot_matrix(object, panel = panel_tiles, color = color, |
| 20 | 7x |
diag = diag, upper = upper, lower = lower, |
| 21 | 7x |
freq = freq, margin = margin, drop_zero = FALSE, |
| 22 | 7x |
axes = axes, legend = legend, asp = fixed_ratio) |
| 23 | ||
| 24 | 7x |
invisible(object) |
| 25 |
} |
|
| 26 |
) |
|
| 27 | ||
| 28 |
#' @export |
|
| 29 |
#' @rdname plot_heatmap |
|
| 30 |
#' @aliases plot_heatmap,data.frame-method |
|
| 31 |
setMethod( |
|
| 32 |
f = "plot_heatmap", |
|
| 33 |
signature = signature(object = "data.frame"), |
|
| 34 |
definition = function(object, color = NULL, |
|
| 35 |
diag = TRUE, upper = TRUE, lower = TRUE, |
|
| 36 |
freq = FALSE, margin = 1, fixed_ratio = TRUE, |
|
| 37 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 38 | 4x |
object <- data.matrix(object) |
| 39 | 4x |
methods::callGeneric(object, color = color, |
| 40 | 4x |
diag = diag, upper = upper, lower = lower, |
| 41 | 4x |
freq = freq, margin = margin, |
| 42 | 4x |
fixed_ratio = fixed_ratio, |
| 43 | 4x |
axes = axes, legend = legend) |
| 44 |
} |
|
| 45 |
) |
|
| 46 | ||
| 47 |
#' @export |
|
| 48 |
#' @rdname plot_heatmap |
|
| 49 |
#' @aliases plot_heatmap,dist-method |
|
| 50 |
setMethod( |
|
| 51 |
f = "plot_heatmap", |
|
| 52 |
signature = signature(object = "dist"), |
|
| 53 |
definition = function(object, color = NULL, |
|
| 54 |
diag = FALSE, upper = FALSE, lower = !upper, |
|
| 55 |
axes = TRUE, legend = TRUE, ...) {
|
|
| 56 | 2x |
object <- as.matrix(object) |
| 57 | 2x |
methods::callGeneric(object, color = color, |
| 58 | 2x |
diag = diag, upper = upper, lower = lower, |
| 59 | 2x |
axes = axes, legend = legend) |
| 60 |
} |
|
| 61 |
) |
| 1 |
#' @include AllGenerics.R AllClasses.R |
|
| 2 |
NULL |
|
| 3 | ||
| 4 |
# Shannon ====================================================================== |
|
| 5 |
variance_shannon <- function(x, base = exp(1), na.rm = FALSE, ...) {
|
|
| 6 |
## Validation |
|
| 7 | 5x |
x <- x[x > 0] # Remove unobserved species |
| 8 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 9 | ! |
if (anyNA(x)) return(NA) |
| 10 | ||
| 11 | 5x |
N <- sum(x) |
| 12 | 5x |
S <- length(x) |
| 13 | 5x |
p <- x / N |
| 14 | ||
| 15 | 5x |
a <- sum(p * (log(p, base = base))^2) |
| 16 | 5x |
b <- sum(p * log(p, base = base))^2 |
| 17 | ||
| 18 | 5x |
var <- ((a - b) / N) + ((S - 1) / (2 * N^2)) |
| 19 | 5x |
var |
| 20 |
} |
|
| 21 | ||
| 22 |
#' @export |
|
| 23 |
#' @rdname test |
|
| 24 |
#' @aliases test_shannon,numeric,numeric-method |
|
| 25 |
setMethod( |
|
| 26 |
f = "test_shannon", |
|
| 27 |
signature = c(x = "numeric", y = "numeric"), |
|
| 28 |
definition = function(x, y, ...) {
|
|
| 29 |
## Validation |
|
| 30 | 2x |
arkhe::assert_length(y, length(x)) |
| 31 | ||
| 32 |
## Calculate the number of individuals |
|
| 33 | 2x |
Nx <- sum(x, na.rm = TRUE) |
| 34 | 2x |
Ny <- sum(y, na.rm = TRUE) |
| 35 | ||
| 36 |
## Calculate Shannon diversity |
|
| 37 |
## See PAST documentation, p. 208 |
|
| 38 | 2x |
d <- (sum(x > 0) - 1) / (2 * sum(x)) |
| 39 | 2x |
Hx <- index_shannon(x, ...) |
| 40 | 2x |
Hy <- index_shannon(y, ...) |
| 41 | ||
| 42 |
## Calculate Shannon variance |
|
| 43 | 2x |
Vx <- variance_shannon(x, ...) |
| 44 | 2x |
Vy <- variance_shannon(y, ...) |
| 45 | ||
| 46 |
## t test statistic |
|
| 47 | 2x |
tt <- (Hx - Hy) / sqrt(Vx + Vy) |
| 48 | ||
| 49 |
## Degrees of freedom |
|
| 50 | 2x |
df <- (Vx + Vy)^2 / sum(c(Vx, Vy)^2 / c(Nx, Ny)) |
| 51 | ||
| 52 |
## p value |
|
| 53 | 2x |
p <- 2 * (1 - stats::pt(q = abs(tt), df = df)) |
| 54 | ||
| 55 | 2x |
list( |
| 56 | 2x |
statistic = tt, |
| 57 | 2x |
parameter = df, |
| 58 | 2x |
p.value = p |
| 59 |
) |
|
| 60 |
} |
|
| 61 |
) |
|
| 62 | ||
| 63 |
#' @export |
|
| 64 |
#' @describeIn test Produces two sided pairwise comparisons. |
|
| 65 |
#' @aliases test_shannon,matrix,missing-method |
|
| 66 |
setMethod( |
|
| 67 |
f = "test_shannon", |
|
| 68 |
signature = c(x = "matrix", y = "missing"), |
|
| 69 |
definition = function(x, adjust = "holm", ...) {
|
|
| 70 |
## Get the names of the assemblages |
|
| 71 | 1x |
row_names <- rownames(x) |
| 72 | 1x |
if (length(row_names) != 0) {
|
| 73 | 1x |
row_names <- factor(row_names, levels = unique(row_names)) |
| 74 |
} else {
|
|
| 75 | ! |
row_names <- factor(seq_len(nrow(x))) |
| 76 |
} |
|
| 77 | ||
| 78 |
## Compute t test |
|
| 79 | 1x |
compare <- function(i, j) {
|
| 80 | 1x |
test_shannon(x[i, ], x[j, ], ...)$p.value |
| 81 |
} |
|
| 82 | ||
| 83 | 1x |
result <- stats::pairwise.table( |
| 84 | 1x |
compare.levels = compare, |
| 85 | 1x |
level.names = row_names, |
| 86 | 1x |
p.adjust.method = adjust |
| 87 |
) |
|
| 88 | 1x |
result |
| 89 |
} |
|
| 90 |
) |
|
| 91 | ||
| 92 |
#' @export |
|
| 93 |
#' @describeIn test Produces two sided pairwise comparisons. |
|
| 94 |
#' @aliases test_shannon,data.frame,missing-method |
|
| 95 |
setMethod( |
|
| 96 |
f = "test_shannon", |
|
| 97 |
signature = c(x = "data.frame", y = "missing"), |
|
| 98 |
definition = function(x, adjust = "holm", ...) {
|
|
| 99 | ! |
x <- data.matrix(x) |
| 100 | ! |
methods::callGeneric(x, adjust = adjust, ...) |
| 101 |
} |
|
| 102 |
) |
|
| 103 | ||
| 104 |
# Simpson ====================================================================== |
|
| 105 |
variance_simpson <- function(x, na.rm = FALSE, ...) {
|
|
| 106 |
## Validation |
|
| 107 | 4x |
x <- x[x > 0] # Remove unobserved species |
| 108 | ! |
if (na.rm) x <- stats::na.omit(x) # Remove NAs |
| 109 | ! |
if (anyNA(x)) return(NA) |
| 110 | ||
| 111 | 4x |
N <- sum(x) |
| 112 | 4x |
S <- length(x) |
| 113 | 4x |
p <- x / N |
| 114 | ||
| 115 | 4x |
a <- 4 * N * (N - 1) * (N - 2) * sum(p^3) |
| 116 | 4x |
b <- 2 * N * (N - 1) * sum(p^2) |
| 117 | 4x |
c <- 2 * N * (N - 1) * (2 * N - 3) * sum(p^2)^2 |
| 118 | ||
| 119 | 4x |
var <- (a + b - c) / (N^2 * (N - 1)^2) |
| 120 | 4x |
var |
| 121 |
} |
|
| 122 | ||
| 123 |
#' @export |
|
| 124 |
#' @rdname test |
|
| 125 |
#' @aliases test_simpson,numeric,numeric-method |
|
| 126 |
setMethod( |
|
| 127 |
f = "test_simpson", |
|
| 128 |
signature = c(x = "numeric", y = "numeric"), |
|
| 129 |
definition = function(x, y, adjust = "holm", ...) {
|
|
| 130 |
## Validation |
|
| 131 | 2x |
arkhe::assert_length(y, length(x)) |
| 132 | ||
| 133 |
## Calculate the number of individuals |
|
| 134 | 2x |
Nx <- sum(x, na.rm = TRUE) |
| 135 | 2x |
Ny <- sum(y, na.rm = TRUE) |
| 136 | ||
| 137 |
## Calculate Shannon diversity |
|
| 138 | 2x |
Hx <- index_simpson(x, ...) |
| 139 | 2x |
Hy <- index_simpson(y, ...) |
| 140 | ||
| 141 |
## Calculate Shannon variance |
|
| 142 | 2x |
Vx <- variance_simpson(x, ...) |
| 143 | 2x |
Vy <- variance_simpson(y, ...) |
| 144 | ||
| 145 |
## t test statistic |
|
| 146 | 2x |
tt <- (Hx - Hy) / sqrt(Vx + Vy) |
| 147 | ||
| 148 |
## Degrees of freedom |
|
| 149 | 2x |
df <- (Vx + Vy)^2 / sum(c(Vx, Vy)^2 / c(Nx, Ny)) |
| 150 | ||
| 151 |
## p value |
|
| 152 | 2x |
p <- 2 * (1 - stats::pt(q = abs(tt), df = df)) |
| 153 | ||
| 154 | 2x |
list( |
| 155 | 2x |
statistic = tt, |
| 156 | 2x |
parameter = df, |
| 157 | 2x |
p.value = p |
| 158 |
) |
|
| 159 |
} |
|
| 160 |
) |
|
| 161 | ||
| 162 |
#' @export |
|
| 163 |
#' @describeIn test Produces two sided pairwise comparisons. |
|
| 164 |
#' @aliases test_simpson,matrix,missing-method |
|
| 165 |
setMethod( |
|
| 166 |
f = "test_simpson", |
|
| 167 |
signature = c(x = "matrix", y = "missing"), |
|
| 168 |
definition = function(x, adjust = "holm", ...) {
|
|
| 169 |
## Get the names of the assemblages |
|
| 170 | 1x |
row_names <- rownames(x) |
| 171 | 1x |
if (length(row_names) != 0) {
|
| 172 | 1x |
row_names <- factor(row_names, levels = unique(row_names)) |
| 173 |
} else {
|
|
| 174 | ! |
row_names <- factor(seq_len(nrow(x))) |
| 175 |
} |
|
| 176 | ||
| 177 |
## Compute t test |
|
| 178 | 1x |
compare <- function(i, j) {
|
| 179 | 1x |
test_simpson(x[i, ], x[j, ])$p.value |
| 180 |
} |
|
| 181 | ||
| 182 | 1x |
result <- stats::pairwise.table( |
| 183 | 1x |
compare.levels = compare, |
| 184 | 1x |
level.names = row_names, |
| 185 | 1x |
p.adjust.method = adjust |
| 186 |
) |
|
| 187 | 1x |
result |
| 188 |
} |
|
| 189 |
) |
|
| 190 | ||
| 191 |
#' @export |
|
| 192 |
#' @describeIn test Produces two sided pairwise comparisons. |
|
| 193 |
#' @aliases test_simpson,data.frame,missing-method |
|
| 194 |
setMethod( |
|
| 195 |
f = "test_simpson", |
|
| 196 |
signature = c(x = "data.frame", y = "missing"), |
|
| 197 |
definition = function(x, adjust = "holm", ...) {
|
|
| 198 | ! |
x <- data.matrix(x) |
| 199 | ! |
methods::callGeneric(x, adjust = adjust) |
| 200 |
} |
|
| 201 |
) |
| 1 |
# MUTATORS |
|
| 2 |
#' @include AllClasses.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# Getters ====================================================================== |
|
| 6 |
#' @export |
|
| 7 |
#' @method labels DiversityIndex |
|
| 8 | ! |
labels.DiversityIndex <- function(object, ...) object@labels |
| 9 | ||
| 10 |
#' @rdname mutators |
|
| 11 |
#' @aliases labels,DiversityIndex-method |
|
| 12 |
setMethod("labels", "DiversityIndex", labels.DiversityIndex)
|
|
| 13 | ||
| 14 |
#' @export |
|
| 15 |
#' @method labels RarefactionIndex |
|
| 16 | 2x |
labels.RarefactionIndex <- function(object, ...) object@labels |
| 17 | ||
| 18 |
#' @rdname mutators |
|
| 19 |
#' @aliases labels,RarefactionIndex-method |
|
| 20 |
setMethod("labels", "RarefactionIndex", labels.RarefactionIndex)
|
|
| 21 | ||
| 22 |
#' @export |
|
| 23 |
#' @rdname mutators |
|
| 24 |
#' @aliases get_method,DiversityIndex-method |
|
| 25 |
setMethod( |
|
| 26 |
f = "get_method", |
|
| 27 |
signature = "DiversityIndex", |
|
| 28 | 14x |
definition = function(x) x@method |
| 29 |
) |
|
| 30 | ||
| 31 |
# Setters ====================================================================== |
| 1 |
# SERIOGRAPH |
|
| 2 |
#' @include AllClasses.R AllGenerics.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
#' @export |
|
| 6 |
#' @rdname seriograph |
|
| 7 |
#' @aliases eppm,matrix-method |
|
| 8 |
setMethod( |
|
| 9 |
f = "eppm", |
|
| 10 |
signature = signature(object = "matrix"), |
|
| 11 |
definition = function(object) {
|
|
| 12 | 4x |
eppm <- (object - expected(object)) * 100 / rowSums(object) |
| 13 | 4x |
eppm[eppm < 0] <- 0 |
| 14 | 4x |
dimnames(eppm) <- dimnames(object) |
| 15 | 4x |
eppm |
| 16 |
} |
|
| 17 |
) |
|
| 18 | ||
| 19 |
#' @export |
|
| 20 |
#' @rdname seriograph |
|
| 21 |
#' @aliases eppm,data.frame-method |
|
| 22 |
setMethod( |
|
| 23 |
f = "eppm", |
|
| 24 |
signature = signature(object = "data.frame"), |
|
| 25 |
definition = function(object) {
|
|
| 26 | 1x |
object <- data.matrix(object) |
| 27 | 1x |
methods::callGeneric(object) |
| 28 |
} |
|
| 29 |
) |
|
| 30 | ||
| 31 |
#' @export |
|
| 32 |
#' @rdname seriograph |
|
| 33 |
#' @aliases seriograph,matrix-method |
|
| 34 |
setMethod( |
|
| 35 |
f = "seriograph", |
|
| 36 |
signature = signature(object = "matrix"), |
|
| 37 |
definition = function(object, weights = FALSE, |
|
| 38 |
fill = "darkgrey", border = NA, |
|
| 39 |
axes = TRUE, ...) {
|
|
| 40 | 1x |
plot_ford(object, weights = weights, EPPM = TRUE, |
| 41 | 1x |
fill = fill, border = border, axes = axes) |
| 42 | 1x |
invisible(object) |
| 43 |
} |
|
| 44 |
) |
|
| 45 | ||
| 46 |
#' @export |
|
| 47 |
#' @rdname seriograph |
|
| 48 |
#' @aliases seriograph,data.frame-method |
|
| 49 |
setMethod( |
|
| 50 |
f = "seriograph", |
|
| 51 |
signature = signature(object = "data.frame"), |
|
| 52 |
definition = function(object, weights = FALSE, |
|
| 53 |
fill = "darkgrey", border = NA, |
|
| 54 |
axes = TRUE, ...) {
|
|
| 55 | 1x |
object <- data.matrix(object) |
| 56 | 1x |
methods::callGeneric(object, weights = weights, |
| 57 | 1x |
fill = fill, border = border, axes = axes) |
| 58 |
} |
|
| 59 |
) |
| 1 |
# SHOW METHODS |
|
| 2 |
#' @include AllClasses.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
setMethod( |
|
| 6 |
f = "show", |
|
| 7 |
signature = "DiversityIndex", |
|
| 8 |
definition = function(object) {
|
|
| 9 | ! |
methods::callGeneric(object@.Data) |
| 10 |
} |
|
| 11 |
) |
|
| 12 |
setMethod( |
|
| 13 |
f = "show", |
|
| 14 |
signature = "RarefactionIndex", |
|
| 15 |
definition = function(object) {
|
|
| 16 | ! |
methods::callGeneric(object@.Data) |
| 17 |
} |
|
| 18 |
) |
| 1 |
# COERCION |
|
| 2 |
#' @include AllGenerics.R AllClasses.R |
|
| 3 |
NULL |
|
| 4 | ||
| 5 |
# To data.frame ================================================================ |
|
| 6 |
#' @method as.data.frame DiversityIndex |
|
| 7 |
#' @export |
|
| 8 |
as.data.frame.DiversityIndex <- function(x, ...) {
|
|
| 9 | ! |
data.frame( |
| 10 | ! |
size = x@size, |
| 11 | ! |
observed = apply(X = x@data, MARGIN = 1, FUN = observed), |
| 12 | ! |
singleton = apply(X = x@data, MARGIN = 1, FUN = singleton), |
| 13 | ! |
doubleton = apply(X = x@data, MARGIN = 1, FUN = doubleton), |
| 14 | ! |
index = x@.Data, |
| 15 | ! |
row.names = labels(x), |
| 16 | ! |
stringsAsFactors = FALSE |
| 17 |
) |
|
| 18 |
} |
|
| 19 | ||
| 20 |
#' @export |
|
| 21 |
#' @rdname data.frame |
|
| 22 |
#' @aliases as.data.frame,DiversityIndex-method |
|
| 23 |
setMethod("as.data.frame", "DiversityIndex", as.data.frame.DiversityIndex)
|