Starting out with the R Sensitivity Package

The R Sensitivity Package contains sampling and analysis methods for Sobol sensitivity analysis, the Fourier amplitude sensitivity test (FAST) and Method of Morris. This post will show you how to set up this package on a Linux system and run a simple example of Sobol sampling. (Note that there are also GUIs available to run R on Windows and Mac — but this will help you run on the clusters, so it’s more scalable).

This assumes that you already have R installed. If you need more info about how to do this (it’s free!) you may want to start with this guide before proceeding.

First, go to the directory where you want to install the package. You may want to create a new directory for this specific purpose. If you’re on the clusters, type: module load R, which will, of course, load R into your current session. On other Linux systems, R might be available without loading a module.

Download the package source to this directory like so:

wget http://cran.r-project.org/src/contrib/sensitivity_1.5.tar.gz

Now you can install the package:

R CMD INSTALL -l . sensitivity_1.5.tar.gz

Notice a few things about this command. Usually when you start R, it will open an interactive session similar to Matlab’s command window. By calling R CMD, we tell it to run in command-line mode. The -l flag tells the INSTALL command where to put the new library. For lack of a better idea, I’m installing to the current directory. More advanced users might want to install this to /usr/local or somewhere like that. Finally, notice that you don’t have to untar the package in order to install it.

If this works properly, it will create a new directory inside your current one, named “sensitivity”. This is the name by which you should refer to the library when invoking it in an R script. Let’s look at an example of using the library (courtesy of Dave Hadka). Create a file called createSamples.R. Open it in a text editor and write the following:

library("sensitivity")
n<-1000
X1<-data.frame(matrix(runif(8*n), nrow=n))
X2<-data.frame(matrix(runif(8*n), nrow=n))
sa<-sobol2002(model=NULL, X1, X2, nboot=10)
write(t(sa$X), file="test.samples", 8)

First, the library is loaded. If you’re operating in a different directory, you will need to replace “sensitivity” with “/path/to/sensitivity” as necessary. The most important function here is sobol2002, which accepts as arguments a model, two matrices of random samples, and the number of bootstrap resamples. Here, we only want to perform sampling, so the model is NULL. The last line will write the 8 columns of parameter samples to a file called test.samples.

In order to run this script from the command line, just do the following:

R CMD BATCH sampler.R

…and then check to make sure test.samples contains your samples.

In general, we will probably not want to run our models through R. We will just create samples like this and then run the file test.samples (or something else) through our actual simulations. After that, we’ll come back to this R library and give it our model output to perform analysis (to be continued in a forthcoming post).

 

5 thoughts on “Starting out with the R Sensitivity Package

  1. Hi Jacqueline,

    The library is set up to do that, it’s just not described in this particular post. I’d suggest checking out the authors’ documentation here:
    https://cran.r-project.org/web/packages/sensitivity/index.html

    If the model is written in a different language, I imagine you’d read in the model output the same way you would any other data file in R.

    You might be interested in this command-line wrapper which handles some of this — but I wrote this years ago and am not sure if it’s still compatible with the R sensitivity package.
    https://github.com/jdherman/r-sensitivity-wrapper

    Thanks,
    Jon

  2. Pingback: Water Programming Blog Guide (Part I) – Water Programming: A Collaborative Research Blog

  3. Pingback: Open Source Sensitivity Analysis Tools – Water Programming: A Collaborative Research Blog

Leave a comment