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

