Sensitivity Analysis

Jan Salecker

2019-12-03

Sensitivity Analysis with nlrx

Different types of sensitivity analyses can be conducted using the nlrx package. To perform a local sensitivity analysis, we recommend using the simdesign_distinct() to specify local changes of parameters. Afterwards the proportion of output change can be easily calculated from the simulation results. The nlrx package also provides simdesign helper functions to conduct more sophisticated methods such as Morris Elementary Effects Screening (simdesign_morris()), Sobol variance decomposition (simdesign_sobol(), simdesign_sobol2007(), simdesign_soboljansen()) and Extended Fourier amplitude sensitivity test (simdesign_eFAST). Additionally, output of Latin Hypercube Sampling designs (simdesign_lhs()) can be used to calculate parameter effects based on Partial (rank) correlation coefficients or Standardised (rank) regression coefficients.

In this vignette, we present an example of the Morris Elementary Effects screening. Other sensitivity analyses simdesigns work in a quite similar way. Details on the specific methods can be found in the corresponding simdesign help pages and the documentation of the sensitivity package. The second example shows how Latin Hypercube Sampling can be used to calculate Partial (rank) correlation coefficients and Standardised (rank) regression coefficients.

Example 1: Morris elementary effects screening

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.

Step 2: Attach an experiment

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).

Step 3: Attach a simulation design

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).

Step 4: Run simulations

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).

Step 5: Investigate output

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.

Example 2: Latin Hypercube Sampling

Here we perform a Latin Hypercube Sampling to calculate Partial (rank) correlation coefficients and Standardised (rank) regression coefficients.

Step 3: Attach a simulation design

Here we want to run a Latin Hypercube Sampling, thus we use the simdesign_lhs() function.

Step 4: Run simulations

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).

Step 5: Investigate output

First, we need to attach the results to the nl object.

After results have been attached, we need to post-process our data to run the pcc and src function of the sensitivity package. We first take our parameter matrix (siminput) and select only columns with variable parameters and drop all other columns. We also need to rename the columns because pcc and src do not support special characters (-) in column names.

Our simulation results are measured for each tick, thus we first need to aggregate our output. Here we just calculate the mean and standard deviation of outputs for each random-seed and siminputrow combination. Afterwards, we drop the random seed and siminputrow columns and rename the columns to remove special characters.

Finally, we use both datasets to run the pcc and src functions. These functions can only compute coefficients for one output at a time. Thus, we nested the function call inside a purrr::map() function that iterates over the column names of our output tibble.

The results are reported as a nested list, where each outer element represents one of the calculated model outputs. The inner list items represent the different outputs from the pcc and src functions.

We can for example look at the pcc results of one specific output by using the basic plot function:

We can also extract all the data to a tidy data format and create nice plots with the ggplot package: