From marc_grt at yahoo.fr  Mon Jun 26 15:52:18 2017
From: marc_grt at yahoo.fr (Marc Girondot)
Date: Mon, 26 Jun 2017 15:52:18 +0200
Subject: [Rd] Suggestion for optim() in stats package
Message-ID: <ae0f3671-c240-d67c-7c73-3d70966e277a@yahoo.fr>

When optim() is used with method="BFGS", the name of parameters within
the vector are transmitted (see below, first example).

When method="Brent", the name of parameter (only one parameter can be
fitted with Brent method) is not transmitted. As there is only one, of
course, we know which parameter it is, but it makes things
non-consistent between methods.

It would be better that same convention is used for different method as
it will permit to make more general use of optim().

Marc

Tested in R-3.4.1

For example, here:

@@@@@@@@@@@
Method BFGS
@@@@@@@@@@@

# The names of values in par are transmitted
fitnorm_meansd<-function(par, val) {
    print(par)
    -sum(dnorm(x=val, mean=par["mean"], sd=par["sd"], log = TRUE))
}

val <- rnorm(100, mean=20, sd=2)
p<-c(mean=20, sd=2)
result<-optim(par=p, fn=fitnorm_meansd, val=val, method="BFGS")

The print(par) shows the named vector:
  > result<-optim(par=p, fn=fitnorm_meansd, val=val, method="BFGS")
mean   sd
    20    2
    mean     sd
20.001  2.000
    mean     sd
19.999  2.000
    mean     sd
20.000  2.001
etc...

@@@@@@@@@@@
Method Brent
@@@@@@@@@@@

# The name of value in par is not transmitted

fitnorm_mean<-function(par, val) {
    print(par)
    -sum(dnorm(x=val, mean=par, sd=2, log = TRUE))
}

val <- rnorm(100, mean=20, sd=2)
p<-c(mean=20)
result<-optim(par=p, fn=fitnorm_mean, val=val, method="Brent", lower=10,
upper=30)


The print(par) does not show named vector:
  > result<-optim(par=p, fn=fitnorm_mean, val=val, method="Brent",
lower=10, upper=30)
[1] 17.63932
[1] 22.36068



[Rd] Suggestion for optim() in stats package
Marc Girondot