-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExperiments.R
186 lines (144 loc) · 6.35 KB
/
Experiments.R
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
library(divermeta)
# Experiments
# ----------------------------
# Taxonomic
# --------
# High Multiplicity
# Three clusters of each ten different units
# Multiplicity should be 10
ab <- rep(10,30)
clust <- c(rep(1,10), rep(2,10), rep(3,10))
print(paste("Multiplicity: ",multiplicity.taxonomic(ab, clust)))
# Low Multiplicity
# Three clusters of each one different units
# Multiplicity should be 1
ab <- rep(10,3)
clust <- c(1,2,3)
print(paste("Multiplicity: ",multiplicity.taxonomic(ab, clust)))
# Functional
# ----------
# The idea of this experiments is to see why we needed a tweaked definition of
# functional diversity if we want to use ratio of the unclustered / clustersed
# as the definition of Multiplicity.
# The traditional functional diversity by Chao fails in some cases to capture
# what we want
# Basic
# ----------------------------
# n units with max distance and equal abundance
# functional diversity should be n
# Both work
sig <- 0.5
n <- 10
ab <- matrix(1, nrow = 1, ncol = n)
diss <- matrix(sig, nrow = n, ncol = n)
diag(diss) <- 0
print("Basic")
print(paste("Functional diversity (traditional):", diversity.functional.traditional(ab, diss)))
print(paste("Functional diversity", diversity.functional(ab, diss, sig)))
print("")
print("")
print("")
# Doubling property
# ----------------------------
# Builds an assemblage of three identical groups, inter Clustered distance are maximum.
# The diversity of the assemblage should be three times the individual
# diversity.
# Note: We use ratio to check for doubeling property, but this is not
# multiplicity since there is there is no actual clustering.
# Traditional is pretty close, the tweaked one we use works fine.
# Ratio should be 1
sig <- 0.9
n <- 10
ab_unit <- rbind(runif(n, 100, 1000))
diss_unit <- random_matrix <- matrix(runif(n * n, min = 0.3, max = 0.6), nrow = n, ncol = n)
diag(diss_unit) <- 0
div_trad <- diversity.functional.traditional(ab_unit, diss_unit)
div <- diversity.functional(ab_unit, diss_unit, sig)
# Assemblage
diss <- matrix(sig, nrow = 3 * n, ncol = 3 * n)
diss[1:n, 1:n] <- diss_unit
diss[(n + 1):(2 * n), (n + 1):(2 * n)] <- diss_unit
diss[(2 * n + 1):(3 * n), (2 * n + 1):(3 * n)] <- diss_unit
ab <- cbind(ab_unit, ab_unit, ab_unit)
print("Doubleling Property")
print(paste("Unit - Functional diversity (traditional):", diversity.functional.traditional(ab_unit, diss_unit)))
print(paste("Assemblage - Functional diversity (traditional):", diversity.functional.traditional(ab, diss)))
print(paste("Ratio:", diversity.functional.traditional(ab, diss) / diversity.functional.traditional(ab_unit, diss_unit)))
print("")
print(paste("Unit - Functional diversity", diversity.functional(ab_unit, diss_unit, sig)))
print(paste("Assemblage - Functional diversity", diversity.functional(ab, diss, sig)))
print(paste("Ratio:", diversity.functional(ab, diss, sig) / diversity.functional(ab_unit, diss_unit, sig)))
print("")
print("")
print("")
# Assemblage of units
# ----------------------------
# Diversity of an assemblage of groups of identical units (distance zero
# between them) should be the same as an assemblage of representative units (a
# matrix of ones with zero on the diagonal)
# Only the tweaked one we use works, the traditional one fails.
# ratio (or multiplicity) should be 1
sig <- 1
n <- 10
ab_unit <- rbind(runif(n, 100, 1000))
diss_unit <- matrix(0, nrow = n, ncol = n)
div_trad <- diversity.functional.traditional(ab_unit, diss_unit)
div <- diversity.functional(ab_unit, diss_unit, sig)
# Assemblage
diss <- matrix(sig, nrow = 3 * n, ncol = 3 * n)
diss[1:n, 1:n] <- diss_unit
diss[(n + 1):(2 * n), (n + 1):(2 * n)] <- diss_unit
diss[(2 * n + 1):(3 * n), (2 * n + 1):(3 * n)] <- diss_unit
ab <- cbind(ab_unit, ab_unit, ab_unit)
# Equivalent (Clusteres)
ab_clust <- cbind(sum(ab_unit), sum(ab_unit), sum(ab_unit))
diss_clust <- matrix(sig, ncol = 3, nrow = 3)
diag(diss_clust) <- 0
print("Assemblage of Identical units")
print(paste("Assemblage - Functional diversity (traditional):", diversity.functional.traditional(ab, diss)))
print(paste("Representatives - Functional diversity (traditional):", diversity.functional.traditional(ab_clust, diss_clust)))
print(paste("Ratio:", diversity.functional.traditional(ab, diss) / diversity.functional.traditional(ab_clust, diss_clust)))
print("")
print(paste("Assemblage - Functional diversity", diversity.functional(ab, diss, sig)))
print(paste("Representatives - Functional diversity", diversity.functional(ab_clust, diss_clust, sig)))
print(paste("Ratio (Multiplicity):", diversity.functional(ab, diss, sig) / diversity.functional(ab_clust, diss_clust, sig)))
print("")
print("")
print("")
# Assemblage of units with some multiplicity
# Same as the previous experiment but instead of identical, they will have
# some variability, increasing the multiplicity.
# Move around some of the parameters to see build intuition
# Parameters
sig <- 0.5
n <- 10
min_abundance = 100
max_abundance = 1000
min_intra_distance = 0.1
max_intra_distance = 0.5
ab_unit <- rbind(runif(n, min_abundance, max_abundance))
diss_unit <- matrix(runif(n * n, min = min_intra_distance, max = max_intra_distance), nrow = n, ncol = n)
diag(diss_unit) <- 0
div_trad <- diversity.functional.traditional(ab_unit, diss_unit)
div <- diversity.functional(ab_unit, diss_unit, sig)
# Assemblage
diss <- matrix(sig, nrow = 3 * n, ncol = 3 * n)
diss[1:n, 1:n] <- diss_unit
diss[(n + 1):(2 * n), (n + 1):(2 * n)] <- diss_unit
diss[(2 * n + 1):(3 * n), (2 * n + 1):(3 * n)] <- diss_unit
ab <- cbind(ab_unit, ab_unit, ab_unit)
# Equivalent (Clusteres)
ab_clust <- cbind(sum(ab_unit), sum(ab_unit), sum(ab_unit))
diss_clust <- matrix(sig, ncol = 3, nrow = 3)
diag(diss_clust) <- 0
print("Assemblage of Identical units")
print(paste("Assemblage - Functional diversity (traditional):", diversity.functional.traditional(ab, diss)))
print(paste("Representatives - Functional diversity (traditional):", diversity.functional.traditional(ab_clust, diss_clust)))
print(paste("Ratio:", diversity.functional.traditional(ab, diss) / diversity.functional.traditional(ab_clust, diss_clust)))
print("")
print(paste("Assemblage - Functional diversity", diversity.functional(ab, diss, sig)))
print(paste("Representatives - Functional diversity", diversity.functional(ab_clust, diss_clust, sig)))
print(paste("Ratio (Multiplicity):", diversity.functional(ab, diss, sig) / diversity.functional(ab_clust, diss_clust, sig)))
print("")
print("")
print("")