Mercurial > repos > greg > insect_phenology_model
comparison insect_phenology_model.R @ 60:393085589438 draft
Uploaded
author | greg |
---|---|
date | Thu, 20 Dec 2018 09:15:52 -0500 |
parents | 892cf703be62 |
children | 734f8e4dfbe5 |
comparison
equal
deleted
inserted
replaced
59:892cf703be62 | 60:393085589438 |
---|---|
248 mortality.probability = temperature * 0.0005 + 0.02; | 248 mortality.probability = temperature * 0.0005 + 0.02; |
249 } | 249 } |
250 return(mortality.probability) | 250 return(mortality.probability) |
251 } | 251 } |
252 | 252 |
253 mortality.egg = function(temperature, adj=0) { | 253 #mortality.egg = function(temperature, adj=0) { |
254 # If no input from adjustment, default | 254 # # If no input from adjustment, default |
255 # value is 0 (data from Nielsen, 2008). | 255 # # value is 0 (data from Nielsen, 2008). |
256 T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35); | 256 # T.mortality = c(15, 17, 20, 25, 27, 30, 33, 35); |
257 egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100); | 257 # egg.mortality = c(50, 2, 1, 0, 0, 0, 5, 100); |
258 # Calculates slopes and intercepts for lines. | 258 # # Calculates slopes and intercepts for lines. |
259 slopes = NULL; | 259 # slopes = NULL; |
260 intercepts = NULL; | 260 # intercepts = NULL; |
261 for (i in 1:length(T.mortality)) { | 261 # for (i in 1:length(T.mortality)) { |
262 slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]); | 262 # slopes[i] = (egg.mortality[i+1] - egg.mortality[i]) / (T.mortality[i+1] - T.mortality[i]); |
263 intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i]; | 263 # intercepts[i] = -slopes[i] * T.mortality[i] + egg.mortality[i]; |
264 } | 264 # } |
265 # Calculates mortality based on temperature. | 265 # # Calculates mortality based on temperature. |
266 mortality.probability = NULL; | 266 # mortality.probability = NULL; |
267 for (j in 1:length(temperature)) { | 267 # for (j in 1:length(temperature)) { |
268 mortality.probability[j] = if(temperature[j] <= T.mortality[2]) { | 268 # mortality.probability[j] = if(temperature[j] <= T.mortality[2]) { |
269 temperature[j] * slopes[1] + intercepts[1]; | 269 # temperature[j] * slopes[1] + intercepts[1]; |
270 } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) { | 270 # } else if (temperature[j] > T.mortality[2] && temperature[j] <= T.mortality[3]) { |
271 temperature[j] * slopes[2] + intercepts[2]; | 271 # temperature[j] * slopes[2] + intercepts[2]; |
272 } else if (temperature[j] > T.mortality[3] && temperature[j] <= T.mortality[4]) { | 272 # } else if (temperature[j] > T.mortality[3] && temperature[j] <= T.mortality[4]) { |
273 temperature[j] * slopes[3] + intercepts[3]; | 273 # temperature[j] * slopes[3] + intercepts[3]; |
274 } else if (temperature[j] > T.mortality[4] && temperature[j] <= T.mortality[5]) { | 274 # } else if (temperature[j] > T.mortality[4] && temperature[j] <= T.mortality[5]) { |
275 temperature[j] * slopes[4] + intercepts[4]; | 275 # temperature[j] * slopes[4] + intercepts[4]; |
276 } else if (temperature[j] > T.mortality[5] && temperature[j] <= T.mortality[6]) { | 276 # } else if (temperature[j] > T.mortality[5] && temperature[j] <= T.mortality[6]) { |
277 temperature[j] * slopes[5] + intercepts[5]; | 277 # temperature[j] * slopes[5] + intercepts[5]; |
278 } else if (temperature[j] > T.mortality[6] && temperature[j] <= T.mortality[7]) { | 278 # } else if (temperature[j] > T.mortality[6] && temperature[j] <= T.mortality[7]) { |
279 temperature[j] * slopes[6] + intercepts[6]; | 279 # temperature[j] * slopes[6] + intercepts[6]; |
280 } else if (temperature[j] > T.mortality[7]) { | 280 # } else if (temperature[j] > T.mortality[7]) { |
281 temperature[j] * slopes[7] + intercepts[7]; | 281 # temperature[j] * slopes[7] + intercepts[7]; |
282 } | 282 # } |
283 # If mortality > 100, make it equal to 100. | 283 # # If mortality > 100, make it equal to 100. |
284 mortality.probability[mortality.probability>100] = 100; | 284 # mortality.probability[mortality.probability>100] = 100; |
285 # If mortality <0, make equal to 0. | 285 # # If mortality <0, make equal to 0. |
286 mortality.probability[mortality.probability<0] = 0; | 286 # mortality.probability[mortality.probability<0] = 0; |
287 } | 287 # } |
288 # Make mortality adjustments based on adj parameter. | 288 # # Make mortality adjustments based on adj parameter. |
289 mortality.probability = (100 - mortality.probability) * adj + mortality.probability; | 289 # mortality.probability = (100 - mortality.probability) * adj + mortality.probability; |
290 # if mortality > 100, make it equal to 100. | 290 # # if mortality > 100, make it equal to 100. |
291 mortality.probability[mortality.probability>100] = 100; | 291 # mortality.probability[mortality.probability>100] = 100; |
292 # If mortality <0, make equal to 0. | 292 # # If mortality <0, make equal to 0. |
293 mortality.probability[mortality.probability<0] = 0; | 293 # mortality.probability[mortality.probability<0] = 0; |
294 # Change percent to proportion. | 294 # # Change percent to proportion. |
295 mortality.probability = mortality.probability / 100; | 295 # mortality.probability = mortality.probability / 100; |
296 return(mortality.probability) | 296 # return(mortality.probability) |
297 #} | |
298 | |
299 mortality.egg = function(temperature) { | |
300 if (temperature < 12.7) { | |
301 mortality.probability = 0.8; | |
302 } else { | |
303 mortality.probability = 0.8 - temperature / 40.0; | |
304 if (mortality.probability < 0) { | |
305 mortality.probability = 0.01; | |
306 } | |
307 } | |
308 return (mortality.probability); | |
297 } | 309 } |
298 | 310 |
299 mortality.nymph = function(temperature) { | 311 mortality.nymph = function(temperature) { |
300 if (temperature < 12.7) { | 312 if (temperature < 12.7) { |
301 mortality.probability = 0.03; | 313 mortality.probability = 0.03; |
364 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); | 376 end_date_ytd_row = which(temperature_data_frame$DATE==end_date); |
365 if (length(end_date_ytd_row) > 0) { | 377 if (length(end_date_ytd_row) > 0) { |
366 end_date_ytd_row = end_date_ytd_row[1]; | 378 end_date_ytd_row = end_date_ytd_row[1]; |
367 # The end date is contained within the input_ytd data. | 379 # The end date is contained within the input_ytd data. |
368 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); | 380 end_doy_ytd = as.integer(temperature_data_frame$DOY[end_date_ytd_row]); |
369 if (end_doy_ytd > end_date_ytd_row + 1) { | |
370 # The input year-to-date dataset is missing 1 or more | |
371 # days of data. | |
372 days_missing = end_doy_ytd - end_date_ytd_row; | |
373 msg = cat("The year-to-date dataset is missing ", days_missing, " days of data.\n"); | |
374 stop_err(msg); | |
375 } | |
376 } else { | 381 } else { |
377 end_date_ytd_row = 0; | 382 end_date_ytd_row = 0; |
378 } | 383 } |
379 } else { | 384 } else { |
380 # We're plotting an entire year. | 385 # We're plotting an entire year. |
394 # Properly set the end_date to be Dec 31 of the year. | 399 # Properly set the end_date to be Dec 31 of the year. |
395 end_date = paste(year, "12", "31", sep="-"); | 400 end_date = paste(year, "12", "31", sep="-"); |
396 # Save the first DOY to later check if start_date is Jan 1. | 401 # Save the first DOY to later check if start_date is Jan 1. |
397 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); | 402 start_doy_ytd = as.integer(temperature_data_frame$DOY[1]); |
398 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]); | 403 end_doy_ytd = as.integer(temperature_data_frame$DOY[num_ytd_rows]); |
399 if (end_doy_ytd > end_date_ytd_row + 1) { | |
400 # The input year-to-date dataset is missing 1 or more | |
401 # days of data. | |
402 days_missing = end_doy_ytd - end_date_ytd_row; | |
403 msg = cat("The year-to-date dataset is missing ", days_missing, " days of data.\n"); | |
404 stop_err(msg); | |
405 } | |
406 } | 404 } |
407 } else { | 405 } else { |
408 # We're processing only the 30 year normals data, so create an empty | 406 # We're processing only the 30 year normals data, so create an empty |
409 # data frame for containing temperature data after it is converted | 407 # data frame for containing temperature data after it is converted |
410 # from the 30 year normals format to the year-to-date format. | 408 # from the 30 year normals format to the year-to-date format. |
555 for (i in 1:total_days) { | 553 for (i in 1:total_days) { |
556 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); | 554 temperature_data_frame[i,] = get_next_normals_row(norm_data_frame, year, i); |
557 } | 555 } |
558 } | 556 } |
559 } | 557 } |
558 # Ensure all DOY values are consectuive integers. | |
559 validate_doys(temperature_data_frame); | |
560 # Add a column containing the daylight length for each day. | 560 # Add a column containing the daylight length for each day. |
561 temperature_data_frame = add_daylight_length(temperature_data_frame); | 561 temperature_data_frame = add_daylight_length(temperature_data_frame); |
562 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); | 562 return(list(temperature_data_frame, start_date, end_date, prepend_end_doy_norm, append_start_doy_norm, is_leap_year, location)); |
563 } | 563 } |
564 | 564 |
854 post.mortality = 2; | 854 post.mortality = 2; |
855 day.kill = 250; | 855 day.kill = 250; |
856 } | 856 } |
857 if (vector.individual[2] == 0) { | 857 if (vector.individual[2] == 0) { |
858 # Egg. | 858 # Egg. |
859 death.probability = opt$egg_mortality * mortality.egg(mean.temp, adj=opt$egg_mortality); | 859 # death.probability = opt$egg_mortality * mortality.egg(mean.temp, adj=opt$egg_mortality); |
860 death.probability = opt$egg_mortality * mortality.egg(mean.temp); | |
860 } | 861 } |
861 else if (vector.individual[2] == 1 | vector.individual[2] == 2) { | 862 else if (vector.individual[2] == 1 | vector.individual[2] == 2) { |
862 # Nymph. | 863 # Nymph. |
863 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp); | 864 death.probability = opt$nymph_mortality * mortality.nymph(mean.temp); |
864 } | 865 } |