0

I am trying to solve the attached equation using R.

equation screenshot

I tried nleqslv but it failed do to unequal length of equations and number of parameters.

myFun<- function(p) {
  y <- numeric(7)
  y[1] = sum(gamma[,1]*x2)/p[1]-sum(gamma[,1]*x1)/(1-p[1]) - sum(gamma[,2]*x2)/p[2]+sum(gamma[,2]*x1)/(1-p[2])
  y[2] = sum(gamma[,1]*x2)/p[1]-sum(gamma[,1]*x1)/(1-p[1]) - sum(gamma[,3]*x2)/p[3]+sum(gamma[,3]*x1)/(1-p[3])
  y[3] = sum(gamma[,1]*x2)/p[1]-sum(gamma[,1]*x1)/(1-p[1]) - sum(gamma[,4]*x2)/p[4]+sum(gamma[,4]*x1)/(1-p[4])
  y[4] = sum(gamma[,2]*x2)/p[2]-sum(gamma[,2]*x1)/(1-p[2]) - sum(gamma[,3]*x2)/p[3]+sum(gamma[,3]*x1)/(1-p[3])
  y[5] = sum(gamma[,2]*x2)/p[2]-sum(gamma[,2]*x1)/(1-p[2]) - sum(gamma[,4]*x2)/p[4]+sum(gamma[,4]*x1)/(1-p[4])
  y[6] = sum(gamma[,3]*x2)/p[3]-sum(gamma[,3]*x1)/(1-p[3]) - sum(gamma[,4]*x2)/p[4]+sum(gamma[,4]*x1)/(1-p[4])
  y[7]=1-(p[1] + p[2] + p[3] + p[4] )
  return(y)
}

p=c(0.2,0.4,0.1,0.3)
pi=nleqslv(p,myFun)$x
Error in nleqslv(p, myFun) : Length of fn result <> length of x!

Any ideas?

nrennie
  • 1,877
  • 1
  • 4
  • 14
Ali
  • 1
  • 3
  • What are x1, x2 and gamma? – Stéphane Laurent Jul 22 '23 at 08:42
  • gamma_ij is known and is between 0 and 1, and for a fixed i, it sums to 1 over j, i.e., gammai1+gammai2+gammai3+gammai4=1. x1i and x2i are positive numbers between 0 and for each fixed i, they sum to a constant number such as ti, i.e., x1i+xi2=ti, where ti is positive and known. – Ali Jul 23 '23 at 14:28

0 Answers0