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"),
|
35
|
7 make_option(c("-b", "--adult_accum"), action="store", dest="adult_accum", type="integer", help="Adjustment of DD accumulation (old nymph->adult)"),
|
33
|
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"),
|
35
|
12 make_option(c("-i", "--max_clutch_size"), action="store", dest="max_clutch_size", type="integer", help="Adjustment of maximum clutch size"),
|
33
|
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
|
35
|
210 cat("Replications: ", opt$replications, "\n")
|
|
211 cat("Photoperiod: ", opt$photoperiod, "\n")
|
|
212 cat("Oviposition rate: ", opt$oviposition, "\n")
|
|
213 cat("Egg mortality rate: ", opt$egg_mort, "\n")
|
|
214 cat("Nymph mortality rate: ", opt$nymph_mort, "\n")
|
|
215 cat("Adult mortality rate: ", opt$adult_mort, "\n")
|
|
216 cat("Min clutch size: ", opt$min_clutch_size, "\n")
|
|
217 cat("Max clutch size: ", opt$max_clutch_size, "\n")
|
|
218 cat("(egg->young nymph): ", opt$young_nymph_accum, "\n")
|
|
219 cat("(young nymph->old nymph): ", opt$old_nymph_accum, "\n")
|
|
220 cat("(old nymph->adult): ", opt$adult_accum, "\n")
|
|
221
|
33
|
222 # Read in the input temperature datafile
|
|
223 temperature_file_path <- data.input(opt$location, opt$year, opt$temperature_dataset)
|
|
224
|
|
225 # Initialize matrix for results from all replications
|
|
226 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, 365 * opt$replications), ncol = opt$replications)
|
|
227 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)
|
|
228
|
|
229 # loop through replications
|
|
230 for (N.rep in 1:opt$replications)
|
|
231 {
|
|
232 # during each replication
|
|
233 # start with 1000 individuals -- user definable as well?
|
|
234 n <- 1000
|
|
235 # Generation, Stage, DD, T, Diapause
|
|
236 vec.ini <- c(0, 3, 0, 0, 0)
|
|
237 # overwintering, previttelogenic, DD=0, T=0, no-diapause
|
|
238 vec.mat <- rep(vec.ini, n)
|
|
239 # complete matrix for the population
|
|
240 vec.mat <- t(matrix(vec.mat, nrow=5))
|
|
241 # complete photoperiod profile in a year, requires daylength function
|
|
242 ph.p <- daylength(opt$latitude)
|
|
243
|
|
244 # time series of population size
|
|
245 tot.pop <- NULL
|
|
246 # gen.0 pop size
|
|
247 gen0.pop <- rep(0, 365)
|
|
248 gen1.pop <- rep(0, 365)
|
|
249 gen2.pop <- rep(0, 365)
|
|
250 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, 365)
|
|
251 g0.adult <- g1.adult <- g2.adult <- rep(0, 365)
|
|
252 N.newborn <- N.death <- N.adult <- rep(0, 365)
|
|
253 dd.day <- rep(0, 365)
|
|
254
|
|
255 # start tick
|
|
256 ptm <- proc.time()
|
|
257
|
|
258 # all the days
|
|
259 for (day in 1:365)
|
|
260 {
|
|
261 # photoperiod in the day
|
|
262 photoperiod <- ph.p[day]
|
|
263 temp.profile <- hourtemp(opt$latitude, day, temperature_file_path)
|
|
264 mean.temp <- temp.profile[1]
|
|
265 dd.temp <- temp.profile[2]
|
|
266 dd.day[day] <- dd.temp
|
|
267 # trash bin for death
|
|
268 death.vec <- NULL
|
|
269 # new born
|
|
270 birth.vec <- NULL
|
|
271
|
|
272 # all individuals
|
|
273 for (i in 1:n)
|
|
274 {
|
|
275 # find individual record
|
|
276 vec.ind <- vec.mat[i,]
|
|
277 # first of all, still alive?
|
|
278 # adjustment for late season mortality rate
|
|
279 if (opt$latitude < 40.0)
|
|
280 {
|
|
281 post.mort <- 1
|
|
282 day.kill <- 300
|
|
283 }
|
|
284 else
|
|
285 {
|
|
286 post.mort <- 2
|
|
287 day.kill <- 250
|
|
288 }
|
|
289 if (vec.ind[2] == 0)
|
|
290 {
|
|
291 # egg
|
|
292 death.prob = opt$egg_mort * mortality.egg(mean.temp)
|
|
293 }
|
|
294 else if (vec.ind[2] == 1 | vec.ind[2] == 2)
|
|
295 {
|
|
296 death.prob = opt$nymph_mort * mortality.nymph(mean.temp)
|
|
297 }
|
|
298 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5)
|
|
299 {
|
|
300 # for adult
|
|
301 if (day < day.kill)
|
|
302 {
|
|
303 death.prob = opt$adult_mort * mortality.adult(mean.temp)
|
|
304 }
|
|
305 else
|
|
306 {
|
|
307 # increase adult mortality after fall equinox
|
|
308 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp)
|
|
309 }
|
|
310 }
|
|
311 # (or dependent on temperature and life stage?)
|
|
312 u.d <- runif(1)
|
|
313 if (u.d < death.prob)
|
|
314 {
|
|
315 death.vec <- c(death.vec, i)
|
|
316 }
|
|
317 else
|
|
318 {
|
|
319 # aggregrate index of dead bug
|
|
320 # event 1 end of diapause
|
|
321 if (vec.ind[1] == 0 && vec.ind[2] == 3)
|
|
322 {
|
|
323 # overwintering adult (previttelogenic)
|
|
324 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180)
|
|
325 {
|
|
326 # add 68C to become fully reproductively matured
|
|
327 # transfer to vittelogenic
|
|
328 vec.ind <- c(0, 4, 0, 0, 0)
|
|
329 vec.mat[i,] <- vec.ind
|
|
330 }
|
|
331 else
|
|
332 {
|
|
333 # add to DD
|
|
334 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
335 # add 1 day in current stage
|
|
336 vec.ind[4] <- vec.ind[4] + 1
|
|
337 vec.mat[i,] <- vec.ind
|
|
338 }
|
|
339 }
|
|
340 if (vec.ind[1] != 0 && vec.ind[2] == 3)
|
|
341 {
|
|
342 # NOT overwintering adult (previttelogenic)
|
|
343 current.gen <- vec.ind[1]
|
|
344 if (vec.ind[3] > 68)
|
|
345 {
|
|
346 # add 68C to become fully reproductively matured
|
|
347 # transfer to vittelogenic
|
|
348 vec.ind <- c(current.gen, 4, 0, 0, 0)
|
|
349 vec.mat[i,] <- vec.ind
|
|
350 }
|
|
351 else
|
|
352 {
|
|
353 # add to DD
|
|
354 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
355 # add 1 day in current stage
|
|
356 vec.ind[4] <- vec.ind[4] + 1
|
|
357 vec.mat[i,] <- vec.ind
|
|
358 }
|
|
359 }
|
|
360
|
|
361 # event 2 oviposition -- where population dynamics comes from
|
|
362 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10)
|
|
363 {
|
|
364 # vittelogenic stage, overwintering generation
|
|
365 if (vec.ind[4] == 0)
|
|
366 {
|
|
367 # just turned in vittelogenic stage
|
|
368 n.birth=round(runif(1, 2 + min.ovi.adj, 8 + max.ovi.adj))
|
|
369 }
|
|
370 else
|
|
371 {
|
|
372 # daily probability of birth
|
|
373 p.birth = opt$oviposition * 0.01
|
|
374 u1 <- runif(1)
|
|
375 if (u1 < p.birth)
|
|
376 {
|
|
377 n.birth=round(runif(1, 2, 8))
|
|
378 }
|
|
379 }
|
|
380 # add to DD
|
|
381 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
382 # add 1 day in current stage
|
|
383 vec.ind[4] <- vec.ind[4] + 1
|
|
384 vec.mat[i,] <- vec.ind
|
|
385 if (n.birth > 0)
|
|
386 {
|
|
387 # add new birth -- might be in different generations
|
|
388 # generation + 1
|
|
389 new.gen <- vec.ind[1] + 1
|
|
390 # egg profile
|
|
391 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
392 new.vec <- rep(new.ind, n.birth)
|
|
393 # update batch of egg profile
|
|
394 new.vec <- t(matrix(new.vec, nrow=5))
|
|
395 # group with total eggs laid in that day
|
|
396 birth.vec <- rbind(birth.vec, new.vec)
|
|
397 }
|
|
398 }
|
|
399
|
|
400 # event 2 oviposition -- for gen 1.
|
|
401 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222)
|
|
402 {
|
|
403 # vittelogenic stage, 1st generation
|
|
404 if (vec.ind[4] == 0)
|
|
405 {
|
|
406 # just turned in vittelogenic stage
|
|
407 n.birth=round(runif(1, 2 + min.ovi.adj, 8 + max.ovi.adj))
|
|
408 }
|
|
409 else
|
|
410 {
|
|
411 # daily probability of birth
|
|
412 p.birth = opt$oviposition * 0.01
|
|
413 u1 <- runif(1)
|
|
414 if (u1 < p.birth)
|
|
415 {
|
|
416 n.birth = round(runif(1, 2, 8))
|
|
417 }
|
|
418 }
|
|
419 # add to DD
|
|
420 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
421 # add 1 day in current stage
|
|
422 vec.ind[4] <- vec.ind[4] + 1
|
|
423 vec.mat[i,] <- vec.ind
|
|
424 if (n.birth > 0)
|
|
425 {
|
|
426 # add new birth -- might be in different generations
|
|
427 # generation + 1
|
|
428 new.gen <- vec.ind[1] + 1
|
|
429 # egg profile
|
|
430 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
431 new.vec <- rep(new.ind, n.birth)
|
|
432 # update batch of egg profile
|
|
433 new.vec <- t(matrix(new.vec, nrow=5))
|
|
434 # group with total eggs laid in that day
|
|
435 birth.vec <- rbind(birth.vec, new.vec)
|
|
436 }
|
|
437 }
|
|
438
|
|
439 # event 3 development (with diapause determination)
|
|
440 # event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg)
|
|
441 if (vec.ind[2] == 0)
|
|
442 {
|
|
443 # egg stage
|
|
444 # add to DD
|
|
445 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
446 if (vec.ind[3] >= (68 + dd.adj1))
|
|
447 {
|
|
448 # from egg to young nymph, DD requirement met
|
|
449 current.gen <- vec.ind[1]
|
|
450 # transfer to young nym stage
|
|
451 vec.ind <- c(current.gen, 1, 0, 0, 0)
|
|
452 }
|
|
453 else
|
|
454 {
|
|
455 # add 1 day in current stage
|
|
456 vec.ind[4] <- vec.ind[4] + 1
|
|
457 }
|
|
458 vec.mat[i,] <- vec.ind
|
|
459 }
|
|
460
|
|
461 # event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause)
|
|
462 if (vec.ind[2] == 1)
|
|
463 {
|
|
464 # young nymph stage
|
|
465 # add to DD
|
|
466 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
467 if (vec.ind[3] >= (250 +dd.adj2))
|
|
468 {
|
|
469 # from young to old nymph, DD requirement met
|
|
470 current.gen <- vec.ind[1]
|
|
471 # transfer to old nym stage
|
|
472 vec.ind <- c(current.gen, 2, 0, 0, 0)
|
|
473 if (photoperiod < opt$photoperiod && day > 180)
|
|
474 {
|
|
475 vec.ind[5] <- 1
|
|
476 } # prepare for diapausing
|
|
477 }
|
|
478 else
|
|
479 {
|
|
480 # add 1 day in current stage
|
|
481 vec.ind[4] <- vec.ind[4] + 1
|
|
482 }
|
|
483 vec.mat[i,] <- vec.ind
|
|
484 }
|
|
485
|
|
486 # event 3.3 old nymph to adult: previttelogenic or diapausing?
|
|
487 if (vec.ind[2] == 2)
|
|
488 {
|
|
489 # old nymph stage
|
|
490 # add to DD
|
|
491 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
492 if (vec.ind[3] >= (200 + dd.adj3))
|
|
493 {
|
|
494 # from old to adult, DD requirement met
|
|
495 current.gen <- vec.ind[1]
|
|
496 if (vec.ind[5] == 0)
|
|
497 {
|
|
498 # non-diapausing adult -- previttelogenic
|
|
499 vec.ind <- c(current.gen, 3, 0, 0, 0)
|
|
500 }
|
|
501 else
|
|
502 {
|
|
503 # diapausing
|
|
504 vec.ind <- c(current.gen, 5, 0, 0, 1)
|
|
505 }
|
|
506 }
|
|
507 else
|
|
508 {
|
|
509 # add 1 day in current stage
|
|
510 vec.ind[4] <- vec.ind[4] + 1
|
|
511 }
|
|
512 vec.mat[i,] <- vec.ind
|
|
513 }
|
|
514
|
|
515 # event 4 growing of diapausing adult (unimportant, but still necessary)##
|
|
516 if (vec.ind[2] == 5)
|
|
517 {
|
|
518 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
519 vec.ind[4] <- vec.ind[4] + 1
|
|
520 vec.mat[i,] <- vec.ind
|
|
521 }
|
|
522 } # else if it is still alive
|
|
523 } # end of the individual bug loop
|
|
524
|
|
525 # find how many died
|
|
526 n.death <- length(death.vec)
|
|
527 if (n.death > 0)
|
|
528 {
|
|
529 vec.mat <- vec.mat[-death.vec, ]
|
|
530 }
|
|
531 # remove record of dead
|
|
532 # find how many new born
|
|
533 n.newborn <- length(birth.vec[,1])
|
|
534 vec.mat <- rbind(vec.mat, birth.vec)
|
|
535 # update population size for the next day
|
|
536 n <- n - n.death + n.newborn
|
|
537
|
|
538 # aggregate results by day
|
|
539 tot.pop <- c(tot.pop, n)
|
|
540 # egg
|
|
541 s0 <- sum(vec.mat[,2] == 0)
|
|
542 # young nymph
|
|
543 s1 <- sum(vec.mat[,2] == 1)
|
|
544 # old nymph
|
|
545 s2 <- sum(vec.mat[,2] == 2)
|
|
546 # previtellogenic
|
|
547 s3 <- sum(vec.mat[,2] == 3)
|
|
548 # vitellogenic
|
|
549 s4 <- sum(vec.mat[,2] == 4)
|
|
550 # diapausing
|
|
551 s5 <- sum(vec.mat[,2] == 5)
|
|
552 # overwintering adult
|
|
553 gen0 <- sum(vec.mat[,1] == 0)
|
|
554 # first generation
|
|
555 gen1 <- sum(vec.mat[,1] == 1)
|
|
556 # second generation
|
|
557 gen2 <- sum(vec.mat[,1] == 2)
|
|
558 # sum of all adults
|
|
559 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
|
|
560 # gen.0 pop size
|
|
561 gen0.pop[day] <- gen0
|
|
562 gen1.pop[day] <- gen1
|
|
563 gen2.pop[day] <- gen2
|
|
564 S0[day] <- s0
|
|
565 S1[day] <- s1
|
|
566 S2[day] <- s2
|
|
567 S3[day] <- s3
|
|
568 S4[day] <- s4
|
|
569 S5[day] <- s5
|
|
570 g0.adult[day] <- sum(vec.mat[,1] == 0)
|
|
571 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))
|
|
572 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))
|
|
573
|
|
574 N.newborn[day] <- n.newborn
|
|
575 N.death[day] <- n.death
|
|
576 N.adult[day] <- n.adult
|
|
577 #print(c(N.rep, day, n, n.adult))
|
|
578 } # end of 365 days
|
|
579
|
|
580 dd.cum <- cumsum(dd.day)
|
|
581 # collect all the outputs
|
|
582 S0.rep[,N.rep] <- S0
|
|
583 S1.rep[,N.rep] <- S1
|
|
584 S2.rep[,N.rep] <- S2
|
|
585 S3.rep[,N.rep] <- S3
|
|
586 S4.rep[,N.rep] <- S4
|
|
587 S5.rep[,N.rep] <- S5
|
|
588 newborn.rep[,N.rep] <- N.newborn
|
|
589 death.rep[,N.rep] <- N.death
|
|
590 adult.rep[,N.rep] <- N.adult
|
|
591 pop.rep[,N.rep] <- tot.pop
|
|
592 g0.rep[,N.rep] <- gen0.pop
|
|
593 g1.rep[,N.rep] <- gen1.pop
|
|
594 g2.rep[,N.rep] <- gen2.pop
|
|
595 g0a.rep[,N.rep] <- g0.adult
|
|
596 g1a.rep[,N.rep] <- g1.adult
|
|
597 g2a.rep[,N.rep] <- g2.adult
|
|
598 }
|
|
599
|
|
600 # 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)
|
|
601 # maybe do not need to export this bit, but for now just leave it as-is
|
|
602 # do we need to export this Rdat file?
|
|
603
|
|
604 # Data analysis and visualization
|
|
605 # default: plot 1 year of result
|
|
606 # but can be expanded to accommodate multiple years
|
|
607 n.yr <- 1
|
|
608 day.all <- c(1:365 * n.yr)
|
|
609
|
|
610 # mean value for adults
|
|
611 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
|
|
612 # mean value for nymphs
|
|
613 sn <- apply((S1.rep + S2.rep), 1,mean)
|
|
614 # mean value for eggs
|
|
615 se <- apply(S0.rep, 1, mean)
|
|
616 # mean value for P
|
|
617 g0 <- apply(g0.rep, 1, mean)
|
|
618 # mean value for F1
|
|
619 g1 <- apply(g1.rep, 1, mean)
|
|
620 # mean value for F2
|
|
621 g2 <- apply(g2.rep, 1, mean)
|
|
622 # mean value for P adult
|
|
623 g0a <- apply(g0a.rep, 1, mean)
|
|
624 # mean value for F1 adult
|
|
625 g1a <- apply(g1a.rep, 1, mean)
|
|
626 # mean value for F2 adult
|
|
627 g2a <- apply(g2a.rep, 1, mean)
|
|
628
|
|
629 # SE for adults
|
|
630 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications)
|
|
631 # SE for nymphs
|
|
632 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd)
|
|
633 # SE for eggs
|
35
|
634 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications)
|
33
|
635 # SE value for P
|
|
636 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications)
|
|
637 # SE for F1
|
|
638 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications)
|
|
639 # SE for F2
|
|
640 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications)
|
|
641 # SE for P adult
|
|
642 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications)
|
|
643 # SE for F1 adult
|
|
644 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications)
|
|
645 # SE for F2 adult
|
|
646 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications)
|
|
647
|
35
|
648 dev.new(width=20, height=20)
|
33
|
649
|
|
650 # Start PDF device driver to save charts to output.
|
|
651 pdf(file=opt$output, height=20, width=20, bg="white")
|
|
652
|
|
653 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1))
|
|
654
|
|
655 # Subfigure 2: population size by life stage
|
|
656 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)
|
|
657 # Young and old nymphs
|
|
658 lines(day.all, sn, lwd = 2, lty = 1, col = 2)
|
|
659 # Eggs
|
|
660 lines(day.all, se, lwd = 2, lty = 1, col = 4)
|
|
661 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"))
|
|
662 axis(2, cex.axis = 2)
|
|
663 leg.text <- c("Egg", "Nymph", "Adult")
|
|
664 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(4, 2, 1), cex = 2)
|
|
665 if (opt$se_plot == 1)
|
|
666 {
|
|
667 # add SE lines to plot
|
|
668 # SE for adults
|
|
669 lines (day.all, sa + sa.se, lty = 2)
|
|
670 lines (day.all, sa - sa.se, lty = 2)
|
|
671 # SE for nymphs
|
|
672 lines (day.all, sn + sn.se, col = 2, lty = 2)
|
|
673 lines (day.all, sn - sn.se, col = 2, lty = 2)
|
|
674 # SE for eggs
|
|
675 lines (day.all, se + se.se, col = 4, lty = 2)
|
|
676 lines (day.all, se - se.se, col = 4, lty = 2)
|
|
677 }
|
|
678
|
|
679 # Subfigure 3: population size by generation
|
|
680 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)
|
|
681 lines(day.all, g1, lwd = 2, lty = 1, col = 2)
|
|
682 lines(day.all, g2, lwd = 2, lty = 1, col = 4)
|
|
683 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"))
|
|
684 axis(2, cex.axis = 2)
|
|
685 leg.text <- c("P", "F1", "F2")
|
|
686 legend("topleft", leg.text, lty = c(1, 1, 1), col =c(1, 2, 4), cex = 2)
|
|
687 if (opt$se_plot == 1)
|
|
688 {
|
|
689 # add SE lines to plot
|
|
690 # SE for adults
|
|
691 lines (day.all, g0 + g0.se, lty = 2)
|
|
692 lines (day.all, g0 - g0.se, lty = 2)
|
|
693 # SE for nymphs
|
|
694 lines (day.all, g1 + g1.se, col = 2, lty = 2)
|
|
695 lines (day.all, g1 - g1.se, col = 2, lty = 2)
|
|
696 # SE for eggs
|
|
697 lines (day.all, g2 + g2.se, col = 4, lty = 2)
|
|
698 lines (day.all, g2 - g2.se, col = 4, lty = 2)
|
|
699 }
|
|
700
|
|
701 # Subfigure 4: adult population size by generation
|
|
702 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)
|
|
703 lines(day.all, g1a, lwd = 2, lty = 1, col = 2)
|
|
704 lines(day.all, g2a, lwd = 2, lty = 1, col = 4)
|
|
705 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"))
|
|
706 axis(2, cex.axis = 2)
|
|
707 leg.text <- c("P", "F1", "F2")
|
|
708 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(1, 2, 4), cex = 2)
|
|
709 if (opt$se_plot == 1)
|
|
710 {
|
|
711 # add SE lines to plot
|
|
712 # SE for adults
|
|
713 lines (day.all, g0a + g0a.se, lty = 2)
|
|
714 lines (day.all, g0a - g0a.se, lty = 2)
|
|
715 # SE for nymphs
|
|
716 lines (day.all, g1a + g1a.se, col = 2, lty = 2)
|
|
717 lines (day.all, g1a - g1a.se, col = 2, lty = 2)
|
|
718 # SE for eggs
|
|
719 lines (day.all, g2a + g2a.se, col = 4, lty = 2)
|
|
720 lines (day.all, g2a - g2a.se, col = 4, lty = 2)
|
|
721 }
|
|
722
|
|
723 # Turn off device driver to flush output.
|
|
724 dev.off()
|