###This file defines the source functions used for each of the four trade-off models as implemented in the main model ## Define a function to simulate age at death in a Gompertz model where the b parameter varies G_mort_b <- function(G_aa = G_aa, bbs = bbs){ age_death <- round(rgompertz(length(bbs), shape = bbs, rate = G_aa)) return(age_death) } ####Define a function to calculate lifespans, fertilities, and reproductive successes for each trade-off model. ##The single-trade-off model #This one ignores trade-off2 and uses only trade-off 1 live1 <- function(i = i, t_o1s = t_o1s, t_o2s = t_o2s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, sig1 = sig1, b0 = b0, maxfert = maxfert, n_inds = n_inds, aa = aa, cc = cc, yy = yy) { x <- t_o2s/t_o2_wt #Does nothing; Just to use the parameters for consistency t_os <- t_o1s #The full trade-off is trade-off 1 in this model t_o_wt <- t_o1_wt #Trade-off weight fot T-) 1 #The b is multiplied by the exponent of the trade-off adjusted by its weight bs <- b0 + aa*exp((t_os + yy)*cc/t_o_wt) #Age at death is calculated based on a Gompertz model a_ds <- G_mort_b(G_aa = G_aa, bbs = bs) #Fertility is perturbed like b. Constant at all ages, bounded between zero and a maximum ferts <- maxfert/(1 + exp(-t_os*t_o_wt)) #Reproductive success is the product of fertility and age at death, with a random perturbation rep_sucs <- pmax(0, ferts*a_ds + rnorm(n_inds, 0, ferts*a_ds*sig1)) #This makes the reproductive success sum to 1 so we can use it as a weight for sampling the next generation probs <- rep_sucs/sum(rep_sucs) #Output results res <- cbind(t_o1s, t_o2s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } ##The "Additive" model #This model multiplies the exponential of the trade-offs #The two trade-offs interact by both applying their effect to fertility and mortality #They can thus be either antagonistic or synergistic depending on their directions (weights) live2 <- function(i = i, t_o1s = t_o1s, t_o2s = t_o2s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, sig1 = sig1, b0 = b0, maxfert = maxfert, n_inds = n_inds, aa = aa, cc = cc, yy = yy) { t_os <- t_o1s + t_o2s #Does nothing; Just to use the parameters for consistency bs <- b0 + aa*exp(((t_o1s + yy/2)*cc)/t_o1_wt + ((t_o2s + yy/2)*cc)/t_o2_wt) a_ds <- G_mort_b(G_aa = G_aa, bbs = bs) ferts <- maxfert/(1 + exp(-t_o1s*t_o1_wt + -t_o2s*t_o2_wt)) rep_sucs <- pmax(0, ferts*a_ds + rnorm(n_inds, 0, ferts*a_ds*sig1)) probs <- rep_sucs/sum(rep_sucs) res <- cbind(t_o1s, t_o2s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } ##The "Maximum" model #This model chooses the larger of the two trade-offs in either direction #Each individual can have a different trade-off from the others live3 <- function(i = i, t_o1s = t_o1s, t_o2s = t_o2s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, sig1 = sig1, b0 = b0, maxfert = maxfert, n_inds = n_inds, aa = aa, cc = cc, yy = yy) { t_os <- ifelse(abs(t_o1s) > abs(t_o2s), t_o1s, t_o2s) #Choose which trade based on the one with the larger absolute effect in either direction t_o_wts <- ifelse(abs(t_o1s) > abs(t_o2s), t_o1_wt, t_o2_wt) bs <- b0 + aa*exp((t_os + yy)*cc/t_o_wts) a_ds <- G_mort_b(G_aa = G_aa, bbs = bs) ferts <- maxfert/(1 + exp(-t_os*t_o_wts)) rep_sucs <- pmax(0, ferts*a_ds + rnorm(n_inds, 0, ferts*a_ds*sig1)) probs <- rep_sucs/sum(rep_sucs) res <- cbind(t_o1s, t_o2s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } ##The "Minimum" model #This model chooses the smaller of the two trade-offs in either direction #Each individual can have a different trade-off from the others live4 <- function(i = i, t_o1s = t_o1s, t_o2s = t_o2s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, sig1 = sig1, b0 = b0, maxfert = maxfert, n_inds = n_inds, aa = aa, cc = cc, yy = yy) { t_os <- ifelse(abs(t_o1s) < abs(t_o2s), t_o1s, t_o2s) #Choose which trade based on the one with the larger absolute effect in either direction t_o_wts <- ifelse(abs(t_o1s) < abs(t_o2s), t_o1_wt, t_o2_wt) bs <- b0 + aa*exp((t_os + yy)*cc/t_o_wts) a_ds <- G_mort_b(G_aa = G_aa, bbs = bs) ferts <- maxfert/(1 + exp(-t_os*t_o_wts)) rep_sucs <- pmax(0, ferts*a_ds + rnorm(n_inds, 0, ferts*a_ds*sig1)) probs <- rep_sucs/sum(rep_sucs) res <- cbind(t_o1s, t_o2s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) }