[R] gradient option in "nlm" function

yuanji@mdanderson.org yuanji at mdanderson.org
Mon Nov 17 16:48:56 CET 2003

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))

The R gave me this error message

"Error in nlm(targ, c(1,2,3,4,5)) : probable coding error in analytic

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

More information about the R-help mailing list