Solving non-linear equation with number of equations greater than number of unknown parameters in R

43 Views Asked by At

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?

0

There are 0 best solutions below