optimx uses exactly the same code as optim for BFGS. However, the call to optim in optimx is preceded by a check of the gradient at the starting values supplied using numDeriv.
That is, we evaluate the gradient with gr=(user's function for gradient) and then with the grad() function from numDeriv. There are some tolerances used, and depending on your function and its perversities (and most nonlinear functions do have some), there won't be total agreement, so the message can get popped up. Suggestion: Try evaluating the function and gradient at several sets of inputs along with the numDeriv grad() equivalent and see if they are close enough in your own view. If not, then possibly your gradient code is not quite right. This suggestion is appropriate generally in building optimization problem codes, and is part of the optimgui package of Yixuan Qui built in the recent Google Summer of Code effort. John Nash On 08/30/2011 06:00 AM, r-help-requ...@r-project.org wrote: > Message: 10 > Date: Mon, 29 Aug 2011 02:10:36 -0700 (PDT) > From: Kathie <kathryn.lord2...@gmail.com> > To: r-help@r-project.org > Subject: [R] gradient function in OPTIMX > Message-ID: <1314609036951-3775791.p...@n4.nabble.com> > Content-Type: text/plain; charset=us-ascii > > Dear R users > > > When I use OPTIM with BFGS, I've got a significant result without an error > message. However, when I use OPTIMX with BFGS( or spg), I've got the > following an error message. > > ---------------------------------------------------------------------------------------------------- > >> > optimx(par=theta0, fn=obj.fy, gr=gr.fy, method="BFGS", >> > control=list(maxit=10000)) > Error: Gradient function might be wrong - check it! > > ---------------------------------------------------------------------------------------------------- > > I checked and checked my gradient function line by line. I could not find > anything wrong. > > Is it a bug or something? I prefer OPTIMX, so I'd like to know why. > > Thanks a lot in advance > > Regards, > > Kathryn Lord > > -- > View this message in context: > http://r.789695.n4.nabble.com/gradient-function-in-OPTIMX-tp3775791p3775791.html > Sent from the R help mailing list archive at Nabble.com. > > > > ------------------------------ ______________________________________________ R-help@r-project.org 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.