comparison insect_phenology_model.R @ 97:58bc1a2ca936 draft

Uploaded
author greg
date Mon, 04 Dec 2017 11:21:25 -0500
parents f0c65c9f1078
children d33a401c0153
comparison
equal deleted inserted replaced
96:f0c65c9f1078 97:58bc1a2ca936
199 # so get the value from the first row. 199 # so get the value from the first row.
200 latitude <- temperature_data_frame$LATITUDE[1] 200 latitude <- temperature_data_frame$LATITUDE[1]
201 201
202 cat("Number of days: ", opt$num_days, "\n") 202 cat("Number of days: ", opt$num_days, "\n")
203 203
204 # Initialize matrices for results from all replications. 204 # Initialize matrices.
205 S0.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 205 S0.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
206 S1.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 206 S1.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
207 S2.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 207 S2.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
208 S3.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 208 S3.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
209 S4.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 209 S4.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
210 S5.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 210 S5.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
211 newborn.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 211 newborn.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
212 death.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 212 death.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
213 adult.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 213 adult.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
214 pop.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 214 pop.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
215 g0.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 215 g0.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
216 g1.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 216 g1.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
217 g2.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 217 g2.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
218 g0a.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 218 g0a.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
219 g1a.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 219 g1a.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
220 g2a.rep <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications) 220 g2a.replications <- matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications)
221 221
222 # Loop through replications. 222 # Loop through replications.
223 for (N.rep in 1:opt$replications) { 223 for (N.replications in 1:opt$replications) {
224 # During each replication start with 1000 individuals. 224 # During each replication start with 1000 individuals.
225 # TODO: user definable as well? 225 # TODO: user definable as well?
226 num_insects <- 1000 226 num_insects <- 1000
227 # Generation, Stage, degree-days, T, Diapause. 227 # Generation, Stage, degree-days, T, Diapause.
228 vec.ini <- c(0, 3, 0, 0, 0) 228 vector.ini <- c(0, 3, 0, 0, 0)
229 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause. 229 # Overwintering, previttelogenic, degree-days=0, T=0, no-diapause.
230 vec.mat <- rep(vec.ini, num_insects) 230 vector.matrix <- rep(vector.ini, num_insects)
231 # Complete matrix for the population. 231 # Complete matrix for the population.
232 vec.mat <- base::t(matrix(vec.mat, nrow=5)) 232 vector.matrix <- base::t(matrix(vector.matrix, nrow=5))
233 # Time series of population size. 233 # Time series of population size.
234 tot.pop <- NULL 234 tot.pop <- NULL
235 gen0.pop <- rep(0, opt$num_days) 235 gen0.pop <- rep(0, opt$num_days)
236 gen1.pop <- rep(0, opt$num_days) 236 gen1.pop <- rep(0, opt$num_days)
237 gen2.pop <- rep(0, opt$num_days) 237 gen2.pop <- rep(0, opt$num_days)
248 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days) 248 temp.profile <- get_temperature_at_hour(latitude, temperature_data_frame, row, opt$num_days)
249 mean.temp <- temp.profile[1] 249 mean.temp <- temp.profile[1]
250 degree_days.temp <- temp.profile[2] 250 degree_days.temp <- temp.profile[2]
251 degree_days.day[row] <- degree_days.temp 251 degree_days.day[row] <- degree_days.temp
252 # Trash bin for death. 252 # Trash bin for death.
253 death.vec <- NULL 253 death.vector <- NULL
254 # Newborn. 254 # Newborn.
255 birth.vec <- NULL 255 birth.vector <- NULL
256 # All individuals. 256 # All individuals.
257 for (i in 1:num_insects) { 257 for (i in 1:num_insects) {
258 # Find individual record. 258 # Find individual record.
259 vec.ind <- vec.mat[i,] 259 vector.ind <- vector.matrix[i,]
260 # First of all, still alive? 260 # First of all, still alive?
261 # Adjustment for late season mortality rate. 261 # Adjustment for late season mortality rate.
262 if (latitude < 40.0) { 262 if (latitude < 40.0) {
263 post.mort <- 1 263 post.mort <- 1
264 day.kill <- 300 264 day.kill <- 300
265 } 265 }
266 else { 266 else {
267 post.mort <- 2 267 post.mort <- 2
268 day.kill <- 250 268 day.kill <- 250
269 } 269 }
270 if (vec.ind[2] == 0) { 270 if (vector.ind[2] == 0) {
271 # Egg. 271 # Egg.
272 death.prob = opt$egg_mort * mortality.egg(mean.temp) 272 death.prob = opt$egg_mort * mortality.egg(mean.temp)
273 } 273 }
274 else if (vec.ind[2] == 1 | vec.ind[2] == 2) { 274 else if (vector.ind[2] == 1 | vector.ind[2] == 2) {
275 death.prob = opt$nymph_mort * mortality.nymph(mean.temp) 275 death.prob = opt$nymph_mort * mortality.nymph(mean.temp)
276 } 276 }
277 else if (vec.ind[2] == 3 | vec.ind[2] == 4 | vec.ind[2] == 5) { 277 else if (vector.ind[2] == 3 | vector.ind[2] == 4 | vector.ind[2] == 5) {
278 # For adult. 278 # For adult.
279 if (doy < day.kill) { 279 if (doy < day.kill) {
280 death.prob = opt$adult_mort * mortality.adult(mean.temp) 280 death.prob = opt$adult_mort * mortality.adult(mean.temp)
281 } 281 }
282 else { 282 else {
285 } 285 }
286 } 286 }
287 # (or dependent on temperature and life stage?) 287 # (or dependent on temperature and life stage?)
288 u.d <- runif(1) 288 u.d <- runif(1)
289 if (u.d < death.prob) { 289 if (u.d < death.prob) {
290 death.vec <- c(death.vec, i) 290 death.vector <- c(death.vector, i)
291 } 291 }
292 else { 292 else {
293 # Aggregrate index of dead bug. 293 # Aggregrate index of dead bug.
294 # Event 1 end of diapause. 294 # Event 1 end of diapause.
295 if (vec.ind[1] == 0 && vec.ind[2] == 3) { 295 if (vector.ind[1] == 0 && vector.ind[2] == 3) {
296 # Overwintering adult (previttelogenic). 296 # Overwintering adult (previttelogenic).
297 if (photoperiod > opt$photoperiod && vec.ind[3] > 68 && doy < 180) { 297 if (photoperiod > opt$photoperiod && vector.ind[3] > 68 && doy < 180) {
298 # Add 68C to become fully reproductively matured. 298 # Add 68C to become fully reproductively matured.
299 # Transfer to vittelogenic. 299 # Transfer to vittelogenic.
300 vec.ind <- c(0, 4, 0, 0, 0) 300 vector.ind <- c(0, 4, 0, 0, 0)
301 vec.mat[i,] <- vec.ind 301 vector.matrix[i,] <- vector.ind
302 } 302 }
303 else { 303 else {
304 # Add to degree_days. 304 # Add to degree_days.
305 vec.ind[3] <- vec.ind[3] + degree_days.temp 305 vector.ind[3] <- vector.ind[3] + degree_days.temp
306 # Add 1 day in current stage. 306 # Add 1 day in current stage.
307 vec.ind[4] <- vec.ind[4] + 1 307 vector.ind[4] <- vector.ind[4] + 1
308 vec.mat[i,] <- vec.ind 308 vector.matrix[i,] <- vector.ind
309 } 309 }
310 } 310 }
311 if (vec.ind[1] != 0 && vec.ind[2] == 3) { 311 if (vector.ind[1] != 0 && vector.ind[2] == 3) {
312 # Not overwintering adult (previttelogenic). 312 # Not overwintering adult (previttelogenic).
313 current.gen <- vec.ind[1] 313 current.gen <- vector.ind[1]
314 if (vec.ind[3] > 68) { 314 if (vector.ind[3] > 68) {
315 # Add 68C to become fully reproductively matured. 315 # Add 68C to become fully reproductively matured.
316 # Transfer to vittelogenic. 316 # Transfer to vittelogenic.
317 vec.ind <- c(current.gen, 4, 0, 0, 0) 317 vector.ind <- c(current.gen, 4, 0, 0, 0)
318 vec.mat[i,] <- vec.ind 318 vector.matrix[i,] <- vector.ind
319 } 319 }
320 else { 320 else {
321 # Add to degree_days. 321 # Add to degree_days.
322 vec.ind[3] <- vec.ind[3] + degree_days.temp 322 vector.ind[3] <- vector.ind[3] + degree_days.temp
323 # Add 1 day in current stage. 323 # Add 1 day in current stage.
324 vec.ind[4] <- vec.ind[4] + 1 324 vector.ind[4] <- vector.ind[4] + 1
325 vec.mat[i,] <- vec.ind 325 vector.matrix[i,] <- vector.ind
326 } 326 }
327 } 327 }
328 # Event 2 oviposition -- where population dynamics comes from. 328 # Event 2 oviposition -- where population dynamics comes from.
329 if (vec.ind[2] == 4 && vec.ind[1] == 0 && mean.temp > 10) { 329 if (vector.ind[2] == 4 && vector.ind[1] == 0 && mean.temp > 10) {
330 # Vittelogenic stage, overwintering generation. 330 # Vittelogenic stage, overwintering generation.
331 if (vec.ind[4] == 0) { 331 if (vector.ind[4] == 0) {
332 # Just turned in vittelogenic stage. 332 # Just turned in vittelogenic stage.
333 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) 333 num_insects.birth = round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
334 } 334 }
335 else { 335 else {
336 # Daily probability of birth. 336 # Daily probability of birth.
339 if (u1 < p.birth) { 339 if (u1 < p.birth) {
340 num_insects.birth = round(runif(1, 2, 8)) 340 num_insects.birth = round(runif(1, 2, 8))
341 } 341 }
342 } 342 }
343 # Add to degree_days. 343 # Add to degree_days.
344 vec.ind[3] <- vec.ind[3] + degree_days.temp 344 vector.ind[3] <- vector.ind[3] + degree_days.temp
345 # Add 1 day in current stage. 345 # Add 1 day in current stage.
346 vec.ind[4] <- vec.ind[4] + 1 346 vector.ind[4] <- vector.ind[4] + 1
347 vec.mat[i,] <- vec.ind 347 vector.matrix[i,] <- vector.ind
348 if (num_insects.birth > 0) { 348 if (num_insects.birth > 0) {
349 # Add new birth -- might be in different generations. 349 # Add new birth -- might be in different generations.
350 new.gen <- vec.ind[1] + 1 350 new.gen <- vector.ind[1] + 1
351 # Egg profile. 351 # Egg profile.
352 new.ind <- c(new.gen, 0, 0, 0, 0) 352 new.ind <- c(new.gen, 0, 0, 0, 0)
353 new.vec <- rep(new.ind, num_insects.birth) 353 new.vector <- rep(new.ind, num_insects.birth)
354 # Update batch of egg profile. 354 # Update batch of egg profile.
355 new.vec <- t(matrix(new.vec, nrow=5)) 355 new.vector <- t(matrix(new.vector, nrow=5))
356 # Group with total eggs laid in that day. 356 # Group with total eggs laid in that day.
357 birth.vec <- rbind(birth.vec, new.vec) 357 birth.vector <- rbind(birth.vector, new.vector)
358 } 358 }
359 } 359 }
360 # Event 2 oviposition -- for generation 1. 360 # Event 2 oviposition -- for generation 1.
361 if (vec.ind[2] == 4 && vec.ind[1] == 1 && mean.temp > 12.5 && doy < 222) { 361 if (vector.ind[2] == 4 && vector.ind[1] == 1 && mean.temp > 12.5 && doy < 222) {
362 # Vittelogenic stage, 1st generation 362 # Vittelogenic stage, 1st generation
363 if (vec.ind[4] == 0) { 363 if (vector.ind[4] == 0) {
364 # Just turned in vittelogenic stage. 364 # Just turned in vittelogenic stage.
365 num_insects.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size)) 365 num_insects.birth=round(runif(1, 2 + opt$min_clutch_size, 8 + opt$max_clutch_size))
366 } 366 }
367 else { 367 else {
368 # Daily probability of birth. 368 # Daily probability of birth.
371 if (u1 < p.birth) { 371 if (u1 < p.birth) {
372 num_insects.birth = round(runif(1, 2, 8)) 372 num_insects.birth = round(runif(1, 2, 8))
373 } 373 }
374 } 374 }
375 # Add to degree_days. 375 # Add to degree_days.
376 vec.ind[3] <- vec.ind[3] + degree_days.temp 376 vector.ind[3] <- vector.ind[3] + degree_days.temp
377 # Add 1 day in current stage. 377 # Add 1 day in current stage.
378 vec.ind[4] <- vec.ind[4] + 1 378 vector.ind[4] <- vector.ind[4] + 1
379 vec.mat[i,] <- vec.ind 379 vector.matrix[i,] <- vector.ind
380 if (num_insects.birth > 0) { 380 if (num_insects.birth > 0) {
381 # Add new birth -- might be in different generations. 381 # Add new birth -- might be in different generations.
382 new.gen <- vec.ind[1] + 1 382 new.gen <- vector.ind[1] + 1
383 # Egg profile. 383 # Egg profile.
384 new.ind <- c(new.gen, 0, 0, 0, 0) 384 new.ind <- c(new.gen, 0, 0, 0, 0)
385 new.vec <- rep(new.ind, num_insects.birth) 385 new.vector <- rep(new.ind, num_insects.birth)
386 # Update batch of egg profile. 386 # Update batch of egg profile.
387 new.vec <- t(matrix(new.vec, nrow=5)) 387 new.vector <- t(matrix(new.vector, nrow=5))
388 # Group with total eggs laid in that day. 388 # Group with total eggs laid in that day.
389 birth.vec <- rbind(birth.vec, new.vec) 389 birth.vector <- rbind(birth.vector, new.vector)
390 } 390 }
391 } 391 }
392 # Event 3 development (with diapause determination). 392 # Event 3 development (with diapause determination).
393 # Event 3.1 egg development to young nymph (vec.ind[2]=0 -> egg). 393 # Event 3.1 egg development to young nymph (vector.ind[2]=0 -> egg).
394 if (vec.ind[2] == 0) { 394 if (vector.ind[2] == 0) {
395 # Egg stage. 395 # Egg stage.
396 # Add to degree_days. 396 # Add to degree_days.
397 vec.ind[3] <- vec.ind[3] + degree_days.temp 397 vector.ind[3] <- vector.ind[3] + degree_days.temp
398 if (vec.ind[3] >= (68 + opt$young_nymph_accum)) { 398 if (vector.ind[3] >= (68 + opt$young_nymph_accum)) {
399 # From egg to young nymph, degree-days requirement met. 399 # From egg to young nymph, degree-days requirement met.
400 current.gen <- vec.ind[1] 400 current.gen <- vector.ind[1]
401 # Transfer to young nymph stage. 401 # Transfer to young nymph stage.
402 vec.ind <- c(current.gen, 1, 0, 0, 0) 402 vector.ind <- c(current.gen, 1, 0, 0, 0)
403 } 403 }
404 else { 404 else {
405 # Add 1 day in current stage. 405 # Add 1 day in current stage.
406 vec.ind[4] <- vec.ind[4] + 1 406 vector.ind[4] <- vector.ind[4] + 1
407 } 407 }
408 vec.mat[i,] <- vec.ind 408 vector.matrix[i,] <- vector.ind
409 } 409 }
410 # Event 3.2 young nymph to old nymph (vec.ind[2]=1 -> young nymph: determines diapause). 410 # Event 3.2 young nymph to old nymph (vector.ind[2]=1 -> young nymph: determines diapause).
411 if (vec.ind[2] == 1) { 411 if (vector.ind[2] == 1) {
412 # Young nymph stage. 412 # Young nymph stage.
413 # Add to degree_days. 413 # Add to degree_days.
414 vec.ind[3] <- vec.ind[3] + degree_days.temp 414 vector.ind[3] <- vector.ind[3] + degree_days.temp
415 if (vec.ind[3] >= (250 + opt$old_nymph_accum)) { 415 if (vector.ind[3] >= (250 + opt$old_nymph_accum)) {
416 # From young to old nymph, degree_days requirement met. 416 # From young to old nymph, degree_days requirement met.
417 current.gen <- vec.ind[1] 417 current.gen <- vector.ind[1]
418 # Transfer to old nym stage. 418 # Transfer to old nym stage.
419 vec.ind <- c(current.gen, 2, 0, 0, 0) 419 vector.ind <- c(current.gen, 2, 0, 0, 0)
420 if (photoperiod < opt$photoperiod && doy > 180) { 420 if (photoperiod < opt$photoperiod && doy > 180) {
421 vec.ind[5] <- 1 421 vector.ind[5] <- 1
422 } # Prepare for diapausing. 422 } # Prepare for diapausing.
423 } 423 }
424 else { 424 else {
425 # Add 1 day in current stage. 425 # Add 1 day in current stage.
426 vec.ind[4] <- vec.ind[4] + 1 426 vector.ind[4] <- vector.ind[4] + 1
427 } 427 }
428 vec.mat[i,] <- vec.ind 428 vector.matrix[i,] <- vector.ind
429 } 429 }
430 # Event 3.3 old nymph to adult: previttelogenic or diapausing? 430 # Event 3.3 old nymph to adult: previttelogenic or diapausing?
431 if (vec.ind[2] == 2) { 431 if (vector.ind[2] == 2) {
432 # Old nymph stage. 432 # Old nymph stage.
433 # Add to degree_days. 433 # Add to degree_days.
434 vec.ind[3] <- vec.ind[3] + degree_days.temp 434 vector.ind[3] <- vector.ind[3] + degree_days.temp
435 if (vec.ind[3] >= (200 + opt$adult_accum)) { 435 if (vector.ind[3] >= (200 + opt$adult_accum)) {
436 # From old to adult, degree_days requirement met. 436 # From old to adult, degree_days requirement met.
437 current.gen <- vec.ind[1] 437 current.gen <- vector.ind[1]
438 if (vec.ind[5] == 0) { 438 if (vector.ind[5] == 0) {
439 # Non-diapausing adult -- previttelogenic. 439 # Non-diapausing adult -- previttelogenic.
440 vec.ind <- c(current.gen, 3, 0, 0, 0) 440 vector.ind <- c(current.gen, 3, 0, 0, 0)
441 } 441 }
442 else { 442 else {
443 # Diapausing. 443 # Diapausing.
444 vec.ind <- c(current.gen, 5, 0, 0, 1) 444 vector.ind <- c(current.gen, 5, 0, 0, 1)
445 } 445 }
446 } 446 }
447 else { 447 else {
448 # Add 1 day in current stage. 448 # Add 1 day in current stage.
449 vec.ind[4] <- vec.ind[4] + 1 449 vector.ind[4] <- vector.ind[4] + 1
450 } 450 }
451 vec.mat[i,] <- vec.ind 451 vector.matrix[i,] <- vector.ind
452 } 452 }
453 # Event 4 growing of diapausing adult (unimportant, but still necessary). 453 # Event 4 growing of diapausing adult (unimportant, but still necessary).
454 if (vec.ind[2] == 5) { 454 if (vector.ind[2] == 5) {
455 vec.ind[3] <- vec.ind[3] + degree_days.temp 455 vector.ind[3] <- vector.ind[3] + degree_days.temp
456 vec.ind[4] <- vec.ind[4] + 1 456 vector.ind[4] <- vector.ind[4] + 1
457 vec.mat[i,] <- vec.ind 457 vector.matrix[i,] <- vector.ind
458 } 458 }
459 } # Else if it is still alive. 459 } # Else if it is still alive.
460 } # End of the individual bug loop. 460 } # End of the individual bug loop.
461 # Find how many died. 461 # Find how many died.
462 num_insects.death <- length(death.vec) 462 num_insects.death <- length(death.vector)
463 if (num_insects.death > 0) { 463 if (num_insects.death > 0) {
464 vec.mat <- vec.mat[-death.vec, ] 464 vector.matrix <- vector.matrix[-death.vector, ]
465 } 465 }
466 # Remove record of dead. 466 # Remove record of dead.
467 # Find how many new born. 467 # Find how many new born.
468 num_insects.newborn <- length(birth.vec[,1]) 468 num_insects.newborn <- length(birth.vector[,1])
469 vec.mat <- rbind(vec.mat, birth.vec) 469 vector.matrix <- rbind(vector.matrix, birth.vector)
470 # Update population size for the next day. 470 # Update population size for the next day.
471 num_insects <- num_insects - num_insects.death + num_insects.newborn 471 num_insects <- num_insects - num_insects.death + num_insects.newborn
472 472
473 # Aggregate results by day. 473 # Aggregate results by day.
474 tot.pop <- c(tot.pop, num_insects) 474 tot.pop <- c(tot.pop, num_insects)
475 # Egg. 475 # Egg.
476 sum_eggs <- sum(vec.mat[,2] == 0) 476 sum_eggs <- sum(vector.matrix[,2] == 0)
477 # Young nymph. 477 # Young nymph.
478 sum_young_nymphs <- sum(vec.mat[,2] == 1) 478 sum_young_nymphs <- sum(vector.matrix[,2] == 1)
479 # Old nymph. 479 # Old nymph.
480 sum_old_nymphs <- sum(vec.mat[,2] == 2) 480 sum_old_nymphs <- sum(vector.matrix[,2] == 2)
481 # Previtellogenic. 481 # Previtellogenic.
482 sum_previtellogenic <- sum(vec.mat[,2] == 3) 482 sum_previtellogenic <- sum(vector.matrix[,2] == 3)
483 # Vitellogenic. 483 # Vitellogenic.
484 sum_vitellogenic <- sum(vec.mat[,2] == 4) 484 sum_vitellogenic <- sum(vector.matrix[,2] == 4)
485 # Diapausing. 485 # Diapausing.
486 sum_diapausing <- sum(vec.mat[,2] == 5) 486 sum_diapausing <- sum(vector.matrix[,2] == 5)
487 # Overwintering adult. 487 # Overwintering adult.
488 gen0 <- sum(vec.mat[,1] == 0) 488 gen0 <- sum(vector.matrix[,1] == 0)
489 # First generation. 489 # First generation.
490 gen1 <- sum(vec.mat[,1] == 1) 490 gen1 <- sum(vector.matrix[,1] == 1)
491 # Second generation. 491 # Second generation.
492 gen2 <- sum(vec.mat[,1] == 2) 492 gen2 <- sum(vector.matrix[,1] == 2)
493 # Sum of all adults. 493 # Sum of all adults.
494 num_insects.adult <- sum(vec.mat[,2] == 3) + sum(vec.mat[,2] == 4) + sum(vec.mat[,2] == 5) 494 num_insects.adult <- sum(vector.matrix[,2] == 3) + sum(vector.matrix[,2] == 4) + sum(vector.matrix[,2] == 5)
495 495
496 # Generation 0 pop size. 496 # Generation 0 pop size.
497 gen0.pop[row] <- gen0 497 gen0.pop[row] <- gen0
498 gen1.pop[row] <- gen1 498 gen1.pop[row] <- gen1
499 gen2.pop[row] <- gen2 499 gen2.pop[row] <- gen2
503 S2[row] <- sum_old_nymphs 503 S2[row] <- sum_old_nymphs
504 S3[row] <- sum_previtellogenic 504 S3[row] <- sum_previtellogenic
505 S4[row] <- sum_vitellogenic 505 S4[row] <- sum_vitellogenic
506 S5[row] <- sum_diapausing 506 S5[row] <- sum_diapausing
507 507
508 g0.adult[row] <- sum(vec.mat[,1] == 0) 508 g0.adult[row] <- sum(vector.matrix[,1] == 0)
509 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)) 509 g1.adult[row] <- sum((vector.matrix[,1] == 1 & vector.matrix[,2] == 3) | (vector.matrix[,1] == 1 & vector.matrix[,2] == 4) | (vector.matrix[,1] == 1 & vector.matrix[,2] == 5))
510 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)) 510 g2.adult[row] <- sum((vector.matrix[,1]== 2 & vector.matrix[,2] == 3) | (vector.matrix[,1] == 2 & vector.matrix[,2] == 4) | (vector.matrix[,1] == 2 & vector.matrix[,2] == 5))
511 511
512 N.newborn[row] <- num_insects.newborn 512 N.newborn[row] <- num_insects.newborn
513 N.death[row] <- num_insects.death 513 N.death[row] <- num_insects.death
514 N.adult[row] <- num_insects.adult 514 N.adult[row] <- num_insects.adult
515 } # end of days specified in the input temperature data 515 } # end of days specified in the input temperature data
516 516
517 degree_days.cum <- cumsum(degree_days.day) 517 degree_days.cum <- cumsum(degree_days.day)
518 518
519 # Collect all the outputs. 519 # Collect all the outputs.
520 S0.rep[,N.rep] <- S0 520 S0.replications[,N.replications] <- S0
521 S1.rep[,N.rep] <- S1 521 S1.replications[,N.replications] <- S1
522 S2.rep[,N.rep] <- S2 522 S2.replications[,N.replications] <- S2
523 S3.rep[,N.rep] <- S3 523 S3.replications[,N.replications] <- S3
524 S4.rep[,N.rep] <- S4 524 S4.replications[,N.replications] <- S4
525 S5.rep[,N.rep] <- S5 525 S5.replications[,N.replications] <- S5
526 newborn.rep[,N.rep] <- N.newborn 526 newborn.replications[,N.replications] <- N.newborn
527 death.rep[,N.rep] <- N.death 527 death.replications[,N.replications] <- N.death
528 adult.rep[,N.rep] <- N.adult 528 adult.replications[,N.replications] <- N.adult
529 pop.rep[,N.rep] <- tot.pop 529 pop.replications[,N.replications] <- tot.pop
530 g0.rep[,N.rep] <- gen0.pop 530 g0.replications[,N.replications] <- gen0.pop
531 g1.rep[,N.rep] <- gen1.pop 531 g1.replications[,N.replications] <- gen1.pop
532 g2.rep[,N.rep] <- gen2.pop 532 g2.replications[,N.replications] <- gen2.pop
533 g0a.rep[,N.rep] <- g0.adult 533 g0a.replications[,N.replications] <- g0.adult
534 g1a.rep[,N.rep] <- g1.adult 534 g1a.replications[,N.replications] <- g1.adult
535 g2a.rep[,N.rep] <- g2.adult 535 g2a.replications[,N.replications] <- g2.adult
536 } 536 }
537 537
538 # Mean value for adults. 538 # Mean value for adults.
539 mean_value_adult <- apply((S3.rep + S4.rep + S5.rep), 1, mean) 539 adult_mean <- apply((S3.replications + S4.replications + S5.replications), 1, mean)
540 # Mean value for nymphs. 540 # Mean value for nymphs.
541 mean_value_nymph <- apply((S1.rep + S2.rep), 1, mean) 541 nymph_mean <- apply((S1.replications + S2.replications), 1, mean)
542 # Mean value for eggs. 542 # Mean value for eggs.
543 mean_value_egg <- apply(S0.rep, 1, mean) 543 egg_mean <- apply(S0.replications, 1, mean)
544 # Mean value for P. 544 # Mean value for P.
545 g0 <- apply(g0.rep, 1, mean) 545 g0 <- apply(g0.replications, 1, mean)
546 # Mean value for F1. 546 # Mean value for F1.
547 g1 <- apply(g1.rep, 1, mean) 547 g1 <- apply(g1.replications, 1, mean)
548 # Mean value for F2. 548 # Mean value for F2.
549 g2 <- apply(g2.rep, 1, mean) 549 g2 <- apply(g2.replications, 1, mean)
550 # Mean value for P adult. 550 # Mean value for P adult.
551 g0a <- apply(g0a.rep, 1, mean) 551 g0a <- apply(g0a.replications, 1, mean)
552 # Mean value for F1 adult. 552 # Mean value for F1 adult.
553 g1a <- apply(g1a.rep, 1, mean) 553 g1a <- apply(g1a.replications, 1, mean)
554 # Mean value for F2 adult. 554 # Mean value for F2 adult.
555 g2a <- apply(g2a.rep, 1, mean) 555 g2a <- apply(g2a.replications, 1, mean)
556 556
557 # Standard error for adults. 557 # Standard error for adults.
558 mean_value_adult.std_error <- apply((S3.rep + S4.rep + S5.rep), 1, sd) / sqrt(opt$replications) 558 adult_mean.std_error <- apply((S3.replications + S4.replications + S5.replications), 1, sd) / sqrt(opt$replications)
559 # Standard error for nymphs. 559 # Standard error for nymphs.
560 mean_value_nymph.std_error <- apply((S1.rep + S2.rep) / sqrt(opt$replications), 1, sd) 560 nymph_mean.std_error <- apply((S1.replications + S2.replications) / sqrt(opt$replications), 1, sd)
561 # Standard error for eggs. 561 # Standard error for eggs.
562 mean_value_egg.std_error <- apply(S0.rep, 1, sd) / sqrt(opt$replications) 562 egg_mean.std_error <- apply(S0.replications, 1, sd) / sqrt(opt$replications)
563 # Standard error value for P. 563 # Standard error value for P.
564 g0.std_error <- apply(g0.rep, 1, sd) / sqrt(opt$replications) 564 g0.std_error <- apply(g0.replications, 1, sd) / sqrt(opt$replications)
565 # Standard error for F1. 565 # Standard error for F1.
566 g1.std_error <- apply(g1.rep, 1, sd) / sqrt(opt$replications) 566 g1.std_error <- apply(g1.replications, 1, sd) / sqrt(opt$replications)
567 # Standard error for F2. 567 # Standard error for F2.
568 g2.std_error <- apply(g2.rep, 1, sd) / sqrt(opt$replications) 568 g2.std_error <- apply(g2.replications, 1, sd) / sqrt(opt$replications)
569 # Standard error for P adult. 569 # Standard error for P adult.
570 g0a.std_error <- apply(g0a.rep, 1, sd) / sqrt(opt$replications) 570 g0a.std_error <- apply(g0a.replications, 1, sd) / sqrt(opt$replications)
571 # Standard error for F1 adult. 571 # Standard error for F1 adult.
572 g1a.std_error <- apply(g1a.rep, 1, sd) / sqrt(opt$replications) 572 g1a.std_error <- apply(g1a.replications, 1, sd) / sqrt(opt$replications)
573 # Standard error for F2 adult. 573 # Standard error for F2 adult.
574 g2a.std_error <- apply(g2a.rep, 1, sd) / sqrt(opt$replications) 574 g2a.std_error <- apply(g2a.replications, 1, sd) / sqrt(opt$replications)
575 575
576 dev.new(width=20, height=30) 576 dev.new(width=20, height=30)
577 577
578 # Start PDF device driver to save charts to output. 578 # Start PDF device driver to save charts to output.
579 pdf(file=opt$output, width=20, height=30, bg="white") 579 pdf(file=opt$output, width=20, height=30, bg="white")
586 start_date <- temperature_data_frame$DATE[1] 586 start_date <- temperature_data_frame$DATE[1]
587 end_date <- temperature_data_frame$DATE[opt$num_days] 587 end_date <- temperature_data_frame$DATE[opt$num_days]
588 588
589 # Subfigure 1: population size by life stage 589 # Subfigure 1: population size by life stage
590 title <- paste(opt$insect, ": Total pop. by life stage :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") 590 title <- paste(opt$insect, ": Total pop. by life stage :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ")
591 plot(day.all, mean_value_adult, main=title, type="l", ylim=c(0, max(mean_value_egg + mean_value_egg.std_error, mean_value_nymph + mean_value_nymph.std_error, mean_value_adult + mean_value_adult.std_error)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3) 591 plot(day.all, adult_mean, main=title, type="l", ylim=c(0, max(egg_mean + egg_mean.std_error, nymph_mean + nymph_mean.std_error, adult_mean + adult_mean.std_error)), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3)
592 # Young and old nymphs. 592 # Young and old nymphs.
593 lines(day.all, mean_value_nymph, lwd=2, lty=1, col=2) 593 lines(day.all, nymph_mean, lwd=2, lty=1, col=2)
594 # Eggs 594 # Eggs
595 lines(day.all, mean_value_egg, lwd=2, lty=1, col=4) 595 lines(day.all, egg_mean, lwd=2, lty=1, col=4)
596 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")) 596 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"))
597 axis(2, cex.axis=3) 597 axis(2, cex.axis=3)
598 leg.text <- c("Egg", "Nymph", "Adult") 598 legend("topleft", c("Egg", "Nymph", "Adult"), lty=c(1, 1, 1), col=c(4, 2, 1), cex=3)
599 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(4, 2, 1), cex=3)
600 if (opt$std_error_plot == 1) { 599 if (opt$std_error_plot == 1) {
601 # Add Standard error lines to plot 600 # Add Standard error lines to plot
602 # Standard error for adults 601 # Standard error for adults
603 lines (day.all, mean_value_adult+mean_value_adult.std_error, lty=2) 602 lines (day.all, adult_mean+adult_mean.std_error, lty=2)
604 lines (day.all, mean_value_adult-mean_value_adult.std_error, lty=2) 603 lines (day.all, adult_mean-adult_mean.std_error, lty=2)
605 # Standard error for nymphs 604 # Standard error for nymphs
606 lines (day.all, mean_value_nymph+mean_value_nymph.std_error, col=2, lty=2) 605 lines (day.all, nymph_mean+nymph_mean.std_error, col=2, lty=2)
607 lines (day.all, mean_value_nymph-mean_value_nymph.std_error, col=2, lty=2) 606 lines (day.all, nymph_mean-nymph_mean.std_error, col=2, lty=2)
608 # Standard error for eggs 607 # Standard error for eggs
609 lines (day.all, mean_value_egg+mean_value_egg.std_error, col=4, lty=2) 608 lines (day.all, egg_mean+egg_mean.std_error, col=4, lty=2)
610 lines (day.all, mean_value_egg-mean_value_egg.std_error, col=4, lty=2) 609 lines (day.all, egg_mean-egg_mean.std_error, col=4, lty=2)
611 } 610 }
612 611
613 # Subfigure 2: population size by generation 612 # Subfigure 2: population size by generation
614 title <- paste(opt$insect, ": Total pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ") 613 title <- paste(opt$insect, ": Total pop. by generation :", opt$location, ": Lat:", latitude, ":", start_date, "-", end_date, sep=" ")
615 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) 614 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)
616 lines(day.all, g1, lwd = 2, lty = 1, col=2) 615 lines(day.all, g1, lwd = 2, lty = 1, col=2)
617 lines(day.all, g2, lwd = 2, lty = 1, col=4) 616 lines(day.all, g2, lwd = 2, lty = 1, col=4)
618 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")) 617 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"))
619 axis(2, cex.axis=3) 618 axis(2, cex.axis=3)
620 leg.text <- c("P", "F1", "F2") 619 legend("topleft", c("P", "F1", "F2"), lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
621 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
622 if (opt$std_error_plot == 1) { 620 if (opt$std_error_plot == 1) {
623 # Add Standard error lines to plot 621 # Add Standard error lines to plot
624 # Standard error for adults 622 # Standard error for adults
625 lines (day.all, g0+g0.std_error, lty=2) 623 lines (day.all, g0+g0.std_error, lty=2)
626 lines (day.all, g0-g0.std_error, lty=2) 624 lines (day.all, g0-g0.std_error, lty=2)
637 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) 635 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)
638 lines(day.all, g1a, lwd = 2, lty = 1, col=2) 636 lines(day.all, g1a, lwd = 2, lty = 1, col=2)
639 lines(day.all, g2a, lwd = 2, lty = 1, col=4) 637 lines(day.all, g2a, lwd = 2, lty = 1, col=4)
640 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")) 638 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"))
641 axis(2, cex.axis=3) 639 axis(2, cex.axis=3)
642 leg.text <- c("P", "F1", "F2") 640 legend("topleft", c("P", "F1", "F2"), lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
643 legend("topleft", leg.text, lty=c(1, 1, 1), col=c(1, 2, 4), cex=3)
644 if (opt$std_error_plot == 1) { 641 if (opt$std_error_plot == 1) {
645 # Add Standard error lines to plot 642 # Add Standard error lines to plot
646 # Standard error for adults 643 # Standard error for adults
647 lines (day.all, g0a+g0a.std_error, lty=2) 644 lines (day.all, g0a+g0a.std_error, lty=2)
648 lines (day.all, g0a-g0a.std_error, lty=2) 645 lines (day.all, g0a-g0a.std_error, lty=2)