Printing intermediate stages of the R optimization function

I want to numerically optimize a function in R if derivatives are not available. I am curious how I can print the intermediate steps of the optimization process. I know how to do this when I use optim (). I'm talking about control = list (trace ... etc.). How do you do this kind of work during optimization?

+3


source to share


1 answer


Track your goal. Please note, if you have a very old version of R, you will need to upgrade to a version returnValue()

in order to be available.

Here's the first example help(optimize)

with a trail added - see the instruction marked with ##:

f <- function (x, a) (x - a)^2
trace(f, exit = quote(cat("x:", x, "objective:", returnValue(), "\n")), print = FALSE) ##
optimize(f, c(0, 1), tol = 0.0001, a = 1/3)

      



giving:

x: 0.381966 objective: 0.002365137 
x: 0.618034 objective: 0.08105446 
x: 0.236068 objective: 0.009460549 
x: 0.3333333 objective: 0 
x: 0.3333 objective: 1.111442e-09 
x: 0.3333667 objective: 1.111442e-09 
x: 0.3333333 objective: 0 
$minimum
[1] 0.3333333

$objective
[1] 0

      

+4


source







All Articles