简体   繁体   中英

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.

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.

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. 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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