[R] optim()

ripley@stats.ox.ac.uk ripley at stats.ox.ac.uk
Wed Apr 3 19:51:16 CEST 2002


The default Nelder-Mead method does not work well in one dimension.
> optim(1,sqr, method="BFGS")
$par
[1] -1

$value
[1] 2.071992e-29

$counts
function gradient
       8        3

...

I never really expected people to use optim for one-dimensional input
(there is optimize).

I'll add a warning in that case.


On Wed, 3 Apr 2002, Mike Lonergan wrote:

>
> I was having some problems persuading optim() to give me the answers I
> wanted,
>  & simplified down to:
>
>    sqr<-function(x){(x+1)^2}
>    optim(1,sqr)
>
> I accept this is a hammer to crack a nut, but was still expecting the
> answer -1.
> I got:
>
>    $par
>    [1] -0.8
>
>    $value
>    [1] 0.04
>
>    $counts
>    function gradient
>       12       NA
>
>    $convergence
>    [1] 0
>
>    $message
>    NULL
>
> so I've obviously misunderstood something.
> (The default 'robust but slow' method did seem appropriate.)
>
> Repeating this for:
>    v<--100:100/5
>    v2<-1;for(i in 1:201) v2[i]<-optim(v[i],sqr)$par
>    plot(v,v2)
>
> gave a bit more than 10% unexpected answers. Playing with control reduces
> the scatter, but rather relies on knowing the answer in advance.
> (function(x){x^2} gives another nice pattern.)
>
> So, with the usual apologies for my ignorance (of both kinds, though I have
> had a look in the archives), I'd be grateful for an indication of where I've
> messed up.
>
> Thanks,
>
> Mike.
>
> > version
>          _
> platform i386-pc-mingw32
> arch     x86
> os       Win32
> system   x86, Win32
> status   Patched
> major    1
> minor    4.0
> year     2002
> month    01
> day      23
> language R
>
> (but also tried on a couple of other machines)
>
>
>
> ----------------------------------------------------------------
> Mike Lonergan
> Research Unit for Wildlife Population Assessment
> Mathematical Institute
> University of St Andrews
> North Haugh                             Tel: +44 (0) 1334 463760
> St Andrews                              Fax: +44 (0) 1334 463714
> Fife KY16 9SS                      Email: mel at mcs.st-and.ac.uk
> Scotland
> ----------------------------------------------------------------
>
>
>
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
>

-- 
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 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list