[R] optim()

Mike Lonergan
Wed, 03 Apr 2002 17:33:02 -0000

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