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