1

I have a function f_2 and need to minimize it's parameters by nlminb in order to estimate log likelihood (that is the objective in nlminb output). There is one constrain I want to add (u1+u2+u3+u4+u5+u6+u7+u8+u9)==1 or sum of the parameters to be equal 1. My code is below:

f_2<-function(par){
  u1<-par[1]
  u2<-par[2]
  u3<-par[3]
  u4<-par[4]
  u5<-par[5]
  u6<-par[6]
  u7<-par[7]
  u8<-par[8]
  u9<-par[9]
  u1+u2+u3+u4+u5+u6+u7+u8+u9==1
  -(n1_2*log(u1/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+n2_2*log(u2/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+
      n3_2*log(u3/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+n4_2*log(u4/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+
      n5_2*log(u5/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+n6_2*log(u6/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+
      n7_2*log(u7/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+n8_2*log(u8/(u1+u2+u3+u4+u5+u6+u7+u8+u9))+
      n9_2*log(u9/(u1+u2+u3+u4+u5+u6+u7+u8+u9)))
}
z1_2<-nlminb(c(1,1,1,1,1,1,1,1,1),f_2, lower = 0.0001, upper = Inf)

My output is

$par
[1] 0.57573059 1.62807832 2.99217185 0.01711788 0.01621807 0.60546191 0.39102822 0.03153362 0.19371337

. As you see the constrain for the sum of parameters to be 1 is just ignored. It is important to set up this constrain to be able to compare par for different models. I understand that I can do that manually but want to find out the way to make it in coding. Could you, please, help me solve this issue and to make constrain to be applied? Thank you!

  • This statement `u1+u2+u3+u4+u5+u6+u7+u8+u9==1` does nothing. It returns a logical value (TRUE or FALSE) which is then just thrown away since you haven't assigned it to anything and does not really make sense anyways. You can remove that statement. If `par` is the answer you get from the unconstrained problem shown then`par/sum(par)` is the answer with the constraint. Note that if nn is the vector of n values then the objective can be written as `-sum(nn * log(par/sum(par))` – G. Grothendieck Nov 19 '17 at 17:23
  • @G.Grothendieck thank you for your comment! – Lyudmyla Starostyuk Nov 19 '17 at 20:03

0 Answers0