/
other_functions.Rmd
155 lines (106 loc) · 5.04 KB
/
other_functions.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "Other functions of interest"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Other functions of interest}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>"
)
```
```{r setup}
library(funrar)
```
In addition to single index functions `funrar` proposes several functions to
help you compute functional rarity indices. The aim of this vignette is to
provide detailed examples of how to use each of them.
In this vignette we will use the dataset `aravo` (you can learn more about it
through `help("aravo", package = "ade4")`) from the `ade4` package as for the
"Getting Started" vignette:
```{r load-data}
data("aravo", package = "ade4")
# Extract the traits of all species of `aravo`
traits = aravo$traits
head(traits)
```
## Compute Distinctiveness from Global Pool - `distinctiveness_global()`
While functional distinctiveness has been envisioned initially at local scale,
that's what the `distinctiveness()` function computes, it can be useful to
compute it at regional scales. This functions provides exactly that based on a
provided functional dissimilarity matrix.
Using the `aravo` dataset we can compute the global distinctiveness of all species
across the dataset in the following way:
```{r global-di}
# Compute a Euclidean distance matrix (because all traits are quantitative)
dist_matrix = compute_dist_matrix(traits, metric = "euclidean")
# Compute global distinctiveness
global_di = distinctiveness_global(dist_matrix)
head(global_di)
```
The global distinctiveness is available in column `global_di`.
You can rename this column using the second argument of
`distinctiveness_global()`:
```{r global-di-2}
aravo_di = distinctiveness_global(dist_matrix, di_name = "aravo_di")
head(aravo_di)
```
**NB**: `distinctiveness_global()` assumes that all species in the provided
trait dataset are present at global/regional scale. It also assumes only a
presence-absence matrix.
## Compute functional rarity indices across combinations of traits
Distinctiveness is generally computed through a dissimilarity matrix that itself
comes from aggregating differences across multiple traits. Sometimes it can be
useful to compare it to distinctiveness computed on each trait separately. This
way, it is possible that some species appear distinct on certain traits and not
others. The two functions below let you do exactly that for distinctiveness and
uniqueness.
### Compare Distinctiveness computed on all traits and each trait - `distinctiveness_dimensions()`
The function `distinctiveness_dimensions()` computes species local
distinctiveness values from a site-species matrix and a trait data.frame.
It will return a data frame with distinctiveness computed from all traits taken
together and each trait taken separately.
Let's do this with the traits from the `aravo` dataset. The first argument of `distinctiveness_dimensions()` is supposed to be site-species matrix while the second should be a traits table:
```{r di-dimensions}
di_dim = distinctiveness_dimensions(as.matrix(aravo$spe), traits, metric = "euclidean")
str(di_dim, max.level = 1)
```
We see that it returns a list of 9 local distinctiveness matrices: one for each
trait and one for all traits taken together (named `di_all`).
### Compare Uniqueness computed on all traits and each trait - `uniqueness_dimensions()`
The function `uniqueness_dimensions()` computes species regional uniqueness
values from a site-species matrix and a trait data.frame. It will return a data
frame with uniqueness computed from all traits taken together and each trait
taken separately.
Let's do this with the traits from the `aravo` dataset. The first argument of `uniqueness_dimensions()` is supposed to be site-species matrix while the second should be a traits table:
```{r ui-dimensions}
ui_dim = uniqueness_dimensions(as.matrix(aravo$spe), traits, metric = "euclidean")
str(ui_dim, max.level = 1)
```
We see that it returns a data frame with 10 columns: one for species names,
one for each trait separately, and one for all traits taken together
(named `Ui_all`).
## Compute Relative Abundances - `make_relative()`
This function, probably largest source of citations of `funrar`, transforms a
matrix of absolute abundances to relative abundances. It takes an abundance
matrix and returns the same matrix with relative abundances.
We can check that with `aravo` site-species matrix:
```{r relative}
aravo_site_sp = as.matrix(aravo$spe)
# There are clearly abundances and not only presence-absence in this table
aravo_site_sp[1:5, 1:5]
# Compute total abundance per site
site_abundance = rowSums(aravo_site_sp)
head(site_abundance)
# Compute a relative abundance matrix
relative_site_sp = make_relative(aravo_site_sp)
relative_site_sp[1:5, 1:5]
rel_site_abundance = rowSums(relative_site_sp)
head(rel_site_abundance)
```
We see, through `rel_site_abundance`, that `make_relative()` transformed the
abundances in each site so that they all sum to one. It divides the abundance of
each species by the total abundance of the site.