comparison insect_phenology_model.R @ 0:244c373f2a34 draft

Uploaded
author greg
date Tue, 08 Aug 2017 13:14:39 -0400
parents
children 24fa0d35a8bf
comparison
equal deleted inserted replaced
-1:000000000000 0:244c373f2a34
1 #!/usr/bin/env Rscript
2
3 suppressPackageStartupMessages(library("optparse"))
4
5 option_list <- list(
6 make_option(c("-a", "--adult_mort"), action="store", dest="adult_mort", type="integer", help="Adjustment rate for adult mortality"),
7 make_option(c("-b", "--adult_accum"), action="store", dest="adult_accum", type="integer", help="Adjustment of DD accumulation (old nymph->adult)"),
8 make_option(c("-c", "--egg_mort"), action="store", dest="egg_mort", type="integer", help="Adjustment rate for egg mortality"),
9 make_option(c("-d", "--latitude"), action="store", dest="latitude", type="double", help="Latitude of selected location"),
10 make_option(c("-e", "--location"), action="store", dest="location", help="Selected location"),
11 make_option(c("-f", "--min_clutch_size"), action="store", dest="min_clutch_size", type="integer", help="Adjustment of minimum clutch size"),
12 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
13 make_option(c("-j", "--nymph_mort"), action="store", dest="nymph_mort", type="integer", help="Adjustment rate for nymph mortality"),
14 make_option(c("-k", "--old_nymph_accum"), action="store", dest="old_nymph_accum", type="integer", help="Adjustment of DD accumulation (young nymph->old nymph)"),
15 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"),
16 make_option(c("-p", "--oviposition"), action="store", dest="oviposition", type="integer", help="Adjustment for oviposition rate"),
17 make_option(c("-q", "--photoperiod"), action="store", dest="photoperiod", type="double", help="Critical photoperiod for diapause induction/termination"),
18 make_option(c("-s", "--replications"), action="store", dest="replications", type="integer", help="Number of replications"),
19 make_option(c("-t", "--se_plot"), action="store", dest="se_plot", help="Plot SE"),
20 make_option(c("-u", "--year"), action="store", dest="year", type="integer", help="Starting year"),
21 make_option(c("-v", "--temperature_dataset"), action="store", dest="temperature_dataset", help="Temperature data for selected location"),
22 make_option(c("-y", "--young_nymph_accum"), action="store", dest="young_nymph_accum", type="integer", help="Adjustment of DD accumulation (egg->young nymph)")
23 )
24
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list)
26 args <- parse_args(parser, positional_arguments=TRUE)
27 opt <- args$options
28
29 data.input=function(loc, year, temperature.dataset)
30 {
31 expdata <- matrix(rep(0, 365 * 3), nrow=365)
32 namedat <- paste(loc, year, ".Rdat", sep="")
33 temp.data <- read.csv(file=temperature.dataset, header=T)
34
35 expdata[,1] <- c(1:365)
36 # Minimum
37 expdata[,2] <- temp.data[c(1:365), 3]
38 # Maximum
39 expdata[,3] <- temp.data[c(1:365), 2]
40 save(expdata, file=namedat)
41 namedat
42 }
43
44 daylength=function(latitude)
45 {
46 # from Forsythe 1995
47 p=0.8333
48 dl <- NULL
49 for (i in 1:365) {
50 theta <- 0.2163108 + 2 * atan(0.9671396 * tan(0.00860 * (i - 186)))
51 phi <- asin(0.39795 * cos(theta))
52 dl[i] <- 24 - 24 / pi * acos((sin(p * pi / 180) + sin(latitude * pi / 180) * sin(phi)) / (cos(latitude * pi / 180) * cos(phi)))
53 }
54 dl # return a vector of daylength in 365 days
55 }
56
57 hourtemp=function(latitude, date, temperature_file_path)
58 {
59 load(temperature_file_path)
60 threshold <- 14.17 # base development threshold for BMSB
61 dnp <- expdata[date, 2] # daily minimum
62 dxp <- expdata[date, 3] # daily maximum
63 dmean <- 0.5 * (dnp + dxp)
64 dd <- 0 # initialize degree day accumulation
65
66 if (dxp<threshold) {
67 dd <- 0
68 }
69 else {
70 dlprofile <- daylength(latitude) # extract daylength data for entire year
71 T <- NULL # initialize hourly temperature
72 dh <- NULL #initialize degree hour vector
73 # date <- 200
74 y <- dlprofile[date] # calculate daylength in given date
75 z <- 24 - y # night length
76 a <- 1.86 # lag coefficient
77 b <- 2.20 # night coefficient
78 #tempdata <- read.csv("tempdata.csv") #import raw data set
79 # Should be outside function otherwise its redundant
80 risetime <- 12 - y / 2 # sunrise time
81 settime <- 12 + y / 2 # sunset time
82 ts <- (dxp - dnp) * sin(pi * (settime - 5) / (y + 2 * a)) + dnp
83 for (i in 1:24) {
84 if (i > risetime && i<settime) {
85 m <- i - 5 # number of hours after Tmin until sunset
86 T[i]=(dxp - dnp) * sin(pi * m / (y + 2 * a)) + dnp
87 if (T[i]<8.4) {
88 dh[i] <- 0
89 }
90 else {
91 dh[i] <- T[i] - 8.4
92 }
93 }
94 else if (i > settime) {
95 n <- i - settime
96 T[i]=dnp + (ts - dnp) * exp( - b * n / z)
97 if (T[i]<8.4) {
98 dh[i] <- 0
99 }
100 else {
101 dh[i] <- T[i] - 8.4
102 }
103 }
104 else {
105 n <- i + 24 - settime
106 T[i]=dnp + (ts - dnp) * exp( - b * n / z)
107 if (T[i]<8.4) {
108 dh[i] <- 0
109 }
110 else {
111 dh[i] <- T[i] - 8.4
112 }
113 }
114 }
115 dd <- sum(dh) / 24
116 }
117 return=c(dmean, dd)
118 return
119 }
120
121 dev.egg = function(temperature)
122 {
123 dev.rate= -0.9843 * temperature + 33.438
124 return = dev.rate
125 return
126 }
127
128 dev.young = function(temperature)
129 {
130 n12 <- -0.3728 * temperature + 14.68
131 n23 <- -0.6119 * temperature + 25.249
132 dev.rate = mean(n12 + n23)
133 return = dev.rate
134 return
135 }
136
137 dev.old = function(temperature)
138 {
139 n34 <- -0.6119 * temperature + 17.602
140 n45 <- -0.4408 * temperature + 19.036
141 dev.rate = mean(n34 + n45)
142 return = dev.rate
143 return
144 }
145
146 dev.emerg = function(temperature)
147 {
148 emerg.rate <- -0.5332 * temperature + 24.147
149 return = emerg.rate
150 return
151 }
152
153 mortality.egg = function(temperature)
154 {
155 if (temperature < 12.7) {
156 mort.prob = 0.8
157 }
158 else {
159 mort.prob = 0.8 - temperature / 40.0
160 if (mort.prob < 0) {
161 mort.prob = 0.01
162 }
163 }
164 return = mort.prob
165 return
166 }
167
168 mortality.nymph = function(temperature)
169 {
170 if (temperature < 12.7) {
171 mort.prob = 0.03
172 }
173 else {
174 mort.prob = temperature * 0.0008 + 0.03
175 }
176 return = mort.prob
177 return
178 }
179
180 mortality.adult = function(temperature)
181 {
182 if (temperature < 12.7) {
183 mort.prob = 0.002
184 }
185 else {
186 mort.prob = temperature * 0.0005 + 0.02
187 }
188 return = mort.prob
189 return
190 }
191
192 cat("Replications: ", opt$replications, "\n")
193 cat("Photoperiod: ", opt$photoperiod, "\n")
194 cat("Oviposition rate: ", opt$oviposition, "\n")
195 cat("Egg mortality rate: ", opt$egg_mort, "\n")
196 cat("Nymph mortality rate: ", opt$nymph_mort, "\n")
197 cat("Adult mortality rate: ", opt$adult_mort, "\n")
198 cat("Min clutch size: ", opt$min_clutch_size, "\n")
199 cat("Max clutch size: ", opt$max_clutch_size, "\n")
200 cat("(egg->young nymph): ", opt$young_nymph_accum, "\n")
201 cat("(young nymph->old nymph): ", opt$old_nymph_accum, "\n")
202 cat("(old nymph->adult): ", opt$adult_accum, "\n")
203
204 # Read in the input temperature datafile
205 temperature_file_path <- data.input(opt$location, opt$year, opt$temperature_dataset)
206
207 # Initialize matrix for results from all replications
208 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, 365 * opt$replications), ncol = opt$replications)
209 newborn.rep <- death.rep <- adult.rep <- pop.rep <- g0.rep <- g1.rep <- g2.rep <- g0a.rep <- g1a.rep <- g2a.rep <- matrix(rep(0, 365 * opt$replications), ncol=opt$replications)
210
211 # loop through replications
212 for (N.rep in 1:opt$replications) {
213 # during each replication
214 # start with 1000 individuals -- user definable as well?
215 n <- 1000
216 # Generation, Stage, DD, T, Diapause
217 vec.ini <- c(0, 3, 0, 0, 0)
218 # overwintering, previttelogenic, DD=0, T=0, no-diapause
219 vec.mat <- rep(vec.ini, n)
220 # complete matrix for the population
221 vec.mat <- t(matrix(vec.mat, nrow=5))
222 # complete photoperiod profile in a year, requires daylength function
223 ph.p <- daylength(opt$latitude)
224
225 # time series of population size
226 tot.pop <- NULL
227 # gen.0 pop size
228 gen0.pop <- rep(0, 365)
229 gen1.pop <- rep(0, 365)
230 gen2.pop <- rep(0, 365)
231 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365)
232 g0.adult <- g1.adult <- g2.adult <- rep(0, 365)
233 N.newborn <- N.death <- N.adult <- rep(0, 365)
234 dd.day <- rep(0, 365)
235
236 # start tick
237 ptm <- proc.time()
238
239 # all the days
240 for (day in 1:365) {
241 # photoperiod in the day
242 photoperiod <- ph.p[day]
243 temp.profile <- hourtemp(opt$latitude, day, temperature_file_path)
244 mean.temp <- temp.profile[1]
245 dd.temp <- temp.profile[2]
246 dd.day[day] <- dd.temp
247 # trash bin for death
248 death.vec <- NULL
249 # new born
250 birth.vec <- NULL
251
252 # all individuals
253 for (i in 1:n) {
254 # find individual record
255 vec.ind <- vec.mat[i,]
256 # first of all, still alive?
257 # adjustment for late season mortality rate
258 if (opt$latitude < 40.0) {
259 post.mort <- 1
260 day.kill <- 300
261 }
262 else {
263 post.mort <- 2
264 day.kill <- 250
265 }
266 if (vec.ind[2] == 0) {
267 # egg
268 death.prob = opt$egg_mort * mortality.egg(mean.temp)
269 }
270 else if (vec.ind[2] == 1 | vec.ind[2] == 2) {
271 death.prob = opt$nymph_mort * mortality.nymph(mean.temp)
272 }
273 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) {
274 # for adult
275 if (day < day.kill) {
276 death.prob = opt$adult_mort * mortality.adult(mean.temp)
277 }
278 else {
279 # increase adult mortality after fall equinox
280 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp)
281 }
282 }
283 # (or dependent on temperature and life stage?)
284 u.d <- runif(1)
285 if (u.d < death.prob) {
286 death.vec <- c(death.vec, i)
287 }
288 else {
289 # aggregrate index of dead bug
290 # event 1 end of diapause
291 if (vec.ind[1] == 0 && vec.ind[2] == 3) {
292 # overwintering adult (previttelogenic)
293 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180) {
294 # add 68C to become fully reproductively matured
295 # transfer to vittelogenic
296 vec.ind <- c(0, 4, 0, 0, 0)
297 vec.mat[i,] <- vec.ind
298 }
299 else {
300 # add to DD
301 vec.ind[3] <- vec.ind[3] + dd.temp
302 # add 1 day in current stage
303 vec.ind[4] <- vec.ind[4] + 1
304 vec.mat[i,] <- vec.ind
305 }
306 }
307 if (vec.ind[1] != 0 && vec.ind[2] == 3) {
308 # NOT overwintering adult (previttelogenic)
309 current.gen <- vec.ind[1]
310 if (vec.ind[3] > 68) {
311 # add 68C to become fully reproductively matured
312 # transfer to vittelogenic
313 vec.ind <- c(current.gen, 4, 0, 0, 0)
314 vec.mat[i,] <- vec.ind
315 }
316 else {
317 # add to DD
318 vec.ind[3] <- vec.ind[3] + dd.temp
319 # add 1 day in current stage
320 vec.ind[4] <- vec.ind[4] + 1
321 vec.mat[i,] <- vec.ind
322 }
323 }
324
325 # event 2 oviposition -- where population dynamics comes from
326 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) {
327 # vittelogenic stage, overwintering generation
328 if (vec.ind[4] == 0) {
329 # just turned in vittelogenic stage
330 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
331 }
332 else {
333 # daily probability of birth
334 p.birth = opt$oviposition * 0.01
335 u1 <- runif(1)
336 if (u1 < p.birth) {
337 n.birth=round(runif(1, 2, 8))
338 }
339 }
340 # add to DD
341 vec.ind[3] <- vec.ind[3] + dd.temp
342 # add 1 day in current stage
343 vec.ind[4] <- vec.ind[4] + 1
344 vec.mat[i,] <- vec.ind
345 if (n.birth > 0) {
346 # add new birth -- might be in different generations
347 # generation + 1
348 new.gen <- vec.ind[1] + 1
349 # egg profile
350 new.ind <- c(new.gen, 0, 0, 0, 0)
351 new.vec <- rep(new.ind, n.birth)
352 # update batch of egg profile
353 new.vec <- t(matrix(new.vec, nrow=5))
354 # group with total eggs laid in that day
355 birth.vec <- rbind(birth.vec, new.vec)
356 }
357 }
358
359 # event 2 oviposition -- for gen 1.
360 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222) {
361 # vittelogenic stage, 1st generation
362 if (vec.ind[4] == 0) {
363 # just turned in vittelogenic stage
364 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
365 }
366 else {
367 # daily probability of birth
368 p.birth = opt$oviposition * 0.01
369 u1 <- runif(1)
370 if (u1 < p.birth) {
371 n.birth = round(runif(1, 2, 8))
372 }
373 }
374 # add to DD
375 vec.ind[3] <- vec.ind[3] + dd.temp
376 # add 1 day in current stage
377 vec.ind[4] <- vec.ind[4] + 1
378 vec.mat[i,] <- vec.ind
379 if (n.birth > 0) {
380 # add new birth -- might be in different generations
381 # generation + 1
382 new.gen <- vec.ind[1] + 1
383 # egg profile
384 new.ind <- c(new.gen, 0, 0, 0, 0)
385 new.vec <- rep(new.ind, n.birth)
386 # update batch of egg profile
387 new.vec <- t(matrix(new.vec, nrow=5))
388 # group with total eggs laid in that day
389 birth.vec <- rbind(birth.vec, new.vec)
390 }
391 }
392
393 # event 3 development (with diapause determination)
394 # event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg)
395 if (vec.ind[2] == 0) {
396 # egg stage
397 # add to DD
398 vec.ind[3] <- vec.ind[3] + dd.temp
399 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) {
400 # from egg to young nymph, DD requirement met
401 current.gen <- vec.ind[1]
402 # transfer to young nym stage
403 vec.ind <- c(current.gen, 1, 0, 0, 0)
404 }
405 else {
406 # add 1 day in current stage
407 vec.ind[4] <- vec.ind[4] + 1
408 }
409 vec.mat[i,] <- vec.ind
410 }
411
412 # event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause)
413 if (vec.ind[2] == 1) {
414 # young nymph stage
415 # add to DD
416 vec.ind[3] <- vec.ind[3] + dd.temp
417 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) {
418 # from young to old nymph, DD requirement met
419 current.gen <- vec.ind[1]
420 # transfer to old nym stage
421 vec.ind <- c(current.gen, 2, 0, 0, 0)
422 if (photoperiod < opt$photoperiod && day > 180) {
423 vec.ind[5] <- 1
424 } # prepare for diapausing
425 }
426 else {
427 # add 1 day in current stage
428 vec.ind[4] <- vec.ind[4] + 1
429 }
430 vec.mat[i,] <- vec.ind
431 }
432
433 # event 3.3 old nymph to adult: previttelogenic or diapausing?
434 if (vec.ind[2] == 2) {
435 # old nymph stage
436 # add to DD
437 vec.ind[3] <- vec.ind[3] + dd.temp
438 if (vec.ind[3] >= (200 + opt$adult_accum)) {
439 # from old to adult, DD requirement met
440 current.gen <- vec.ind[1]
441 if (vec.ind[5] == 0) {
442 # non-diapausing adult -- previttelogenic
443 vec.ind <- c(current.gen, 3, 0, 0, 0)
444 }
445 else {
446 # diapausing
447 vec.ind <- c(current.gen, 5, 0, 0, 1)
448 }
449 }
450 else {
451 # add 1 day in current stage
452 vec.ind[4] <- vec.ind[4] + 1
453 }
454 vec.mat[i,] <- vec.ind
455 }
456
457 # event 4 growing of diapausing adult (unimportant, but still necessary)##
458 if (vec.ind[2] == 5) {
459 vec.ind[3] <- vec.ind[3] + dd.temp
460 vec.ind[4] <- vec.ind[4] + 1
461 vec.mat[i,] <- vec.ind
462 }
463 } # else if it is still alive
464 } # end of the individual bug loop
465
466 # find how many died
467 n.death <- length(death.vec)
468 if (n.death > 0) {
469 vec.mat <- vec.mat[-death.vec, ]
470 }
471 # remove record of dead
472 # find how many new born
473 n.newborn <- length(birth.vec[,1])
474 vec.mat <- rbind(vec.mat, birth.vec)
475 # update population size for the next day
476 n <- n - n.death + n.newborn
477
478 # aggregate results by day
479 tot.pop <- c(tot.pop, n)
480 # egg
481 s0 <- sum(vec.mat[,2] == 0)
482 # young nymph
483 s1 <- sum(vec.mat[,2] == 1)
484 # old nymph
485 s2 <- sum(vec.mat[,2] == 2)
486 # previtellogenic
487 s3 <- sum(vec.mat[,2] == 3)
488 # vitellogenic
489 s4 <- sum(vec.mat[,2] == 4)
490 # diapausing
491 s5 <- sum(vec.mat[,2] == 5)
492 # overwintering adult
493 gen0 <- sum(vec.mat[,1] == 0)
494 # first generation
495 gen1 <- sum(vec.mat[,1] == 1)
496 # second generation
497 gen2 <- sum(vec.mat[,1] == 2)
498 # sum of all adults
499 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
500 # gen.0 pop size
501 gen0.pop[day] <- gen0
502 gen1.pop[day] <- gen1
503 gen2.pop[day] <- gen2
504 S0[day] <- s0
505 S1[day] <- s1
506 S2[day] <- s2
507 S3[day] <- s3
508 S4[day] <- s4
509 S5[day] <- s5
510 g0.adult[day] <- sum(vec.mat[,1] == 0)
511 g1.adult[day] <- sum((vec.mat[,1] == 1 & vec.mat[,2] == 3) | (vec.mat[,1] == 1 & vec.mat[,2] == 4) | (vec.mat[,1] == 1 & vec.mat[,2] == 5))
512 g2.adult[day] <- sum((vec.mat[,1]== 2 & vec.mat[,2] == 3) | (vec.mat[,1] == 2 & vec.mat[,2] == 4) | (vec.mat[,1] == 2 & vec.mat[,2] == 5))
513
514 N.newborn[day] <- n.newborn
515 N.death[day] <- n.death
516 N.adult[day] <- n.adult
517 #print(c(N.rep, day, n, n.adult))
518 } # end of 365 days
519
520 dd.cum <- cumsum(dd.day)
521 # collect all the outputs
522 S0.rep[,N.rep] <- S0
523 S1.rep[,N.rep] <- S1
524 S2.rep[,N.rep] <- S2
525 S3.rep[,N.rep] <- S3
526 S4.rep[,N.rep] <- S4
527 S5.rep[,N.rep] <- S5
528 newborn.rep[,N.rep] <- N.newborn
529 death.rep[,N.rep] <- N.death
530 adult.rep[,N.rep] <- N.adult
531 pop.rep[,N.rep] <- tot.pop
532 g0.rep[,N.rep] <- gen0.pop
533 g1.rep[,N.rep] <- gen1.pop
534 g2.rep[,N.rep] <- gen2.pop
535 g0a.rep[,N.rep] <- g0.adult
536 g1a.rep[,N.rep] <- g1.adult
537 g2a.rep[,N.rep] <- g2.adult
538 }
539
540 # save(dd.day, dd.cum, S0.rep, S1.rep, S2.rep, S3.rep, S4.rep, S5.rep, newborn.rep, death.rep, adult.rep, pop.rep, g0.rep, g1.rep, g2.rep, g0a.rep, g1a.rep, g2a.rep, file=opt$output)
541 # maybe do not need to export this bit, but for now just leave it as-is
542 # do we need to export this Rdat file?
543
544 # Data analysis and visualization
545 # default: plot 1 year of result
546 # but can be expanded to accommodate multiple years
547 n.yr <- 1
548 day.all <- c(1:365 * n.yr)
549
550 # mean value for adults
551 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
552 # mean value for nymphs
553 sn <- apply((S1.rep + S2.rep), 1,mean)
554 # mean value for eggs
555 se <- apply(S0.rep, 1, mean)
556 # mean value for P
557 g0 <- apply(g0.rep, 1, mean)
558 # mean value for F1
559 g1 <- apply(g1.rep, 1, mean)
560 # mean value for F2
561 g2 <- apply(g2.rep, 1, mean)
562 # mean value for P adult
563 g0a <- apply(g0a.rep, 1, mean)
564 # mean value for F1 adult
565 g1a <- apply(g1a.rep, 1, mean)
566 # mean value for F2 adult
567 g2a <- apply(g2a.rep, 1, mean)
568
569 # SE for adults
570 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications)
571 # SE for nymphs
572 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd)
573 # SE for eggs
574 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications)
575 # SE value for P
576 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications)
577 # SE for F1
578 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications)
579 # SE for F2
580 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications)
581 # SE for P adult
582 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications)
583 # SE for F1 adult
584 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications)
585 # SE for F2 adult
586 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications)
587
588 dev.new(width=20, height=20)
589
590 # Start PDF device driver to save charts to output.
591 pdf(file=opt$output, height=20, width=20, bg="white")
592
593 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1))
594
595 # Subfigure 2: population size by life stage
596 plot(day.all, sa, main = "BSMB Total Population Size by Life Stage", type = "l", ylim = c(0, max(se + se.se, sn + sn.se, sa + sa.se)), axes = F, lwd = 2, xlab = "", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
597 # Young and old nymphs
598 lines(day.all, sn, lwd = 2, lty = 1, col = 2)
599 # Eggs
600 lines(day.all, se, lwd = 2, lty = 1, col = 4)
601 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
602 axis(2, cex.axis = 2)
603 leg.text <- c("Egg", "Nymph", "Adult")
604 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(4, 2, 1), cex = 2)
605 if (opt$se_plot == 1) {
606 # add SE lines to plot
607 # SE for adults
608 lines (day.all, sa + sa.se, lty = 2)
609 lines (day.all, sa - sa.se, lty = 2)
610 # SE for nymphs
611 lines (day.all, sn + sn.se, col = 2, lty = 2)
612 lines (day.all, sn - sn.se, col = 2, lty = 2)
613 # SE for eggs
614 lines (day.all, se + se.se, col = 4, lty = 2)
615 lines (day.all, se - se.se, col = 4, lty = 2)
616 }
617
618 # Subfigure 3: population size by generation
619 plot(day.all, g0, main = "BSMB Total Population Size by Generation", type = "l", ylim = c(0, max(g2)), axes = F, lwd = 2, xlab = "", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
620 lines(day.all, g1, lwd = 2, lty = 1, col = 2)
621 lines(day.all, g2, lwd = 2, lty = 1, col = 4)
622 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
623 axis(2, cex.axis = 2)
624 leg.text <- c("P", "F1", "F2")
625 legend("topleft", leg.text, lty = c(1, 1, 1), col =c(1, 2, 4), cex = 2)
626 if (opt$se_plot == 1) {
627 # add SE lines to plot
628 # SE for adults
629 lines (day.all, g0 + g0.se, lty = 2)
630 lines (day.all, g0 - g0.se, lty = 2)
631 # SE for nymphs
632 lines (day.all, g1 + g1.se, col = 2, lty = 2)
633 lines (day.all, g1 - g1.se, col = 2, lty = 2)
634 # SE for eggs
635 lines (day.all, g2 + g2.se, col = 4, lty = 2)
636 lines (day.all, g2 - g2.se, col = 4, lty = 2)
637 }
638
639 # Subfigure 4: adult population size by generation
640 plot(day.all, g0a, ylim = c(0, max(g2a) + 100), main = "BSMB Adult Population Size by Generation", type = "l", axes = F, lwd = 2, xlab = "Year", ylab = "Number", cex = 2, cex.lab = 2, cex.axis = 2, cex.main = 2)
641 lines(day.all, g1a, lwd = 2, lty = 1, col = 2)
642 lines(day.all, g2a, lwd = 2, lty = 1, col = 4)
643 axis(1, at = c(1:12) * 30 - 15, cex.axis = 2, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"))
644 axis(2, cex.axis = 2)
645 leg.text <- c("P", "F1", "F2")
646 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(1, 2, 4), cex = 2)
647 if (opt$se_plot == 1) {
648 # add SE lines to plot
649 # SE for adults
650 lines (day.all, g0a + g0a.se, lty = 2)
651 lines (day.all, g0a - g0a.se, lty = 2)
652 # SE for nymphs
653 lines (day.all, g1a + g1a.se, col = 2, lty = 2)
654 lines (day.all, g1a - g1a.se, col = 2, lty = 2)
655 # SE for eggs
656 lines (day.all, g2a + g2a.se, col = 4, lty = 2)
657 lines (day.all, g2a - g2a.se, col = 4, lty = 2)
658 }
659
660 # Turn off device driver to flush output.
661 dev.off()