From otoomet @end|ng |rom gm@||@com  Sun Aug  6 08:55:34 2023
From: otoomet @end|ng |rom gm@||@com (Ott Toomet)
Date: Sat, 5 Aug 2023 23:55:34 -0700
Subject: [Rd] 
 feature request: optim() iteration of functions that return
 multiple values
In-Reply-To: <f0591cf8-a6b9-cc99-7afc-2d4957ab594b@mx.uni-saarland.de>
References: <PAXP189MB1928AA39727C26A434F8D3FFD608A@PAXP189MB1928.EURP189.PROD.OUTLOOK.COM>
 <f0591cf8-a6b9-cc99-7afc-2d4957ab594b@mx.uni-saarland.de>
Message-ID: <CAMMJQ0ZatzN9-JNM6RT+PFUXVQ2XXq87pOuByj6HMqWUgherUg@mail.gmail.com>

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 Sat, Aug 5, 2023 at 1:13?AM Martin Becker <
martin.becker at mx.uni-saarland.de> wrote:

> 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:
>
> fr <- function(x) {   ## Rosenbrock Banana function
>    x1 <- x[1]
>    x2 <- x[2]
>    ans <- 100 * (x2 - x1 * x1)^2 + (1 - x1)^2
>    list(ans=ans, extra1 = 1:10, extra2 = letters)
> }
>
> fr2 <- returnFirst(fr)
> tmp <- optim(c(-1.2,1), fr2)
> fr(tmp$par)
>
>
> Am 03.08.23 um 22:21 schrieb Sami Tuomivaara:
> > 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 the
> calculations, etc.
> >
> > myfun  <- function()
> > {
> > ...
> > return(list(minimize.me = minimize.me, R2 = R2, pval = pval, etc.))
> > }
> >
> > During the iteration, optim could utilize just the first value from the
> myfun return list; all the other values calculated and returned by myfun
> could be ignored by optim.
> > After convergence, the other return values of myfun could be finally
> extracted and appended into the optim return value (which is a list) as
> additional entry e.g.: $aux <- list(R2, pval, etc.), (without 'minimize.me'
> as it is already returned as $value).
> >
> > The usual ways for accessing optim return values, e.g., $par, $value,
> etc. are not affected.  Computational cost may not be prohibitive either.
> Is this feasible to consider?
> >
> >
> >       [[alternative HTML version deleted]]
> >
> > ______________________________________________
> > R-devel at r-project.org mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-devel
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

	[[alternative HTML version deleted]]



[Rd] feature request: optim() iteration of functions that return multiple values
Ott Toomet