简体   繁体   English

在 R 中使用 3 个参数进行目标搜索

[英]Goal seek in R with 3 parameters

#========
#DATABASE
#========
database <- matrix(c(51,43,-22,-92,28,-21,68,22,9,-20,-30,-1,-10,10,-10,-5,10,-2,30,-3,-5),ncol=3,byrow=TRUE)
colnames(database ) <- c("A","B","C")
database  <- as.data.frame(database )
x<-1
y<-1
z<-1
database$RES<-c(1,0,0,0,1,0,1)
database$SCORE<- database$A*x+database$B*y+database$C*z
database$PREV<- ifelse(database$SCORE>1,1,0)

#========
#TARGET
#========
t<-table(database$RES, database$PREV)
P<-(t[1]+t[4])/nrow(database)

This is an example of my database (60k rows), I want to find values for xyz (in the code I put "1" just for convenience to run the script but I want to find them!) to have maximum value of P. The target P must be 1 or closed to 1.这是我的数据库(60k 行)的一个示例,我想找到 xyz 的值(在代码中,我为了方便运行脚本而设置了“1”,但我想找到它们!)以获得 P 的最大值。目标 P 必须为 1 或接近 1。

I didnt find what I'm looking for in thread with similiar title.我没有在具有类似标题的线程中找到我正在寻找的内容。 In excel is pretty simple but can't find more than 1 parameter.在excel中非常简单,但找不到超过1个参数。

Thanx in advance.提前谢谢。

I'm not satisfied with this answer, but maybe this is something that can at least get you started.我对这个答案并不满意,但也许这至少可以让你开始。

The optim() function finds the optimum set of answers for the problem you're trying to solve, but it looks to me, at least with the toy data, that it finds itself into a local maxima. optim()函数为您尝试解决的问题找到最佳答案集,但在我看来,至少对于玩具数据,它发现自己处于局部最大值。 You'd have to run it several times to find the best parameters, for me it occurs when P = 0.8571429 , and even then the x , y , z values can vary quite significantly, which would indicate that there are several equally optimal solutions for this particular data.您必须多次运行它才能找到最佳参数,对我而言,它发生在P = 0.8571429 ,即使如此, xyz值也可能变化很大,这表明有几个同样最佳的解决方案这个特定的数据。

database <- matrix(c(51,43,-22,-92,28,-21,68,22,9,-20,-30,-1,-10,10,-10,-5,10,-2,30,-3,-5),ncol=3,byrow=TRUE)
colnames(database ) <- c("A","B","C")
database  <- as.data.frame(database )
database$RES <- c(1,0,0,0,1,0,1)

find_best <- function(data, x) {
  SCORE <- data$A*x[1]+data$B*x[2]+data$C*x[3]
  PREV <- ifelse(SCORE>1,1,0)
  t <- table(data$RES, PREV)
  P <- (t[1]+t[4])/nrow(data)
  P
}

result <- optim(c(1, 1, 1), find_best, data = database, method = "SANN", control = list(fnscale = -1))

result$value
[1] 0.8571429 # The P value

result$par
[1]  2.396844 -4.460343 -7.137460 # These are your sought after x, y, z parameters.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM