[R] Gradient problem in nlm

From: singyee ling <singyee.ling_at_googlemail.com>
Date: Sat 30 Sep 2006 - 16:09:06 GMT


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@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. Received on Sun Oct 01 02:19:44 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Sat 14 Oct 2006 - 18:30:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.