Optimization function argument missing

This is my code. The function kum.loglikreturns a negative likelihood identifier and takes two arguments a and b. I need to find a and b that minimize this function using the optimization function. (n1, n2, n3 are predefined and passed to the optimization function.

kum.loglik = function(a, b, n1, n2, n3) {
  loglik = n1*log(b*beta(1+2/a,b)) + n2 * log(b*beta(1+2/a,b)-2*b*beta(1+1/a,b)+1) +
    n3 * log(b*beta(1+1/a,b)-b*beta(1+2/a,b))
  return(-loglik)
}
optim(par=c(1,1), kum.loglik, method="L-BFGS-B",
      n1=n1, n2=n2, n3=n3,
      control=list(ndeps=c(5e-4,5e-4)))

This code should work well, but it gives an error message

Error in b * beta(1 + 2/a, b) : 'b' is missing

What is wrong with this code?

+4
source share
1 answer

Problem (directly from the help system):

fn: A function to be minimized (or maximized), with first
argument the vector of parameters over which minimization is
to take place.

The function kum.loglikshould take a vector vthat you pull from the parameters, for example:

kum.loglik=function(v) { a = v[1]; b = v[2]; ...}

+7
source

All Articles