Skip to content

Instantly share code, notes, and snippets.

@hinkelman
Created February 10, 2019 07:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hinkelman/d5b8414b0c6383057d7846509a724bbf to your computer and use it in GitHub Desktop.
Save hinkelman/d5b8414b0c6383057d7846509a724bbf to your computer and use it in GitHub Desktop.
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