R sensitivity package (fast99)

2k Views Asked by At

I am trying to do global sensitivity analysis using fast99() in sensitivity package in R. Just to give you an idea of what I'm trying to do, here is the model I built just for demonstration:

library(sensitivity)
factors <- c("x1", "x2", "x3")
modelRun <- function (Input) {
  (Input[,1]-0.5)*2 + (Input[,2]+1)*5 + (Input[,3]-0.2)*3
}
test <- fast99(modelRun, factors, n = 1000, q.arg=list(min=0, max=2) )

with the following test results:

> test

Call:
fast99(model = modelRun, factors = factors, n = 1000, q.arg = list(min = 0, max = 2))

Model runs: 3000 

Estimations of the indices:
   first order total order
x1   0.1053816   0.1061664
x2   0.6572669   0.6593234
x3   0.2368125   0.2388793

I can now use this to say variable x2 is the key variable.

My question is: can I implement fast99() on a black box model that reads a txt file as input parameters? For example:

factors <- c("x1", "x2", "x3")
newModel <- function(Input) {
   params <- readLines("inputtext.txt")
   params[17] <- toString(Input[,1])
   params[23] <- toString(Input[,2])
   params[25] <- toString(Input[,3])
   writeLine(params, "inputtext.txt")

   source("blackboxmodel.R") # this model then reads inputtext.txt file as input parameters
   y <- read.csv("output.csv")
   return(y$results)
}

library(sensitivity)
test <- fast99(newModel, factors, n = 10, q.arg=list(min=0, max=2) )

I have a lot more parameters and my code is really bulky, so I'm using condensed version for this post. When I run this, the model stops because I think it vectorizes all 10 samples and passes them to the text file.

Instead of what I need like this for the text line:

"x1 = 1"

I get

"x1 = 1, 1.4, 1.8, 1.8, 1.4, 1, 0.6, 0.2, 0.2, 0.6, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN, NaN"

Since the text file has multiple values for variable x1 (and the rest of the variables as well), the black box model stops running.

I did not design the black box model so the only way for me to iterate through the model is by changing the text file. How can I use fast99() by passing these parameters to textile first?

1

There are 1 best solutions below

0
On

Ok. I figured out how to pass the sample parameters to txt using sobol() instead of fast99().

newModel <- function(Input) {
 for (i in 1:nrow(Input)) {
    params <- readLines("inputtext.txt")
    params[17] <- toString(Input[i,1])
    params[23] <- toString(Input[i,2])
    params[25] <- toString(Input[i,3])
    writeLine(params, "inputtext.txt")

    source("blackboxmodel.R") # this model then reads inputtext.txt file as input parameters
    y <- read.csv("output.csv")
    }
    return(y$results)
}

library(sensitivity)
n <- 100
x1 <- data.frame(matrix(runif(3*n, min = 0.1, max = 2), nrow=n))
x2 <- data.frame(matrix(runif(3*n, min = 0.1, max = 2), nrow=n))

results <- sobol(model=newModel, X1=x1, X2=x2, order=2, nboot=100)

The issue I'm having now is that the blackboxmodel.R craps out after a few iterations. It's an issue with how the model was designed and I have no way of knowing what to fix.

Given my situation, is there a way to just tabulate the results and input parameters in a single data frame and run some sort of sensitivity analysis on it? At least this way, I can manually run the blackbox model and build a table.