-
Notifications
You must be signed in to change notification settings - Fork 1
/
expert.just1plot.R
503 lines (365 loc) · 16.4 KB
/
expert.just1plot.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
#This file is part of ElicitN.
#Copyright 2011 Rebecca Fisher and Rebecca O'Leary.
#ElicitN is free software: you can redistribute it and/or modify it under
#the terms of the GNU General Public License as published by the Free Software
#Foundation, either version 3 of the License, or any later version.
#This program is distributed in the hope that it will be useful, but WITHOUT ANY
#WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
#PARTICULAR PURPOSE. See the GNU General Public License
#(http://www.gnu.org/licenses/) for more details.
expert.just1plot<-function(Lower.N, Upper.N, Best.N,
Lower.P, Upper.P, Best.P,
Lower.M, Upper.M, Best.M, Sureness,new.alpha,
expert.K.ALLnorm.results,best.type.status){
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/expert.P.BETA.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("expert.P.BETA.R")
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/expert.P.BETA.just1plot.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("expert.P.BETA.just1plot.R")
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/expert.K.noplot.ALLNorm.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("expert.K.noplot.ALLNorm.R")
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/expert.K.noplot.ALLNorm.just1plot.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("expert.K.noplot.ALLNorm.just1plot.R")
#############################################
#IF UHAT = 0
#e.g. zero number of species are cryptic
#put all Uhat into a matrix
uhat.matrix <- c(Upper.N, Upper.P,Upper.M)
#test which Uhat does not contain NA
uhat.na <- which(c(is.na(Upper.N),is.na(Upper.P), is.na(Upper.M))=="FALSE")
#test if Uhat without NA value is equal to zero
if (uhat.matrix[uhat.na] ==0){
ss.mode.results <- list( mean=0,var=0, mode=0, skew=0, kurtosis=0, lower=0, median=0, upper=0)
feedback.mode.results <- list(lower=0, median=0, upper=0, mean=0, mode=0, prob.upper=0,
prob.lower=0, new.alpha=0, new.lower =0, new.upper=0)
output <- list(ss.mode.results=ss.mode.results,
fit.best.mode.mu=0,
fit.best.mode.sig=0,
feedback.mode.results=feedback.mode.results,
Ksp=0,
MhatK=0,
which.dist=0)
best.type =0
Cryp=0
} else {
X11()
##############################################
#IF 1
#EXPERT PROVIDES % OF KNOWN SPECIES (INFLATION)
if (is.na(Best.P) =="FALSE") {
expert.P.BETA.results <- expert.P.BETA.just1plot(Lower.P, Upper.P, Best.P, Sureness, new.alpha, best.type.status, ee.type=2)
###
#INFO FROM ELICITED KNOWN SPP.
#pihat is returned from expert.K.LN function
pihat <- expert.K.ALLnorm.results$pihat
#which distribution was used in expert.K.ALLnorm.results
which.dist <- expert.K.ALLnorm.results$which.dist
MhatK <-expert.K.ALLnorm.results$MhatK
#get values of mu, sig, lower.K & upper.K
mu <- expert.K.ALLnorm.results$fit.best.mode.mu
sig <- expert.K.ALLnorm.results$fit.best.mode.sig
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/feedback.AllNorm.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("feedback.AllNorm.R")
feedback.AllNorm.results <- feedback.AllNorm(mu, sig,Sureness,new.alpha, MhatK, which.dist)
lower.K <- feedback.AllNorm.results$lower
upper.K <- feedback.AllNorm.results$upper
mode.K <- feedback.AllNorm.results$mode
new.lower <- feedback.AllNorm.results$new.lower
new.upper <- feedback.AllNorm.results$new.upper
if (new.lower<0){
new.lower=0
}
if (lower.K<0){
lower.K=0
}
#####
#PLOT
#PERCENT of KNOWN SPECIES
####
X11()
par(mfrow=c(2,1), mar=c(1, 4.5, 4,.75)+.5, mgp=c(3, .75,0), omi=c(.1, .5, .1,.1))
if (expert.P.BETA.results$ssBETA.mode.results$lower <0) {
expert.P.BETA.results$ssBETA.mode.results$lower =0
}
if (expert.P.BETA.results$feedbackBETA.mode.results$new.lower<0){
expert.P.BETA.results$feedbackBETA.mode.results$new.lower=0
}
#MODE
plot.P.min <- min(
expert.P.BETA.results$ssBETA.mode.results$lower,
expert.P.BETA.results$feedbackBETA.mode.results$new.lower)
plot.P.max <- max(
expert.P.BETA.results$ssBETA.mode.results$upper,
expert.P.BETA.results$feedbackBETA.mode.results$new.upper)
#options(scipen=100)
plot(rep(c(1),2), rep(c(expert.P.BETA.results$ssBETA.mode.results$lower*100,
expert.P.BETA.results$ssBETA.mode.results$upper*100),1)
, xlim=c(-0.5,3), xlab="", ylab="", main="%", type="l",
ylim=c((plot.P.min-plot.P.min*.1)*100, (plot.P.max+plot.P.max*.1)*100),
xaxt = "n", lwd=2, las=2)
lines(rep(2,2),c(expert.P.BETA.results$ssBETA.mode.results$lower*100,
expert.P.BETA.results$ssBETA.mode.results$upper*100), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$lower*100,2), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$upper*100,2), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$mode*100,2),col="red", lwd=2)
lines(c(1,2), rep(Best.P*100,2), lwd=2) #best guess
legend("topleft", lty=1, col=c(1, "red"), bty="n", legend=c("elicited parameters",
"estimated (fitted) "),cex=.8) #BEST GUESS IS MODE
legend("topright", paste("new
sureness =", new.alpha), cex=.8, bty="n")
#plot new lower & upper conf
lines(c(1,2), rep(expert.P.BETA.results$feedbackBETA.mode.results$new.lower*100,2), col="red" , lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$feedbackBETA.mode.results$new.upper*100,2), col="red", lwd=2)
###
# USE INFO FROM ELICITED KNOWN SPP. (EXPERT.N.LN.RESULTS
#TO PLOT N (INFLATION (%) * K)
###
#calculate K
Ksp <- expert.K.ALLnorm.results$Ksp
#calculate number of species for cryptic species
Cryp <- rbeta(10000,expert.P.BETA.results$fitBETA.best.mode.alpha, expert.P.BETA.results$fitBETA.best.mode.beta)
#calculate number of species (including cryptic species
#Nsp <- Ksp*(Cryp+1)
#calculate number of cryptic spp.
Cryp<- Ksp*(Cryp)
###
#plot N
###
if (expert.P.BETA.results$ssBETA.mode.results$lower<0) {
expert.P.BETA.results$ssBETA.mode.results$lower =0
}
if (lower.K<0) {
lower.K=0
}
if (expert.P.BETA.results$feedbackBETA.mode.results$new.lower<0){
expert.P.BETA.results$feedbackBETA.mode.results$new.lower =0
}
plot.PN.min <- min(
expert.P.BETA.results$ssBETA.mode.results$lower * lower.K,
expert.P.BETA.results$feedbackBETA.mode.results$new.lower * lower.K)
plot.PN.max <- max(
expert.P.BETA.results$ssBETA.mode.results$upper* upper.K,
expert.P.BETA.results$feedbackBETA.mode.results$new.upper* new.upper)
options(scipen=100)
plot(rep(c(1),2), rep(c(expert.P.BETA.results$ssBETA.mode.results$lower * lower.K,
expert.P.BETA.results$ssBETA.mode.results$upper * upper.K),1)
, xlim=c(-0.5,3), xlab="", ylab="", main="N", type="l",
ylim=c(plot.PN.min-plot.PN.min*.1, plot.PN.max+plot.PN.max*.1),
xaxt = "n", lwd=2, las=2)
lines(rep(2,2),c(expert.P.BETA.results$ssBETA.mode.results$lower * lower.K,
expert.P.BETA.results$ssBETA.mode.results$upper * upper.K), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$lower * lower.K,2), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$upper * upper.K,2), lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$ssBETA.mode.results$mode * mode.K,2),col="red", lwd=2)
#plot new lower & upper conf
lines(c(1,2), rep(expert.P.BETA.results$feedbackBETA.mode.results$new.lower * new.lower,2)
, col="red" , lwd=2)
lines(c(1,2), rep(expert.P.BETA.results$feedbackBETA.mode.results$new.upper * new.upper,2)
, col="red", lwd=2)
#####
#GET RESULTS FOR OUTPUT
infl.lower=expert.P.BETA.results$ssBETA.mode.results$lower * lower.K
infl.upper=expert.P.BETA.results$ssBETA.mode.results$upper * upper.K
infl.best =expert.P.BETA.results$ssBETA.mode.results$mode * mode.K
infl.type="p"
###
#store selected best type (mode, median, mean)
best.type <- expert.P.BETA.results$best.type
######################################################################################################
# IF 2
# EXPERT PROVIDES N OF UNKNOWN SPECIES
} else if (is.na(Best.N) =="FALSE") {
expert.N.noplot.ALLnorm.results <- expert.K.noplot.ALLnorm.just1plot(Lower.N, Upper.N, Best.N, Sureness, new.alpha, best.type.status, ee.type=1)
#####
#GET RESULTS FOR OUTPUT
infl.lower=expert.N.noplot.ALLnorm.results$ss.mode.results$lower #%Lower.N
infl.upper=expert.N.noplot.ALLnorm.results$ss.mode.results$upper #%Upper.N
infl.best =expert.N.noplot.ALLnorm.results$ss.mode.results$mode #%Best.N
infl.type="n"
###
#store selected best type (mode, median, mean)
best.type <-expert.N.noplot.ALLnorm.results$best.type
#calculate K
Ksp <- expert.K.ALLnorm.results$Ksp
#calculate number of species for cryptic species
Cryp <- expert.N.noplot.ALLnorm.results$Ksp
#calculate number of species (including cryptic species
Nsp <- Ksp+Cryp
########
#PLOT
#NUMBER OF UNDISCOVER OR UNNAMED
par(mfrow=c(1,1), mar=c(1, 4.5, 4,.75)+.5, mgp=c(3, .75,0), omi=c(.1, .5, .1,.1))
#find min & max for plot
if (expert.N.noplot.ALLnorm.results$ss.mode.results$lower<0) {
expert.N.noplot.ALLnorm.results$ss.mode.results$lower =0
}
if (expert.N.noplot.ALLnorm.results$feedback.mode.results$new.lower<0){
expert.N.noplot.ALLnorm.results$feedback.mode.results$new.lower =0
}
plot.N.min <- min(
expert.N.noplot.ALLnorm.results$ss.mode.results$lower,
expert.N.noplot.ALLnorm.results$feedback.mode.results$new.lower)
plot.N.max <- max(
expert.N.noplot.ALLnorm.results$ss.mode.results$upper,
expert.N.noplot.ALLnorm.results$feedback.mode.results$new.upper)
##
#PLOT
options(scipen=100)
plot(rep(c(1),2), rep(c(expert.N.noplot.ALLnorm.results$ss.mode.results$lower,
expert.N.noplot.ALLnorm.results$ss.mode.results$upper),1)
, xlim=c(-0.5,3), xlab="", ylab="", type="l", main="N",
ylim=c(plot.N.min-plot.N.min*.1, plot.N.max+plot.N.max*.1),
xaxt = "n", lwd=2, las=2)
lines(rep(2,2),c(expert.N.noplot.ALLnorm.results$ss.mode.results$lower,
expert.N.noplot.ALLnorm.results$ss.mode.results$upper),lwd=2)
lines(c(1,2), rep(expert.N.noplot.ALLnorm.results$ss.mode.results$lower,2),lwd=2)
lines(c(1,2), rep(expert.N.noplot.ALLnorm.results$ss.mode.results$upper,2),lwd=2)
lines(c(1,2), rep(expert.N.noplot.ALLnorm.results$ss.mode.results$mode,2),col="red",lwd=2)
lines(c(1,2), rep(Best.N,2),col=1, lwd=2) #best guess
legend("topleft", lty=1, col=c(1, "red"), bty="n", legend=c("elicited parameters",
"estimated (fitted) "),cex=.8) #BEST GUESS IS MODE
legend("topright", paste("new
sureness =", new.alpha), cex=.8, bty="n")
#plot new lower & upper conf
lines(c(1,2), rep(expert.N.noplot.ALLnorm.results$feedback.mode.results$new.lower,2), col="red" ,lwd=2)
lines(c(1,2), rep(expert.N.noplot.ALLnorm.results$feedback.mode.results$new.upper,2), col="red",lwd=2)
###################################################################
# IF 3
# EXPERT PROVIDES M MULTIPLICAB FACTOR
#e.g CRYPTIC SPP. IS X TIMES KNOWN SPECIES
} else {#Best.M = multiplicab factor
expert.M.noplot.ALLnorm.results <- expert.K.noplot.ALLnorm.just1plot(Lower.M, Upper.M, Best.M, Sureness, new.alpha, best.type.status, ee.type=3)
###
#INFO FROM ELICITED KNOWN SPP.
#pihat is returned from expert.K.LN function
pihat <- expert.K.ALLnorm.results$pihat
#which distribution was used in expert.K.ALLnorm.results
which.dist <- expert.K.ALLnorm.results$which.dist
MhatK <-expert.K.ALLnorm.results$MhatK
#get values of mu, sig, lower.K & upper.K
mu <- expert.K.ALLnorm.results$fit.best.mode.mu
sig <- expert.K.ALLnorm.results$fit.best.mode.sig
eval.text <- getURL("https://raw.githubusercontent.com/beckyfisher/ElicitN/master/feedback.AllNorm.R", ssl.verifypeer = FALSE)
eval(parse(text = eval.text))
#source("feedback.AllNorm.R")
feedback.AllNorm.results <- feedback.AllNorm(mu, sig,Sureness,new.alpha, MhatK, which.dist)
lower.K <- feedback.AllNorm.results$lower
upper.K <- feedback.AllNorm.results$upper
mean.K <- feedback.AllNorm.results$mean
mode.K <- feedback.AllNorm.results$mode
median.K <- feedback.AllNorm.results$median
new.lower <- feedback.AllNorm.results$new.lower
new.upper <- feedback.AllNorm.results$new.upper
if (new.lower<0){
new.lower=0
}
if (lower.K<0){
lower.K=0
}
####
#PLOT
par(mfrow=c(2,1), mar=c(1, 4.5, 4,.75)+.5, mgp=c(3, .75,0), omi=c(.1, .5, .1,.1))
if (expert.M.noplot.ALLnorm.results$ss.mode.results$lower<0) {
expert.M.noplot.ALLnorm.results$ss.mode.results$lower =0
}
if (expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower<0){
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower =0
}
###
#find min & max for plot
plot.M.min <- min(
expert.M.noplot.ALLnorm.results$ss.mode.results$lower,
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower)
plot.M.max <- max(
expert.M.noplot.ALLnorm.results$ss.mode.results$upper,
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.upper)
###
options(scipen=100)
plot(rep(c(1),2), rep(c(expert.M.noplot.ALLnorm.results$ss.mode.results$lower,
expert.M.noplot.ALLnorm.results$ss.mode.results$upper),1)
, xlim=c(-0.5,3), xlab="", ylab="", main="x", type="l",
ylim=c(plot.M.min-plot.M.min*.1, plot.M.max+plot.M.max*.1),
xaxt = "n", lwd=2, las=2)
lines(rep(2,2),c(expert.M.noplot.ALLnorm.results$ss.mode.results$lower,
expert.M.noplot.ALLnorm.results$ss.mode.results$upper),lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$lower,2),lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$upper,2),lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$mode,2),col="red",lwd=2)
lines(c(1,2), rep(Best.M,2),col=1, lwd=2) #best guess
legend("topleft", lty=1, col=c(1, "red"), bty="n", legend=c("elicited parameters",
"estimated (fitted) "),cex=.8) #BEST GUESS IS MODE
legend("topright", paste("new
sureness =", new.alpha), cex=.8, bty="n")
#plot new lower & upper conf
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower,2), col="red" ,lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$feedback.mode.results$new.upper,2), col="red",lwd=2)
###
# USE INFO FROM ELICITED KNOWN SPP. (EXPERT.K.LN.RESULTS
#TO PLOT N (M * K)
#where M is MULTIPLICAB FACTOR
###
###
#plot
###
if (expert.M.noplot.ALLnorm.results$ss.mode.results$lower<0) {
expert.M.noplot.ALLnorm.results$ss.mode.results$lower =0
}
if (lower.K<0) {
lower.K=0
}
if (expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower<0){
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower =0
}
plot.PM.min <- min(
expert.M.noplot.ALLnorm.results$ss.mode.results$lower * lower.K,
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower * lower.K)
plot.PM.max <- max(
expert.M.noplot.ALLnorm.results$ss.mode.results$upper* upper.K,
expert.M.noplot.ALLnorm.results$feedback.mode.results$new.upper* new.upper)
options(scipen=100)
plot(rep(c(1),2), rep(c(expert.M.noplot.ALLnorm.results$ss.mode.results$lower * lower.K,
expert.M.noplot.ALLnorm.results$ss.mode.results$upper * upper.K),1)
, xlim=c(-0.5,3), xlab="", ylab="", main="N", type="l",
ylim=c(plot.PM.min-plot.PM.min*.1, plot.PM.max+plot.PM.max*.1),
xaxt = "n", lwd=2, las=2)
lines(rep(2,2),c(expert.M.noplot.ALLnorm.results$ss.mode.results$lower * lower.K,
expert.M.noplot.ALLnorm.results$ss.mode.results$upper * upper.K), lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$lower * lower.K,2), lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$upper * upper.K,2), lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$ss.mode.results$mode * mode.K,2),col="red", lwd=2)
#plot new lower & upper conf
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$feedback.mode.results$new.lower * new.lower,2)
, col="red" , lwd=2)
lines(c(1,2), rep(expert.M.noplot.ALLnorm.results$feedback.mode.results$new.upper * new.upper,2)
, col="red", lwd=2)
#####
#GET RESULTS FOR OUTPUT
infl.lower=expert.M.noplot.ALLnorm.results$ss.mode.results$lower * lower.K
infl.upper=expert.M.noplot.ALLnorm.results$ss.mode.results$upper * upper.K
infl.best =expert.M.noplot.ALLnorm.results$ss.mode.results$mode * mode.K
infl.type="m"
#calculate K
Ksp <- expert.K.ALLnorm.results$Ksp
#calculate number of species for cryptic species
Cryp <- expert.M.noplot.ALLnorm.results$Ksp
#calculate number of species (including cryptic species
Nsp <- Ksp*Cryp
#############################################################
#END IF STATEMENT
}
#CALCULATE N FOR INFLATION
output <- expert.K.noplot.ALLnorm(Lhat=infl.lower, Uhat=infl.upper, Mhat=infl.best, Sureness,
new.alpha, best.type.status, ee.type=1)
}
return(list(pihat=output$pihat,
ss.mode.results=output$ss.mode.results,
fit.best.mode.mu=output$fit.best.mode.mu,
fit.best.mode.sig=output$fit.best.mode.sig,
feedback.mode.results=output$feedback.mode.results,
Ksp=output$Ksp, MhatK=output$MhatK,which.dist=output$which.dist,
Cryp=Cryp, new.alpha=new.alpha))
}