###This model is identical to the main model, #except that it lets the same model (or variations on it) be iterated multiple times ##n_its sets the number of iterations #In order to vary the parameters from one iteration to another, simply use a function of n_its #instead of a constant to define the parameter in question ##This code was the base of most sensitivity analyses #Load required packages require(corrplot) require(flexsurv) #Set the working directory setwd("") ###Insert your working directory path here source("S2_File.r") ########Set parameters n_its <- 3 #Number of times to repeat the model #Population size and generations n_inds <- 10000 #Population size n_gens <- 500 #Number of generations ###Trade-off parameters #The weight of the trade-off to favor fertility versus survival. #Higher values ( > 1) favor fertility, lower (<1) favor survival #Should be positive, 1 is "neutral" (conserves default fertility and survival) #t_o1 is trade-off 1, t_o2 is trade-off 2 (only t_o1 is used in the single-trade-off model) t_o1_wt <- 0.43 #W1 in article Table 1, Eqns (3) - (6) t_o2_wt <- 0.1 #W2 in article Table 1, Eqns (3) - (6) #Parameters relating to stochasticity of TO values herit <- 0.95 #Heritability, loosely defined (h in article Table 1, Eqn (S1)) sig_to <- 0.1 #Standard deviation of TOs in the first generation #Parameters for trade-off functions b0 <- 0.01 #Minimum possible aging rate, b0 in article Table 1, Eqn (3) yy <- -2 #Lag for TO in the b-TO function, gamma in article Table 1, Eqn (3) aa <- 1 #coefficient of exponent in b-TO function, fixed at 1 (neutral) except in sensitivity analyses cc <- 1 #TO division factor in b-TO function, fixed at 1 (neutral) except in sensitivity analyses #Variance (stochastic component) in reproductive success, not too important sig1 <- 0.1 #Indicated as "SD of LRS" in article Table 1, Eqn (2) #Constraint on maximum fertility (asymptotic limit) maxfert <- 1 #fmax in article Table 1, Eqn (4) #Parameters for simulating age at death in various models G_aa <- 0.08 #a parameter in the Gompertz model, when fixed, indicated as "a" in article Table 1, Eqn (1) ##The following parameters are only used in sensitivity analyses on the mortality function GM_aa <- 0.07 #a parameter in the Gompertz-Makeham model, when fixed, Eqn (S4) GM_bb <- 0.09 #b parameter in the Gompertz and Gompertz-Makeham models, when fixed, Eqn (S4) G_bb <- 0.1 #b parameter in the Gompertz model, when fixed, Eqns (1) and (S3) GM_c <- 0.01 #Gompertz-Makeham c parameter, Eqn (S4) L_a <- 0.08 #Intercept in the linear model, Eqn (S2) #Create vectors to store mean data across generations mean_vecs <- c("meant_o1s", "meant_o2s", "meanbs", "meana_ds", "meanferts", "meanr_ss") mods <- c("live1", "live2", "live3", "live4") vec <- matrix(NA, n_gens, 1) for (j in 1:length(mean_vecs)){ for (k in 1:length(mods)){ assign(paste(mean_vecs[j], k, sep = ""), vec) }} #Create matrix to store final outputs across iterations TO1_results <- matrix(NA, n_its, 4) colnames(TO1_results) <- c("m1", "m2", "m3", "m4") TO2_results <- matrix(NA, n_its, 4) colnames(TO2_results) <- c("m1", "m2", "m3", "m4") fert_results <- matrix(NA, n_its, 4) colnames(fert_results) <- c("m1", "m2", "m3", "m4") AD_results <- matrix(NA, n_its, 4) colnames(AD_results) <- c("m1", "m2", "m3", "m4") LRS_results <- matrix(NA, n_its, 4) colnames(LRS_results) <- c("m1", "m2", "m3", "m4") for (xxx in 1:n_its){ #Generate trade-offs in the first generation and first generation data using functions sourced from other file t_o1s <- rnorm(n_inds, 0, sig_to) t_o2s <- rnorm(n_inds, 0, sig_to) data1_0 <- live1(i = 0, 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) data2_0 <- live1(i = 0, 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) data3_0 <- live1(i = 0, 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) data4_0 <- live1(i = 0, 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) #Run for all generations, model1 for (i in 1:n_gens){ k <- 1 #This says it's model 1 data <- get(paste("data1_", i-1, sep = "")) #Get previous generation data tt1 <- data[, 1] #Extract trade-off 1 from previous generation tt2 <- data[, 2] #Extract trade-off 2 from previous generation pp <- data[, 8] #Extract weights based on reproductive success from previous generation t_o1s <- sample(x = tt1, size = n_inds, replace = T, prob = pp) #Sample previous generation proporional to reproductive success to obtain current generation trade-off values t_o1s <- t_o1s + rnorm(n_inds, 0, abs(t_o1s)*(1-herit)) #Perturb random as function of heritability t_o2s <- sample(x = tt2, size = n_inds, replace = T, prob = pp) #Ditto t_o2s <- t_o2s + rnorm(n_inds, 0, abs(t_o2s)*(1-herit)) #Ditto #Run function to calculate all current generation data res <- live1(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) #Save results for (j in 1:length(mean_vecs)){ vec <- get(paste(mean_vecs[j], k, sep = "")) vec[i] <- mean(res[, j]) assign(paste(mean_vecs[j], k, sep = ""), vec) } assign(paste("data1_", i, sep = ""), res) } TO1_results[xxx, 1] <- meant_o1s1[n_gens] TO2_results[xxx, 1] <- meant_o2s1[n_gens] fert_results[xxx, 1] <- meanferts1[n_gens] AD_results[xxx, 1] <- meana_ds1[n_gens] LRS_results[xxx, 1] <- meanr_ss1[n_gens] #Run for all generations, model2 for (i in 1:n_gens){ k <- 2 data <- get(paste("data2_", i-1, sep = "")) tt1 <- data[, 1] tt2 <- data[, 2] pp <- data[, 8] t_o1s <- sample(x = tt1, size = n_inds, replace = T, prob = pp) t_o1s <- t_o1s + rnorm(n_inds, 0, abs(t_o1s)*(1-herit)) t_o2s <- sample(x = tt2, size = n_inds, replace = T, prob = pp) t_o2s <- t_o2s + rnorm(n_inds, 0, abs(t_o2s)*(1-herit)) res <- live2(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) for (j in 1:length(mean_vecs)){ vec <- get(paste(mean_vecs[j], k, sep = "")) vec[i] <- mean(res[, j]) assign(paste(mean_vecs[j], k, sep = ""), vec) } assign(paste("data2_", i, sep = ""), res) } TO1_results[xxx, 2] <- meant_o1s2[n_gens] TO2_results[xxx, 2] <- meant_o2s2[n_gens] fert_results[xxx, 2] <- meanferts2[n_gens] AD_results[xxx, 2] <- meana_ds2[n_gens] LRS_results[xxx, 2] <- meanr_ss2[n_gens] #Run for all generations, model3 for (i in 1:n_gens){ k <- 3 data <- get(paste("data3_", i-1, sep = "")) tt1 <- data[, 1] tt2 <- data[, 2] pp <- data[, 8] t_o1s <- sample(x = tt1, size = n_inds, replace = T, prob = pp) t_o1s <- t_o1s + rnorm(n_inds, 0, abs(t_o1s)*(1-herit)) t_o2s <- sample(x = tt2, size = n_inds, replace = T, prob = pp) t_o2s <- t_o2s + rnorm(n_inds, 0, abs(t_o2s)*(1-herit)) res <- live3(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) for (j in 1:length(mean_vecs)){ vec <- get(paste(mean_vecs[j], k, sep = "")) vec[i] <- mean(res[, j]) assign(paste(mean_vecs[j], k, sep = ""), vec) } assign(paste("data3_", i, sep = ""), res) } TO1_results[xxx, 3] <- meant_o1s3[n_gens] TO2_results[xxx, 3] <- meant_o2s3[n_gens] fert_results[xxx, 3] <- meanferts3[n_gens] AD_results[xxx, 3] <- meana_ds3[n_gens] LRS_results[xxx, 3] <- meanr_ss3[n_gens] #Run for all generations, model4 for (i in 1:n_gens){ k <- 4 data <- get(paste("data4_", i-1, sep = "")) tt1 <- data[, 1] tt2 <- data[, 2] pp <- data[, 8] t_o1s <- sample(x = tt1, size = n_inds, replace = T, prob = pp) t_o1s <- t_o1s + rnorm(n_inds, 0, abs(t_o1s)*(1-herit)) t_o2s <- sample(x = tt2, size = n_inds, replace = T, prob = pp) t_o2s <- t_o2s + rnorm(n_inds, 0, abs(t_o2s)*(1-herit)) res <- live4(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) for (j in 1:length(mean_vecs)){ vec <- get(paste(mean_vecs[j], k, sep = "")) vec[i] <- mean(res[, j]) assign(paste(mean_vecs[j], k, sep = ""), vec) } assign(paste("data4_", i, sep = ""), res) } TO1_results[xxx, 4] <- meant_o1s4[n_gens] TO2_results[xxx, 4] <- meant_o2s4[n_gens] fert_results[xxx, 4] <- meanferts4[n_gens] AD_results[xxx, 4] <- meana_ds4[n_gens] LRS_results[xxx, 4] <- meanr_ss4[n_gens] } #write.csv(TO1_results, "100 rpt TO1.csv") #write.csv(TO2_results, "100 rpt TO2.csv") #write.csv(fert_results, "100 rpt fert.csv") #write.csv(AD_results, "100 rpt AD.csv") #write.csv(LRS_results, "100 rpt LRS.csv") #Make plots of results groups <- c("1 TO", "Additive", "Max", "Min") cols <- c("black", "red", "blue", "green") windows(height = 10, width = 15) tiff(filename = "100 repeats.tif", 9, 6, "in", res = 500, compression = "lzw") par(mfrow = c(2, 3), mar = c(3, 2, 1, 1), mgp = c(2, 0.7, 0)) boxplot(TO1_results, names = groups, horizontal = T, xlab = "Final mean TO1", col = cols) boxplot(TO2_results, names = groups, horizontal = T, xlab = "Final mean TO2", col = cols) boxplot(fert_results, names = groups, horizontal = T, xlab = "Final mean fertility", col = cols) boxplot(AD_results, names = groups, horizontal = T, xlab = "Final mean age at death", col = cols) boxplot(LRS_results, names = groups, horizontal = T, xlab = "Final mean lifetime reproductive success", col = cols) dev.off()