我想在R的灵敏度包中使用fast99()进行全局灵敏度分析。只是为了让你知道我正在尝试做什么,这里是我为示范:R灵敏度包(fast99)
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))
与试验结果如下:
> 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
我现在可以用这样一段话量X2是关键变量。
我的问题是:我可以在读取txt文件作为输入参数的黑盒模型上实现fast99()吗?例如:
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))
我有更多的参数,我的代码是非常庞大的,所以我使用的是精简版的这篇文章。当我运行它时,模型会停止,因为我认为它将所有10个样本矢量化并将它们传递给文本文件。
而不是什么,我需要这样的文本行:
"x1 = 1"
我得到
"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"
由于文本文件具有变量X1(和变量作为休息多个值好吧),黑匣子模型停止运行。
我没有设计黑盒模型,所以我通过模型迭代的唯一方法是更改文本文件。如何通过将这些参数传递给纺织品来使用fast99()?