Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 20:214217142600 draft
Uploaded
author | greg |
---|---|
date | Thu, 08 Mar 2018 08:06:18 -0500 |
parents | 3c6e94e477cb |
children | 6349699fc9fa |
comparison
equal
deleted
inserted
replaced
19:3c6e94e477cb | 20:214217142600 |
---|---|
69 } | 69 } |
70 } | 70 } |
71 return(c(unlist(month_labels))); | 71 return(c(unlist(month_labels))); |
72 } | 72 } |
73 | 73 |
74 | |
75 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { | 74 get_file_path = function(life_stage, base_name, life_stage_nymph=NULL, life_stage_adult=NULL) { |
76 if (!is.null(life_stage_nymph)) { | 75 if (!is.null(life_stage_nymph)) { |
77 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); | 76 lsi = get_life_stage_index(life_stage, life_stage_nymph=life_stage_nymph); |
78 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_"); | 77 file_name = paste(lsi, tolower(life_stage_nymph), base_name, sep="_"); |
79 } else if (!is.null(life_stage_adult)) { | 78 } else if (!is.null(life_stage_adult)) { |
112 } | 111 } |
113 } else if (life_stage=="Total") { | 112 } else if (life_stage=="Total") { |
114 lsi = "09"; | 113 lsi = "09"; |
115 } | 114 } |
116 return(lsi); | 115 return(lsi); |
116 } | |
117 | |
118 get_mean_and_std_error = function(p_replications, f1_replications, f2_replications) { | |
119 # P mean. | |
120 p_m = apply(p_replications, 1, mean); | |
121 # P standard error. | |
122 p_se = apply(p_replications, 1, sd) / sqrt(opt$replications); | |
123 # F1 mean. | |
124 f1_m = apply(f1_replications, 1, mean); | |
125 # F1 standard error. | |
126 f1_se = apply(f1_replications, 1, sd) / sqrt(opt$replications); | |
127 # F2 mean. | |
128 f2_m = apply(f2_replications, 1, mean); | |
129 # F2 standard error. | |
130 f2_se = apply(f2_replications, 1, sd) / sqrt(opt$replications); | |
131 return(list(p_m, p_se, f1_m, f1_se, f2_m, f2_se)) | |
117 } | 132 } |
118 | 133 |
119 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) { | 134 get_temperature_at_hour = function(latitude, temperature_data_frame, row, num_days) { |
120 # Base development threshold for Brown Marmorated Stink Bug | 135 # Base development threshold for Brown Marmorated Stink Bug |
121 # insect phenology model. | 136 # insect phenology model. |
235 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN"); | 250 colnames(temperature_data_frame) = c("LATITUDE","LONGITUDE", "DATE", "DOY", "TMIN", "TMAX", "DAYLEN"); |
236 } | 251 } |
237 return(temperature_data_frame); | 252 return(temperature_data_frame); |
238 } | 253 } |
239 | 254 |
240 | |
241 render_chart = function(date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, | 255 render_chart = function(date_labels, chart_type, plot_std_error, insect, location, latitude, start_date, end_date, days, maxval, |
242 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, | 256 replications, life_stage, group, group_std_error, group2=NULL, group2_std_error=NULL, group3=NULL, group3_std_error=NULL, |
243 life_stages_adult=NULL, life_stages_nymph=NULL) { | 257 life_stages_adult=NULL, life_stages_nymph=NULL) { |
258 cat("In render_chart, chart_type: ", chart_type, "\n"); | |
244 if (chart_type=="pop_size_by_life_stage") { | 259 if (chart_type=="pop_size_by_life_stage") { |
245 if (life_stage=="Total") { | 260 if (life_stage=="Total") { |
246 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 261 title = paste(insect, ": Reps", replications, ":", life_stage, "Pop :", location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
247 legend_text = c("Egg", "Nymph", "Adult"); | 262 legend_text = c("Egg", "Nymph", "Adult"); |
248 columns = c(4, 2, 1); | 263 columns = c(4, 2, 1); |
298 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" "); | 313 title_str = paste(":", life_stages_nymph, "Nymph Pop by Gen", ":", sep=" "); |
299 } else if (life_stage=="Adult") { | 314 } else if (life_stage=="Adult") { |
300 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" "); | 315 title_str = paste(":", life_stages_adult, "Adult Pop by Gen", ":", sep=" "); |
301 } | 316 } |
302 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); | 317 title = paste(insect, ": Reps", replications, title_str, location, ": Lat", latitude, ":", start_date, "-", end_date, sep=" "); |
318 cat("In render_chart, title: ", title, "\n"); | |
319 cat("In render_chart, group: ", group, "\n"); | |
320 cat("In render_chart, group2: ", group2, "\n"); | |
321 cat("In render_chart, group3: ", group3, "\n"); | |
303 legend_text = c("P", "F1", "F2"); | 322 legend_text = c("P", "F1", "F2"); |
304 columns = c(1, 2, 4); | 323 columns = c(1, 2, 4); |
305 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); | 324 plot(days, group, main=title, type="l", ylim=c(0, maxval), axes=F, lwd=2, xlab="", ylab="", cex=3, cex.lab=3, cex.axis=3, cex.main=3); |
306 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3); | 325 legend("topleft", legend_text, lty=c(1, 1, 1), col=columns, cex=3); |
307 lines(days, group2, lwd=2, lty=1, col=2); | 326 lines(days, group2, lwd=2, lty=1, col=2); |
334 date_labels = get_date_labels(temperature_data_frame, opt$num_days); | 353 date_labels = get_date_labels(temperature_data_frame, opt$num_days); |
335 # All latitude values are the same, so get the value for plots from the first row. | 354 # All latitude values are the same, so get the value for plots from the first row. |
336 latitude = temperature_data_frame$LATITUDE[1]; | 355 latitude = temperature_data_frame$LATITUDE[1]; |
337 # Get the number of days for plots. | 356 # Get the number of days for plots. |
338 num_columns = dim(temperature_data_frame)[2]; | 357 num_columns = dim(temperature_data_frame)[2]; |
358 # Determine the specified life stages for processing. | |
339 # Split life_stages into a list of strings for plots. | 359 # Split life_stages into a list of strings for plots. |
340 life_stages_str = as.character(opt$life_stages); | 360 life_stages_str = as.character(opt$life_stages); |
341 life_stages = strsplit(life_stages_str, ",")[[1]]; | 361 life_stages = strsplit(life_stages_str, ",")[[1]]; |
342 # Determine the data we need to generate for plotting. | 362 # Determine the data we need to generate for plotting. |
343 process_eggs = FALSE; | 363 process_eggs = FALSE; |
344 process_nymphs = FALSE; | 364 process_nymphs = FALSE; |
365 process_young_nymphs = FALSE; | |
366 process_old_nymphs = FALSE; | |
367 process_total_nymphs = FALSE; | |
345 process_adults = FALSE; | 368 process_adults = FALSE; |
369 process_previtellogenic_adults = FALSE; | |
370 process_vitellogenic_adults = FALSE; | |
371 process_diapausing_adults = FALSE; | |
372 process_total_adults = FALSE; | |
346 for (life_stage in life_stages) { | 373 for (life_stage in life_stages) { |
347 if (life_stage=="Total") { | 374 if (life_stage=="Total") { |
348 process_eggs = TRUE; | 375 process_eggs = TRUE; |
349 process_nymphs = TRUE; | 376 process_nymphs = TRUE; |
350 process_adults = TRUE; | 377 process_adults = TRUE; |
354 process_nymphs = TRUE; | 381 process_nymphs = TRUE; |
355 } else if (life_stage=="Adult") { | 382 } else if (life_stage=="Adult") { |
356 process_adults = TRUE; | 383 process_adults = TRUE; |
357 } | 384 } |
358 } | 385 } |
386 if (process_nymphs) { | |
387 # Split life_stages_nymph into a list of strings for plots. | |
388 life_stages_nymph_str = as.character(opt$life_stages_nymph); | |
389 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]]; | |
390 for (life_stage_nymph in opt$life_stages_nymph) { | |
391 if (life_stage_nymph=="Young") { | |
392 process_young_nymphs = TRUE; | |
393 } else if (life_stage_nymph=="Old") { | |
394 process_old_nymphs = TRUE; | |
395 } else if (life_stage_nymph=="Total") { | |
396 process_total_nymphs = TRUE; | |
397 } | |
398 } | |
399 } | |
359 if (process_adults) { | 400 if (process_adults) { |
360 # Split life_stages_adult into a list of strings for plots. | 401 # Split life_stages_adult into a list of strings for plots. |
361 life_stages_adult_str = as.character(opt$life_stages_adult); | 402 life_stages_adult_str = as.character(opt$life_stages_adult); |
362 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]]; | 403 life_stages_adult = strsplit(life_stages_adult_str, ",")[[1]]; |
363 } | 404 for (life_stage_adult in opt$life_stages_adult) { |
364 if (process_nymphs) { | 405 if (life_stage_adult=="Previtellogenic") { |
365 # Split life_stages_nymph into a list of strings for plots. | 406 process_previtellogenic_adults = TRUE; |
366 life_stages_nymph_str = as.character(opt$life_stages_nymph); | 407 } else if (life_stage_adult=="Vitellogenic") { |
367 life_stages_nymph = strsplit(life_stages_nymph_str, ",")[[1]]; | 408 process_vitellogenic_adults = TRUE; |
368 } | 409 } else if (life_stage_adult=="Diapausing") { |
410 process_diapausing_adults = TRUE; | |
411 } else if (life_stage_adult=="Total") { | |
412 process_total_adults = TRUE; | |
413 } | |
414 } | |
415 } | |
416 cat("process_eggs: ", process_eggs, "\n"); | |
417 cat("process_nymphs: ", process_nymphs, "\n"); | |
418 cat("process_young_nymphs: ", process_young_nymphs, "\n"); | |
419 cat("process_old_nymphs: ", process_old_nymphs, "\n"); | |
420 cat("process_total_nymphs: ", process_total_nymphs, "\n"); | |
421 cat("process_adults: ", process_adults, "\n"); | |
422 cat("process_previtellogenic_adults: ", process_previtellogenic_adults, "\n"); | |
423 cat("process_vitellogenic_adults: ", process_vitellogenic_adults, "\n"); | |
424 cat("process_diapausing_adults: ", process_diapausing_adults, "\n"); | |
425 cat("process_total_adults: ", process_total_adults, "\n"); | |
426 cat("life_stages: ", life_stages, "\n"); | |
427 cat("life_stages_nymph: ", life_stages_nymph, "\n"); | |
428 cat("life_stages_adult: ", life_stages_adult, "\n"); | |
429 | |
369 # Initialize matrices. | 430 # Initialize matrices. |
370 if (process_eggs) { | 431 if (process_eggs) { |
371 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 432 Eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
372 } | 433 } |
373 if (process_nymphs) { | 434 cat("process_young_nymphs==TRUE: ", process_young_nymphs==TRUE, "\n"); |
435 cat("process_total_nymphs==TRUE: ", process_total_nymphs==TRUE, "\n"); | |
436 if (process_young_nymphs==TRUE | process_total_nymphs==TRUE) { | |
374 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 437 YoungNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
438 } | |
439 if (process_old_nymphs==TRUE | process_total_nymphs==TRUE) { | |
375 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 440 OldNymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
376 } | 441 } |
377 if (process_adults) { | 442 if (process_adults) { |
378 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 443 Previtellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
379 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 444 Vitellogenic.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
392 if (process_eggs) { | 457 if (process_eggs) { |
393 P_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 458 P_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
394 F1_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 459 F1_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
395 F2_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 460 F2_eggs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
396 } | 461 } |
397 if (process_nymphs) { | 462 if (process_young_nymphs) { |
398 P_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 463 P_young_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
399 F1_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 464 F1_young_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
400 F2_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 465 F2_young_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
466 } | |
467 if (process_old_nymphs) { | |
468 P_old_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
469 F1_old_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
470 F2_old_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
471 } | |
472 if (process_total_nymphs) { | |
473 P_total_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
474 F1_total_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
475 F2_total_nymphs.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | |
401 } | 476 } |
402 if (process_adults) { | 477 if (process_adults) { |
403 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 478 P_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
404 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 479 F1_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
405 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); | 480 F2_adults.replications = matrix(rep(0, opt$num_days*opt$replications), ncol=opt$replications); |
447 if (process_eggs) { | 522 if (process_eggs) { |
448 P.egg = rep(0, opt$num_days); | 523 P.egg = rep(0, opt$num_days); |
449 F1.egg = rep(0, opt$num_days); | 524 F1.egg = rep(0, opt$num_days); |
450 F2.egg = rep(0, opt$num_days); | 525 F2.egg = rep(0, opt$num_days); |
451 } | 526 } |
452 if (process_nymphs) { | 527 if (process_young_nymphs) { |
453 P.nymph = rep(0, opt$num_days); | 528 P.young_nymph = rep(0, opt$num_days); |
454 F1.nymph = rep(0, opt$num_days); | 529 F1.young_nymph = rep(0, opt$num_days); |
455 F2.nymph = rep(0, opt$num_days); | 530 F2.young_nymph = rep(0, opt$num_days); |
531 } | |
532 if (process_old_nymphs) { | |
533 P.old_nymph = rep(0, opt$num_days); | |
534 F1.old_nymph = rep(0, opt$num_days); | |
535 F2.old_nymph = rep(0, opt$num_days); | |
536 } | |
537 if (process_total_nymphs) { | |
538 P.total_nymph = rep(0, opt$num_days); | |
539 F1.total_nymph = rep(0, opt$num_days); | |
540 F2.total_nymph = rep(0, opt$num_days); | |
456 } | 541 } |
457 if (process_adults) { | 542 if (process_adults) { |
458 P.adult = rep(0, opt$num_days); | 543 P.adult = rep(0, opt$num_days); |
459 F1.adult = rep(0, opt$num_days); | 544 F1.adult = rep(0, opt$num_days); |
460 F2.adult = rep(0, opt$num_days); | 545 F2.adult = rep(0, opt$num_days); |
694 # are now Generation, Stage, degree-days, T, Diapause, | 779 # are now Generation, Stage, degree-days, T, Diapause, |
695 if (process_eggs) { | 780 if (process_eggs) { |
696 # For egg population size, column 2 (Stage), must be 0. | 781 # For egg population size, column 2 (Stage), must be 0. |
697 Eggs[row] = sum(vector.matrix[,2]==0); | 782 Eggs[row] = sum(vector.matrix[,2]==0); |
698 } | 783 } |
699 if (process_nymphs) { | 784 if (process_young_nymphs) { |
700 # For young nymph population size, column 2 (Stage) must be 1. | 785 # For young nymph population size, column 2 (Stage) must be 1. |
701 YoungNymphs[row] = sum(vector.matrix[,2]==1); | 786 YoungNymphs[row] = sum(vector.matrix[,2]==1); |
787 } | |
788 if (process_old_nymphs) { | |
702 # For old nymph population size, column 2 (Stage) must be 2. | 789 # For old nymph population size, column 2 (Stage) must be 2. |
703 OldNymphs[row] = sum(vector.matrix[,2]==2); | 790 OldNymphs[row] = sum(vector.matrix[,2]==2); |
704 } | 791 } |
705 if (process_adults) { | 792 if (process_adults) { |
706 # For pre-vitellogenic population size, column 2 (Stage) must be 3. | 793 # For pre-vitellogenic population size, column 2 (Stage) must be 3. |
740 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0); | 827 F1.egg[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==0); |
741 # For egg life stage of generation F2 population size, | 828 # For egg life stage of generation F2 population size, |
742 # column 1 (generation) is 2 and column 2 (Stage) is 0. | 829 # column 1 (generation) is 2 and column 2 (Stage) is 0. |
743 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0); | 830 F2.egg[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==0); |
744 } | 831 } |
745 if (process_nymphs) { | 832 if (process_young_nymphs) { |
746 # For nymph life stage of generation P population | 833 # For young nymph life stage of generation P population |
834 # size, the following combination is required: | |
835 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph) | |
836 P.young_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==1); | |
837 # For young nymph life stage of generation F1 population | |
838 # size, the following combination is required: | |
839 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph) | |
840 F1.young_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==1); | |
841 # For young nymph life stage of generation F2 population | |
842 # size, the following combination is required: | |
843 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph) | |
844 F2.young_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==1); | |
845 } | |
846 if (process_old_nymphs) { | |
847 # For old nymph life stage of generation P population | |
848 # size, the following combination is required: | |
849 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph) | |
850 P.old_nymph[row] = sum(vector.matrix[,1]==0 & vector.matrix[,2]==2); | |
851 # For old nymph life stage of generation F1 population | |
852 # size, the following combination is required: | |
853 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph) | |
854 F1.old_nymph[row] = sum(vector.matrix[,1]==1 & vector.matrix[,2]==2); | |
855 # For old nymph life stage of generation F2 population | |
856 # size, the following combination is required: | |
857 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph) | |
858 F2.old_nymph[row] = sum(vector.matrix[,1]==2 & vector.matrix[,2]==2); | |
859 } | |
860 if (process_total_nymphs) { | |
861 # For total nymph life stage of generation P population | |
747 # size, one of the following combinations is required: | 862 # size, one of the following combinations is required: |
748 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph) | 863 # - column 1 (Generation) is 0 and column 2 (Stage) is 1 (Young nymph) |
749 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph) | 864 # - column 1 (Generation) is 0 and column 2 (Stage) is 2 (Old nymph) |
750 P.nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2)); | 865 P.total_nymph[row] = sum((vector.matrix[,1]==0 & vector.matrix[,2]==1) | (vector.matrix[,1]==0 & vector.matrix[,2]==2)); |
751 # For nymph life stage of generation F1 population | 866 # For total nymph life stage of generation F1 population |
752 # size, one of the following combinations is required: | 867 # size, one of the following combinations is required: |
753 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph) | 868 # - column 1 (Generation) is 1 and column 2 (Stage) is 1 (Young nymph) |
754 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph) | 869 # - column 1 (Generation) is 1 and column 2 (Stage) is 2 (Old nymph) |
755 F1.nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2)); | 870 F1.total_nymph[row] = sum((vector.matrix[,1]==1 & vector.matrix[,2]==1) | (vector.matrix[,1]==1 & vector.matrix[,2]==2)); |
756 # For nymph life stage of generation F2 population | 871 # For total nymph life stage of generation F2 population |
757 # size, one of the following combinations is required: | 872 # size, one of the following combinations is required: |
758 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph) | 873 # - column 1 (Generation) is 2 and column 2 (Stage) is 1 (Young nymph) |
759 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph) | 874 # - column 1 (Generation) is 2 and column 2 (Stage) is 2 (Old nymph) |
760 F2.nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2)); | 875 F2.total_nymph[row] = sum((vector.matrix[,1]==2 & vector.matrix[,2]==1) | (vector.matrix[,1]==2 & vector.matrix[,2]==2)); |
761 } | 876 } |
762 if (process_adults) { | 877 if (process_adults) { |
763 # For adult life stage of generation P population | 878 # For adult life stage of generation P population |
764 # size, one of the following combinations is required: | 879 # size, one of the following combinations is required: |
765 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vitellogenic) | 880 # - column 1 (Generation) is 0 and column 2 (Stage) is 3 (Pre-vitellogenic) |
786 | 901 |
787 # Define the output values. | 902 # Define the output values. |
788 if (process_eggs) { | 903 if (process_eggs) { |
789 Eggs.replications[,current_replication] = Eggs; | 904 Eggs.replications[,current_replication] = Eggs; |
790 } | 905 } |
791 if (process_nymphs) { | 906 if (process_young_nymphs==TRUE | process_total_nymphs==TRUE) { |
792 YoungNymphs.replications[,current_replication] = YoungNymphs; | 907 YoungNymphs.replications[,current_replication] = YoungNymphs; |
908 } | |
909 if (process_old_nymphs==TRUE | process_total_nymphs==TRUE) { | |
793 OldNymphs.replications[,current_replication] = OldNymphs; | 910 OldNymphs.replications[,current_replication] = OldNymphs; |
794 } | 911 } |
795 if (process_adults) { | 912 if (process_adults) { |
796 Previtellogenic.replications[,current_replication] = Previtellogenic; | 913 Previtellogenic.replications[,current_replication] = Previtellogenic; |
797 Vitellogenic.replications[,current_replication] = Vitellogenic; | 914 Vitellogenic.replications[,current_replication] = Vitellogenic; |
810 if (process_eggs) { | 927 if (process_eggs) { |
811 P_eggs.replications[,current_replication] = P.egg; | 928 P_eggs.replications[,current_replication] = P.egg; |
812 F1_eggs.replications[,current_replication] = F1.egg; | 929 F1_eggs.replications[,current_replication] = F1.egg; |
813 F2_eggs.replications[,current_replication] = F2.egg; | 930 F2_eggs.replications[,current_replication] = F2.egg; |
814 } | 931 } |
815 if (process_nymphs) { | 932 if (process_young_nymphs) { |
816 P_nymphs.replications[,current_replication] = P.nymph; | 933 P_young_nymphs.replications[,current_replication] = P.young_nymph; |
817 F1_nymphs.replications[,current_replication] = F1.nymph; | 934 F1_young_nymphs.replications[,current_replication] = F1.young_nymph; |
818 F2_nymphs.replications[,current_replication] = F2.nymph; | 935 F2_young_nymphs.replications[,current_replication] = F2.young_nymph; |
936 } | |
937 if (process_old_nymphs) { | |
938 P_old_nymphs.replications[,current_replication] = P.old_nymph; | |
939 F1_old_nymphs.replications[,current_replication] = F1.old_nymph; | |
940 F2_old_nymphs.replications[,current_replication] = F2.old_nymph; | |
941 } | |
942 if (process_total_nymphs) { | |
943 P_total_nymphs.replications[,current_replication] = P.total_nymph; | |
944 F1_total_nymphs.replications[,current_replication] = F1.total_nymph; | |
945 F2_total_nymphs.replications[,current_replication] = F2.total_nymph; | |
819 } | 946 } |
820 if (process_adults) { | 947 if (process_adults) { |
821 P_adults.replications[,current_replication] = P.adult; | 948 P_adults.replications[,current_replication] = P.adult; |
822 F1_adults.replications[,current_replication] = F1.adult; | 949 F1_adults.replications[,current_replication] = F1.adult; |
823 F2_adults.replications[,current_replication] = F2.adult; | 950 F2_adults.replications[,current_replication] = F2.adult; |
880 } | 1007 } |
881 } | 1008 } |
882 } | 1009 } |
883 | 1010 |
884 if (plot_generations_separately) { | 1011 if (plot_generations_separately) { |
885 # Mean value for P which is Parental, or overwintered adults. | 1012 m_se = get_mean_and_std_error(P.replications, F1.replications, F2.replications); |
886 P = apply(P.replications, 1, mean); | 1013 P = m_se[[1]]; |
887 # Standard error for P. | 1014 P.std_error = m_se[[2]]; |
888 P.std_error = apply(P.replications, 1, sd) / sqrt(opt$replications); | 1015 F1 = m_se[[3]]; |
889 # Mean value for F1, which is the first field-produced generation. | 1016 F1.std_error = m_se[[4]]; |
890 F1 = apply(F1.replications, 1, mean); | 1017 F2 = m_se[[5]]; |
891 # Standard error for F1. | 1018 F2.std_error = m_se[[6]]; |
892 F1.std_error = apply(F1.replications, 1, sd) / sqrt(opt$replications); | |
893 # Mean value for F2, which is the second field-produced generation. | |
894 F2 = apply(F2.replications, 1, mean); | |
895 # Standard error for F2. | |
896 F2.std_error = apply(F2.replications, 1, sd) / sqrt(opt$replications); | |
897 if (process_eggs) { | 1019 if (process_eggs) { |
898 # Mean value for P eggs. | 1020 m_se = get_mean_and_std_error(P_eggs.replications, F1_eggs.replications, F2_eggs.replications); |
899 P_eggs = apply(P_eggs.replications, 1, mean); | 1021 P_eggs = m_se[[1]]; |
900 # Standard error for P_eggs. | 1022 P_eggs.std_error = m_se[[2]]; |
901 P_eggs.std_error = apply(P_eggs.replications, 1, sd) / sqrt(opt$replications); | 1023 F1_eggs = m_se[[3]]; |
902 # Mean value for F1 eggs. | 1024 F1_eggs.std_error = m_se[[4]]; |
903 F1_eggs = apply(F1_eggs.replications, 1, mean); | 1025 F2_eggs = m_se[[5]]; |
904 # Standard error for F1 eggs. | 1026 F2_eggs.std_error = m_se[[6]]; |
905 F1_eggs.std_error = apply(F1_eggs.replications, 1, sd) / sqrt(opt$replications); | 1027 } |
906 # Mean value for F2 eggs. | 1028 if (process_young_nymphs) { |
907 F2_eggs = apply(F2_eggs.replications, 1, mean); | 1029 m_se = get_mean_and_std_error(P_young_nymphs.replications, F1_young_nymphs.replications, F2_young_nymphs.replications); |
908 # Standard error for F2 eggs. | 1030 P_young_nymphs = m_se[[1]]; |
909 F2_eggs.std_error = apply(F2_eggs.replications, 1, sd) / sqrt(opt$replications); | 1031 P_young_nymphs.std_error = m_se[[2]]; |
910 } | 1032 F1_young_nymphs = m_se[[3]]; |
911 if (process_nymphs) { | 1033 F1_young_nymphs.std_error = m_se[[4]]; |
912 # Mean value for P nymphs. | 1034 F2_young_nymphs = m_se[[5]]; |
913 P_nymphs = apply(P_nymphs.replications, 1, mean); | 1035 F2_young_nymphs.std_error = m_se[[6]]; |
914 # Standard error for P_nymphs. | 1036 } |
915 P_nymphs.std_error = apply(P_nymphs.replications, 1, sd) / sqrt(opt$replications); | 1037 if (process_old_nymphs) { |
916 # Mean value for F1 nymphs. | 1038 m_se = get_mean_and_std_error(P_old_nymphs.replications, F1_old_nymphs.replications, F2_old_nymphs.replications); |
917 F1_nymphs = apply(F1_nymphs.replications, 1, mean); | 1039 P_old_nymphs = m_se[[1]]; |
918 # Standard error for F1 nymphs. | 1040 P_old_nymphs.std_error = m_se[[2]]; |
919 F1_nymphs.std_error = apply(F1_nymphs.replications, 1, sd) / sqrt(opt$replications); | 1041 F1_old_nymphs = m_se[[3]]; |
920 # Mean value for F2 nymphs. | 1042 F1_old_nymphs.std_error = m_se[[4]]; |
921 F2_nymphs = apply(F2_nymphs.replications, 1, mean); | 1043 F2_old_nymphs = m_se[[5]]; |
922 # Standard error for F2 eggs. | 1044 F2_old_nymphs.std_error = m_se[[6]]; |
923 F2_nymphs.std_error = apply(F2_nymphs.replications, 1, sd) / sqrt(opt$replications); | 1045 } |
1046 if (process_total_nymphs) { | |
1047 m_se = get_mean_and_std_error(P_total_nymphs.replications, F1_total_nymphs.replications, F2_total_nymphs.replications); | |
1048 P_total_nymphs = m_se[[1]]; | |
1049 P_total_nymphs.std_error = m_se[[2]]; | |
1050 F1_total_nymphs = m_se[[3]]; | |
1051 F1_total_nymphs.std_error = m_se[[4]]; | |
1052 F2_total_nymphs = m_se[[5]]; | |
1053 F2_total_nymphs.std_error = m_se[[6]]; | |
924 } | 1054 } |
925 if (process_adults) { | 1055 if (process_adults) { |
926 # Mean value for P adults. | 1056 # Mean value for P_adults. |
927 P_adults = apply(P_adults.replications, 1, mean); | 1057 P_adults = apply(P_adults.replications, 1, mean); |
928 # Standard error for P_adults. | 1058 # Standard error for P_adults. |
929 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications); | 1059 P_adults.std_error = apply(P_adults.replications, 1, sd) / sqrt(opt$replications); |
930 # Mean value for F1 adults. | 1060 # Mean value for F1 adults. |
931 F1_adults = apply(F1_adults.replications, 1, mean); | 1061 F1_adults = apply(F1_adults.replications, 1, mean); |
932 # Standard error for F1 adults. | 1062 # Standard error for F1_adults. |
933 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications); | 1063 F1_adults.std_error = apply(F1_adults.replications, 1, sd) / sqrt(opt$replications); |
934 # Mean value for F2 adults. | 1064 # Mean value for F2_adults. |
935 F2_adults = apply(F2_adults.replications, 1, mean); | 1065 F2_adults = apply(F2_adults.replications, 1, mean); |
936 # Standard error for F2 adults. | 1066 # Standard error for F2_adults. |
937 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications); | 1067 F2_adults.std_error = apply(F2_adults.replications, 1, sd) / sqrt(opt$replications); |
938 } | 1068 } |
939 } | 1069 } |
940 | 1070 |
941 # Display the total number of days in the Galaxy history item blurb. | 1071 # Display the total number of days in the Galaxy history item blurb. |
944 # Information needed for plots plots. | 1074 # Information needed for plots plots. |
945 days = c(1:opt$num_days); | 1075 days = c(1:opt$num_days); |
946 start_date = temperature_data_frame$DATE[1]; | 1076 start_date = temperature_data_frame$DATE[1]; |
947 end_date = temperature_data_frame$DATE[opt$num_days]; | 1077 end_date = temperature_data_frame$DATE[opt$num_days]; |
948 | 1078 |
1079 cat("life_stages: ", toString(life_stages), "\n"); | |
1080 cat("plot_generations_separately: ", plot_generations_separately, "\n"); | |
949 if (plot_generations_separately) { | 1081 if (plot_generations_separately) { |
950 for (life_stage in life_stages) { | 1082 for (life_stage in life_stages) { |
1083 cat("life_stage: ", life_stage, "\n"); | |
951 if (life_stage == "Egg") { | 1084 if (life_stage == "Egg") { |
952 # Start PDF device driver. | 1085 # Start PDF device driver. |
953 dev.new(width=20, height=30); | 1086 dev.new(width=20, height=30); |
954 file_path = get_file_path(life_stage, "egg_pop_by_generation.pdf") | 1087 file_path = get_file_path(life_stage, "egg_pop_by_generation.pdf") |
955 pdf(file=file_path, width=20, height=30, bg="white"); | 1088 pdf(file=file_path, width=20, height=30, bg="white"); |
956 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)); | 1089 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)); |
957 # Egg population size by generation. | 1090 # Egg population size by generation. |
958 maxval = max(P_eggs+F1_eggs+F2_eggs) + 100; | 1091 maxval = max(P_eggs+F1_eggs+F2_eggs) + 100; |
1092 cat("maxval: ", maxval, "\n"); | |
1093 cat("P_eggs: ", toString(P_eggs), "\n"); | |
1094 cat("is.vector(P_eggs): ", is.vector(P_eggs), "\n"); | |
1095 cat("length(P_eggs): ", length(P_eggs), "\n"); | |
1096 cat("P_eggs.std_error: ", toString(P_eggs.std_error), "\n"); | |
1097 cat("F1_eggs: ", toString(F1_eggs), "\n"); | |
1098 cat("F1_eggs.std_error: ", toString(F1_eggs.std_error), "\n"); | |
1099 cat("F2_eggs: ", toString(F2_eggs), "\n"); | |
1100 cat("F2_eggs.std_error: ", toString(F2_eggs.std_error), "\n"); | |
959 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval, | 1101 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval, |
960 opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error, group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs, | 1102 opt$replications, life_stage, group=P_eggs, group_std_error=P_eggs.std_error, group2=F1_eggs, group2_std_error=F1_eggs.std_error, group3=F2_eggs, |
961 group3_std_error=F2_eggs.std_error); | 1103 group3_std_error=F2_eggs.std_error); |
962 # Turn off device driver to flush output. | 1104 # Turn off device driver to flush output. |
963 dev.off(); | 1105 dev.off(); |
964 } else if (life_stage == "Nymph") { | 1106 } else if (life_stage == "Nymph") { |
965 for (life_stage_nymph in life_stages_nymph) { | 1107 for (life_stage_nymph in life_stages_nymph) { |
1108 cat("life_stage_nymph: ", life_stage_nymph, "\n"); | |
966 # Start PDF device driver. | 1109 # Start PDF device driver. |
967 dev.new(width=20, height=30); | 1110 dev.new(width=20, height=30); |
968 file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", life_stage_nymph=life_stage_nymph) | 1111 file_path = get_file_path(life_stage, "nymph_pop_by_generation.pdf", life_stage_nymph=life_stage_nymph) |
969 pdf(file=file_path, width=20, height=30, bg="white"); | 1112 pdf(file=file_path, width=20, height=30, bg="white"); |
970 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)); | 1113 par(mar=c(5, 6, 4, 4), mfrow=c(3, 1)); |
971 # Nymph population size by generation. | 1114 if (life_stage_nymph=="Young") { |
972 maxval = max(P_nymphs+F1_nymphs+F2_nymphs) + 100; | 1115 # Young nymph population size by generation. |
1116 maxval = max(P_young_nymphs+F1_young_nymphs+F2_young_nymphs) + 100; | |
1117 group = P_young_nymphs; | |
1118 group_std_error = P_young_nymphs.std_error; | |
1119 group2 = F1_young_nymphs; | |
1120 group2_std_error = F1_young_nymphs.std_error; | |
1121 group3 = F2_young_nymphs; | |
1122 group3_std_error = F2_young_nymphs.std_error; | |
1123 } else if (life_stage_nymph=="Old") { | |
1124 # Total nymph population size by generation. | |
1125 maxval = max(P_old_nymphs+F1_old_nymphs+F2_old_nymphs) + 100; | |
1126 group = P_old_nymphs; | |
1127 group_std_error = P_old_nymphs.std_error; | |
1128 group2 = F1_old_nymphs; | |
1129 group2_std_error = F1_old_nymphs.std_error; | |
1130 group3 = F2_old_nymphs; | |
1131 group3_std_error = F2_old_nymphs.std_error; | |
1132 } else if (life_stage_nymph=="Total") { | |
1133 # Total nymph population size by generation. | |
1134 maxval = max(P_total_nymphs+F1_total_nymphs+F2_total_nymphs) + 100; | |
1135 group = P_total_nymphs; | |
1136 group_std_error = P_total_nymphs.std_error; | |
1137 group2 = F1_total_nymphs; | |
1138 group2_std_error = F1_total_nymphs.std_error; | |
1139 group3 = F2_total_nymphs; | |
1140 group3_std_error = F2_total_nymphs.std_error; | |
1141 } | |
1142 cat("XXX group: ", group, "\n"); | |
973 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval, | 1143 render_chart(date_labels, "pop_size_by_generation", opt$plot_std_error, opt$insect, opt$location, latitude, start_date, end_date, days, maxval, |
974 opt$replications, life_stage, group=P_nymphs, group_std_error=P_nymphs.std_error, group2=F1_nymphs, group2_std_error=F1_nymphs.std_error, | 1144 opt$replications, life_stage, group=group, group_std_error=group_std_error, group2=group2, group2_std_error=group2_std_error, |
975 group3=F2_nymphs, group3_std_error=F2_nymphs.std_error, life_stages_nymph=life_stage_nymph); | 1145 group3=group3, group3_std_error=group3_std_error, life_stages_nymph=life_stage_nymph); |
976 # Turn off device driver to flush output. | 1146 # Turn off device driver to flush output. |
977 dev.off(); | 1147 dev.off(); |
978 } | 1148 } |
979 } else if (life_stage == "Adult") { | 1149 } else if (life_stage == "Adult") { |
980 for (life_stage_adult in life_stages_adult) { | 1150 for (life_stage_adult in life_stages_adult) { |