Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 90:7433448e313d draft
Uploaded
author | greg |
---|---|
date | Fri, 01 Dec 2017 09:20:56 -0500 |
parents | b387ea636ff8 |
children | b6b42e12e173 |
comparison
equal
deleted
inserted
replaced
89:1615e60cf61c | 90:7433448e313d |
---|---|
2 | 2 |
3 suppressPackageStartupMessages(library("optparse")) | 3 suppressPackageStartupMessages(library("optparse")) |
4 | 4 |
5 option_list <- list( | 5 option_list <- list( |
6 make_option(c("-a", "--adult_mort"), action="store", dest="adult_mort", type="integer", help="Adjustment rate for adult mortality"), | 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)"), | 7 make_option(c("-b", "--adult_accum"), action="store", dest="adult_accum", type="integer", help="Adjustment of degree-days accumulation (old nymph->adult)"), |
8 make_option(c("-c", "--egg_mort"), action="store", dest="egg_mort", type="integer", help="Adjustment rate for egg mortality"), | 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"), | 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"), | 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"), | 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"), | 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 make_option(c("-k", "--old_nymph_accum"), action="store", dest="old_nymph_accum", type="integer", help="Adjustment of degree-days accumulation (young nymph->old nymph)"), |
14 make_option(c("-n", "--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"), | 14 make_option(c("-n", "--num_days"), action="store", dest="num_days", type="integer", help="Total number of days in the temperature dataset"), |
15 make_option(c("-o", "--output"), action="store", dest="output", help="Output dataset"), | 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"), | 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"), | 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"), | 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"), | 19 make_option(c("-t", "--std_error_plot"), action="store", dest="std_error_plot", help="Plot Standard error"), |
20 make_option(c("-v", "--input"), action="store", dest="input", help="Temperature data for selected location"), | 20 make_option(c("-v", "--input"), action="store", dest="input", help="Temperature data for selected location"), |
21 make_option(c("-y", "--young_nymph_accum"), action="store", dest="young_nymph_accum", type="integer", help="Adjustment of DD accumulation (egg->young nymph)"), | 21 make_option(c("-y", "--young_nymph_accum"), action="store", dest="young_nymph_accum", type="integer", help="Adjustment of degree-days accumulation (egg->young nymph)"), |
22 make_option(c("-x", "--insect"), action="store", dest="insect", help="Insect name") | 22 make_option(c("-x", "--insect"), action="store", dest="insect", help="Insect name") |
23 ) | 23 ) |
24 | 24 |
25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) | 25 parser <- OptionParser(usage="%prog [options] file", option_list=option_list) |
26 args <- parse_args(parser, positional_arguments=TRUE) | 26 args <- parse_args(parser, positional_arguments=TRUE) |
75 # Maximum temperature for current row. | 75 # Maximum temperature for current row. |
76 dxp <- temperature_data_frame$TMAX[row] | 76 dxp <- temperature_data_frame$TMAX[row] |
77 # Mean temperature for current row. | 77 # Mean temperature for current row. |
78 dmean <- 0.5 * (dnp + dxp) | 78 dmean <- 0.5 * (dnp + dxp) |
79 # Initialize degree day accumulation | 79 # Initialize degree day accumulation |
80 dd <- 0 | 80 degree_days <- 0 |
81 if (dxp < threshold) { | 81 if (dxp < threshold) { |
82 dd <- 0 | 82 degree_days <- 0 |
83 } | 83 } |
84 else { | 84 else { |
85 # Initialize hourly temperature. | 85 # Initialize hourly temperature. |
86 T <- NULL | 86 T <- NULL |
87 # Initialize degree hour vector. | 87 # Initialize degree hour vector. |
130 else { | 130 else { |
131 dh[i] <- T[i] - 8.4 | 131 dh[i] <- T[i] - 8.4 |
132 } | 132 } |
133 } | 133 } |
134 } | 134 } |
135 dd <- sum(dh) / 24 | 135 degree_days <- sum(dh) / 24 |
136 } | 136 } |
137 return(c(dmean, dd)) | 137 return(c(dmean, degree_days)) |
138 } | 138 } |
139 | 139 |
140 dev.egg = function(temperature) { | 140 dev.egg = function(temperature) { |
141 dev.rate= -0.9843 * temperature + 33.438 | 141 dev.rate= -0.9843 * temperature + 33.438 |
142 return(dev.rate) | 142 return(dev.rate) |
207 | 207 |
208 # Loop through replications. | 208 # Loop through replications. |
209 for (N.rep in 1:opt$replications) { | 209 for (N.rep in 1:opt$replications) { |
210 # During each replication start with 1000 individuals. | 210 # During each replication start with 1000 individuals. |
211 # TODO: user definable as well? | 211 # TODO: user definable as well? |
212 n <- 1000 | 212 num_insects <- 1000 |
213 # Generation, Stage, DD, T, Diapause. | 213 # Generation, Stage, degree-days, T, Diapause. |
214 vec.ini <- c(0, 3, 0, 0, 0) | 214 vec.ini <- c(0, 3, 0, 0, 0) |
215 # Overwintering, previttelogenic, DD=0, T=0, no-diapause. | 215 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause. |
216 vec.mat <- rep(vec.ini, n) | 216 vec.mat <- rep(vec.ini, n) |
217 # Complete matrix for the population. | 217 # Complete matrix for the population. |
218 vec.mat <- base::t(matrix(vec.mat, nrow=5)) | 218 vec.mat <- base::t(matrix(vec.mat, nrow=5)) |
219 # Time series of population size. | 219 # Time series of population size. |
220 tot.pop <- NULL | 220 tot.pop <- NULL |
222 gen1.pop <- rep(0, opt$num_days) | 222 gen1.pop <- rep(0, opt$num_days) |
223 gen2.pop <- rep(0, opt$num_days) | 223 gen2.pop <- rep(0, opt$num_days) |
224 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days) | 224 S0 <- S1 <- S2 <- S3 <- S4 <- S5 <- rep(0, opt$num_days) |
225 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days) | 225 g0.adult <- g1.adult <- g2.adult <- rep(0, opt$num_days) |
226 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days) | 226 N.newborn <- N.death <- N.adult <- rep(0, opt$num_days) |
227 dd.day <- rep(0, opt$num_days) | 227 degree_days.day <- rep(0, opt$num_days) |
228 # All the days included in the input temperature dataset. | 228 # All the days included in the input temperature dataset. |
229 for (row in 1:opt$num_days) { | 229 for (row in 1:opt$num_days) { |
230 # Get the integer day of the year for the current row. | 230 # Get the integer day of the year for the current row. |
231 doy <- temperature_data_frame$DOY[row] | 231 doy <- temperature_data_frame$DOY[row] |
232 # Photoperiod in the day. | 232 # Photoperiod in the day. |
233 photoperiod <- temperature_data_frame$DAYLEN[row] | 233 photoperiod <- temperature_data_frame$DAYLEN[row] |
234 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days) | 234 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days) |
235 mean.temp <- temp.profile[1] | 235 mean.temp <- temp.profile[1] |
236 dd.temp <- temp.profile[2] | 236 degree_days.temp <- temp.profile[2] |
237 dd.day[row] <- dd.temp | 237 degree_days.day[row] <- degree_days.temp |
238 # Trash bin for death. | 238 # Trash bin for death. |
239 death.vec <- NULL | 239 death.vec <- NULL |
240 # Newborn. | 240 # Newborn. |
241 birth.vec <- NULL | 241 birth.vec <- NULL |
242 # All individuals. | 242 # All individuals. |
285 # Transfer to vittelogenic. | 285 # Transfer to vittelogenic. |
286 vec.ind <- c(0, 4, 0, 0, 0) | 286 vec.ind <- c(0, 4, 0, 0, 0) |
287 vec.mat[i,] <- vec.ind | 287 vec.mat[i,] <- vec.ind |
288 } | 288 } |
289 else { | 289 else { |
290 # Add to dd. | 290 # Add to degree_days. |
291 vec.ind[3] <- vec.ind[3] + dd.temp | 291 vec.ind[3] <- vec.ind[3] + degree_days.temp |
292 # Add 1 day in current stage. | 292 # Add 1 day in current stage. |
293 vec.ind[4] <- vec.ind[4] + 1 | 293 vec.ind[4] <- vec.ind[4] + 1 |
294 vec.mat[i,] <- vec.ind | 294 vec.mat[i,] <- vec.ind |
295 } | 295 } |
296 } | 296 } |
302 # Transfer to vittelogenic. | 302 # Transfer to vittelogenic. |
303 vec.ind <- c(current.gen, 4, 0, 0, 0) | 303 vec.ind <- c(current.gen, 4, 0, 0, 0) |
304 vec.mat[i,] <- vec.ind | 304 vec.mat[i,] <- vec.ind |
305 } | 305 } |
306 else { | 306 else { |
307 # Add to dd. | 307 # Add to degree_days. |
308 vec.ind[3] <- vec.ind[3] + dd.temp | 308 vec.ind[3] <- vec.ind[3] + degree_days.temp |
309 # Add 1 day in current stage. | 309 # Add 1 day in current stage. |
310 vec.ind[4] <- vec.ind[4] + 1 | 310 vec.ind[4] <- vec.ind[4] + 1 |
311 vec.mat[i,] <- vec.ind | 311 vec.mat[i,] <- vec.ind |
312 } | 312 } |
313 } | 313 } |
314 # Event 2 oviposition -- where population dynamics comes from. | 314 # Event 2 oviposition -- where population dynamics comes from. |
315 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) { | 315 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) { |
316 # Vittelogenic stage, overwintering generation. | 316 # Vittelogenic stage, overwintering generation. |
317 if (vec.ind[4] == 0) { | 317 if (vec.ind[4] == 0) { |
318 # Just turned in vittelogenic stage. | 318 # Just turned in vittelogenic stage. |
319 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) | 319 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) |
320 } | 320 } |
321 else { | 321 else { |
322 # Daily probability of birth. | 322 # Daily probability of birth. |
323 p.birth = opt$oviposition * 0.01 | 323 p.birth = opt$oviposition * 0.01 |
324 u1 <- runif(1) | 324 u1 <- runif(1) |
325 if (u1 < p.birth) { | 325 if (u1 < p.birth) { |
326 n.birth=round(runif(1, 2, 8)) | 326 num_insects.birth = round(runif(1, 2, 8)) |
327 } | 327 } |
328 } | 328 } |
329 # Add to dd. | 329 # Add to degree_days. |
330 vec.ind[3] <- vec.ind[3] + dd.temp | 330 vec.ind[3] <- vec.ind[3] + degree_days.temp |
331 # Add 1 day in current stage. | 331 # Add 1 day in current stage. |
332 vec.ind[4] <- vec.ind[4] + 1 | 332 vec.ind[4] <- vec.ind[4] + 1 |
333 vec.mat[i,] <- vec.ind | 333 vec.mat[i,] <- vec.ind |
334 if (n.birth > 0) { | 334 if (num_insects.birth > 0) { |
335 # Add new birth -- might be in different generations. | 335 # Add new birth -- might be in different generations. |
336 new.gen <- vec.ind[1] + 1 | 336 new.gen <- vec.ind[1] + 1 |
337 # Egg profile. | 337 # Egg profile. |
338 new.ind <- c(new.gen, 0, 0, 0, 0) | 338 new.ind <- c(new.gen, 0, 0, 0, 0) |
339 new.vec <- rep(new.ind, n.birth) | 339 new.vec <- rep(new.ind, num_insects.birth) |
340 # Update batch of egg profile. | 340 # Update batch of egg profile. |
341 new.vec <- t(matrix(new.vec, nrow=5)) | 341 new.vec <- t(matrix(new.vec, nrow=5)) |
342 # Group with total eggs laid in that day. | 342 # Group with total eggs laid in that day. |
343 birth.vec <- rbind(birth.vec, new.vec) | 343 birth.vec <- rbind(birth.vec, new.vec) |
344 } | 344 } |
346 # Event 2 oviposition -- for generation 1. | 346 # Event 2 oviposition -- for generation 1. |
347 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && doy < 222) { | 347 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && doy < 222) { |
348 # Vittelogenic stage, 1st generation | 348 # Vittelogenic stage, 1st generation |
349 if (vec.ind[4] == 0) { | 349 if (vec.ind[4] == 0) { |
350 # Just turned in vittelogenic stage. | 350 # Just turned in vittelogenic stage. |
351 n.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) | 351 num_insects.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) |
352 } | 352 } |
353 else { | 353 else { |
354 # Daily probability of birth. | 354 # Daily probability of birth. |
355 p.birth = opt$oviposition * 0.01 | 355 p.birth = opt$oviposition * 0.01 |
356 u1 <- runif(1) | 356 u1 <- runif(1) |
357 if (u1 < p.birth) { | 357 if (u1 < p.birth) { |
358 n.birth = round(runif(1, 2, 8)) | 358 num_insects.birth = round(runif(1, 2, 8)) |
359 } | 359 } |
360 } | 360 } |
361 # Add to dd. | 361 # Add to degree_days. |
362 vec.ind[3] <- vec.ind[3] + dd.temp | 362 vec.ind[3] <- vec.ind[3] + degree_days.temp |
363 # Add 1 day in current stage. | 363 # Add 1 day in current stage. |
364 vec.ind[4] <- vec.ind[4] + 1 | 364 vec.ind[4] <- vec.ind[4] + 1 |
365 vec.mat[i,] <- vec.ind | 365 vec.mat[i,] <- vec.ind |
366 if (n.birth > 0) { | 366 if (num_insects.birth > 0) { |
367 # Add new birth -- might be in different generations. | 367 # Add new birth -- might be in different generations. |
368 new.gen <- vec.ind[1] + 1 | 368 new.gen <- vec.ind[1] + 1 |
369 # Egg profile. | 369 # Egg profile. |
370 new.ind <- c(new.gen, 0, 0, 0, 0) | 370 new.ind <- c(new.gen, 0, 0, 0, 0) |
371 new.vec <- rep(new.ind, n.birth) | 371 new.vec <- rep(new.ind, num_insects.birth) |
372 # Update batch of egg profile. | 372 # Update batch of egg profile. |
373 new.vec <- t(matrix(new.vec, nrow=5)) | 373 new.vec <- t(matrix(new.vec, nrow=5)) |
374 # Group with total eggs laid in that day. | 374 # Group with total eggs laid in that day. |
375 birth.vec <- rbind(birth.vec, new.vec) | 375 birth.vec <- rbind(birth.vec, new.vec) |
376 } | 376 } |
377 } | 377 } |
378 # Event 3 development (with diapause determination). | 378 # Event 3 development (with diapause determination). |
379 # Event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg). | 379 # Event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg). |
380 if (vec.ind[2] == 0) { | 380 if (vec.ind[2] == 0) { |
381 # Egg stage. | 381 # Egg stage. |
382 # Add to dd. | 382 # Add to degree_days. |
383 vec.ind[3] <- vec.ind[3] + dd.temp | 383 vec.ind[3] <- vec.ind[3] + degree_days.temp |
384 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) { | 384 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) { |
385 # From egg to young nymph, DD requirement met. | 385 # From egg to young nymph, degree-days requirement met. |
386 current.gen <- vec.ind[1] | 386 current.gen <- vec.ind[1] |
387 # Transfer to young nymph stage. | 387 # Transfer to young nymph stage. |
388 vec.ind <- c(current.gen, 1, 0, 0, 0) | 388 vec.ind <- c(current.gen, 1, 0, 0, 0) |
389 } | 389 } |
390 else { | 390 else { |
393 } | 393 } |
394 vec.mat[i,] <- vec.ind | 394 vec.mat[i,] <- vec.ind |
395 } | 395 } |
396 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause). | 396 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause). |
397 if (vec.ind[2] == 1) { | 397 if (vec.ind[2] == 1) { |
398 # young nymph stage. | 398 # Young nymph stage. |
399 # add to dd. | 399 # Add to degree_days. |
400 vec.ind[3] <- vec.ind[3] + dd.temp | 400 vec.ind[3] <- vec.ind[3] + degree_days.temp |
401 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) { | 401 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) { |
402 # From young to old nymph, dd requirement met. | 402 # From young to old nymph, degree_days requirement met. |
403 current.gen <- vec.ind[1] | 403 current.gen <- vec.ind[1] |
404 # Transfer to old nym stage. | 404 # Transfer to old nym stage. |
405 vec.ind <- c(current.gen, 2, 0, 0, 0) | 405 vec.ind <- c(current.gen, 2, 0, 0, 0) |
406 if (photoperiod < opt$photoperiod && doy > 180) { | 406 if (photoperiod < opt$photoperiod && doy > 180) { |
407 vec.ind[5] <- 1 | 407 vec.ind[5] <- 1 |
414 vec.mat[i,] <- vec.ind | 414 vec.mat[i,] <- vec.ind |
415 } | 415 } |
416 # Event 3.3 old nymph to adult: previttelogenic or diapausing? | 416 # Event 3.3 old nymph to adult: previttelogenic or diapausing? |
417 if (vec.ind[2] == 2) { | 417 if (vec.ind[2] == 2) { |
418 # Old nymph stage. | 418 # Old nymph stage. |
419 # add to dd. | 419 # Add to degree_days. |
420 vec.ind[3] <- vec.ind[3] + dd.temp | 420 vec.ind[3] <- vec.ind[3] + degree_days.temp |
421 if (vec.ind[3] >= (200 + opt$adult_accum)) { | 421 if (vec.ind[3] >= (200 + opt$adult_accum)) { |
422 # From old to adult, dd requirement met. | 422 # From old to adult, degree_days requirement met. |
423 current.gen <- vec.ind[1] | 423 current.gen <- vec.ind[1] |
424 if (vec.ind[5] == 0) { | 424 if (vec.ind[5] == 0) { |
425 # Non-diapausing adult -- previttelogenic. | 425 # Non-diapausing adult -- previttelogenic. |
426 vec.ind <- c(current.gen, 3, 0, 0, 0) | 426 vec.ind <- c(current.gen, 3, 0, 0, 0) |
427 } | 427 } |
436 } | 436 } |
437 vec.mat[i,] <- vec.ind | 437 vec.mat[i,] <- vec.ind |
438 } | 438 } |
439 # Event 4 growing of diapausing adult (unimportant, but still necessary). | 439 # Event 4 growing of diapausing adult (unimportant, but still necessary). |
440 if (vec.ind[2] == 5) { | 440 if (vec.ind[2] == 5) { |
441 vec.ind[3] <- vec.ind[3] + dd.temp | 441 vec.ind[3] <- vec.ind[3] + degree_days.temp |
442 vec.ind[4] <- vec.ind[4] + 1 | 442 vec.ind[4] <- vec.ind[4] + 1 |
443 vec.mat[i,] <- vec.ind | 443 vec.mat[i,] <- vec.ind |
444 } | 444 } |
445 } # Else if it is still alive. | 445 } # Else if it is still alive. |
446 } # End of the individual bug loop. | 446 } # End of the individual bug loop. |
447 # Find how many died. | 447 # Find how many died. |
448 n.death <- length(death.vec) | 448 num_insects.death <- length(death.vec) |
449 if (n.death > 0) { | 449 if (num_insects.death > 0) { |
450 vec.mat <- vec.mat[-death.vec, ] | 450 vec.mat <- vec.mat[-death.vec, ] |
451 } | 451 } |
452 # Remove record of dead. | 452 # Remove record of dead. |
453 # Find how many new born. | 453 # Find how many new born. |
454 n.newborn <- length(birth.vec[,1]) | 454 num_insects.newborn <- length(birth.vec[,1]) |
455 vec.mat <- rbind(vec.mat, birth.vec) | 455 vec.mat <- rbind(vec.mat, birth.vec) |
456 # Update population size for the next day. | 456 # Update population size for the next day. |
457 n <- n - n.death + n.newborn | 457 n <- n - num_insects.death + num_insects.newborn |
458 | 458 |
459 # Aggregate results by day. | 459 # Aggregate results by day. |
460 tot.pop <- c(tot.pop, n) | 460 tot.pop <- c(tot.pop, n) |
461 # Egg. | 461 # Egg. |
462 s0 <- sum(vec.mat[,2] == 0) | 462 s0 <- sum(vec.mat[,2] == 0) |
475 # First generation. | 475 # First generation. |
476 gen1 <- sum(vec.mat[,1] == 1) | 476 gen1 <- sum(vec.mat[,1] == 1) |
477 # Second generation. | 477 # Second generation. |
478 gen2 <- sum(vec.mat[,1] == 2) | 478 gen2 <- sum(vec.mat[,1] == 2) |
479 # Sum of all adults. | 479 # Sum of all adults. |
480 n.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) | 480 num_insects.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) |
481 | 481 |
482 # Generation 0 pop size. | 482 # Generation 0 pop size. |
483 gen0.pop[row] <- gen0 | 483 gen0.pop[row] <- gen0 |
484 gen1.pop[row] <- gen1 | 484 gen1.pop[row] <- gen1 |
485 gen2.pop[row] <- gen2 | 485 gen2.pop[row] <- gen2 |
493 | 493 |
494 g0.adult[row] <- sum(vec.mat[,1] == 0) | 494 g0.adult[row] <- sum(vec.mat[,1] == 0) |
495 g1.adult[row] <- 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)) | 495 g1.adult[row] <- 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)) |
496 g2.adult[row] <- 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)) | 496 g2.adult[row] <- 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)) |
497 | 497 |
498 N.newborn[row] <- n.newborn | 498 N.newborn[row] <- num_insects.newborn |
499 N.death[row] <- n.death | 499 N.death[row] <- num_insects.death |
500 N.adult[row] <- n.adult | 500 N.adult[row] <- num_insects.adult |
501 } # end of days specified in the input temperature data | 501 } # end of days specified in the input temperature data |
502 | 502 |
503 dd.cum <- cumsum(dd.day) | 503 degree_days.cum <- cumsum(degree_days.day) |
504 | 504 |
505 # Collect all the outputs. | 505 # Collect all the outputs. |
506 S0.rep[,N.rep] <- S0 | 506 S0.rep[,N.rep] <- S0 |
507 S1.rep[,N.rep] <- S1 | 507 S1.rep[,N.rep] <- S1 |
508 S2.rep[,N.rep] <- S2 | 508 S2.rep[,N.rep] <- S2 |
519 g0a.rep[,N.rep] <- g0.adult | 519 g0a.rep[,N.rep] <- g0.adult |
520 g1a.rep[,N.rep] <- g1.adult | 520 g1a.rep[,N.rep] <- g1.adult |
521 g2a.rep[,N.rep] <- g2.adult | 521 g2a.rep[,N.rep] <- g2.adult |
522 } | 522 } |
523 | 523 |
524 # Data analysis and visualization can currently | 524 # Mean value for adults |
525 # plot only within a single calendar year. | 525 mean_value_adult <- apply((S3.rep + S4.rep + S5.rep), 1, mean) |
526 # TODO: enhance this to accomodate multiple calendar years. | 526 # Mean value for nymphs |
527 mean_value_nymph <- apply((S1.rep + S2.rep), 1, mean) | |
528 # Mean value for eggs | |
529 mean_value_egg <- apply(S0.rep, 1, mean) | |
530 # Mean value for P | |
531 g0 <- apply(g0.rep, 1, mean) | |
532 # Mean value for F1 | |
533 g1 <- apply(g1.rep, 1, mean) | |
534 # Mean value for F2 | |
535 g2 <- apply(g2.rep, 1, mean) | |
536 # Mean value for P adult | |
537 g0a <- apply(g0a.rep, 1, mean) | |
538 # Mean value for F1 adult | |
539 g1a <- apply(g1a.rep, 1, mean) | |
540 # Mean value for F2 adult | |
541 g2a <- apply(g2a.rep, 1, mean) | |
542 | |
543 # Standard error for adults | |
544 mean_value_adult.std_error <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications) | |
545 # Standard error for nymphs | |
546 mean_value_nymph.std_error <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd) | |
547 # Standard error for eggs | |
548 mean_value_egg.std_error <- apply(S0.rep, 1, sd) / sqrt(opt$replications) | |
549 # Standard error value for P | |
550 g0.std_error <- apply(g0.rep, 1, sd) / sqrt(opt$replications) | |
551 # Standard error for F1 | |
552 g1.std_error <- apply(g1.rep, 1, sd) / sqrt(opt$replications) | |
553 # Standard error for F2 | |
554 g2.std_error <- apply(g2.rep, 1, sd) / sqrt(opt$replications) | |
555 # Standard error for P adult | |
556 g0a.std_error <- apply(g0a.rep, 1, sd) / sqrt(opt$replications) | |
557 # Standard error for F1 adult | |
558 g1a.std_error <- apply(g1a.rep, 1, sd) / sqrt(opt$replications) | |
559 # Standard error for F2 adult | |
560 g2a.std_error <- apply(g2a.rep, 1, sd) / sqrt(opt$replications) | |
561 | |
562 dev.new(width=20, height=30) | |
563 | |
564 # Start PDF device driver to save charts to output. | |
565 pdf(file=opt$output, width=20, height=30, bg="white") | |
566 | |
567 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)) | |
568 | |
569 # Data analysis and visualization plots | |
570 # only within a single calendar year. | |
571 day.all <- c(1:num_days) | |
527 start_date <- temperature_data_frame$DATE[1] | 572 start_date <- temperature_data_frame$DATE[1] |
528 end_date <- temperature_data_frame$DATE[opt$num_days] | 573 end_date <- temperature_data_frame$DATE[opt$num_days] |
529 | 574 |
530 n.yr <- 1 | |
531 day.all <- c(1:opt$num_days * n.yr) | |
532 | |
533 # mean value for adults | |
534 sa <- apply((S3.rep + S4.rep + S5.rep), 1, mean) | |
535 # mean value for nymphs | |
536 sn <- apply((S1.rep + S2.rep), 1,mean) | |
537 # mean value for eggs | |
538 se <- apply(S0.rep, 1, mean) | |
539 # mean value for P | |
540 g0 <- apply(g0.rep, 1, mean) | |
541 # mean value for F1 | |
542 g1 <- apply(g1.rep, 1, mean) | |
543 # mean value for F2 | |
544 g2 <- apply(g2.rep, 1, mean) | |
545 # mean value for P adult | |
546 g0a <- apply(g0a.rep, 1, mean) | |
547 # mean value for F1 adult | |
548 g1a <- apply(g1a.rep, 1, mean) | |
549 # mean value for F2 adult | |
550 g2a <- apply(g2a.rep, 1, mean) | |
551 | |
552 # SE for adults | |
553 sa.se <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications) | |
554 # SE for nymphs | |
555 sn.se <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd) | |
556 # SE for eggs | |
557 se.se <- apply(S0.rep, 1, sd) / sqrt(opt$replications) | |
558 # SE value for P | |
559 g0.se <- apply(g0.rep, 1, sd) / sqrt(opt$replications) | |
560 # SE for F1 | |
561 g1.se <- apply(g1.rep, 1, sd) / sqrt(opt$replications) | |
562 # SE for F2 | |
563 g2.se <- apply(g2.rep, 1, sd) / sqrt(opt$replications) | |
564 # SE for P adult | |
565 g0a.se <- apply(g0a.rep, 1, sd) / sqrt(opt$replications) | |
566 # SE for F1 adult | |
567 g1a.se <- apply(g1a.rep, 1, sd) / sqrt(opt$replications) | |
568 # SE for F2 adult | |
569 g2a.se <- apply(g2a.rep, 1, sd) / sqrt(opt$replications) | |
570 | |
571 dev.new(width=20, height=30) | |
572 | |
573 # Start PDF device driver to save charts to output. | |
574 pdf(file=opt$output, width=20, height=30, bg="white") | |
575 | |
576 par(mar = c(5, 6, 4, 4), mfrow=c(3, 1)) | |
577 | |
578 # Subfigure 1: population size by life stage | 575 # Subfigure 1: population size by life stage |
579 title <- paste(opt$insect, ": Total pop. by life stage :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") | 576 title <- paste(opt$insect, ": Total pop. by life stage :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") |
580 plot(day.all, sa, main=title, type="l", ylim=c(0, max(se + se.se, sn + sn.se, sa + sa.se)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) | 577 plot(day.all, mean_value_adult, main=title, type="l", ylim=c(0, max(mean_value_egg + mean_value_egg.se, mean_value_nymph + mean_value_nymph.se, mean_value_adult + mean_value_adult.se)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) |
581 # Young and old nymphs. | 578 # Young and old nymphs. |
582 lines(day.all, sn, lwd=2, lty=1, col=2) | 579 lines(day.all, mean_value_nymph, lwd=2, lty=1, col=2) |
583 # Eggs | 580 # Eggs |
584 lines(day.all, se, lwd=2, lty=1, col=4) | 581 lines(day.all, mean_value_egg, lwd=2, lty=1, col=4) |
585 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) | 582 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels=c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) |
586 axis(2, cex.axis=3) | 583 axis(2, cex.axis=3) |
587 leg.text <- c("Egg", "Nymph", "Adult") | 584 leg.text <- c("Egg", "Nymph", "Adult") |
588 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(4, 2, 1), cex=3) | 585 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(4, 2, 1), cex=3) |
589 if (opt$se_plot == 1) { | 586 if (opt$se_plot == 1) { |
590 # Add SE lines to plot | 587 # Add Standard error lines to plot |
591 # SE for adults | 588 # Standard error for adults |
592 lines (day.all, sa + sa.se, lty=2) | 589 lines (day.all, mean_value_adult+mean_value_adult.se, lty=2) |
593 lines (day.all, sa - sa.se, lty=2) | 590 lines (day.all, mean_value_adult-mean_value_adult.se, lty=2) |
594 # SE for nymphs | 591 # Standard error for nymphs |
595 lines (day.all, sn + sn.se, col=2, lty=2) | 592 lines (day.all, mean_value_nymph+mean_value_nymph.se, col=2, lty=2) |
596 lines (day.all, sn - sn.se, col=2, lty=2) | 593 lines (day.all, mean_value_nymph-mean_value_nymph.se, col=2, lty=2) |
597 # SE for eggs | 594 # Standard error for eggs |
598 lines (day.all, se + se.se, col=4, lty=2) | 595 lines (day.all, mean_value_egg+mean_value_egg.se, col=4, lty=2) |
599 lines (day.all, se - se.se, col=4, lty=2) | 596 lines (day.all, mean_value_egg-mean_value_egg.se, col=4, lty=2) |
600 } | 597 } |
601 | 598 |
602 # Subfigure 2: population size by generation | 599 # Subfigure 2: population size by generation |
603 title <- paste(opt$insect, ": Total pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") | 600 title <- paste(opt$insect, ": Total pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") |
604 plot(day.all, g0, main=title, type="l", ylim=c(0, max(g2)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) | 601 plot(day.all, g0, main=title, type="l", ylim=c(0, max(g2)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) |
607 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) | 604 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) |
608 axis(2, cex.axis=3) | 605 axis(2, cex.axis=3) |
609 leg.text <- c("P", "F1", "F2") | 606 leg.text <- c("P", "F1", "F2") |
610 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3) | 607 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3) |
611 if (opt$se_plot == 1) { | 608 if (opt$se_plot == 1) { |
612 # Add SE lines to plot | 609 # Add Standard error lines to plot |
613 # SE for adults | 610 # Standard error for adults |
614 lines (day.all, g0+g0.se, lty=2) | 611 lines (day.all, g0+g0.std_error, lty=2) |
615 lines (day.all, g0-g0.se, lty=2) | 612 lines (day.all, g0-g0.std_error, lty=2) |
616 # SE for nymphs | 613 # Standard error for nymphs |
617 lines (day.all, g1+g1.se, col=2, lty=2) | 614 lines (day.all, g1+g1.std_error, col=2, lty=2) |
618 lines (day.all, g1-g1.se, col=2, lty=2) | 615 lines (day.all, g1-g1.std_error, col=2, lty=2) |
619 # SE for eggs | 616 # Standard error for eggs |
620 lines (day.all, g2+g2.se, col=4, lty=2) | 617 lines (day.all, g2+g2.std_error, col=4, lty=2) |
621 lines (day.all, g2-g2.se, col=4, lty=2) | 618 lines (day.all, g2-g2.std_error, col=4, lty=2) |
622 } | 619 } |
623 | 620 |
624 # Subfigure 3: adult population size by generation | 621 # Subfigure 3: adult population size by generation |
625 title <- paste(opt$insect, ": Adult pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") | 622 title <- paste(opt$insect, ": Adult pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") |
626 plot(day.all, g0a, ylim=c(0, max(g2a) + 100), main=title, type="l", axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) | 623 plot(day.all, g0a, ylim=c(0, max(g2a) + 100), main=title, type="l", axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) |
628 lines(day.all, g2a, lwd = 2, lty = 1, col=4) | 625 lines(day.all, g2a, lwd = 2, lty = 1, col=4) |
629 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) | 626 axis(1, at=c(1:12) * 30 - 15, cex.axis=3, labels = c("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec")) |
630 axis(2, cex.axis=3) | 627 axis(2, cex.axis=3) |
631 leg.text <- c("P", "F1", "F2") | 628 leg.text <- c("P", "F1", "F2") |
632 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3) | 629 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3) |
633 if (opt$se_plot == 1) { | 630 if (opt$std_error_plot == 1) { |
634 # Add SE lines to plot | 631 # Add Standard error lines to plot |
635 # SE for adults | 632 # Standard error for adults |
636 lines (day.all, g0a+g0a.se, lty=2) | 633 lines (day.all, g0a+g0a.std_error, lty=2) |
637 lines (day.all, g0a-g0a.se, lty=2) | 634 lines (day.all, g0a-g0a.std_error, lty=2) |
638 # SE for nymphs | 635 # Standard error for nymphs |
639 lines (day.all, g1a+g1a.se, col=2, lty=2) | 636 lines (day.all, g1a+g1a.std_error, col=2, lty=2) |
640 lines (day.all, g1a-g1a.se, col=2, lty=2) | 637 lines (day.all, g1a-g1a.std_error, col=2, lty=2) |
641 # SE for eggs | 638 # Standard error for eggs |
642 lines (day.all, g2a+g2a.se, col=4, lty=2) | 639 lines (day.all, g2a+g2a.std_error, col=4, lty=2) |
643 lines (day.all, g2a-g2a.se, col=4, lty=2) | 640 lines (day.all, g2a-g2a.std_error, col=4, lty=2) |
644 } | 641 } |
645 | 642 |
646 # Turn off device driver to flush output. | 643 # Turn off device driver to flush output. |
647 dev.off() | 644 dev.off() |