comparison insect_phenology_model.R @ 6:fe3f86012394 draft

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