set.seed(42) # set seed for repeatability
# Basic operation, without splitting matrices and with no constraints
rand_lefko_set(
n_models = 3, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
archetype = 4, output = "Type5"
)
# Constrain outputs to A matrices with lambda between 0.9 and 1.1
library(popbio)
constrain_df <- data.frame(
fun = "lambda", arg = NA, lower = 0.9, upper =
1.1
)
rand_lefko_set(
n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
archetype = 4, constraint = constrain_df, output = "Type5"
)
# As above, but using popdemo::eigs function instead of popbio::lambda
# to illustrate use of argument
library(popdemo)
constrain_df <- data.frame(
fun = "eigs", arg = "lambda", lower = 0.9, upper = 1.1
)
rand_lefko_set(
n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
archetype = 4, constraint = constrain_df, output = "Type5"
)
# Multiple constraints
# Constrain outputs to A matrices with lambda between 0.9 and 1.1, generation
# time between 3 and 5 and damping ratio between 1 and 7.
library(popbio)
constrain_df <- data.frame(
fun = c("lambda", "generation.time", "damping.ratio"),
arg = c(NA, NA, NA),
lower = c(0.9, 3.0, 1.0),
upper = c(1.1, 5.0, 7.0)
)
rand_lefko_set(
n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
archetype = 4, constraint = constrain_df, output = "Type5"
)
# Constraints based on user-defined functions are also possible...
# User-defined function to calculate survival from the first stage
simpleFun <- function(x) {
sum(x[, 1])
}
# Define the constraint, based on the user-defined function
constrain_df <- data.frame(
fun = "simpleFun", arg = NA, lower = 0.75, upper = 1
)
rand_lefko_set(
n_models = 10, n_stages = 5, fecundity = c(0, 0, 4, 8, 10),
archetype = 4, output = "Type5", constraint = constrain_df
)
Run the code above in your browser using DataLab