-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbanteng_harvest.R
2490 lines (1914 loc) · 92.2 KB
/
banteng_harvest.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
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
## program for analysis of banteng population projection
## ----------------------------------------
## base model - rates from Choquenot (1993)
## giving stability
## ----------------------------------------
## Removes everything in memory
rm(list = ls())
## Add packages 'base'
library(base)
## define max age
age.max<-17
## define survival - fitted function (Choquenot 1993)
## s0<-0.7402 ## fitted from Choquenot (1993)
## s.vec <- c(0.55,0.9278,0.9346,0.9370,0.9382,0.9389,0.9394,0.9397,0.9400,0.9402,0.9403,0.9405,0.9406,0.9407,0.9408,0.9408,0.9409,0.9409)
## Mortality vector from Choquenot (1993)
q.vec <- c(0.26,0.08,0.075,0.07,0.075,0.06,0.06,0.06,0.06,0.05,0.05,0.05,0.07)
## Fit survival values
library(stats)
library(base)
## Create data frame
age.vec.q <- seq(-1,length(q.vec)-2)
age.vec.q[1] <- 0
age.vec.q[2:(length(age.vec.q))] <- age.vec.q[2:(length(age.vec.q))] + 0.5
q.fit.data <- data.frame(age.vec.q,q.vec)
## model formula (exponential rise to maximum): yhat = a*exp(b/(x+c))
fit.q.vec <- nls(q.fit.data$q.vec ~ a.coeff * exp(b.coeff/(q.fit.data$age.vec.q + c.coeff)),
data = q.fit.data,
start = list(a.coeff = 0.1, b.coeff = 1, c.coeff = 1),
trace = TRUE)
sum.fit.q.vec <- summary(fit.q.vec)
## Coefficients from fit
coeff.fit.q.vec <- as.numeric(sum.fit.q.vec$parameters)
## age vector
age.vec<-rep(0,age.max+1)
for (j in 1:(age.max)) {
age.vec[j+1] <- j
}
## Predict new q vector with integer age inputs
qf.new.vec <- coeff.fit.q.vec[1] * exp(coeff.fit.q.vec[2]/(coeff.fit.q.vec[3]+age.vec))
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(age.vec.q,q.vec,xlab="age (years)",ylab="qx")
title(main="Fitted Survival")
lines(age.vec,qf.new.vec)
par(mfrow=c(1,2))
s.vec <- 1 - qf.new.vec
## change s0 in s.vec to give population stability
##s.vec[1] <- 0.62
## fitted fecundity parameters
## m.vec <- c(0.0000,0.0001,0.0133,0.0947,0.2303,0.3440,0.3964,0.3921,0.3525,0.2979,0.2414,0.1903,0.1471,0.1122,0.0849,0.0638,0.0478,0.0358)
m.vec.obs <- c(0,0,0,0,0.22,0.351,0.5,0.45,0.2,0.325,0.125,0.225) ## Choquenot (1993)
## Create data frame
age.vec.m <- seq(0,11)
m.fit.data <- data.frame(age.vec.m,m.vec.obs)
## model formula (log-normal): yhat=a*exp(-0.5*(ln(x/x0)/b)^2)
fit.m.vec <- nls(m.fit.data$m.vec.obs ~ a.coeff.m * exp(-0.5*(log(m.fit.data$age.vec.m/x0.coeff.m)/b.coeff.m)^2),
data = q.fit.data,
start = list(a.coeff.m = 0.4, x0.coeff.m = 6, b.coeff.m = 0.4),
trace = TRUE)
sum.fit.m.vec <- summary(fit.m.vec)
## Coefficients from fit
coeff.fit.m.vec <- as.numeric(sum.fit.m.vec$parameters)
## Predict new q vector with integer age inputs
mf.new.vec <- coeff.fit.m.vec[1] * exp(-0.5*(log(age.vec/coeff.fit.m.vec[2])/coeff.fit.m.vec[3])^2)
## re-scale m.vec.obs for plotting
m.vec.obs.rs <- rep(0,age.max+1)
m.vec.obs.rs[1:12] <- m.vec.obs
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(age.vec,m.vec.obs.rs,xlab="age (years)",ylab="mx",xlim=range(c(0,age.max)),ylim=range(c(0,1)))
title(main="Fitted Fertility")
lines(age.vec,mf.new.vec)
par(mfrow=c(1,2))
m.vec <- mf.new.vec
primi<-4 ## age at first reproduction (females)
## initial population sizes
## Nmin<-4000 ## J. Christopherson (2004)
## Nmax<-5000 ## J. Christopherson (2004)
Nmin<-7000 ## K. Saalfeld (2004)
Nmax<-8000 ## K. Saalfeld (2004)
Navg <- mean(c(Nmin, Nmax))
## Use 2 scenarios - pop.init at 4000 and 8000
N1 <- 5000
N2 <- 9000
## Density estimates
Area.Cob <- 220000 ## Area of Cobourg Peninsula in hectares
D.avg1 <- N1/(Area.Cob*0.01)
D.avg2 <- N2/(Area.Cob*0.01)
## sex ratios
sr <- 0.5 ## overall sex ratio
x <- 0.5 ## foetal sex ratio (Choquenot 1993)
##total females in population
f1<-N1*sr
f2<-N2*sr
##total males in population
mal1<-N1*(1-sr)
mal2<-N2*(1-sr)
##Initial population size vector
N<-N1
## Matrix size
k <- 34
## Create matrix shell
a <- matrix(data<-0,nrow<-k,ncol<-k)
## Add survival vectors to matrix
diag(a[2:age.max,1:(age.max-1)]) <- s.vec[2:age.max] ## adds s.vec to female quadrant
diag(a[(age.max+2):k,(age.max+1):(k-1)]) <- s.vec[2:age.max] ## adds s.vec to male quadrant
a[1,1:(age.max)] <- s.vec[1] * m.vec[1:(age.max)] ## adds female fecundity
a[(age.max+1),1:(age.max)] <- s.vec[1] * m.vec[1:(age.max)] ## adds male fecundity
## Maximum lambda function
max.lambda <- function(x) Re((eigen(x)$values)[1]) ## where 'x' is a Leslie matrix
## Maximum r function
max.r <- function(x) log(Re((eigen(x)$values)[1])) ## where 'x' is a Leslie matrix
## Stable stage distribution
stable.stage.dist <- function(x) ((x %*% (Re((eigen(x)$vectors)[,1])))/(sum((x %*% (Re((eigen(x)$vectors)[,1]))))))[,1]
## Generation length function
R.val <- function(X,age.max) ## reproductive value (R0) where X = Leslie matrix; age.max = maximum age of females
{
## define the transition matrix
T <- X[1:age.max,1:age.max]
T[1,1:(age.max)] <- 0
## define the fertility matrix
F <- X[1:age.max,1:age.max]
diag(F[2:age.max,1:(age.max-1)]) <- 0
## define the identity matrix
I <- matrix(data<-0,nrow<-age.max,ncol<-age.max)
diag(I) <- 1
## define the fundamental matrix
library(MASS)
N.fund <- ginv(I-T)
## define the reproductive matrix
R <- F %*% N.fund
## define R0 (number of female offspring produced per female during lifetime)
R0 <- Re((eigen(R)$values)[1])
## output
print("number of female offspring produced per female during its lifetime")
print("_________________________________________________________________")
print(R0)
}
## Mean generation time function
G.val <- function (X,age.max) ## where X is a Leslie Matrix
{
G <- (log(R.val(X,age.max)))/(log(Re((eigen(X)$values)[1])))
print("mean generation time")
print("____________________")
print(G)
}
## max lambda
maxlambda<-max.lambda(a)
## r
r <- max.r(a)
## Reproductive value
R0 <- R.val(a,age.max)
## Generation time
G <- G.val(a,age.max)
## Stable stage distribution
ssd <- stable.stage.dist(a)
## ssd classes
## female
ssd.juvf <- sum(ssd[1:primi-1])
ssd.adf <- sum(ssd[primi:age.max])
## male
ssd.juvm <- sum(ssd[(age.max+1):(age.max+primi-1)])
ssd.adm <- sum(ssd[(age.max+primi):k])
## Average age of mothers at stability
G.age.mean <- weighted.mean(age.vec[2:(age.max+1)],ssd[1:age.max])
## pick initial vectors
n<-ssd*N
##Calculate Quasi-extinction times
thresh <- 50 ## Define quasi-exinction threshold
Q<-(log(thresh/sum(n)))/log(maxlambda)
if (Q < 0) {
Q <- "infinity"
}
Q <- Q
## do a simulation
## first specify the initial condition and length of simulation
tlimit <- 30
##set population size year step vector
pop.vec <- rep(1,tlimit+1)
pop.vec[1] <- sum(n)
##set year step vector
yr.vec <- rep(1,tlimit+1)
yr.vec[1] <- 0
for (j in 1:tlimit) {
yr.vec[j+1] <- j
}
##then iterate
for (ii in 1:tlimit) {
n <- a %*% n
pop.vec[ii+1] <- sum(n)
}
log.pop.vec <- log10(pop.vec)
##total population size after 'tlimit' years
pop.st <- N
pop.end <- sum(n)
tlimit
maxlambda
r
Q
R0
G
## continue displays
ssd.juv <- ssd.juvf + ssd.juvm
ssd.ad <- ssd.adf + ssd.adm
##Make density independent plots
row <- 2
col <- 2
par(mfrow=c(row,col))
plot(yr.vec,pop.vec,xlab="year",ylab="N",xlim=range(-0.5:(tlimit+1)),ylim=range(0:(max(pop.vec)+(0.25*(max(pop.vec))))),type='l')
plot(yr.vec,log.pop.vec,xlab="year",ylab="log N",xlim=range(-0.5:(tlimit+1)),ylim=range(0:(max(log.pop.vec)+(0.25*(max(log.pop.vec))))),type='l')
##Make survival and fecundity plots
plot(age.vec,s.vec,xlab="age (years)",ylab="P(survival)",xlim=range(0.5:(age.max+0.5)),ylim=range(0:1),type='l')
plot(age.vec,m.vec,xlab="age (years)",ylab="m (fertility)",xlim=range(0.5:(age.max+0.5)),ylim=range(0:1),type='l')
par(mfrow=c(1,2))
## *******************************************
## Density-dependence in survival & fertility
## Set kill rates
## *******************************************
## ***********************************************************************************************
##K population size vector
Nd <- 9000
## Set time limit for projection
tlimit <- 30
## Set kill type (2 = adult females (>2 years) only; 1 = adult males (>5 years) only; 0 = adults (fem >2, mal >3) only)
kill.type <- 0
if (kill.type == 0) scenario <- "adult kill"
if (kill.type == 1) scenario <- "adult male (>5 years) kill"
if (kill.type == 2) scenario <- "adult female (>2 years) kill"
## Set kill range
min.kill <- 2 ## Set minimum kill rate
max.kill <- 1000 ## Set maximum kill rate
## Set kill interval
kill.interval <- 50 ## (calculate kill in increments of x)
## Set target per cent reduction of original population size
thresh <- 0
## Stochasticity in rainfall
## 0 = deterministic; 1 = stochastic
stoch.select <- 1
rain.cv = 24 ## coefficient of variation in rainfall (Choquenot 1993)
## Set number of iterations to estimate mean & confidence interval for projections based on flooding stochasticity
perm <- 1000 ## number of permutations
if (stoch.select == 0) perm <- 1 else perm <- perm
## Tertiary sex ratio (0.83 = balanced in polygynous ungulates)
tsr.max <- 0.95 ## Tertiary sex ratio (breeding females:breeding adults)
######################################
## Economic analysis
######################################
## Exchange rate (AU$ to Euros); updated live from server
##exch.aud.euro <- 0.587673
exch.rates <- download.file("http://www.securetrading.net/download/currencyrates/curr-gbp-ns-today.txt","C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\exch\\exch.txt", "internal", quiet = FALSE, mode="w")
exch.rat.dat <- scan(file="C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\exch\\exch.txt",what="character",skip=3,nlines=1)
exch.scan.aud <- scan(file="C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\exch\\exch.txt",what="character",skip=7,nlines=1)
##exch.rates <- download.file("http://www.securetrading.net/download/currencyrates/curr-gbp-ns-today.txt","F:\\Banteng\\R\\exch\\exch.txt", "internal", quiet = FALSE, mode="w")
##exch.rat.dat <- scan(file="F:\\Banteng\\R\\exch\\exch.txt",what="character",skip=3,nlines=1)
##exch.scan.aud <- scan(file="F:\\Banteng\\R\\exch\\exch.txt",what="character",skip=7,nlines=1)
aud.gbp <- 1/((as.numeric(as.character((strsplit(exch.scan.aud," ")[2]))))/10000000)
exch.scan.eur <- scan(file="C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\exch\\exch.txt",what="character",skip=11,nlines=1)
##exch.scan.eur <- scan(file="F:\\Banteng\\R\\exch\\exch.txt",what="character",skip=11,nlines=1)
eur.gbp <- 1/((as.numeric(as.character((strsplit(exch.scan.eur," ")[2]))))/10000000)
print(aud.gbp); print(eur.gbp)
exch.aud.euro <- aud.gbp/eur.gbp
print(exch.aud.euro)
print(exch.rat.dat)
## Eastern Young Cattle Indicator (EYCI) market data (AU$/kilo meat)
eyci <- download.file("http://www.mla.com.au/content.cfm?sid=1173","C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\eyci\\eyci.txt", "internal", quiet = FALSE, mode="w")
eyci.lat <- scan(file="C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\eyci\\eyci.txt",what="character",skip=641,nlines=1)
##eyci <- download.file("http://www.mla.com.au/content.cfm?sid=1173","F:\\Banteng\\R\\eyci\\eyci.txt", "internal", quiet = FALSE, mode="w")
##eyci.lat <- scan(file="F:\\Banteng\\R\\eyci\\eyci.txt",what="character",skip=643,nlines=1)
eyci.now <- (as.numeric(substr((as.character(eyci.lat[2])),23,28)))/100
print(eyci.now)
## Read in full range of EYCI since 1996
eyci.dat <- read.table("C:\\Documents and Settings\\c_bradshaw\\My Documents\\Biology\\Banteng\\R\\eyci\\eycidat.csv", sep=",", header=TRUE)
##hist(eyci.dat$adjeyci)
##plot(eyci.dat$year,eyci.dat$adjeyci,type="l")
yrf.rnd <- factor(floor(eyci.dat$year))
yrn.rnd <- (floor(eyci.dat$year))
eycidat <- data.frame(eyci.dat$adjeyci,yrf.rnd,yrn.rnd)
## Summary table
attach(eycidat)
eyci.tab <- table(yrf.rnd)
eyci.xtab <- xtabs(eyci.dat$adjeyci ~ yrf.rnd)
detach(eycidat)
eyci.yr.mean <- eyci.xtab/eyci.tab/100
##plot(eyci.yr.mean,type="l")
## Min and max eyci (since 1996, set in 2004 AU$)
eyci.min <- min(eyci.dat$adjeyci)/100 ## 1.8596
eyci.max <- max(eyci.dat$adjeyci)/100 ## 4.3598
## Temporal autocorrelation
lag1 <- as.numeric(eyci.yr.mean[2:(nrow(eyci.yr.mean))])
base <- as.numeric(eyci.yr.mean[1:(nrow(eyci.yr.mean)-1)])
eyci.fit <- lm(lag1 ~ base)
##plot(base,lag1)
##abline(eyci.fit,col="red")
## Mean & SD
eyci.mean <- as.numeric(mean(eyci.yr.mean))
eyci.var <- as.numeric(var(eyci.yr.mean))
## User-defined values
ppkg.meat <- 10 ## (price in AU$ to obtain 1 kg banteng-equivalent meat); T. Griffiths, pers. comm.
## Meat eaten per day per person
adult.meat.day <- 67 ## g/day
adult.meat.yr <- (adult.meat.day*365)/1000 ## kg/year/adult
minjil.pop <- 254 ## 2001 census data
minjil.juv <- 39 + 53
minjil.ad <- 35 + 69 + 42 + 16
minjil.meat <- (adult.meat.yr*minjil.ad) + ((adult.meat.yr*minjil.juv)/2)
## Proportion of full animal producing marketable meat (dressing proportion)
meat.prop <- 0.526 ## for banteng (Kirby 1979)
## Price per mature male harvested passed to Traditional owners
safari.to <- 2500 ## AU$
## Costing
muster <- 10 ## per head
transport <- 10 ## per head (http://www.agric.nsw.gov.au/reader/an-transport/6980)
##cost.tot <- muster + transport ## set individually above OR,
cost.tot <- 150 ## set total cost in AU$ per head (Alister Trier, Proj. Mgr - NT Indigenous Pastoral Project
## Cost function
## Increasing costs as a function of a reduction in population size
cost.max <- 500 ## max cost (AU$) at 0.1*K
cost.min <- 150 ## min cost (AU$) at K
## Profit (http://www.abs.gov.au/websitedbs/c311215.NSF/0/641c9a7c2c02c6d9ca256d480012c608?OpenDocument)
profit <- 29 ## average % profit per $ invested (Australia average)
## Demand function
## Set the the probability (p.dem) of attracting max n safari hunters (n.saf) to kill trophy males
n.saf <- 40
p.dem <- 0.5
## *********************************************************************************
## ***************
## End user input
## ***************
## *********************************************************************************
## Calculate demand function (3-parameter Hill logistic decay)
## yhat = ((a * x^b)/(c^b + x^b))
## Create data.frame
p.dem.vec <- c(1,0.99,0.98,0.95,0.90,0.80,0.64,p.dem)
n.saf.vec <- c(1,n.saf/4,n.saf/2,n.saf*0.75,n.saf*0.85,n.saf*0.92,n.saf*0.95,n.saf)
dem.data <- data.frame(n.saf.vec,p.dem.vec)
fit.dem <- nls(dem.data$p.dem.vec ~ ((a.dem * ((dem.data$n.saf.vec)^b.dem))/((c.dem^b.dem)+(dem.data$n.saf.vec^b.dem))),
data = dem.data,
start = list(a.dem = 1, b.dem = -(n.saf/2), c.dem = n.saf),
trace = TRUE)
sum.fit.dem <- summary(fit.dem)
coeff.fit.dem <- as.numeric(sum.fit.dem$parameters)
dem.n.vec <- seq(1,n.saf*2,1)
dem.pred <- ((coeff.fit.dem[1] * ((dem.n.vec)^coeff.fit.dem[2]))/((coeff.fit.dem[3]^coeff.fit.dem[2])+(dem.n.vec^coeff.fit.dem[2])))
## Plot demand function
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(dem.n.vec,dem.pred,xlab="number of male trophy tags",ylab="demand probability",ylim=range(c(0,1)),type="l")
title(main="Trophy Demand Function")
par(mfrow=c(1,2))
## Translate demand to numbers of harvest animals
demf.pred <- (1 - (0.97^dem.n.vec))
demf <- demf.pred*n.saf
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(dem.n.vec,demf.pred,xlab="number of male trophy tags",ylab="demand n probability",type="l")
title(main="Trophy n Demand Function")
par(mfrow=c(1,2))
## Cost function
cost.dif <- cost.max - cost.min
kprop.vec <- (seq(0.1,1,0.1))*Nd
cost.vec <- c(cost.max,0.833*cost.max,(cost.min+cost.max)/2,0.66*cost.max,1.25*cost.min,1.2*cost.min,1.15*cost.min,1.1*cost.min,1.05*cost.min,cost.min)
## Fit function (exponential decay) f=y0+a*exp(-b*x)
cost.data <- data.frame(kprop.vec,cost.vec)
fit.cost <- nls(cost.data$cost.vec ~ y0.cost + a.cost*exp(-b.cost*cost.data$kprop.vec),
data = cost.data,
start = list(a.cost = (0.66*cost.max), b.cost = 0.0004, y0.cost = cost.min),
trace = TRUE)
sum.fit.cost <- summary(fit.cost)
coeff.fit.cost <- as.numeric(sum.fit.cost$parameters)
cost.n.vec <- seq(1,Nd,100)
cost.pred <- coeff.fit.cost[3] + coeff.fit.cost[1]*exp(-coeff.fit.cost[2]*cost.n.vec)
for (c in 1:(length(cost.n.vec))) {
if (cost.pred[c] > cost.max) cost.pred[c] <- cost.max
if (cost.pred[c] < cost.min) cost.pred[c] <- cost.min
}
## Plot cost function
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(cost.n.vec,cost.pred,xlab="Population Size",ylab="Cost per head ($AU)",type="l")
title(main="Cost Function")
par(mfrow=c(1,2))
## Growth functions
f.wt.vec <- c(17,82,300); m.wt.vec <- c(17,100,600)
f.age.vec <- c(0,1,4); m.age.vec <- c(0,1,6)
## Create data frame
gr.data <- data.frame(f.wt.vec,f.age.vec,m.wt.vec,m.age.vec)
## model formula
## Von Bertalanffy growth function: M(t) = Mmax - (Mmax - M0) exp(-kt)
## M0 = mean birth mass
## Mmax = mean maximum mass
## k = rate constant per year
fit.gr.f <- nls(gr.data$f.wt.vec ~ gr.data$f.wt.vec[3] - (gr.data$f.wt.vec[3] - gr.data$f.wt.vec[1]) * exp(-k.coeff*gr.data$f.age.vec),
data = gr.data,
start = list(k.coeff = 0.2),
trace = TRUE)
sum.fit.gr.f <- summary(fit.gr.f)
fit.gr.m <- nls(gr.data$m.wt.vec ~ gr.data$m.wt.vec[3] - (gr.data$m.wt.vec[3] - gr.data$m.wt.vec[1]) * exp(-k.coeff*gr.data$m.age.vec),
data = gr.data,
start = list(k.coeff = 0.2),
trace = TRUE)
sum.fit.gr.m <- summary(fit.gr.m)
## Coefficients from fit
coeff.fit.gr.f <- as.numeric(sum.fit.gr.f$parameters)
coeff.fit.gr.m <- as.numeric(sum.fit.gr.m$parameters)
## Predict new q vector with integer age inputs
grf <- gr.data$f.wt.vec[3] - (gr.data$f.wt.vec[3] - gr.data$f.wt.vec[1]) * exp(-coeff.fit.gr.f[1]*age.vec)
grm <- gr.data$m.wt.vec[3] - (gr.data$m.wt.vec[3] - gr.data$m.wt.vec[1]) * exp(-coeff.fit.gr.m[1]*age.vec)
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(age.vec,grf,xlab="age (years)",ylab="mass (kg)",ylim=range(c(0,max(m.wt.vec))),type="l")
lines(age.vec,grm,col="red")
title(main="von Bertalanffy Growth Functions",sub="(male vs. female)")
par(mfrow=c(1,2))
## Derivate growth expressions at "age.input"
vb.exp <- expression(Mmax - (Mmax - M0) * exp((-gr.k)*age))
## For a male
Mmax <- gr.data$m.wt.vec[3]
M0 <- gr.data$m.wt.vec[1]
gr.k <- coeff.fit.gr.m[1]
age1 <- 16/12
age2 <- age1 + (15/12)
age3 <- 10
age <- age1
dvb.exp1 <- D(vb.exp, c("age"))
m1.gr.yr <- eval(dvb.exp1)
m1.yr <- eval(vb.exp)
m1.gr.dy <- m1.gr.yr/365
age <- age2
dvb.exp2 <- D(vb.exp, c("age"))
m2.gr.yr <- eval(dvb.exp2)
m2.yr <- eval(vb.exp)
m2.gr.dy <- m2.gr.yr/365
age <- age3
dvb.exp3 <- D(vb.exp, c("age"))
m3.gr.yr <- eval(dvb.exp3)
m3.yr <- eval(vb.exp)
m3.gr.dy <- m3.gr.yr/365
## Average growth rate (per yr & per day) between
## the ages of 22 and 37 months (Moran 1973)
## Moran's value = 0.22 kg/day
m.sec.yr <- (m2.yr - m1.yr)/(age2 - age1)
m.sec.dy <- m.sec.yr/365 ## predicted kg/day
m.intcpt <- ((m1.yr + m2.yr) - ((age1+age2)*m.sec.yr))/2
row <- 1
col <- 1
par(mfrow=c(row,col))
plot(age.vec,grf,xlab="age (years)",ylab="mass (kg)",ylim=range(c(0,max(m.wt.vec))),type="l")
lines(age.vec,grm,col="red")
abline(v=age1,col="green")
abline(h=m1.yr,col="green")
abline(m.intcpt,m.sec.yr)
abline(v=age2,col="green")
abline(h=m2.yr,col="green")
##abline(v=age3,col="green")
##abline(h=m3.yr,col="green")
points(age1,m1.yr,col="black",pch=19)
points(age2,m2.yr,col="black",pch=19)
##points(age3,m3.yr,col="black",pch=19)
title(main="von Bertalanffy Growth Functions",sub="(male vs. female)")
par(mfrow=c(1,2))
## ************************************************************************************************
## Re-calculate logistic functions for survival & fecundity based on initial population vector
library(stats)
library(base)
pop.k <- Nd
pop.min <- 20
pop.step <- 50
xmid <- (Nd + pop.min)/2
num.vec <- seq(pop.min,pop.k,((pop.k-pop.min)/pop.step))
## Set up base logistic form
quant <- seq(0,1,(1/pop.step)) ## sets up quantile vector
## logistic.dens <- dlogis(quant, location = 0.01, scale = 10, log = FALSE) ## estimates logistic density function with long left tail
logistic.dens <- 0 + ((0.997)/(1+(quant/1.8217)^3.9151))
log.dens <- (logistic.dens - min(logistic.dens)) / max(logistic.dens - min(logistic.dens))
## Set min and max s0 values
s0.min <- 0.92
s0.max <- 0.62
## Calculate logistic function between min and max s0
s0.vec <- (seq(s0.min,s0.max,-((s0.min-s0.max)/pop.step)))
range.s0.vec <- range(s0.vec)[2] - range(s0.vec)[1]
s0.vec.logis <- (range.s0.vec * log.dens) + min(s0.vec)
## Set min and max s1+ values
s1p.min <- 0.99
s1p.max <- s0.min
## Calculate logistic function between min and max s1p
s1p.vec <- (seq(s1p.min,s1p.max,-((s1p.min-s1p.max)/pop.step)))
range.s1p.vec <- range(s1p.vec)[2] - range(s1p.vec)[1]
s1p.vec.logis <- (range.s1p.vec * log.dens) + min(s1p.vec)
## Set min and max fecundity a coefficient values
af.min <- 0.62
af.max <- coeff.fit.m.vec[1]
## Calculate logistic function between min and max
af.vec <- (seq(af.min,af.max,-((af.min-af.max)/pop.step)))
range.af.vec <- range(af.vec)[2] - range(af.vec)[1]
af.vec.logis <- (range.af.vec * log.dens) + min(af.vec)
## Plot dd functions
row <- 2
col <- 2
par(mfrow=c(row,col))
plot(num.vec,s0.vec.logis,xlab="N",ylab="s0",xlim=range(-0.5:((max(num.vec))+1)),ylim=range(c(0,1)),type='l')
title(main="Logistic DD Functions")
plot(num.vec,s1p.vec.logis,xlab="N",ylab="s1+",xlim=range(-0.5:((max(num.vec))+1)),ylim=range(c(0,1)),type='l')
plot(num.vec,af.vec.logis,xlab="N",ylab="a.f",xlim=range(-0.5:((max(num.vec))+1)),ylim=range(c(0,1)),type='l')
par(mfrow=c(1,2))
log.func <- data.frame(num.vec,s0.vec.logis,s1p.vec.logis,af.vec.logis) ## put new vectors into data.frame
## Estimate 4-parameter logistic model coefficients
s0.param <- getInitial(s0.vec.logis ~ SSfpl(num.vec, a.s0, b.s0, xmid.s0, scal.s0), data=log.func)
s1p.param <- getInitial(s1p.vec.logis ~ SSfpl(num.vec, a.s1p, b.s1p, xmid.s1p, scal.s1p), data=log.func)
af.param <- getInitial(af.vec.logis ~ SSfpl(num.vec, a.af, b.af, xmid.af, scal.af), data=log.func)
## ************************************************************************************************
## Stochastic catastrophe scenario modelled from 0.14G (Reed et al. 2004)
p.cat <- 0.147/G.age.mean
sev.cat.vec <- seq(0,100,10)
sev.cat.prob <- (2510*(0.9376^(sev.cat.vec)))/100
sev.cat.dat <- data.frame(sev.cat.vec,sev.cat.prob)
sev.cat.fit <- nls(sev.cat.vec ~ (a.cat*(sev.cat.prob^b.cat)),
data = sev.cat.dat,
start = list(a.cat = 50, b.cat = -0.2),
trace = TRUE)
sum.sev.cat.fit <- summary(sev.cat.fit)
sev.cat.coeff <- as.numeric(sum.sev.cat.fit$parameters)
karray <- seq(min.kill,max.kill,kill.interval) ## kill rate array
lkarray <- length(karray)
## Rainfall setup for stochastic projections
rain.sum.mean <- round(sum(c(275,240,300,140,20,5,2,0,5,20,120,230)),-1) ## mean monthly rainfall (1921-1978 - Choquenot 1993)
rain.var <- (rain.cv*rain.sum.mean)/100
rain.mult.sd <- (sqrt(rain.cv))/100
rain.mult.min <- 1-(2*rain.mult.sd)
rain.mult.max <- 1+(2*rain.mult.sd)
## Stochastic loop
## Set storage vectors for mean and confidence intervals for population sizes
min.popd.perm <- matrix(0,nrow=perm,ncol=lkarray)
minmal6p.perm <- matrix(0,nrow=perm,ncol=lkarray)
meat.dol.perm <- matrix(0,nrow=perm,ncol=lkarray)
cost.dol.perm <- matrix(0,nrow=perm,ncol=lkarray)
proft.dol.perm <- matrix(0,nrow=perm,ncol=lkarray)
profm.dol.perm <- matrix(0,nrow=perm,ncol=lkarray)
meat.mass.perm <- matrix(0,nrow=perm,ncol=lkarray)
safari.dol.perm <- matrix(0,nrow=perm,ncol=lkarray)
## Start stochastic loop
for (p in 1:perm) {
min.popd.vec <- rep(0,length(karray)) ## Minimum population size vector
mald.vec <- rep(0,length(karray)) ## Number of males vector
minmal6p.vec <- rep(0,length(karray)) ## Minimum male (>5 years) vector
minpmal6p.vec <- rep(0,length(karray)) ## Minimum proportion of males (>5 years) per males vector
minfem3p.vec <- rep(0,length(karray)) ## Minimum female (>2 years) vector
minpfem3p.vec <- rep(0,length(karray)) ## Minimum proportion of females (>2 years) per females vector
lambdad.vec <- rep(0,length(karray)) ## mean population rate of change (r)
mal6p.kill <- rep(0,length(karray)) ## mature males killed vector
## Economic vectors
meat.mass.tot <- rep(0,length(karray)) ## Total mass meat harvested
meat.dol.tot <- rep(0,length(karray)) ## total AU$ of harvested meat
safari.dol.tot <- rep(0,length(karray)) ## total AU$ of harvested meat
cost.dol.tot <- rep(0,length(karray)) ## total AU$ costs
meat.prop.worth <- rep(0,length(karray)) ## proportional worth of harvested meat
safari.prop.worth <- rep(0,length(karray)) ## proporational worth of safari
## Start simulation over kill vector
for (z in 1:lkarray) {
## Stochastic measure (variation in rainfall)
rain.mult <- runif(1,min=rain.mult.min,max=rain.mult.max)
## define start survival
s0.d <- rain.mult*(as.numeric((SSfpl(sum(Nd),s0.param[1],s0.param[2],s0.param[3],s0.param[4]))))
if (s0.d < s0.max) s0.d <- s0.max else s0.d <- s0.d
if (s0.d > s0.min) s0.d <- s0.min else s0.d <- s0.d
s1p.d <- rain.mult*(as.numeric((SSfpl(sum(Nd),s1p.param[1],s1p.param[2],s1p.param[3],s1p.param[4]))))
if (s1p.d < s1p.max) s1p.d <- s1p.max else s1p.d <- s1p.d
if (s1p.d > s1p.min) s1p.d <- s1p.min else s1p.d <- s1p.d
## define start fecundity
a.md <- as.numeric((SSfpl(sum(Nd),af.param[1],af.param[2],af.param[3],af.param[4])))
if (a.md < af.max) a.md <- af.max else a.md <- a.md
if (a.md > af.min) a.md <- af.min else a.md <- a.md
## Re-define fertility vector
md.vec <- rep(0,age.max); b.fert <- coeff.fit.m.vec[3]; x0.fert <- coeff.fit.m.vec[2]
for (j in (1:age.max)) {
md<-a.md*exp(-0.5*(log(j/x0.fert)/b.fert)^2)
md.vec[j] <- md
}
##total females in population
f<-Nd*sr
##total males in population
mal<-Nd*(1-sr)
## The normal matrix
ad <- matrix(data<-0,nrow<-k,ncol<-k)
## Fill matrix with survival & fecundity vectors
diag(ad[2:age.max,1:(age.max-1)]) <- s1p.d ## adds s1p.d to female quadrant
diag(ad[(age.max+2):k,(age.max+1):(k-1)]) <- s1p.d ## adds s1p.d to male quadrant
ad[1,2:(age.max)] <- s0.d * md.vec[1:(age.max-1)] ## adds female fecundity
ad[(age.max+1),2:(age.max)] <- s0.d * md.vec[1:(age.max-1)] ## adds male fecundity
## Stable stage distribution
ssdd <- stable.stage.dist(ad)
## pick initial vectors
nd<-ssdd*Nd
## max r
rd <- max.r(ad)
##set population size year step vector
popd.vec <- rep(1,tlimit+1)
popd.vec[1] <- sum(nd)
##set female population size year step vector
femd.vec<-rep(1,tlimit+1)
femd.vec[1]<-sum(nd[1:age.max])
femda.vec<-rep(1,tlimit+1)
femda.vec[1]<-sum(nd[3:age.max])
##set male population size year step vector
mald.vec<-rep(1,tlimit+1)
mald.vec[1]<-sum(nd[(age.max+1):k])
malda.vec<-rep(1,tlimit+1)
malda.vec[1]<-sum(nd[21:k])
## set large male year step vector
male6p.vec <- rep(0,tlimit+1)
male6p.vec[1]<-sum(nd[23:k])
##set year step vector
yrd.vec <- rep(1,tlimit+1)
yrd.vec[1] <- 0
for (j in 1:tlimit) {
yrd.vec[j+1] <- j
}
## Mature male kill vector
mal6p.kill.vec <- rep(0,tlimit+1)
## Economic vectors
meat.tot.vec <- rep(0,tlimit)
meat.dol.vec <- rep(0,tlimit)
cost.tot.vec <- rep(0,tlimit)
safari.tot.vec <- rep(0,tlimit)
meat.worth.vec <- rep(0,tlimit)
safari.worth.vec <- rep(0,tlimit)
meat.prop.vec <- rep(0,tlimit)
safari.prop.vec <- rep(0,tlimit)
eyci <- runif(1,eyci.min,eyci.max)
##then iterate
for (ii in 1:tlimit) {
nd <- ad %*% nd
## Calculate severity of a potential catastrophe
sev.cat.input <- runif(1,0,(max(sev.cat.prob)))
sev.cat <- ((sev.cat.coeff[1]*(sev.cat.input^sev.cat.coeff[2])))/100
##1-sev.cat
if (sev.cat > 1) sev.cat <- 0.99
## calculate whether a catastrophe occurs
if (runif(1,0,1) <= p.cat) nd <- nd*(1-sev.cat) else nd <- nd
## Set any negative values in nd to 0
zero.sub <- which(nd<0)
nd[zero.sub] <- 0
na.sub <- which(is.na(nd))
nd[na.sub] <- 0
if (sum(nd) == 0) next
## Stochastic measure (variation in rainfall)
rain.multp <- runif(1,min=rain.mult.min,max=rain.mult.max)
## rain.multp <- ifelse(stoch.select == 1, rain.multp <- rain.multp, rain.mult <- 1)
##rain.multp <- 1
## Set negative density feedback function for the matrix
## Redefine survival probabilities
s0.d <- rain.multp*(as.numeric((SSfpl(sum(nd),s0.param[1],s0.param[2],s0.param[3],s0.param[4]))))
if (s0.d < s0.max) s0.d <- s0.max else s0.d <- s0.d
if (s0.d > s0.min) s0.d <- s0.min else s0.d <- s0.d
s1p.d <- rain.multp*(as.numeric((SSfpl(sum(nd),s1p.param[1],s1p.param[2],s1p.param[3],s1p.param[4]))))
if (s1p.d < s1p.max) s1p.d <- s1p.max else s1p.d <- s1p.d
if (s1p.d > s1p.min) s1p.d <- s1p.min else s1p.d <- s1p.d
## re-define fecundity a coefficient
a.md <- as.numeric(SSfpl(sum(nd),af.param[1],af.param[2],af.param[3],af.param[4]))
if (a.md < af.max) a.md <- af.max else a.md <- a.md
if (a.md > af.min) a.md <- af.min else a.md <- a.md
## Re-define fertility vector
md.vec <- rep(0,age.max)
for (j in (1:age.max)) {
md <- a.md*exp(-0.5*(log(j/x0.fert)/b.fert)^2)
md.vec[j] <- md
}
## Adjust for demographic stochasticity (binomial & poisson resampling)
n.mf <- 2*md.vec[1:(age.max)]*nd[1:age.max]
n.f <- sum(nd[1:age.max])
sum.n.mf <- sum(n.mf)
if (sum.n.mf == 0) sum.n.mf <- 1
if ((is.na(sum.n.mf)) == TRUE) sum.n.mf <- 1
mds.vec <- sum(md.vec)*(rpois(age.max,(n.mf*n.f))/(sum.n.mf*n.f))
for (mm in 1:(length(md.vec))) {
if (md.vec[mm] > af.min) md.vec[mm] <- af.min else md.vec[mm] <- md.vec[mm]
} ## caps m at 0.62
## Tertiary sex ratio
if (sum(nd[1:age.max] / (1+sum(nd))) > tsr.max) md.vec <- rep(0,age.max) else md.vec <- md.vec
n.1 <- round(sum(nd))
if (n.1 == 0) n.1 <- 1
s1p.d <- rbinom(1,n.1,s1p.d)/n.1
if (s1p.d > s1p.min) s1p.d <- s1p.min else s1p.d <- s1p.d
n.0 <- round(sum(md.vec*n[1:age.max]))
if (n.0 == 0) n.0 <- 1
s0.d <- rbinom(1,n.0,s0.d)/n.0
## Fill matrix with adjusted survival & fecundity vectors
diag(ad[2:age.max,1:(age.max-1)]) <- s1p.d ## adds s1p.d to female quadrant
diag(ad[(age.max+2):k,(age.max+1):(k-1)]) <- s1p.d ## adds s1p.d to male quadrant
ad[1,2:(age.max)] <- s0.d * md.vec[1:(age.max-1)] ## adds female fecundity
ad[(age.max+1),2:(age.max)] <- s0.d * md.vec[1:(age.max-1)] ## adds male fecundity
## Fluctuating market values for beef sales (EYCI)
## random uniform distribution between 1996-2004 min and max EYCI (in 2004 AU$)
## Autocorrelated from last year
eyci <- as.numeric(eyci.fit$coefficients[1] + (eyci.fit$coefficients[2])*eyci)
eyci <- as.numeric(rnorm(1, mean=eyci,sd=(sqrt(eyci.var))))
## if (stoch.select == 0) eyci <- eyci.now else eyci <- eyci
##eyci <- ppkg.meat
##print(eyci)
## Cost function
cost.now <- coeff.fit.cost[3] + coeff.fit.cost[1]*exp(-coeff.fit.cost[2]*(sum(nd)))
if (cost.now > cost.max) cost.now <- cost.max
if (cost.now < cost.min) cost.now <- cost.min
##cost.now <- 0
## *****************************************************************
## Kill adult females (3-17 years)
## *****************************************************************
fem.kill <- ifelse(kill.type == 2, karray[z], 0)
nf.kill <- fem.kill/15 ## uniform distribution of kill in ages > 2
kill.f.vec <- rep(0,k)
kill.f.vec[3:age.max] <- nf.kill
if (sum(nd) < fem.kill) {
nd <- nd
}
nd <- nd - kill.f.vec ## Adjust nd vector for kill
f.mass.tot <- sum(kill.f.vec[3:age.max] * grf[4:(age.max+1)])
f.meat.tot <- f.mass.tot*meat.prop
f.cost.tot <- sum(kill.f.vec[3:age.max]) * cost.now
f.meat.dol <- f.meat.tot*eyci
## *****************************************************************
## *****************************************************************
## Kill large males (males 6-17 years)
## *****************************************************************
male.kill <- ifelse(kill.type == 1, karray[z], 0)
nm.kill <- male.kill/12 ## uniform distribution of kill in ages > 5
kill.m.vec <- rep(0,k)
kill.m.vec[23:k] <- nm.kill
if (sum(nd) < male.kill) {
nd <- nd
}
nd <- nd - kill.m.vec ## Adjust nd vector for kill
m.mal6p.kill <- sum(kill.m.vec[23:k])
m.mass.tot <- sum(kill.m.vec[23:k] * grm[7:(age.max+1)])
m.cost.tot <- sum(kill.m.vec[23:k]) * cost.now
m.meat.tot <- m.mass.tot*meat.prop
m.meat.dol <- m.meat.tot*eyci
m.m.kill <- m.mal6p.kill
dem.sub <- ifelse((m.mal6p.kill > length(dem.pred)), length(dem.pred), ceiling(m.mal6p.kill))
dem.kill <- (demf[dem.sub])
m.safari.tot <- dem.kill * safari.to
if (sum(kill.m.vec) == 0) m.safari.tot <- 0 else m.safari.tot <- m.safari.tot
## *****************************************************************
## *****************************************************************
## Kill adults (females >2 years; males >3)
## *****************************************************************
all.kill <- ifelse(kill.type == 0, karray[z], 0)
naf.kill <- (all.kill/2)/15 ## uniform distribution of females killed (> 2 years)
nam.kill <- (all.kill/2)/14 ## uniform distribution of males killed (> 3 years)
kill.all.vec <- rep(0,k)
kill.all.vec[3:age.max] <- naf.kill
kill.all.vec[21:k] <- nam.kill
if (sum(nd) < all.kill) {