[R] gradient option in 'nlm' function
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 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 ProfessorDepartment of BistatisticsThe University of Texas M.D. Anderson Cancer Center1515 Holcombe Blvd. - Unit 447Houston, TX 77030-4009(713)794-4153__ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
[R] gradient option in "nlm" function
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 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 __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Re: [R] gradient option in "nlm" function
Oh, that's a typo. I passed the function target. Seems to me R requires some kind of specific syntex. 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 |-+--> | | | | | | | |Thomas W Blackwell| | | <[EMAIL PROTECTED]>| | | | | | 11/17/03 10:18 AM| | | | |-+--> >-| | | | | | | |To: | | [EMAIL PROTECTED] | |cc: | | [EMAIL PROTECTED] | |Subject: | | Re: [R] gradient option in "nlm" function | | | >-| On Mon, 17 Nov 2003 [EMAIL PROTECTED] 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 > __ [EMAIL PROTECTED] mailing list https://www.stat.math.ethz.ch/mailman/listinfo/r-help