From ripley at stats.ox.ac.uk  Wed Dec  3 12:49:06 2008
From: ripley at stats.ox.ac.uk (Prof Brian Ripley)
Date: Wed, 3 Dec 2008 11:49:06 +0000 (GMT)
Subject: [Rd] nlminb: names of parameter vector not passed to objective
 function
In-Reply-To: <49366B01.8070202@tu-dresden.de>
References: <49366B01.8070202@tu-dresden.de>
Message-ID: <alpine.LFD.2.00.0812031144110.28671@gannet.stats.ox.ac.uk>

On Wed, 3 Dec 2008, Thomas Petzoldt wrote:

> 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?

The 'idiosyncratic behavior' is to expect that a new vector of parameters 
will magically inherit names from the start vector.  optim() was changed 
(and documented) because some users asked for this, and if a user who 
wants it for nlminb provides a tested patch, I am sure it will be 
considered.

Note the documentation difference.

>
> 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
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272866 (PA)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595



[Rd] nlminb: names of parameter vector not passed to objective function
Prof Brian Ripley