# define a population matrix (columns move to rows)
nclass <- 5
popmat <- matrix(0, nrow = nclass, ncol = nclass)
popmat[reproduction(popmat, dims = 4:5)] <- c(10, 20)
popmat[transition(popmat)] <- c(0.25, 0.3, 0.5, 0.65)
# define a dynamics object
dyn <- dynamics(popmat)
# remove up to 10 individuals from stages 4 and 5 prior to the
# matrix update
removals <- add_remove_pre(
masks = all_classes(popmat, dims = 4:5),
funs = \(x) ifelse(x > 10, x - 10, 0)
)
# update the dynamics object
dyn <- update(dyn, removals)
# simulate trajectories
sims <- simulate(dyn, nsim = 100, options = list(ntime = 50))
# and plot
plot(sims)
# remove up to 10 individuals from stages 4 and 5 after to the
# matrix update
removals <- add_remove_post(
masks = all_classes(popmat, dims = 4:5),
funs = \(x) ifelse(x > 10, x - 10, 0)
)
# update the dynamics object (can't update because that will
# include the add_remove_pre as well)
dyn <- dynamics(popmat, removals)
# simulate trajectories
sims <- simulate(dyn, nsim = 100, options = list(ntime = 50))
# and plot
plot(sims)
Run the code above in your browser using DataLab