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