Get started with a template
Stock-and-flow models can be created in three ways in sdbuildR.
Firstly, dozens of example models can be loaded using
xmile(). Plot the stock-and-flow diagram to get an overview
of the model:
The model can be simulated using simulate():
Import models from Insight Maker
Secondly, an Insight Maker model can be imported to R using its URL:
URL <- "https://insightmaker.com/insight/5LxQr0waZGgBcPJcNTC029/Crielaard-2022"
sfm <- insightmaker_to_sfm(URL = URL)
sim <- simulate(sfm)
plot(sim)Build a model from scratch
Lastly, a stock-and-flow model can be created from scratch. We first
initialize an empty stock and flow model with xmile(), and
then use build() to create a logistic model of population
growth. The simulation specifications, such as the start time, stop
time, simulation time step (dt), and time units, are set
with sim_specs(). Below, we make use of the convenient pipe
operator |>, which simply passes the result of an
expression to the next expression as a first argument.
sfm <- xmile() |>
header(name = "Population growth") |>
build("X", "stock", eqn = "100", label = "Population size") |>
build("change", "flow",
eqn = "r * (1 - X/K)", to = "X",
label = "Births and Deaths"
) |>
build("r", "constant", eqn = "5", label = "Growth rate") |>
build("K", "constant", eqn = "10000", label = "Carrying capacity") |>
sim_specs(stop = 10000, time_units = "days")
sim <- simulate(sfm)
plot(sim)