eFF eFF - 2 years ago 156
R Question

extracting values from matrix for sensitivity analysis

I'm working on a sensitivity analysis based on some outputs from a model. Let's say I have a matrix as follows:

parameter_range <- matrix(c(1.61-(1.61*0.10),1.61,1.61+(1.61*0.10), #parameter 1
20-(20*0.1),20,20+(20*0.1), #parameter 2
-2.5-(2.5*0.1),-2.5,-2.5+(2.5*0.1), #parameter 3
-1.007-(-1.007*0.1),-1.007,-1.007+(-1.007*0.1), #parameter 4
4000-(4000*0.1),4000,4000+(4000*0.1)), nrow=3, ncol=5) #parameter 5 parameter

>parameter_range

[,1] [,2] [,3] [,4] [,5]
[1,] 1.449 18 -2.75 -0.9063 3600
[2,] 1.610 20 -2.50 -1.0070 4000
[3,] 1.771 22 -2.25 -1.1077 4400


The dataset assumes that position [1,] = min value (mean value-10%) , [2,] = mean value, [3,] = max value (mean value+10%).

I need to create a loop to extract each possible parameter combination around the min and max values. This is 15 parameter combinations:

First combination:
1.449 20 -2.50 -1.0070 4000

Second combination:
1.610 20 -2.50 -1.0070 4000

Third combination:
1.771 20 -2.50 -1.0070 4000

Fourth combination:
1.610 18 -2.50 -1.0070 4000

... and so on.

Thank you!

Answer Source

Something like this:

library(dplyr)

all_comb = parameter_range %>%
  data.frame() %>%
  expand.grid() %>%
  arrange(X1) %>%
  as.matrix()

Result:

> head(all_comb, 10)
         X1 X2    X3      X4   X5
 [1,] 1.449 18 -2.75 -0.9063 3600
 [2,] 1.449 20 -2.75 -0.9063 3600
 [3,] 1.449 22 -2.75 -0.9063 3600
 [4,] 1.449 18 -2.50 -0.9063 3600
 [5,] 1.449 20 -2.50 -0.9063 3600
 [6,] 1.449 22 -2.50 -0.9063 3600
 [7,] 1.449 18 -2.25 -0.9063 3600
 [8,] 1.449 20 -2.25 -0.9063 3600
 [9,] 1.449 22 -2.25 -0.9063 3600
[10,] 1.449 18 -2.75 -1.0070 3600
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download