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