[Rd] nlminb: names of parameter vector not passed to objective function
Thomas Petzoldt
Thomas.Petzoldt at tu-dresden.de
Wed Dec 3 12:18:25 CET 2008
Dear R developers,
I tried to use nlminb instead of optim for a current problem (fitting
parameters of a differential equation model). The PORT algorithm
converged much better than any of optim's methods and the identified
parameters are plausible. However, it took me a while before spotting
the reason of a technical problem that nlminb, in contrast to optim,
does not pass names of the start parameters to the objective function.
Please find below a minimum reproducible example. There is, of course, a
workaround, but in order to make optim and nlme more compatible I would
ask whether it would be possible to change this idiosyncratic behavior?
Tested with:
R version 2.8.0 Patched (2008-11-04 r46830) i386-pc-mingw32
and also
R version 2.9.0 Under development (unstable) (2008-12-03 r47039)
i386-pc-mingw32
Thanks a lot
Thomas Petzoldt
set.seed(3577) # make it reproducible
## 1) example taken from ?nlminb -------------------------------------
x <- rnbinom(100, mu = 10, size = 10)
hdev <- function(par) {
-sum(dnbinom(x, mu = par[1], size = par[2], log = TRUE))
}
nlminb(c(20, 20), hdev, lower = 0.001, upper = Inf)
## --> works without problems
## 2) same example, but with named vectors -----------------------------
hdev <- function(par) {
cat(names(par), "\n") # show what happens
-sum(dnbinom(x, mu = par["mu"], size = par["size"], log = TRUE))
}
start <- c(mu=20, size=20)
optim(start, hdev, lower = 0.001, upper = Inf, method="L-BFGS-B")
## --> works without problems
## 3) THE PROBLEM
nlminb(start, hdev, lower = 0.001, upper = Inf)
## --> $objective is NA because names of "start" are not passed through
## 4) workaround -------------------------------------------------------
hdev <- function(par, pnames) {
names(par) <- pnames
-sum(dnbinom(x, mu = par["mu"], size = par["size"], log = TRUE))
}
nlminb(start, hdev, pnames = names(start), lower = 0.001, upper = Inf)
## --> works, but is it possible to improve nlminb
## so that the workaround can be avoided ?
--
Thomas Petzoldt
Technische Universitaet Dresden
Institut fuer Hydrobiologie thomas.petzoldt at tu-dresden.de
01062 Dresden http://tu-dresden.de/hydrobiologie/
GERMANY
More information about the R-devel
mailing list