On Mon, 17 Nov 2003 yuanji at mdanderson.org wrote:
> Dear list members,
>
> 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
>
> > target <- function(x)
> > { resp <- ....
> > grad <- rep(0,5) ## I have 5 paramters
> > grad[1] <- ...; grad[2] <- ...; grad[3] <- ...; grad[4]
<- ...; grad[5]
> <- ...
> > attr(resp, "gradient") <- grad
> > resp
> > }
> > nlm(targ, c(1,2,3,4,5))
It would probably help if you passed the function target() to
nlm(), rather than passing targ().
> The R gave me this error message
>
> "Error in nlm(targ, c(1,2,3,4,5)) : probable coding error in analytic
> gradient"
>
> 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.
>
> 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?
>
> Thanks a lot,
>
> Yuan Ji, Ph.D.
>
> Assistant Professor
> Department of Bistatistics
> The University of Texas M.D. Anderson Cancer Center
> 1515 Holcombe Blvd. - Unit 447
> Houston, TX 77030-4009
> (713)794-4153
>