Hi, If you look at the source code for optim() in the optim.c file, you will see the following lines for "BFGS":
if (trace && (iter % nREPORT == 0)) Rprintf("iter%4d value %f\n", iter, f); This means that "BFGS" does not output gradient values when you "trace" the iterations. Let us look at the code for "L-BFGS-B": if(trace == 1 && (iter % nREPORT == 0)) { Rprintf("iter %4d value %f\n", iter, f); So, it seems like even "L-BFGS-B" algorithm is also not going to be useful to you. You can use the spg() function in the "BB" package. Its usage is very similar to that of optim(). When you specify trace=TRUE, it will give you both function and (projected) gradient information. You can use the "triter" parameter to control the frequency of output, i.e. settig triter=1, will give you the fn and gr values at each iteration. library(BB) ?spg Hope this helps, Ravi. ---------------------------------------------------------------------------- ------- Ravi Varadhan, Ph.D. Assistant Professor, The Center on Aging and Health Division of Geriatric Medicine and Gerontology Johns Hopkins University Ph: (410) 502-2619 Fax: (410) 614-9625 Email: rvarad...@jhmi.edu Webpage: http://www.jhsph.edu/agingandhealth/People/Faculty/Varadhan.html ---------------------------------------------------------------------------- -------- -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Shimrit Abraham Sent: Tuesday, February 24, 2009 9:00 AM To: r-help@r-project.org Subject: [R] Tracing gradient during optimization Hi everyone, I am currently using the function optim() to maximize/minimize functions and I would like to see more output of the optimization procedure, in particular the numerical gradient of the parameter vector during each iteration. The documentation of optim() describes that the trace parameter should allow one to trace the progress of the optimization. I use the following command: optim(par = vPar, fn = calcLogLik, method = "BFGS", control = list(trace = TRUE, fnscale = -1, maxit = 2000)); which gives very little information: initial value 3.056998 final value 2.978351 converged Specifying trace >1, for instance trace = 20, does not result in more information. Is there a way to view more details of the progress perhaps by using another optimizer? Thanks, Shimrit Abraham [[alternative HTML version deleted]] ______________________________________________ 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. ______________________________________________ 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.