Another option that is similar to Enrico's is to use object oriented
programming with R6 or reference objects. I prefer the R6 package
(which will still use an environment like Enrico, but with some
different syntax and a little easier if you want to do this multiple
times.
Here is some example c
But why time methods that the author (me!) has been telling the community for
years have updates? Especially as optimx::optimr() uses same syntax as optim()
and gives access to a number of solvers, both production and didactic. This set
of solvers is being improved or added to regularly, with a ma
Thank you all very much for the suggestions, after testing, each of them would
be a viable solution in certain contexts. Code for benchmarking:
# preliminaries
install.packages("microbenchmark")
library(microbenchmark)
data <- new.env()
data$ans2 <- 0
data$ans3 <- 0
data$i <- 0
data$fun.value
I have done this using attributes:
fr <- function(x) { ## Rosenbrock Banana function
x1 <- x[1]
x2 <- x[2]
ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
attr(ans, "extra1") <- 1:10
attr(ans, "extra2") <- letters
ans
}
Not sure if this works in your case though.
Cheers,
Ott
On S
For a solution that does not require any change to the original function
being optimized, the following one-liner could be used, which converts
existing functions to functions that return only the first element:
returnFirst <- function(fun) function(...) do.call(fun,list(...))[[1]]
Example:
f
Enrico gave you a workaround that stores the extra values in an environment.
Another possible workaround is an optional argument to myfun() that asks
it to return more information, e.g.
fr <- function(x, data, extraInfo = FALSE) { ## Rosenbrock Banana
function
x1 <- x[1]
On Thu, 03 Aug 2023, Sami Tuomivaara writes:
> Dear all,
>
> I have used optim a lot in contexts where it would
> useful to be able to iterate function myfun that, in
> addition to the primary objective to be minimized
> ('minimize.me'), could return other values such as
> alternative metrics of t
Dear all,
I have used optim a lot in contexts where it would useful to be able to iterate
function myfun that, in addition to the primary objective to be minimized
('minimize.me'), could return other values such as alternative metrics of the
minimization, informative intermediate values from th