## Define a function to simulate age at death in a Gombertz model where the a 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. #This one ignores trade-off2 and uses only trade-off 1 live1 <- function(i = 0, t_o1s = t_o1s, t_o2s = t_o2s, t_o3s = t_o3s, t_o4s = t_o4s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, t_o3_wt = t_o3_wt, t_o4_wt = t_o4_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 TO 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, t_o3s, t_o4s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } #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 = 0, t_o1s = t_o1s, t_o2s = t_o2s, t_o3s = t_o3s, t_o4s = t_o4s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, t_o3_wt = t_o3_wt, t_o4_wt = t_o4_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, t_o3s, t_o4s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } #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 = 0, t_o1s = t_o1s, t_o2s = t_o2s, t_o3s = t_o3s, t_o4s = t_o4s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, t_o3_wt = t_o3_wt, t_o4_wt = t_o4_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/3)*cc)/t_o1_wt + ((t_o2s + yy/3)*cc)/t_o2_wt + ((t_o3s + yy/3)*cc)/t_o3_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 + -t_o3s*t_o3_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, t_o3s, t_o4s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) } #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 = 0, t_o1s = t_o1s, t_o2s = t_o2s, t_o3s = t_o3s, t_o4s = t_o4s, t_o1_wt = t_o1_wt, t_o2_wt = t_o2_wt, t_o3_wt = t_o3_wt, t_o4_wt = t_o4_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/4)*cc)/t_o1_wt + ((t_o2s + yy/4)*cc)/t_o2_wt + ((t_o3s + yy/4)*cc)/t_o3_wt + ((t_o4s + yy/4)*cc)/t_o4_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 + -t_o3s*t_o3_wt + -t_o4s*t_o4_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, t_o3s, t_o4s, bs, a_ds, ferts, rep_sucs, t_os, probs) return(res) }