-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction.r
385 lines (309 loc) · 12.3 KB
/
function.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
### ==================================================
### This file contains the functions used by NorInAliS
### ==================================================
## Auxiliary functions
## -------------------
# Combines text strings
"%+%" <- function(string1, string2) paste0(string1, string2)
# Removes an element of a set
"%-%" <- function(arg1, arg2) sort(unique(arg1[which(!(arg1 %in% na.omit(arg2)))]))
# Calculates the union of two sets (vectors)
"%A%" <- function(set1, set2)
if (is.null(set1)) logical(0) else as.vector(na.omit(set1[set1 %in% set2]))
# I just find this more intuitive...
"%contains%" <- function (textstring, searchtext) grepl(searchtext, textstring)
# Calculates a running mean
running.mean <- function(x, n) {
z <- y <- x
if (n > 1) {
for (i in 2:n) {
z <- c(z[-1], NA)
y <- y + z
}
}
return(y / n)
}
## Functions to calculate indicator S2
## -----------------------------------
asFreq <- function(x) {
# translates a frequency interval into a numerical value
# Note that input is in events per decade,
# whereas output is in events per year!
sapply(x, function(z) switch(z,
"<1"=0.07, "1-8"=0.5, "9-19"=1.4, ">19"=4.6, NA))
}
asAbund <- function(x) {
# translates an abundance interval into a numerical value
sapply(x, function(z) switch(z,
"1"=1, "2-10"=5, "11-100"=50, "101-1000"=500, ">1000"=5000, NA))
}
rincr <- function(n, min, max, r=TRUE) {
# generates random numbers according to an increasing
# triangular probability distribution
min + sqrt( if(r) runif(n) else 0.5) * (max - min)
}
rdecr <- function(n, min, max, r=TRUE) {
# generates random numbers according to a decreasing
# triangular probability distribution
max - sqrt(1 - if(r) runif(n) else 0.5) * (max - min)
}
assignFrequencies <- function(n, pathways, pw, r=TRUE) {
# assigns unknown frequencies according to the distribution of known frequencies,
# separately for main pathway categories
freq <- table(
pathways$Freq[which(pathways$Freq != "unknown" & pathways$Cat == pw)]
)[c("<1", "1-8", "9-19", ">19")]
if (any(is.na(freq))) {
freq[which(is.na(freq))] <- 0
}
freq <- freq / sum(freq)
for (i in 4:2) {
freq[i] <- sum(freq[1:i])
}
seq <- if (r) runif(n) else (1:n - 0.5) / n
return(c("<1", "1-8", "9-19", ">19")[sapply(seq, function(x) which(freq > x)[1])])
}
S2a <- function(pathways) {
# estimates S2(a) according to Equation ¤1
return(length(unique(
(pathways$Name %+% pathways$Subcat)[pathways$Introd & pathways$Time == "current"]
)))
}
S2b <- function(pathways, nsim=0, CL=c(0.025, 0.25, 0.5, 0.75, 0.975)) {
# estimates S2(b) according to Equation ¤2
pathways <- pathways[which(pathways$Introd & pathways$Time == "current"),]
results <- matrix(0, nrow(pathways), max(1, nsim))
freq <- matrix(pathways$Freq, nrow(pathways), max(1, nsim))
for (pw in unique(pathways$Cat)) {
w <- which(pathways$Freq == "unknown" & pathways$Cat == pw)
if (length(w)) {
freq[w,] <- assignFrequencies(length(w) * max(1, nsim),
pathways, pw, r = nsim >= 1)
}
}
W <- freq == "<1"
results[W] <- rincr(sum(W), 0.01, 0.1, r = nsim >= 1)
W <- freq == "1-8"
results[W] <- if (nsim >= 1) runif(sum(W), 0.10, 0.9) else 0.5
W <- freq == "9-19"
results[W] <- if (nsim >= 1) runif(sum(W), 0.90, 1.9) else 1.4
W <- freq == ">19"
results[W] <- rdecr(sum(W), 1.90, 10.0, r = nsim >= 1)
if (nsim >=1 ) {
freq <- c(mean(apply(results, 2, sum)),
sd(apply(results, 2, sum)),
quantile(apply(results, 2, sum), CL))
names(freq) <- c("Average", "St.Dev.", (CL * 100) %+% "%CL")
return(freq)
} else {
return(sum(asFreq(freq)))
}
}
S2c <- function(pathways) {
# estimates S2(c) according to Equation ¤3
# (Note: this is a minimum implementation of indicator definition S2c!
# It illustrates the way S2c would be estimated, but it currently ignores
# uncertainty and simply assumes that the (utterly few) abundances reported
# are in fact representative of the unknown abundances, which is unlikely.)
freq <- asFreq(pathways$Freq[which(pathways$Introd & pathways$Time == "current")])
w <- which(is.na(freq))
if (length(w)) {
mod <- lm(asFreq(Freq) ~ Cat, data=pathways, subset=which(Freq != "unknown"))
freq[w] <- predict(mod, data.frame(Cat = pathways$Cat[w]))
}
abund <- asAbund(pathways$Abund[pathways$Introd & pathways$Time == "current"])
w <- which(is.na(abund))
if (length(w)) {
abund[w] <- mean(abund[-w])
}
return(sum(freq * abund))
}
## Functions to calculate indicator S3
## -----------------------------------
asFreq <- function(x) {
# translates a frequency interval into a numerical value
# Note that input is in events per decade,
# whereas output is in events per year!
sapply(x, function(z) switch(z,
"<1"=0.07, "1-8"=0.5, "9-19"=1.4, ">19"=4.6, NA))
}
asAbund <- function(x) {
# translates an abundance interval into a numerical value
sapply(x, function(z) switch(z,
"1"=1, "2-10"=5, "11-100"=50, "101-1000"=500, ">1000"=5000, NA))
}
rincr <- function(n, min, max, r=TRUE) {
# generates random numbers according to an increasing
# triangular probability distribution
min + sqrt( if(r) runif(n) else 0.5) * (max - min)
}
rdecr <- function(n, min, max, r=TRUE) {
# generates random numbers according to a decreasing
# triangular probability distribution
max - sqrt(1 - if(r) runif(n) else 0.5) * (max - min)
}
assignFrequencies <- function(n, pathways, pw, r=TRUE) {
# assigns unknown frequencies according to the distribution of known frequencies,
# separately for main pathway categories
freq <- table(
pathways$Freq[which(pathways$Freq != "unknown" & pathways$Cat == pw)]
)[c("<1", "1-8", "9-19", ">19")]
if (any(is.na(freq))) {
freq[which(is.na(freq))] <- 0
}
freq <- freq / sum(freq)
for (i in 4:2) {
freq[i] <- sum(freq[1:i])
}
seq <- if (r) runif(n) else (1:n - 0.5) / n
return(c("<1", "1-8", "9-19", ">19")[sapply(seq, function(x) which(freq > x)[1])])
}
S3a <- function(pathways) {
# estimates S3(a) according to Equation ¤1
return(length(unique(
(pathways$Name %+% pathways$Subcat)[!pathways$Introd & pathways$Time == "current"]
)))
}
S3b <- function(pathways, nsim=0, CL=c(0.025, 0.25, 0.5, 0.75, 0.975)) {
# estimates S3(b) according to Equation ¤2
pathways <- pathways[which(!pathways$Introd & pathways$Time == "current"),]
results <- matrix(0, nrow(pathways), max(1, nsim))
freq <- matrix(pathways$Freq, nrow(pathways), max(1, nsim))
for (pw in unique(pathways$Cat)) {
w <- which(pathways$Freq == "unknown" & pathways$Cat == pw)
if (length(w)) {
freq[w,] <- assignFrequencies(length(w) * max(1, nsim),
pathways, pw, r = nsim >= 1)
}
}
W <- freq == "<1"
results[W] <- rincr(sum(W), 0.01, 0.1, r = nsim >= 1)
W <- freq == "1-8"
results[W] <- if (nsim >= 1) runif(sum(W), 0.10, 0.9) else 0.5
W <- freq == "9-19"
results[W] <- if (nsim >= 1) runif(sum(W), 0.90, 1.9) else 1.4
W <- freq == ">19"
results[W] <- rdecr(sum(W), 1.90, 10.0, r = nsim >= 1)
if (nsim >=1 ) {
freq <- c(mean(apply(results, 2, sum)),
sd(apply(results, 2, sum)),
quantile(apply(results, 2, sum), CL))
names(freq) <- c("Average", "St.Dev.", (CL * 100) %+% "%CL")
return(freq)
} else {
return(sum(asFreq(freq)))
}
}
S3c <- function(pathways) {
# estimates S3(c) according to Equation ¤3
# (Note: this is a minimum implementation of indicator definition S3c!
# It illustrates the way S3c would be estimated, but it currently ignores
# uncertainty and simply assumes that the (utterly few) abundances reported
# are in fact representative of the unknown abundances, which is unlikely.)
freq <- asFreq(pathways$Freq[!pathways$Introd & pathways$Time == "current"])
w <- which(is.na(freq))
if (length(w)) {
freq[w] <- mean(freq[-w])
}
abund <- asAbund(pathways$Abund[!pathways$Introd & pathways$Time == "current"])
w <- which(is.na(abund))
if (length(w)) {
abund[w] <- mean(abund[-w])
}
return(sum(freq * abund))
}
## Functions to calculate indicator P1
## -----------------------------------
as.nr <- function(x)
# assigns numerical values from 1 to 5 to the five ecological impact categories
return(sapply(x, function(z) switch(z, "SE"=5,"HI"=4,"PH"=3,"LO"=2,"NK"=1,NA)))
P1a <- function(dataset, column = "Impact")
# estimates P1(a)
return(length(which(dataset[, column] %in% c("SE", "HI", "PH", "LO", "NK"))))
P1b <- function(dataset, column = c("minImp", "Impact", "maxImp")) {
# estimates P1(b)
q0 <- sum(as.nr(dataset[, column[1]])) # minimum and 2.5% confidence level
q1 <- round(sum(as.nr(dataset[, column[2]]) * 0.75 +
as.nr(dataset[, column[1]]) * 0.25)) # 1st quartile
q2 <- sum(as.nr(dataset[, column[2]])) # best estimate (mean and median)
q3 <- round(sum(as.nr(dataset[, column[2]]) * 0.75 +
as.nr(dataset[, column[3]]) * 0.25)) # 3rd quartile
q4 <- sum(as.nr(dataset[, column[3]])) # maximum and 97.5% confidence level
SD <- round(sd(c(rep(q0, floor(P1a(dataset, column[2]) / 4)),
rep(q2, ceiling(P1a(dataset, column[2]) / 2)),
rep(q4, floor(P1a(dataset, column[2]) / 4)))))
# approximation of the S.D.
p1b <- c(q2, SD, q0, q1, q2, q3, q4)
names(p1b) <- c("Average", "St.Dev.", c(2.5, 25, 50, 75, 97.5) %+% "%CL")
return(p1b)
}
## Functions to calculate indicator P2
## -----------------------------------
# The first two functions use maximum-likelihood estimation to infer the
# standard deviation of the AOOs, based on the best estimate (median),
# low estimate (1st quartile) and high estimate (3rd quartile),
# and assuming a log-normal distribution.
f <- function(s, mean, q1, q3) return(
(q1 - qlnorm(0.25, log(mean) - exp(2*s)/2, exp(s)))^2 +
(q3 - qlnorm(0.75, log(mean) - exp(2*s)/2, exp(s)))^2
)
findSD <- function(Ex, q1, q3) return(
exp(optimise(f, c(-12, 12), mean=Ex, q1=q1, q3=q3)$min)
)
P2 <- function(dataset,
column = c("known", "low", "best", "high"),
nsim = 100000,
maxArea = 323800) {
# simulates AOOs for all species, which is the basis of indicator P2
N <- nsim # random numbers per species
M <- maxArea # maximum possible area in km^2
aoo <- dataset[, column]
AOO <- matrix(0, N, nrow(aoo))
for (i in 1:nrow(aoo)) {
if (any(is.na(aoo[i, c("low", "best", "high")]))) {
if (is.na(aoo$best[i])) {
if (is.na(aoo$known[i])) {
# if no AOO is provided, assume it is 0
AOO[, i] <- 0
} else {
# of no total AOOs are provided, assume they equal the known AOO
AOO[, i] <- aoo$known[i]
}
} else {
# if no low and high estimates are provided, use the best estimate
AOO[, i] <- aoo$best[i]
}
} else {
if (aoo$best[i] == 0) {
# if the best estimate is 0, keep it 0
AOO[, i] <- 0
} else {
# if low, best and high estimates are provided, estimate the standard
# deviation and generate log-normally distributed random numbers
SD <- findSD(aoo$best[i], aoo$low[i], aoo$high[i])
AOO[, i] <- qlnorm(runif(N), log(aoo$best[i]) - SD * SD / 2, SD)
AOO[, i] <- sapply(AOO[, i], min, M) # constrain to area of Norway
AOO[, i] <- ceiling(AOO[, i] / 4) * 4 # ensure multiples of 4 km^2
}
}
} # i
return(AOO)
} # P2
## Functions to calculate indicator E1
## -----------------------------------
E1 <- function(List1, List2,
ID1 = "Name", ID2 = "Name",
EstCat1 = "CategN", EstCat2 = "CategN") {
# It is crucially important that the identifiers are identical in the two lists.
# Here, the species names are used as identifiers.
# However, species names may change due to taxonomic revision.
# Therefore, the use of unequivocal (e.g. numerical) species identifiers is
# to be preferred.
w1 <- which(List1[, EstCat1] %in% c("C2", "C3", "D1", "D2", "E"))
# species that are reproducing unaidedly at time 1
w2 <- which(List2[, EstCat2] == "A")
# species that are absent from the assessment area at time 2
eradicated <- List1[w1, ID1] %A% List2[w2, ID2]
# union of the two sets of identifiers
return(length(eradicated))
}