run_simple | R Documentation |
A generic function to run a simulation loop for a fixed period of time.
run_simple()
will call step_function(initial.pop, ...)
over
and over until the time ends or step_function()
reports that the
experiment has ended.
run_simple(step_function, initial.pop, end.time, ...)
step_function |
Function to run a timestep ( |
initial.pop |
Initial population data frame with columns corresponding
to function requirements. This *must* include a |
end.time |
End time of simulation |
... |
(optionally) any other arguments for |
Data frame containing population history of simulation over time
run_simulation
if you want a more flexible version
of this function that will allow your step_function()
to return just a
data frame and will print some debugging information on request.
growth <- function(latest.df, growth.rate) { current.count <- latest.df$count growth.num <- current.count * growth.rate next.count <- current.count + growth.num next.time <- latest.df$time + 1 new.df <- data.frame(time=next.time, count=next.count) finished <- next.count == 0 list(updated.pop=new.df, end.experiment=finished) } df <- data.frame(time=0, count=1) results <- run_simple(growth, df, 100, growth.rate = 0.1) plot_populations(results)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.