[Rd] bug in 'optim' documentation : "Brent" method doesn't copy 'par' names

Carles Bretó Martínez cbreto@@t@t @ending from gm@il@com
Fri Jun 22 20:31:18 CEST 2018


The optim documentation states (second from last sentence of Details Section) that "Any names given to par will be copied to the vectors passed to \code{fn} and \code{gr}." This does not seem to be the case when the method argument is set to "Brent".

Consider finding an optimum with the "Brent" method and a fn argument that does not rely on a named par argument, e.g., 

> quad <- function (x) x^2 + x
> par <- -0.75
> x <- optim(par = par, method = "Brent", lower = -1, upper = 0, fn = quad)
> x$par
[1] -0.5
> x$value
[1] -0.25
 
However, if the fn argument relies in the names in par, this optimum might not be achieved, e.g.,

> names(par) <- "par_name"
> par
par_name 
   -0.75 
> named <- function (x) if (length(names(x)) > 0) quad(x) else 123.456
> y <- optim(par = par, method = "Brent", lower = -1, upper = 0, fn = named)
> y$par
[1] -6.474096e-09
> y$value
[1] 123.456

The y$value suggests that the names of par are not being "copied to the vectors passed to \code{fn}." Note the lack of
> warnings()
> 

> sessionInfo()
R version 3.5.0 (2018-04-23)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: Ubuntu 14.04.5 LTS

Matrix products: default
BLAS: /usr/lib/atlas-base/atlas/libblas.so.3.0
LAPACK: /usr/lib/lapack/liblapack.so.3.0

locale:
 [1] LC_CTYPE=en_US.UTF-8       LC_NUMERIC=C              
 [3] LC_TIME=en_US.UTF-8        LC_COLLATE=en_US.UTF-8    
 [5] LC_MONETARY=en_US.UTF-8    LC_MESSAGES=en_US.UTF-8   
 [7] LC_PAPER=en_US.UTF-8       LC_NAME=C                 
 [9] LC_ADDRESS=C               LC_TELEPHONE=C            
[11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C       

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
[1] compiler_3.5.0
> 
> proc.time()
   user  system elapsed 
  0.328   0.056   0.443 



More information about the R-devel mailing list