Here we present a simple example for running a Morris Sensitivity Analysis with nlrx. We use the Wolf Sheep Predation model from the models library for this example.
library(nlrx)
# Windows default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("C:/Program Files/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("C:/out")
# Unix default NetLogo installation path (adjust to your needs!):
netlogopath <- file.path("/home/NetLogo 6.0.3")
modelpath <- file.path(netlogopath, "app/models/Sample Models/Biology/Wolf Sheep Predation.nlogo")
outpath <- file.path("/home/out")
nl <- nl(nlversion = "6.0.3",
nlpath = netlogopath,
modelpath = modelpath,
jvmmem = 1024)
In this example, we want to calculate sensitivity for 3 outputs (number of sheep, number of wolves, number of grass patches). We vary all numeric model parameters to estimate their sensitivity on the three defined output metrics. Thus, we define parameter ranges and distribution functions for all our numeric model parameters. We set the runtime of the model to 500 ticks and measure our metrics on each tick (evalticks = "true"
). However, for calculation of sensitivity indices, we only want to consider the last 200 ticks. Thus, we set evalticks to seq(300,500)
.
nl@experiment <- experiment(expname = "wolf-sheep-morris",
outpath = outpath,
repetition = 1,
tickmetrics = "true",
idsetup = "setup",
idgo = "go",
runtime = 500,
evalticks = seq(300,500),
metrics=c("count sheep", "count wolves", "count patches with [pcolor = green]"),
variables = list("initial-number-sheep" = list(min=50, max=150, step=10, qfun="qunif"),
"initial-number-wolves" = list(min=50, max=150, step=10, qfun="qunif"),
"grass-regrowth-time" = list(min=0, max=100, step=10, qfun="qunif"),
"sheep-gain-from-food" = list(min=0, max=50, step=10, qfun="qunif"),
"wolf-gain-from-food" = list(min=0, max=100, step=10, qfun="qunif"),
"sheep-reproduce" = list(min=0, max=20, step=5, qfun="qunif"),
"wolf-reproduce" = list(min=0, max=20, step=5, qfun="qunif")),
constants = list("model-version" = "\"sheep-wolves-grass\"",
"show-energy?" = "false"))
We use the simdesgin_morris()
function to attach a Morris Sensitivity Analysis design. The morrislevels
parameter sets the number of different values for each parameter (sampling density). The morrisr
paramater sets the number of repeated samplings (sampling size). The morrisgridjump
parameter sets the number of levels that are increased/decreased for computing the elementary effects. Morris recommendation is to set this value to levels / 2
. We can increase the nseeds
parameter in order to perform multiple runs of the same parameter matrix with different random seeds. The variation between those repetitions is an indicator of the stochasticity effects within the model. More information on the Morris specific parameters can be found in the description of the morris function in the sensitivity package (?morris
).
To execute the simulations, we can use the function run_nl_all()
. Sensitivity analyses typically have many runs that need to be simulated, thus we recommend to parallelize model runs by adjusting the future plan (more details on parallelization can be found in the “Advanced configuration” vignette).
First, we need to attach the results to the nl object.
After results have been attached, we can use the analyze_nl()
function to calculate morris sensetivity indices.