[R] gradient option in 'nlm' function

yuanji@mdanderson.org yuanji at mdanderson.org
Mon Nov 17 02:16:42 CET 2003


<FONT face="Default Sans Serif, Verdana, Arial, Helvetica, sans-serif" size=2><DIV>Dear list members,</DIV><DIV>&nbsp;</DIV><DIV>I am trying to use "nlm" function to maximize a mixture likelihood of beta densities. There are five unknown parameters in the likelihood. Since I can get the analytic gradient, I attach the "gradient" attribute in my target likehood function. The code is as the following </DIV><DIV>&nbsp;</DIV><DIV>&nbsp;target &lt;- function(x)</DIV><DIV>&nbsp;{ resp &lt;- ....</DIV><DIV>&nbsp;&nbsp; grad &lt;- rep(0,5) ## I have 5 paramters</DIV><DIV>&nbsp;&nbsp; grad[1] &lt;- ...; grad[2] &lt;- ...; grad[3] &lt;- ...; grad[4] &lt;- ...; grad[5] &lt;- ...</DIV><DIV>&nbsp;&nbsp; attr(resp, "gradient") &lt;- grad</DIV><DIV>&nbsp;&nbsp; resp<BR>&nbsp;}</DIV><DIV>nlm(targ, c(1,2,3,4,5))</DIV><DIV>The R gave me this error message</DIV><DIV>&nbsp;</DIV><DIV>VVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVVV</DIV><DIV>"Error in nlm(targ, c(1,2,3,4,5)) : probable coding error in analytic gradient"</DIV><DIV>^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^</DIV><DIV>I ran my code for defining gradient separately, and there seemed to be no coding error. I provided other options for nlm() function and it gave me the same error message. I removed the gradient part and let nlm() do the numerical derivative, it ran but the algorithm was not converging. </DIV><DIV>&nbsp;</DIV><DIV>I want to know if nlm can handle multiple parameters problems, and if yes, was there any error in my code? How do I properly provide the gradient for my function? </DIV><DIV>&nbsp;</DIV><DIV>Thanks a lot,</DIV><DIV>&nbsp;</DIV><DIV>&nbsp;</DIV><DIV>Yuan&nbsp;Ji,&nbsp;Ph.D.<BR><BR>############################################<BR>Assistant&nbsp;Professor<BR>Department&nbsp;of&nbsp;Bistatistics<BR>The&nbsp;University&nbsp;of&nbsp;Texas&nbsp;M.D.&nbsp;Anderson&nbsp;Cancer&nbsp;Center<BR>1515&nbsp;Holcombe&nbsp;Blvd.&nbsp;-&nbsp;Unit&nbsp;447<BR>Houston,&nbsp;TX&nbsp;77030-4009<BR>(713)794-4153<BR>############################################<BR></DIV></FONT>


More information about the R-help mailing list