The hardware and bandwidth for this mirror is donated by dogado GmbH, the Webhosting and Full Service-Cloud Provider. Check out our Wordpress Tutorial.
If you wish to report a bug, or if you are interested in having us mirror your free-software or open-source project, please feel free to contact us at mirror[@]dogado.de.

Spatial Uncertainty Propagation Analysis

Case study with calling external model.

Kasia Sawicka and Dennis Walvoort

2024-01-10

Sys.sleep(100)

Case study with calling external model

Introduction

Often, environmental models are developed in other languages than R, for example C or FORTRAN. It can significantly speed up processing. In this simple example, it is shown how to perform uncertainty analysis with a model developed in a different language than R. We use an example with a basic model written in C.


Monte Carlo methodology for uncertainty analysis

The adapted uncertainty propagation analysis approach is based on the Monte Carlo method that computes the output of the model repeatedly, with input values that are randomly sampled from their pdfs. The set of model outputs forms a random sample from the output pdf. The method thus consists of the following steps:

  1. Characterise uncertain model inputs/parameters with pdfs.
  2. Repeatedly sample from the pdfs of uncertain inputs/parameters.
  3. Run model with sampled inputs and store model outputs.
  4. Compute summary statistics of model outputs.


Uncertainty propagation analysis with ‘spup’

Spatial (or other) inputs to the models are often stored in ASCII files. In that case, when using external models in R we need additional code to:

  1. Modify ASCII input files.
  2. Run the external model.

Modify ASCII input files - rendering

For rendering ASCII input files, the mustache templating framework is implemented (https://mustache.github.io). In R this is implemented in the package whisker.

Function template() allow user to define a ‘container’ class to store all templates with model inputs. The aim of this class is to organise model input files and perform necessary checks. A print() method is also provided for the class “template”.

A template is simply a model input file with:

  1. The additional extension .template.
  2. Input that needs to be modified is replaced by mustache-style tags.

For example, suppose we have a model that needs the input file: input.txt. This input file contains two parameters “b0” and “b1”. The contents of the original file may look like:

[1] "3 4"

The corresponding template file should be named as input.txt.template. It contains:

[1] "{{b0}} {{b1}}"

We can see that the original numbers are replaced by symbols b0 and b1 placed in moustaches {{ and }}.

Rendering is the process of replacing the tags in moustaches by text. For this, render-methods for class “character” and “template” are provided. For example:

[1] "Hello Winnie the Pooh. How are you doing?"

The example above calls method render() for the class “character”. It is also possible to fill an entire table:

  X Y
1 1 a
2 2 b
3 3 c
4 4 d
5 5 e
| x | y |
|---|---|
| 1 | a |
| 2 | b |
| 3 | c |
| 4 | d |
| 5 | e |

A template stored as a file will always be rendered on disk. Let’s return to our template on disk:

with contents:

[1] "{{b0}} {{b1}}"

Rendering will create a new file, called input.txt.

[1] "examples/input.txt"

As can be seen above, the path of this file is also the return value of the render method. This facilitates further processing by means of the pipe-operator:

[1] "3 4"

Running external models

An external model can be called from R by means of the system or system2 function. To facilitate this, spup includes the wrapper function executable().

Below is an example of an external model written in the C language:

You can copy this code into a text file and save with the extension “.C”. For example, dummy_model.C. This model calculates a value of a simple simple linear regression model. To compile this code to a MS-Windows executable you can use a free C compiler GCC running command gcc dummy_model.c -o dummy_model. This will create a file dummy_model.exe.

We can now wrap this model in R as follows:

Running the rendering procedure allows to pass any values for b0 ad b1 and the model gives:

To perform the uncertainty propagation analysis we need to derive multiple realizations of the model output in steps as follows:

  1. Render the template.
  2. Run the model.
  3. Read the results.
  4. Process the results.

For example:

Acknowledgements

Thanks to Dennis Walvoort for his valuable contribution to the development of the ‘spup’ package.

This project has received funding from the European Union’s Seventh Framework Programme for research, technological development and demonstration under grant agreement no 607000.

These binaries (installable software) and packages are in development.
They may not be fully stable and should be used with caution. We make no claims about them.
Health stats visible at Monitor.