title: ML solution of a large Dirichlet PDF using optimx()
body: <p>I have tried gradient ascent to find ML solution to an 18 category Dirichlet model in R using the optimx package, but I can find initial parameter values that will make the CG algorithm run. I have tried different combinations of positive integers for the alphas. Could someone tell me what I'm doing wrong?
  Code:</p>
  
  <pre><code>####Dirichlet log likelihood function
  LnL_Dir &lt;- function(alpha, p, N){
  expr1 &lt;- log(gamma(sum(alpha)))
  expr2 &lt;- sum(log(gamma(alpha)))
  expr3 &lt;- alpha-1
  expr4 &lt;- log(p)
  lnl &lt;- N*(expr1 - expr2 + sum(expr3*expr4))
  return(-lnl)
  } 
  
  ###gradient function
  grad_Dir &lt;- function(alpha, p, N){
  expr5 &lt;- digamma(sum(alpha))
  expr6 &lt;- digamma(alpha)
  expr7 &lt;- log(p)
  gradient &lt;- N*(expr5 - expr6 + expr7)
  return(-gradient)
  }
  
  inits &lt;- rep(c(1),18)    
  
  optimx(inits, LnL_Dir, grad_Dir, method=c("CG"), p=theta.x, N=300)
  </code></pre>
  
  <p>The error message that is returned is:
  "Cannot evaluate function at initial parameters"</p>