[Rd] names treatment in optim()
Christophe Dutang
dutangc at gmail.com
Thu Oct 8 19:58:20 CEST 2015
Dear list,
A possible patch to correct the treatment of names consists in adding the following lines
if(!is.null(names(par))) names(res$par) <- names(par)
if(!is.null(names(fn1(par)))) names(res$value) <- names(fn1(par))
just before returning the variable res in optim. That is
optim <-
function(par, fn, gr = NULL, ...,
method = c("Nelder-Mead", "BFGS", "CG", "L-BFGS-B", "SANN", "Brent"),
lower = -Inf, upper = Inf,
control = list(), hessian = FALSE)
{
…
if (hessian)
res$hessian <- .External2(C_optimhess, res$par, fn1, gr1, con)
if(!is.null(names(par))) names(res$par) <- names(par)
if(!is.null(names(fn1(par)))) names(res$value) <- names(fn1(par))
res
}
Regards, Christophe
---------------------------------------
Christophe Dutang
LMM, UdM, Le Mans, France
web: http://dutangc.free.fr
> Le 17 sept. 2015 à 13:04, Christophe Dutang <dutangc at gmail.com> a écrit :
>
> Dear both,
>
> I have found that names are not treated in the same way in optim() depending on the optimization method (argument method).
>
> The example below shows the difference between the Brent method and the L-BFGS-B method.
>
> f <- function(x){ y <- x^2;names(y) <-"f(x)";y}
> optim(10, f, method="Brent", lower=-1, upper=10)$value
> optim(10, f, method="L-BFGS-B", lower=-1, upper=10)$value
>
> z <- 10
> names(z) <- "x"
> z
> optim(z, f, method="Brent", lower=-1, upper=10)$par
> optim(z, f, method="L-BFGS-B", lower=-1, upper=10)$par
>
>
> Do you obtain the same behavior? would you consider interesting to have the names passed to the components value and par?
>
> I’m using R version 3.2.2 (2015-08-14)
> Platform: x86_64-apple-darwin13.4.0 (64-bit)
> Running under: OS X 10.10.5 (Yosemite)
>
> locale:
> [1] fr_FR.UTF-8/fr_FR.UTF-8/fr_FR.UTF-8/C/fr_FR.UTF-8/fr_FR.UTF-8
>
> attached base packages:
> [1] stats graphics grDevices utils datasets methods base
>
>
>
> Kind regards, Christophe
>
> ---------------------------------------
> Christophe Dutang
> LMM, UdM, Le Mans, France
> web: http://dutangc.free.fr
>
More information about the R-devel
mailing list