Search code examples
roptimizationensemble-learning

Optimize ensemble model weights with for loop


i have three model probability predictions mod.p1, mod.p2, mod.p3

require(Runuran)
mod.p1<- urnorm(n = 1000, mean = 0.2, sd = 0.35, lb = 0, ub = 1)
mod.p2<- urnorm(n = 1000, mean = 0.23, sd = 0.37, lb = 0, ub = 1)
mod.p3 = urnorm(n = 1000, mean = 0.19, sd = 0.39, lb = 0, ub = 1)

final outcomes is given in verdict ( Yes , No )

Verdict <- sample( c("yes","No"), 1000, replace=TRUE, prob=c(0.2,0.8) )

I have three weights for each model

i1=0.3
i2=0.3
i3=0.4

creating ensemble predictions and calculating auc

  Ensemble=i1*mod.p1+i2*mod.p2+i3*mod.p3
  require(ROCR)    
  Ensemble.pred = prediction(Ensemble, Verdict)
  Ensemble.auc = as.numeric(performance(Ensemble.pred, "auc")@y.values)
  Ensemble.auc # 0.52

Now how do i write a for loop to try out different values of i1 , i2 and i3 , and return best possible values of i1 , i2 and i3 which give me maximum AUC.


Solution

  • Got it , this is what i was looking at -

    # creation of Dummy data
    mod.p1<- urnorm(n = 1000, mean = 0.2, sd = 0.35, lb = 0, ub = 1)
    mod.p2<- urnorm(n = 1000, mean = 0.23, sd = 0.37, lb = 0, ub = 1)
    mod.p3 = urnorm(n = 1000, mean = 0.19, sd = 0.39, lb = 0, ub = 1)
    Verdict <- sample( c("yes","No"), 1000, replace=TRUE, prob=c(0.2,0.8) )
    
    #loop for optimizing the weights in ensemble
    auc = 0
    i1 = 0
    i2 = 0
    i3 = 0
    
    for(i in seq(0,1,0.05))  {
      for (j in seq(0,1-i,0.05))  {
        k = 1-i-j
        e = i1*mod.p1 + i2*mod.p2 + i3*mod.p3
        Ensemble.pred = prediction(e, Verdict)
        Ensemble.auc = as.numeric(performance(Ensemble.pred, "auc")@y.values)
        if (Ensemble.auc>auc)  {
          auc = Ensemble.auc
          i1 = i
          i2 = j
          i3 = k
          pred = e
        }
      }
    }
    
    # get final values
    auc #0.524
    i1  #0
    i2  #0.1
    i3  #0.9