Mercurial > repos > ecology > obisindicators
annotate analyze.r @ 2:01e08ae72a98 draft
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 6c70a542eb19d8e86dab692b4c08c6ecaf18edb7
author | ecology |
---|---|
date | Wed, 25 Jan 2023 20:31:16 +0000 |
parents | 1015a0070bac |
children |
rev | line source |
---|---|
0
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
1 #' Calculate Biodiversity Indicators, including ES50 (Hurlbert index) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
2 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
3 #' Calculate the expected number of marine species in a random sample of 50 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
4 #' individuals (records) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
5 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
6 #' @param df data frame with unique species observations containing columns: |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
7 #' `cell`, `species`, `records` |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
8 #' @param esn expected number of marine species |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
9 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
10 #' @return Data frame with the following extra columns: - `n`: number of records |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
11 #' - `sp`: species richness - `shannon`: Shannon index - `simpson`: Simpson |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
12 #' index - `es`: Hurlbert index (n = 50), i.e. expected species from 50 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
13 #' samples ES(50) - `hill_1`: Hill number `exp(shannon)` - `hill_2`: Hill |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
14 #' number `1/simpson` - `hill_inf`: Hill number `1/maxp` |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
15 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
16 #' @details The expected number of marine species in a random sample of 50 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
17 #' individuals (records) is an indicator on marine biodiversity richness. The |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
18 #' ES50 is defined in OBIS as the `sum(esi)` over all species of the following |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
19 #' per species calculation: |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
20 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
21 #' - when `n - ni >= 50 (with n as the total number of records in the cell and |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
22 #' ni the total number of records for the ith-species) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
23 #' - `esi = 1 - exp(lngamma(n-ni+1) + lngamma(n-50+1) - lngamma(n-ni-50+1) - lngamma(n+1))` |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
24 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
25 #' - when `n >= 50` - `esi = 1` |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
26 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
27 #' - else - `esi = NULL` |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
28 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
29 #' Warning: ES50 assumes that individuals are randomly distributed, the sample |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
30 #' size is sufficiently large, the samples are taxonomically similar, and that |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
31 #' all of the samples have been taken in the same manner. |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
32 #' |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
33 #' @export |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
34 #' @concept analyze |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
35 #' @examples |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
36 #' @importFrom gsl lngamma |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
37 calc_indicators <- function(df, esn = 50) { |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
38 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
39 stopifnot(is.data.frame(df)) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
40 stopifnot(is.numeric(esn)) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
41 stopifnot(all(c("cell", "species", "records") %in% names(df))) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
42 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
43 df %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
44 dplyr::group_by(cell, species) %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
45 dplyr::summarize( |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
46 ni = sum(records), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
47 .groups = "drop_last") %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
48 dplyr::mutate(n = sum(ni)) %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
49 dplyr::group_by(cell, species) %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
50 dplyr::mutate( |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
51 hi = -(ni / n * log(ni / n)), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
52 si = (ni / n)^2, |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
53 qi = ni / n, |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
54 esi = dplyr::case_when( |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
55 n - ni >= esn ~ 1 - exp(gsl::lngamma(n - ni + 1) + gsl::lngamma(n - esn + 1) - gsl::lngamma(n - ni - esn + 1) - gsl::lngamma(n + 1)), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
56 n >= esn ~ 1 |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
57 ) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
58 ) %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
59 dplyr::group_by(cell) %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
60 dplyr::summarize( |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
61 n = sum(ni), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
62 sp = dplyr::n(), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
63 shannon = sum(hi), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
64 simpson = sum(si), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
65 maxp = max(qi), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
66 es = sum(esi), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
67 .groups = "drop") %>% |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
68 dplyr::mutate( |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
69 hill_1 = exp(shannon), |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
70 hill_2 = 1 / simpson, |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
71 hill_inf = 1 / maxp) |
1015a0070bac
planemo upload for repository https://github.com/galaxyecology/tools-ecology/tree/master/tools/obisindicators commit 1e0b3b101d2380338030e607e6949331439c6dfc
ecology
parents:
diff
changeset
|
72 } |