Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Deterministic multistage Beverton-Holt model using nested for loops in R
years = 30
prop_female = 0.5
egg_surv = 0.6
fecundity = c(0, 0, 200, 400, 800)
survival = c(0.2, 0.4, 0.6, 0.8, 0)
capacity = c(1e6, 1e5, 1e4, 1e3, 1e2)
beverton_holt <- function(N, p, c){
N / ((1/p) + (N/c))
}
results = matrix(data = 0, nrow = years, ncol = length(fecundity))
results[1,] = 10
for (i in 1:(nrow(results) - 1)){
for (j in 1:(ncol(results))){
N = results[i,j]
fec_age_j = fecundity[j]
surv_age_j = survival[j]
# spawning
if (fec_age_j > 0){
spawn_age_j = N * prop_female
results[i+1, 1] = results[i+1, 1] + beverton_holt(spawn_age_j, fec_age_j * egg_surv, capacity[1] - results[i+1, 1])
}
# survival
if (surv_age_j > 0){
results[i+1, j+1] = results[i+1, j+1] + beverton_holt(N, surv_age_j, capacity[j+1] - results[i+1, j+1])
}
}
}
results
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment