【问题标题】:R sensitivity package (fast99)R灵敏度包(fast99)
【发布时间】: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


    【解决方案1】:

    好的。我想出了如何使用 sobol() 而不是 fast99() 将示例参数传递给 txt。

    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)
    

    我现在遇到的问题是 blackboxmodel.R 在几次迭代后就崩溃了。这是模型设计方式的问题,我不知道要修复什么。

    鉴于我的情况,有没有办法将结果和输入参数制成表格,并在单个数据框中进行某种敏感性分析?至少这样,我可以手动运行黑盒模型并建表。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-23
      • 2011-01-30
      • 2021-02-20
      相关资源
      最近更新 更多