[R] Gradient problem in nlm

Spencer Graves spencer.graves at pdf.com
Sat Oct 14 20:21:29 CEST 2006


      Because of the lack of a self-contained, reproducible example, I 
can only guess.  If it were my problem, I might try the following: 

      1.  Try 'nlm' with 'print.level=2'.  This should provide more 
detail about the circumstances under which it failed.  If that didn't 
provide adequate detail, I might modify my objective function to print 
more detailed trace information each time it's called. 

      2.  Compare the gradient with that computed numerically, e.g., 
using 'grad' in library(numDeriv), especially for combinations of values 
apparently tested by 'nlm'. 

      3.  Work through all the examples on the 'nlm' help page and 
demo(nlm), making sure I understood those in full detail.  Doing so 
might identify something I was doing wrong, etc. 

      4.  If I can't solve the problem after all of the above, I might 
try to develop the simplest, self-contained example I can find that 
still exhibits the problem.  This often leads me to the problem.  If it 
doesn't, I then have a simple, self-contained example that I can then 
post to this list;  including such an example on average tends to 
increase the speed and quality of responses (and sometimes even the 
quantity). 

      Hope this helps. 
      Spencer Graves

singyee ling wrote:
> Hello everyone!
>
>
> I am having some trouble supplying the gradient function to nlm in R for
> windows version 2.2.1.
>
> What follows are the R-code I use:
>
> fredcs39<-function(a1,b1,b2,x){return(a1+exp(b1+b2*x))}
> loglikcs39<-function(theta,len){
> value<-sum(mcs39[1:len]*fredcs39(theta[1],theta[2],theta[3],c(8:(7+len))) -
> pcs39[1:len] * log(fredcs39(theta[1],theta[2],theta[3],c(8:(7+len)))))
> a1<-theta[1]; b1<-theta[2]; b2<-theta[3]
> df.a1<-sum(-mcs39[1:len] + pcs39[1:len]/(a1+exp(b1+b2*c(8:(7+len)))))
> df.b1<-sum( -mcs39[1:len] * exp(b1+b2*c(8:(7+len))) + (pcs39[1:len] *
> exp(b1+b2*c(8:(7+len))) ) /(a1+exp(b1+b2*c(8:(7+len)))))
> df.b2<- sum(-mcs39[1:len] * exp(b1+b2*c(8:(7+len))) * c(8:(7+len))  +
> (pcs39[1:len] * exp(b1+b2*c(8:(7+len)))  * c(8:(7+len))  )
> /(a1+exp(b1+b2*c(8:(7+len)))) )
>
>
> attr(value,"gradient")<-c(df.a1,df.b1,df.b2)
> return(value)
> }
>
> theta.start<-c(0.01 ,-1.20, -0.0005)
> outcs39<-nlm(loglikcs39,theta.start,len=50)
>
>
> Error in nlm(loglikcs39, theta.start, len = 50) :
>         probable coding error in analytic gradient
>
>
> Any light that can be shed on this would be highly appreciated.
> Many thanks
>
> Singyee Ling
>
> 	[[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list