【发布时间】:2015-06-09 19:09:07
【问题描述】:
我正在尝试使用 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()?
【问题讨论】:
标签: r statistics data-analysis