diff --git a/halomar/documentation/HALOMAR_results_final_deliverable_WMR2024.pdf b/halomar/documentation/HALOMAR_results_final_deliverable_WMR2024.pdf new file mode 100644 index 0000000000000000000000000000000000000000..86e3d78e5bbe845e81466ee68ff5cdc770f4734d --- /dev/null +++ b/halomar/documentation/HALOMAR_results_final_deliverable_WMR2024.pdf @@ -0,0 +1,3 @@ +version https://git-lfs.github.com/spec/v1 +oid sha256:5197ef8efc0293851b20edbe533a1651196294d23e9e574cf35b23cc56a58d85 +size 1313458 diff --git a/halomar/scripts/README.md b/halomar/scripts/README.md new file mode 100644 index 0000000000000000000000000000000000000000..ab73024fa9443da5b50c712a7a14ad9ee134c2f0 --- /dev/null +++ b/halomar/scripts/README.md @@ -0,0 +1,83 @@ +# HALOMAR + +## Requirements +R 4.3.1 + +See libs.R and setup_packages.R for the required packages. + +## Model +HALOMAR is a non-spatially individual-based model (IBM) using an energy budget approach that was designed to assess HAbitat LOss effects on MARine birds (therefore the model name HALOMAR) caused by offshore windfarms (OWF). In this version, HALOMAR can be used to assess the effects of habitat loss on mortality and body condition of Common guillemots (Uria aalge) in the southern North Sea during the non-breeding season (Sep-Feb). This version of HALOMAR does not incorporate density dependence. + +The model consists of two steps, initially calibrations should be run for a certain set of parameters to determine the metabolic rate. Subsequently effect simulations can be done using that metabolic rate to calculate the effect of an OWF scenario on the survival. + +The file run_HALOMAR.R contains an example code that can be used to run HALOMAR. +Scripts are all located in the scr/ folder, all data is placed in the data/ folder. +The functions underlying HALOMAR can be found in scr/runmodel_HALOMAR.R. +Needed R package names can be found in scr/libs.R. + +## Input data +### Food environment +HALOMAR uses bird density maps as proxy for food availability. For this, bird distribution maps with a grid cell size of 10x10km previously published by Waggitt et al. (2019; https://doi.org/10.1111/1365-2664.13525) were rescaled to values between 0 and 1. In order to reduce the effect of outlier values, values exceeding the 97th percentile were set to the value corresponding to the 97th percentile. Finally, additional rescaling was done to keep the total amount of food over the months constant, by rescaling all food densities such that the total food over the map was equal to the month with the lowest total food available. The OWF scenario was based on the 'KEC rekenvariant 3 international' OWF scenario as defined in Soudijn et al. (2022; https://doi.org/10.18174/565601). +The resulting food maps per month (month_number) can be found in data.zip as sc_data_OWF_CommonGuillemot_[month_number].rds and are read in separately per month in run_HALOMAR.R. + +Variables in the food map files: +* Fitted: fitted and rescaled value representing food availability +* owf: offshore windfarm presence indicator (1 = no windfarm, 0 = windfarm) + +## Input parameters +* nb_agents: the number of agents that will be used for the simulation; values should be integers > 0 +* avoidance: offshore windfarm avoidance rate; values can be decimals from 0 to 1 +* metabolism: metabolic estimate (energy use of an agent per time step); values can be decimals from 0 to 1 +* survival: chance of survival after simulated time; values can be decimals from 0 to 1 +* init_energy: mean initial energy of the agents at the first time step of simulation; values should be > 0 +* avoid_patch: avoidance probability of grid cells with low food availability; values can be decimals from 0 to 1 +* cell_day: amount of grid cells that can be visited in one time step (1 day); values should be integers > 0 +* start_date: first day of simulation; format should be "YYYY-MM-DD" +* end_date: last day of simulation; format should be "YYYY-MM-DD" +* env_folder: folder path to food map data +* owf_effect: provides the option to run simulations with a specific offshore windfarm effect; "no" = no offshore windfarm effect, "avoid" = going to grid cells with windfarms less likely (based on avoidance value), "no_for" = going to grid cells with windfarms less likely (based on avoidance value) + no foraging inside windfarms (= costs of going into windfarm) +* energy_in: option to change distribution of initial energy; "cons" = initial energy is the value as provided in init_energy and the same for all agents, "left" = initial energy is variable between agents and follows a left skewed distribution +* history_made: option to get history per agent with information such as energy levels, food consumed, status (alive or dead), number of grid cells visited for each time step between start and end date; can be "yes" or "no" +* output_perc: if history_made = "yes", the number of agents for which a history is produced can be determined here (e.g. choosing lower value to reduce computational capacity needed for simulation); values can be decimals from 0 to 1 + +## Run the model +Prior to running the model a selection of R-packages needs to be installed. The respective package names can be found in scr/libs.R. + +The file run_HALOMAR.R contains an example code that can be used to run HALOMAR. For this, it is important to first set up the working directory and to have all needed files and scripts used available in the scr and data folders. Both the parameter values used in HALOMAR and the directories referred to in run_HALOMAR.R can be customized. + +The first part of run_HALOMAR.R, "0. Test run", is intended for an initial check to test whether all prerequisites to run HALOMAR are met, e.g. all packages installed, all functions loaded, correct directories provided. + +In the second part, "1. Calibration", the metabolic estimate is calibrated using the bisection() function of the NLRoot package. Note that for each parameter setting tested a separate calibration should be done. The calibrations for HALOMAR are normally done using a no-OWF scenario. + +The last part, "2. Effect simulation", provides a code example for model runs without and with OWF effects, and an example on how to check the respective survival at the last time step. + + +## Default parameters + +Parameter | Value | Unit | Source | +------------------------ |------ | ------- | ------ | +Survival | 0.949 | - | Soudijn et al. (2022), Reynolds et al. (2011), Harris et al. (2007), Sandvik et al. (2005), Meade et al. (2013) | +Avoidance OWF | 0.98 | - | Krijgsveld et. al. (2011), Furness (2016) | +Foraging activity (T_t) | 6.5 | h/d | Dunn et al. (2020) | +Travelling speed (φ) | 5 | km/h | Amélineau et al. (2021) | +Foraging time (T_f) | 2.5 | h/d | Dunn et al. (2020) | +Grid cell dimensions (σ) | 10x10 | km/h | Waggitt et al. (2019) | +Cells max. visitable (c) | 4 | cells/d | calculated (see below) | + +Cells visitable per day: + +c = (T_t φ)/σ + +c = (6.5h/d×5km/h)/10km = 3.25 cells/d + + +## References +* Amélineau, F., Merkel, B., Tarroux, A., Descamps, S., Anker-Nilssen, T., Bjørnstad, O., ... & Strøm, H. (2021). Six pelagic seabird species of the North Atlantic engage in a fly-and-forage strategy during their migratory movements. Marine Ecology Progress Series, 676, 127-144. https://doi.org/10.3354/meps13872 +* Dunn, R. E., Wanless, S., Daunt, F., Harris, M. P., & Green, J. A. (2020). A year in the life of a North Atlantic seabird: behavioural and energetic adjustments during the annual cycle. Scientific Reports, 10(1), 5993. https://doi.org/10.1038/s41598-020-62842-x +* Furness, B. (2016). Qualifying impact assessments for selected seabird populations: A review of recent literature and understanding. MacArthur Green Report: Report commissioned by Vattenfall, Statkraft and ScottishPower Renewables. https://doi.org/10.13140/RG.2.1.4457.6407 +* Harris, M. P., Frederiksen, M., & Wanless, S. (2007). Within- and between-year variation in the juvenile survival of Common Guillemots Uria aalge. Ibis, 149(3), 472-481. https://doi.org/10.1111/j.1474-919X.2007.00667.x +* Meade, J., Hatchwell, B. J., Blanchard, J. L., & Birkhead, T. R. (2013). The population increase of common guillemots Uria aalge on Skomer Island is explained by intrinsic demographic properties. Journal of avian biology, 44(1), 055-061. https://doi.org/10.1111/j.1600-048X.2012.05742.x +* Reynolds, T. J., Harris, M. P., King, R., Swann, R. L., Jardine, D. C., Frederiksen, M., & Wanless, S. (2011). Among‐colony synchrony in the survival of Common Guillemots Uria aalge reflects shared wintering areas. Ibis, 153(4), 818-831. https://doi.org/10.1111/j.1474-919X.2011.01149.x +* Sandvik, H., Erikstad, K. E., Barrett, R. T., & Yoccoz, N. G. (2005). The effect of climate on adult survival in five species of North Atlantic seabirds. Journal of Animal Ecology, 74(5), 817-831. https://doi.org/10.1111/j.1365-2656.2005.00981.x +* Soudijn, F. H., van Donk, S., Leopold, M. F., van der Wal, J. T., & Hin, V. (2022). Cumulative population-level effects of habitat loss on seabirds ‘Kader Ecologie en Cumulatie 4.0’. https://doi.org/10.18174/565601 +* Waggitt, J. J., Evans, P. G., Andrade, J., Banks, A. N., Boisseau, O., Bolton, M., ... & Hiddink, J. G. (2020). Distribution maps of cetacean and seabird populations in the North‐East Atlantic. Journal of Applied Ecology, 57(2), 253-269. https://doi.org/10.1111/1365-2664.13525 diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_1.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_1.rds new file mode 100644 index 0000000000000000000000000000000000000000..e4e8bcc497879f2bf59622302439453943450213 Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_1.rds differ diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_10.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_10.rds new file mode 100644 index 0000000000000000000000000000000000000000..1316cfec27477c314c51496dcd3860173f83054f Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_10.rds differ diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_11.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_11.rds new file mode 100644 index 0000000000000000000000000000000000000000..503c002f05b46a444425ac411cb5914169e2b502 Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_11.rds differ diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_12.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_12.rds new file mode 100644 index 0000000000000000000000000000000000000000..c0ce1ce4fd380a33d8d48400d60cf4c3812f04c8 Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_12.rds differ diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_2.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_2.rds new file mode 100644 index 0000000000000000000000000000000000000000..7fc9a07a486e65ee79722cb084f1b5d8b32d60d5 Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_2.rds differ diff --git a/halomar/scripts/data/sc_data_OWF_CommonGuillemot_9.rds b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_9.rds new file mode 100644 index 0000000000000000000000000000000000000000..6e758ecea6efe39f1bcd29efee5703fa0d89d890 Binary files /dev/null and b/halomar/scripts/data/sc_data_OWF_CommonGuillemot_9.rds differ diff --git a/halomar/scripts/run_HALOMAR.R b/halomar/scripts/run_HALOMAR.R new file mode 100644 index 0000000000000000000000000000000000000000..d22a4d9c45f7a7332b9b2b484f145d2ebac4c8c5 --- /dev/null +++ b/halomar/scripts/run_HALOMAR.R @@ -0,0 +1,128 @@ +################################################################################ +# +# Project : RWS HALOMAR +# +# Title : run_HALOMAR.R +# +# Description : run HALOMAR (habitat loss model marine birds) +# +# Date created: 2023-10-30 +# +# Authors : Floor Soudijn, Eleni Melis +# +# Wageningen Marine Research +# +# R version 4.3.1 (2023-06-16 ucrt); RStudio version 2023.9.1.494 +# +################################################################################ + +# Directories ------------------------------------------------------------------ +## set the working directory to the source file location! +dir.scr = "scr/" +dir.guillemot.sc = "data/" + +# Libraries -------------------------------------------------------------------- +#source("setup_packages.R") ## run this only the first time you run the code + +source(paste0(dir.scr, "libs.R")) + +# Functions -------------------------------------------------------------------- +source(paste0(dir.scr, "runmodel_HALOMAR.R")) + +#------------------------------------------------------------------------------- +# 0. Test run #### +#------------------------------------------------------------------------------- + +modeldata = setupModel(nb_agents = 100, + avoidance = 0.98, + metabolism = 0.683, + survival = 0.949, + init_energy = 10, + avoid_patch = 0.25, + cell_day = 4, + start_date = "2022-10-01", + end_date = "2022-11-01", + env_folder = dir.guillemot.sc, + owf_effect = "no", + energy_in = "left", + history_made = "yes", + output_perc = 1) +simulation = runModel(modeldata) + +## Check: Model output ---------------------------------------------------- +summary(simulation[["history"]]) + +## Energy over time +plot(simulation$history$date, simulation$history$energy, pch = 19, col = rgb(0, 0, 255, max = 255, alpha = 125, names = "blue50"), xlab = "date", ylab= "energy") + +boxplot(energy~date, data = simulation$history, frame = F) + +#------------------------------------------------------------------------------- +# 1. Calibration #### +#------------------------------------------------------------------------------- + +do_simulations <- function(meta) +{ + modeldata = setupModel(nb_agents = 100, + avoidance = 0.98, + metabolism = meta, + survival = 0.949, + init_energy = 10, + avoid_patch = 0.05, + cell_day = 1, + start_date = "2022-10-01", + end_date = "2023-01-01", + env_folder = dir.guillemot.sc, + owf_effect = "no", + energy_in = "left", + history_made = "no", + output_perc = 1) + + simulation = runModel(modeldata) + + return(modeldata$survival - simulation$survival) +} + +meta_cali = bisection(f = do_simulations, a = 0, b = 1, tol = 0.001, m = 100) + +#------------------------------------------------------------------------------- +# 2. Effect simulation #### +#------------------------------------------------------------------------------- + +modeldata_no_owf = setupModel(nb_agents = 100, + avoidance = 0.98, + metabolism = meta_cali, + survival = 0.949, + init_energy = 10, + avoid_patch = 0.05, + cell_day = 1, + start_date = "2022-10-01", + end_date = "2023-01-01", + env_folder = dir.guillemot.sc, + owf_effect = "no", + energy_in = "left", + history_made = "yes", + output_perc = 0.1) + +no_owf = runModel(modeldata_no_owf) +no_owf +no_owf$survival + +modeldata_with_owf = setupModel(nb_agents = 100, + avoidance = 0.98, + metabolism = meta_cali, + survival = 0.949, + init_energy = 10, + avoid_patch = 0.05, + cell_day = 1, + start_date = "2022-10-01", + end_date = "2023-01-01", + env_folder = dir.guillemot.sc, + owf_effect = "avoid", + energy_in = "left", + history_made = "yes", + output_perc = 0.1) + +with_owf = runModel(modeldata_with_owf) +with_owf +with_owf$survival diff --git a/halomar/scripts/scr/libs.R b/halomar/scripts/scr/libs.R new file mode 100644 index 0000000000000000000000000000000000000000..7a13c4b3c55c1d434fd1f1430c6ef2b6749a67db --- /dev/null +++ b/halomar/scripts/scr/libs.R @@ -0,0 +1,28 @@ +################################################################################ +# +# Project : RWS HALOMAR +# +# Title : libs.R +# +# Description : load libraries +# +# Date created: 2023-10-30 +# +# Authors : Floor Soudijn, Eleni Melis +# +# Wageningen Marine Research +# +# R version 4.3.1 (2023-06-16 ucrt); RStudio version 2023.9.1.494 +# +################################################################################ + +# Load libraries --------------------------------------------------------------- +library(raster) +library(lattice) +library(tidyverse) +library(cmna) +library(NLRoot) +library(lubridate) +library(readxl) +library(ggrepel) + diff --git a/halomar/scripts/scr/runmodel_HALOMAR.R b/halomar/scripts/scr/runmodel_HALOMAR.R new file mode 100644 index 0000000000000000000000000000000000000000..e41426f531439c534cec5299453c0a549249ebb9 --- /dev/null +++ b/halomar/scripts/scr/runmodel_HALOMAR.R @@ -0,0 +1,347 @@ +################################################################################ +# +# Project : RWS HALOMAR +# +# Title : runmodel_HALOMAR.R +# +# Description : Functions used in HALOMAR (habitat loss model marine birds) +# +# Date created: 2023-10-30 +# +# Authors : Floor Soudijn, Eleni Melis +# +# Wageningen Marine Research +# +# R version 4.3.1 (2023-06-16 ucrt); RStudio version 2023.9.1.494 +# +################################################################################ + +#------------------------------------------------------------------------------- +# 1. Set Parameters and model settings #### +#------------------------------------------------------------------------------- +## Create parameter values and initial conditions with default values +setupModel <- function(nb_agents, + avoidance = 0.98, ## avoidance probability OWFs + metabolism = 0.50, ## metabolic rate + survival = 0.949, ## survival for calibration without OWFs + init_energy = 10, ## mean initial energy + avoid_patch = 0.0, ## proportion of low food patches that is avoided (0.0 means all are used) + cell_day = 3, ## nb cells used for foraging per day + start_date, ## initial date + end_date, ## end date + env_folder, ## data location + owf_effect = "no", ## OWF scenario: + ## "no" (no OWFs) + ## "avoid" (going to an OWFs less likely) + ## "no_for" (if in OWF, foraging less likely) + ## "av_eng" (avoidance and total energy less) + energy_in = "cons", ## Initial energy: + ## "cons" (constant, mean value) + ## "left" (variable left skewed) + history_made = "no", ## output written + output_perc = 1) ## proportion of output written +{ + + return (list(nb_agents = nb_agents, + avoidance = avoidance, + metabolism = metabolism, + survival = survival, + init_E = init_energy, + avoid_patch = avoid_patch, + cell_day = cell_day, + st_date = start_date, + end_date = end_date, + env_folder = env_folder, + owf_effect = owf_effect, + E_type = energy_in, + history = history_made, + output_perc = output_perc)) +} + + +#------------------------------------------------------------------------------- +# 2. Create Agents and read in Environment #### +#------------------------------------------------------------------------------- + +## Create initial agents and set initial values at timestep 0 +createAgents <- function(modeldata) { + data = data.frame(agent_id = sprintf("agent_%d", seq(1:modeldata$nb_agents)), + alive = TRUE, + ate = 0) + + ## for only partly output if needed + data$output = rbinom(modeldata$nb_agents, 1, modeldata$output_perc) + data$energy = setInitEnergy(modeldata) + + return (data) +} + +setInitEnergy <- function(modeldata){ + + if(modeldata$E_type == "left") + { + ## beta distribution + ## for left skew, shape 1 > shape 2, and shape 1 and shape 2 > 1. + + ## gives values between 0-1, so correct for that. + cor_fac = modeldata$init_E / getMeanBeta(5, 0.5) + + initE_vals = rbeta(n = modeldata$nb_agents, 5, 0.5, ncp = 0) * cor_fac + + } + if(modeldata$E_type == "cons") + { + initE_vals = modeldata$init_E + } + + return(initE_vals) +} + +## Get month specific environment +getEnv <- function(modeldata, model_day) +{ + ## Get name and location of map file + map_name = getEnvFile(modeldata, model_day) + + ## Read in the map + model_map = readRDS(map_name) + + ## Calculate probabilities and food + env = getEnvValues(modeldata, model_map) + + return(env) +} + +## Get file per month and species specific model map +getEnvFile <- function(modeldata, modelday) +{ + date = as.Date(modelday, "%Y-%m-%d") + model_month = lubridate::month(date) + + map_name = grep(paste0("_",model_month,"."), list.files(path = modeldata$env_folder, pattern = "data_OWF*", full.names = T), value = T, fixed = T) + + return(map_name) +} + +getEnvValues <- function(modeldata, env) +{ + ##env = st_drop_geometry(env[c("owf", "Fitted")]) + env_food = EnvtoFood(modeldata, env) + env_prob = EnvtoProb(modeldata, env_food) + + return(env_prob) +} + +## Translate environmental info to list with probabilities of going to the cell +EnvtoProb <- function(modeldata, env){ + + ## normally all cells are equally popular + env$prob = 1 + ## this function only holds for the case where the baseline prob is 1 + + ## In case of avoidance of OWF areas. + ## remember here that OWF areas are indicated as the value 0 on the map. + if (modeldata$owf_effect == "avoid" | modeldata$owf_effect == "av_eng") env$prob = env$prob - ((1 - env$owf) * modeldata$avoidance) + + low_food_boundary = quantile(env$Food, modeldata$avoid_patch) + + if(sum(env$Food < low_food_boundary) > 0) env[env$Food < low_food_boundary,]$prob = 0 + + return(env) +} + +EnvtoFood <- function(modeldata, env) +{ + env$Food = env$Fitted + + ## if OWF avoidance affects the food densities for all individuals + if (modeldata$owf_effect == "av_eng") + { + ## energy lost as total food density in OWF areas, multiplied by chance of avoidance + energy_lost = sum(env[env$owf == 0,]$Fitted)*(modeldata$avoidance) + + total_energy = sum(env$Fitted) + + prop_lost = energy_lost/total_energy + + env[env$owf == 1, ]$Food = env[env$owf == 1, ]$Food * (1 - prop_lost) + } + + return(env) +} + +#------------------------------------------------------------------------------- +# 4. Setup Model, output structure #### +#------------------------------------------------------------------------------- + +## Run model with random movement but avoidance of OWFs +runModel <- function(modeldata) { + agents = createAgents(modeldata) + if(modeldata$history == "yes") history = createHistory() + + model_day = as.Date(modeldata$st_date) + + while (model_day < as.Date(modeldata$end_date)) { + + env = getEnv(modeldata, model_day) + env_last_day = getLastMapDay(model_day, modeldata) + + while (model_day <= env_last_day) { + + for(j in 1:modeldata$cell_day) + { + + agents_alive = agents[agents$alive == "TRUE", ] + + ## Draw random samples without replacement for agents + for (i in sample(agents_alive$agent_id, replace = F)) { + + agent = agents[agents["agent_id"] == i,] + + position = chooseRandomPosWeighted(env) + + agent$ate = forage(position, env, modeldata)/modeldata$cell_day + agent$energy = energyBudget(agent, modeldata) + + if (agent$energy <= 0) { + agent$alive = FALSE + } + + agents[agents["agent_id"] == i,] = agent + + ## Create output for agents that are sampled + if(modeldata$history == "yes") + { + + if(agent$output == 1) + { + agent$date = model_day + agent$step = j + + history[nrow(history) + 1, ] = agent + }} + + } + } + model_day = model_day + 1 + } + } + + output = list(survival = sum( + agents$alive == "TRUE") / modeldata$nb_agents, cells_day = modeldata$cell_day, period = c(modeldata$st_date, modeldata$end_date)) + + if(modeldata$history == "yes") output$history = history + + return (output) +} + +## Create output structure +createHistory <- function() { + data = data.frame( + agent_id = character(), + alive = logical(), + ate = numeric(), + output = numeric(), + energy = numeric(), + date = as.POSIXct(character()), + step = numeric()) + return(data) +} + +#------------------------------------------------------------------------------- +# 5. Model processes #### +#------------------------------------------------------------------------------- +## Weighted random movement +chooseRandomPosWeighted <- function(idxsprob) { + + ## choose row in data frame + pos = sample(x = nrow(idxsprob), + size = 1, + prob = idxsprob$prob) + return (pos) +} + +## Forage function +forage <- function(position, env, modeldata) { + + intake = env[position, "Food"] + + ## In case OWF avoidance leads to less foraging. + ## OWF areas are indicated as the value 0 on the map. + if (modeldata$owf_effect == "no_for" && env[position,"owf"] == 0) intake = intake * rbinom(1, 1, (1 - modeldata$avoidance)) + + return (intake) +} + +## Energy budget function +energyBudget <- function(agent, modeldata) { + + energy = newCondition(agent, modeldata) + + energy = max(energy, 0) # find max between 0 and energy to avoid negative values + + return (energy) +} + +newCondition <- function(agent, modeldata) +{ + new_energy = agent$energy + agent$ate - (modeldata$metabolism / modeldata$cell_day) + + return(new_energy) +} + +#------------------------------------------------------------------------------- +# 6. Helper Functions #### +#------------------------------------------------------------------------------- +getmonth <- function(date){ + + date = as.Date(date, "%Y-%m-%d") + model_month = lubridate::month(date) + + return(model_month) + +} + +## Beta distribution +getMeanBeta <- function(shape1, shape2) +{ + mean = shape1 / (shape1 + shape2) + + return(mean) +} + +getVarBeta <- function(shape1, shape2) +{ + var = shape1*shape2 / ((shape1 + shape2)^2 *(shape1 + shape2 + 1)) + + return(var) +} + +getSh2Beta <- function(var, shape1) +{ + getVarBeta(var) +} + +## Get the last day for the map +getMapDays <- function(modelday, modeldata) +{ + date = as.Date(modelday, "%Y-%m-%d") + + last_map_day = getLastMapDay(modelday, modeldata) + + map_days = seq(as.Date(date),as.Date(last_map_day), by = "days") + + return(map_days) +} + +getLastMapDay <- function(modelday, modeldata) +{ + date = as.Date(modelday, "%Y-%m-%d") + last_map_day = ceiling_date(date, "month") - days(1) + last_map_day = min(as.Date(last_map_day), as.Date(modeldata$end_date)) + + return(last_map_day) +} + + +# END -------------------------------------------------------------------------- \ No newline at end of file