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