# [R] optim() has a non-consistent way to send parameter for different methods

Marc Girondot marc_grt at yahoo.fr
Fri Jun 23 18:21:45 CEST 2017

```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.

Marc

Tested in R-3.4.0

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

```