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
|
28
|
206 cat("Number of days: ", opt$num_days, "\n")
|
26
|
207 cat("Latitude: ", latitude, "\n")
|
29
|
208 cat("Replications: ", opt$replications, "\n")
|
26
|
209
|
27
|
210 # Initialize matrix for results from all replications.
|
13
|
211 S0.rep <- S1.rep <- S2.rep <- S3.rep <- S4.rep <- S5.rep <- matrix(rep(0, opt$num_days * opt$replications), ncol = opt$replications)
|
|
212 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
|
213
|
|
214 # loop through replications
|
|
215 for (N.rep in 1:opt$replications) {
|
26
|
216 # During each replication start with 1000 individuals.
|
|
217 # TODO: user definable as well?
|
0
|
218 n <- 1000
|
26
|
219 # Generation, Stage, DD, T, Diapause.
|
0
|
220 vec.ini <- c(0, 3, 0, 0, 0)
|
26
|
221 # Overwintering, previttelogenic, DD=0, T=0, no-diapause.
|
0
|
222 vec.mat <- rep(vec.ini, n)
|
26
|
223 # Complete matrix for the population.
|
|
224 vec.mat <- base::t(matrix(vec.mat, nrow=5))
|
|
225 # Complete photoperiod profile in a year, requires daylength function.
|
22
|
226 ph.p <- daylength(latitude, opt$num_days)
|
0
|
227
|
26
|
228 # Time series of population size.
|
0
|
229 tot.pop <- NULL
|
13
|
230 gen0.pop <- rep(0, opt$num_days)
|
|
231 gen1.pop <- rep(0, opt$num_days)
|
|
232 gen2.pop <- rep(0, opt$num_days)
|
|
233 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days)
|
|
234 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days)
|
|
235 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days)
|
|
236 dd.day <- rep(0, opt$num_days)
|
0
|
237
|
22
|
238 # All the days included in the input temperature dataset.
|
13
|
239 for (day in 1:opt$num_days) {
|
26
|
240 # Photoperiod in the day.
|
0
|
241 photoperiod <- ph.p[day]
|
22
|
242 temp.profile <- hourtemp(latitude, day, temperature_file_path, opt$num_days)
|
0
|
243 mean.temp <- temp.profile[1]
|
|
244 dd.temp <- temp.profile[2]
|
|
245 dd.day[day] <- dd.temp
|
26
|
246 # Trash bin for death.
|
0
|
247 death.vec <- NULL
|
26
|
248 # Newborn.
|
0
|
249 birth.vec <- NULL
|
|
250
|
26
|
251 # All individuals.
|
0
|
252 for (i in 1:n) {
|
26
|
253 # Find individual record.
|
0
|
254 vec.ind <- vec.mat[i,]
|
26
|
255 # First of all, still alive?
|
|
256 # Adjustment for late season mortality rate.
|
22
|
257 if (latitude < 40.0) {
|
0
|
258 post.mort <- 1
|
|
259 day.kill <- 300
|
|
260 }
|
|
261 else {
|
|
262 post.mort <- 2
|
|
263 day.kill <- 250
|
|
264 }
|
|
265 if (vec.ind[2] == 0) {
|
26
|
266 # Egg.
|
0
|
267 death.prob = opt$egg_mort * mortality.egg(mean.temp)
|
|
268 }
|
|
269 else if (vec.ind[2] == 1 | vec.ind[2] == 2) {
|
|
270 death.prob = opt$nymph_mort * mortality.nymph(mean.temp)
|
|
271 }
|
|
272 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) {
|
26
|
273 # For adult.
|
0
|
274 if (day < day.kill) {
|
|
275 death.prob = opt$adult_mort * mortality.adult(mean.temp)
|
|
276 }
|
|
277 else {
|
26
|
278 # Increase adult mortality after fall equinox.
|
0
|
279 death.prob = opt$adult_mort * post.mort * mortality.adult(mean.temp)
|
|
280 }
|
|
281 }
|
|
282 # (or dependent on temperature and life stage?)
|
|
283 u.d <- runif(1)
|
|
284 if (u.d < death.prob) {
|
|
285 death.vec <- c(death.vec, i)
|
|
286 }
|
|
287 else {
|
26
|
288 # Aggregrate index of dead bug.
|
|
289 # Event 1 end of diapause.
|
0
|
290 if (vec.ind[1] == 0 && vec.ind[2] == 3) {
|
26
|
291 # Overwintering adult (previttelogenic).
|
0
|
292 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && day < 180) {
|
26
|
293 # Add 68C to become fully reproductively matured.
|
|
294 # Transfer to vittelogenic.
|
0
|
295 vec.ind <- c(0, 4, 0, 0, 0)
|
|
296 vec.mat[i,] <- vec.ind
|
|
297 }
|
|
298 else {
|
26
|
299 # Add to dd.
|
0
|
300 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
301 # Add 1 day in current stage.
|
0
|
302 vec.ind[4] <- vec.ind[4] + 1
|
|
303 vec.mat[i,] <- vec.ind
|
|
304 }
|
|
305 }
|
|
306 if (vec.ind[1] != 0 && vec.ind[2] == 3) {
|
26
|
307 # Not overwintering adult (previttelogenic).
|
0
|
308 current.gen <- vec.ind[1]
|
|
309 if (vec.ind[3] > 68) {
|
26
|
310 # Add 68C to become fully reproductively matured.
|
|
311 # Transfer to vittelogenic.
|
0
|
312 vec.ind <- c(current.gen, 4, 0, 0, 0)
|
|
313 vec.mat[i,] <- vec.ind
|
|
314 }
|
|
315 else {
|
26
|
316 # Add to dd.
|
0
|
317 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
318 # Add 1 day in current stage.
|
0
|
319 vec.ind[4] <- vec.ind[4] + 1
|
|
320 vec.mat[i,] <- vec.ind
|
|
321 }
|
|
322 }
|
|
323
|
26
|
324 # Event 2 oviposition -- where population dynamics comes from.
|
0
|
325 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) {
|
26
|
326 # Vittelogenic stage, overwintering generation.
|
0
|
327 if (vec.ind[4] == 0) {
|
26
|
328 # Just turned in vittelogenic stage.
|
0
|
329 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
|
|
330 }
|
|
331 else {
|
26
|
332 # Daily probability of birth.
|
0
|
333 p.birth = opt$oviposition * 0.01
|
|
334 u1 <- runif(1)
|
|
335 if (u1 < p.birth) {
|
|
336 n.birth=round(runif(1, 2, 8))
|
|
337 }
|
|
338 }
|
26
|
339 # Add to dd.
|
0
|
340 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
341 # Add 1 day in current stage.
|
0
|
342 vec.ind[4] <- vec.ind[4] + 1
|
|
343 vec.mat[i,] <- vec.ind
|
|
344 if (n.birth > 0) {
|
26
|
345 # Add new birth -- might be in different generations.
|
0
|
346 new.gen <- vec.ind[1] + 1
|
26
|
347 # Egg profile.
|
0
|
348 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
349 new.vec <- rep(new.ind, n.birth)
|
26
|
350 # Update batch of egg profile.
|
0
|
351 new.vec <- t(matrix(new.vec, nrow=5))
|
26
|
352 # Group with total eggs laid in that day.
|
0
|
353 birth.vec <- rbind(birth.vec, new.vec)
|
|
354 }
|
|
355 }
|
|
356
|
26
|
357 # Event 2 oviposition -- for gen 1.
|
0
|
358 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && day < 222) {
|
26
|
359 # Vittelogenic stage, 1st generation
|
0
|
360 if (vec.ind[4] == 0) {
|
26
|
361 # Just turned in vittelogenic stage.
|
0
|
362 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
|
|
363 }
|
|
364 else {
|
26
|
365 # Daily probability of birth.
|
0
|
366 p.birth = opt$oviposition * 0.01
|
|
367 u1 <- runif(1)
|
|
368 if (u1 < p.birth) {
|
|
369 n.birth = round(runif(1, 2, 8))
|
|
370 }
|
|
371 }
|
26
|
372 # Add to dd.
|
0
|
373 vec.ind[3] <- vec.ind[3] + dd.temp
|
26
|
374 # Add 1 day in current stage.
|
0
|
375 vec.ind[4] <- vec.ind[4] + 1
|
|
376 vec.mat[i,] <- vec.ind
|
|
377 if (n.birth > 0) {
|
26
|
378 # Add new birth -- might be in different generations.
|
0
|
379 new.gen <- vec.ind[1] + 1
|
26
|
380 # Egg profile.
|
0
|
381 new.ind <- c(new.gen, 0, 0, 0, 0)
|
|
382 new.vec <- rep(new.ind, n.birth)
|
26
|
383 # Update batch of egg profile.
|
0
|
384 new.vec <- t(matrix(new.vec, nrow=5))
|
26
|
385 # Group with total eggs laid in that day.
|
0
|
386 birth.vec <- rbind(birth.vec, new.vec)
|
|
387 }
|
|
388 }
|
|
389
|
26
|
390 # Event 3 development (with diapause determination).
|
|
391 # Event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg).
|
0
|
392 if (vec.ind[2] == 0) {
|
26
|
393 # Egg stage.
|
|
394 # Add to dd.
|
0
|
395 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
396 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) {
|
26
|
397 # From egg to young nymph, DD requirement met.
|
0
|
398 current.gen <- vec.ind[1]
|
26
|
399 # Transfer to young nymph stage.
|
0
|
400 vec.ind <- c(current.gen, 1, 0, 0, 0)
|
|
401 }
|
|
402 else {
|
26
|
403 # Add 1 day in current stage.
|
0
|
404 vec.ind[4] <- vec.ind[4] + 1
|
|
405 }
|
|
406 vec.mat[i,] <- vec.ind
|
|
407 }
|
|
408
|
26
|
409 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause).
|
0
|
410 if (vec.ind[2] == 1) {
|
26
|
411 # young nymph stage.
|
|
412 # add to dd.
|
0
|
413 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
414 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) {
|
26
|
415 # From young to old nymph, dd requirement met.
|
0
|
416 current.gen <- vec.ind[1]
|
26
|
417 # Transfer to old nym stage.
|
0
|
418 vec.ind <- c(current.gen, 2, 0, 0, 0)
|
|
419 if (photoperiod < opt$photoperiod && day > 180) {
|
|
420 vec.ind[5] <- 1
|
26
|
421 } # Prepare for diapausing.
|
0
|
422 }
|
|
423 else {
|
26
|
424 # Add 1 day in current stage.
|
0
|
425 vec.ind[4] <- vec.ind[4] + 1
|
|
426 }
|
|
427 vec.mat[i,] <- vec.ind
|
|
428 }
|
|
429
|
26
|
430 # Event 3.3 old nymph to adult: previttelogenic or diapausing?
|
0
|
431 if (vec.ind[2] == 2) {
|
26
|
432 # Old nymph stage.
|
|
433 # add to dd.
|
0
|
434 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
435 if (vec.ind[3] >= (200 + opt$adult_accum)) {
|
26
|
436 # From old to adult, dd requirement met.
|
0
|
437 current.gen <- vec.ind[1]
|
|
438 if (vec.ind[5] == 0) {
|
26
|
439 # Non-diapausing adult -- previttelogenic.
|
0
|
440 vec.ind <- c(current.gen, 3, 0, 0, 0)
|
|
441 }
|
|
442 else {
|
26
|
443 # Diapausing.
|
0
|
444 vec.ind <- c(current.gen, 5, 0, 0, 1)
|
|
445 }
|
|
446 }
|
|
447 else {
|
26
|
448 # Add 1 day in current stage.
|
0
|
449 vec.ind[4] <- vec.ind[4] + 1
|
|
450 }
|
|
451 vec.mat[i,] <- vec.ind
|
|
452 }
|
|
453
|
26
|
454 # Event 4 growing of diapausing adult (unimportant, but still necessary).
|
0
|
455 if (vec.ind[2] == 5) {
|
|
456 vec.ind[3] <- vec.ind[3] + dd.temp
|
|
457 vec.ind[4] <- vec.ind[4] + 1
|
|
458 vec.mat[i,] <- vec.ind
|
|
459 }
|
26
|
460 } # Else if it is still alive.
|
|
461 } # End of the individual bug loop.
|
0
|
462
|
26
|
463 # Find how many died.
|
0
|
464 n.death <- length(death.vec)
|
|
465 if (n.death > 0) {
|
|
466 vec.mat <- vec.mat[-death.vec, ]
|
|
467 }
|
26
|
468 # Remove record of dead.
|
|
469 # Find how many new born.
|
0
|
470 n.newborn <- length(birth.vec[,1])
|
|
471 vec.mat <- rbind(vec.mat, birth.vec)
|
26
|
472 # Update population size for the next day.
|
0
|
473 n <- n - n.death + n.newborn
|
|
474
|
26
|
475 # Aggregate results by day.
|
0
|
476 tot.pop <- c(tot.pop, n)
|
26
|
477 # Egg.
|
0
|
478 s0 <- sum(vec.mat[,2] == 0)
|
26
|
479 # Young nymph.
|
0
|
480 s1 <- sum(vec.mat[,2] == 1)
|
26
|
481 # Old nymph.
|
0
|
482 s2 <- sum(vec.mat[,2] == 2)
|
26
|
483 # Previtellogenic.
|
0
|
484 s3 <- sum(vec.mat[,2] == 3)
|
26
|
485 # Vitellogenic.
|
0
|
486 s4 <- sum(vec.mat[,2] == 4)
|
26
|
487 # Diapausing.
|
0
|
488 s5 <- sum(vec.mat[,2] == 5)
|
26
|
489 # Overwintering adult.
|
0
|
490 gen0 <- sum(vec.mat[,1] == 0)
|
26
|
491 # First generation.
|
0
|
492 gen1 <- sum(vec.mat[,1] == 1)
|
26
|
493 # Second generation.
|
0
|
494 gen2 <- sum(vec.mat[,1] == 2)
|
26
|
495 # Sum of all adults.
|
0
|
496 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5)
|
26
|
497 # Gen eration 0 pop size.
|
0
|
498 gen0.pop[day] <- gen0
|
|
499 gen1.pop[day] <- gen1
|
|
500 gen2.pop[day] <- gen2
|
|
501 S0[day] <- s0
|
|
502 S1[day] <- s1
|
|
503 S2[day] <- s2
|
|
504 S3[day] <- s3
|
|
505 S4[day] <- s4
|
|
506 S5[day] <- s5
|
|
507 g0.adult[day] <- sum(vec.mat[,1] == 0)
|
|
508 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))
|
|
509 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))
|
|
510
|
|
511 N.newborn[day] <- n.newborn
|
|
512 N.death[day] <- n.death
|
|
513 N.adult[day] <- n.adult
|
13
|
514 } # end of days specified in the input temperature data
|
0
|
515
|
|
516 dd.cum <- cumsum(dd.day)
|
26
|
517 # Collect all the outputs.
|
0
|
518 S0.rep[,N.rep] <- S0
|
|
519 S1.rep[,N.rep] <- S1
|
|
520 S2.rep[,N.rep] <- S2
|
|
521 S3.rep[,N.rep] <- S3
|
|
522 S4.rep[,N.rep] <- S4
|
|
523 S5.rep[,N.rep] <- S5
|
|
524 newborn.rep[,N.rep] <- N.newborn
|
|
525 death.rep[,N.rep] <- N.death
|
|
526 adult.rep[,N.rep] <- N.adult
|
|
527 pop.rep[,N.rep] <- tot.pop
|
|
528 g0.rep[,N.rep] <- gen0.pop
|
|
529 g1.rep[,N.rep] <- gen1.pop
|
|
530 g2.rep[,N.rep] <- gen2.pop
|
|
531 g0a.rep[,N.rep] <- g0.adult
|
|
532 g1a.rep[,N.rep] <- g1.adult
|
|
533 g2a.rep[,N.rep] <- g2.adult
|
|
534 }
|
|
535
|
|
536 # Data analysis and visualization
|
|
537 # default: plot 1 year of result
|
|
538 # but can be expanded to accommodate multiple years
|
|
539 n.yr <- 1
|
13
|
540 day.all <- c(1:opt$num_days * n.yr)
|
0
|
541
|
|
542 # mean value for adults
|
|
543 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean)
|
|
544 # mean value for nymphs
|
|
545 sn <- apply((S1.rep + S2.rep), 1,mean)
|
|
546 # mean value for eggs
|
|
547 se <- apply(S0.rep, 1, mean)
|
|
548 # mean value for P
|
|
549 g0 <- apply(g0.rep, 1, mean)
|
|
550 # mean value for F1
|
|
551 g1 <- apply(g1.rep, 1, mean)
|
|
552 # mean value for F2
|
|
553 g2 <- apply(g2.rep, 1, mean)
|
|
554 # mean value for P adult
|
|
555 g0a <- apply(g0a.rep, 1, mean)
|
|
556 # mean value for F1 adult
|
|
557 g1a <- apply(g1a.rep, 1, mean)
|
|
558 # mean value for F2 adult
|
|
559 g2a <- apply(g2a.rep, 1, mean)
|
|
560
|
|
561 # SE for adults
|
|
562 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications)
|
|
563 # SE for nymphs
|
|
564 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd)
|
|
565 # SE for eggs
|
|
566 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications)
|
|
567 # SE value for P
|
|
568 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications)
|
|
569 # SE for F1
|
|
570 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications)
|
|
571 # SE for F2
|
|
572 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications)
|
|
573 # SE for P adult
|
|
574 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications)
|
|
575 # SE for F1 adult
|
|
576 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications)
|
|
577 # SE for F2 adult
|
|
578 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications)
|
|
579
|
|
580 dev.new(width=20, height=20)
|
30
|
581 sink("/dev/null")
|
0
|
582
|
|
583 # Start PDF device driver to save charts to output.
|
|
584 pdf(file=opt$output, height=20, width=20, bg="white")
|
|
585
|
|
586 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1))
|
|
587
|
|
588 # Subfigure 2: population size by life stage
|
4
|
589 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
|
590 # Young and old nymphs
|
|
591 lines(day.all, sn, lwd = 2, lty = 1, col = 2)
|
|
592 # Eggs
|
|
593 lines(day.all, se, lwd = 2, lty = 1, col = 4)
|
|
594 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"))
|
|
595 axis(2, cex.axis = 2)
|
|
596 leg.text <- c("Egg", "Nymph", "Adult")
|
|
597 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(4, 2, 1), cex = 2)
|
|
598 if (opt$se_plot == 1) {
|
|
599 # add SE lines to plot
|
|
600 # SE for adults
|
|
601 lines (day.all, sa + sa.se, lty = 2)
|
|
602 lines (day.all, sa - sa.se, lty = 2)
|
|
603 # SE for nymphs
|
|
604 lines (day.all, sn + sn.se, col = 2, lty = 2)
|
|
605 lines (day.all, sn - sn.se, col = 2, lty = 2)
|
|
606 # SE for eggs
|
|
607 lines (day.all, se + se.se, col = 4, lty = 2)
|
|
608 lines (day.all, se - se.se, col = 4, lty = 2)
|
|
609 }
|
|
610
|
|
611 # Subfigure 3: population size by generation
|
4
|
612 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
|
613 lines(day.all, g1, lwd = 2, lty = 1, col = 2)
|
|
614 lines(day.all, g2, lwd = 2, lty = 1, col = 4)
|
|
615 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"))
|
|
616 axis(2, cex.axis = 2)
|
|
617 leg.text <- c("P", "F1", "F2")
|
|
618 legend("topleft", leg.text, lty = c(1, 1, 1), col =c(1, 2, 4), cex = 2)
|
|
619 if (opt$se_plot == 1) {
|
|
620 # add SE lines to plot
|
|
621 # SE for adults
|
|
622 lines (day.all, g0 + g0.se, lty = 2)
|
|
623 lines (day.all, g0 - g0.se, lty = 2)
|
|
624 # SE for nymphs
|
|
625 lines (day.all, g1 + g1.se, col = 2, lty = 2)
|
|
626 lines (day.all, g1 - g1.se, col = 2, lty = 2)
|
|
627 # SE for eggs
|
|
628 lines (day.all, g2 + g2.se, col = 4, lty = 2)
|
|
629 lines (day.all, g2 - g2.se, col = 4, lty = 2)
|
|
630 }
|
|
631
|
|
632 # Subfigure 4: adult population size by generation
|
4
|
633 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
|
634 lines(day.all, g1a, lwd = 2, lty = 1, col = 2)
|
|
635 lines(day.all, g2a, lwd = 2, lty = 1, col = 4)
|
|
636 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"))
|
|
637 axis(2, cex.axis = 2)
|
|
638 leg.text <- c("P", "F1", "F2")
|
|
639 legend("topleft", leg.text, lty = c(1, 1, 1), col = c(1, 2, 4), cex = 2)
|
|
640 if (opt$se_plot == 1) {
|
|
641 # add SE lines to plot
|
|
642 # SE for adults
|
|
643 lines (day.all, g0a + g0a.se, lty = 2)
|
|
644 lines (day.all, g0a - g0a.se, lty = 2)
|
|
645 # SE for nymphs
|
|
646 lines (day.all, g1a + g1a.se, col = 2, lty = 2)
|
|
647 lines (day.all, g1a - g1a.se, col = 2, lty = 2)
|
|
648 # SE for eggs
|
|
649 lines (day.all, g2a + g2a.se, col = 4, lty = 2)
|
|
650 lines (day.all, g2a - g2a.se, col = 4, lty = 2)
|
|
651 }
|
|
652
|
30
|
653 sink()
|
0
|
654 # Turn off device driver to flush output.
|
|
655 dev.off()
|