Named parameters in optim()

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Named parameters in optim()

Duncan Murdoch
If I name the elements of the vector of initial values passed to
optim(), then it attaches the names to the final result, e.g.

 > f <- function(parms) (parms[1]-1)^2+(parms[2]-2)^2
 > optim(c(x=3,y=4), f)
$par
         x         y
0.9999635 2.0003241

$value
[1] 1.063637e-07

$counts
function gradient
       65       NA

$convergence
[1] 0

$message
NULL

However, the vector that gets passed to f doesn't have its names attached:

 > f <- function(parms) {
+  print(parms)
+  (parms["x"]-1)^2+(parms["y"]-2)^2
+ }
 > optim(c(x=3,y=4), f)
[1] 3 4
Error in optim(c(x = 3, y = 4), f) : function cannot be evaluated at
initial parameters

Is this something that should be fixed, i.e. could it be fixed without
making optim() substantially slower?  If not, it's at least something
that should be documented.

Duncan Murdoch

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Named parameters in optim()

Brian Ripley
I think the cost is small, and have just added this.

On Thu, 2 Mar 2006, Duncan Murdoch wrote:

> If I name the elements of the vector of initial values passed to
> optim(), then it attaches the names to the final result, e.g.
>
> > f <- function(parms) (parms[1]-1)^2+(parms[2]-2)^2
> > optim(c(x=3,y=4), f)
> $par
>         x         y
> 0.9999635 2.0003241
>
> $value
> [1] 1.063637e-07
>
> $counts
> function gradient
>       65       NA
>
> $convergence
> [1] 0
>
> $message
> NULL
>
> However, the vector that gets passed to f doesn't have its names attached:
>
> > f <- function(parms) {
> +  print(parms)
> +  (parms["x"]-1)^2+(parms["y"]-2)^2
> + }
> > optim(c(x=3,y=4), f)
> [1] 3 4
> Error in optim(c(x = 3, y = 4), f) : function cannot be evaluated at
> initial parameters
>
> Is this something that should be fixed, i.e. could it be fixed without
> making optim() substantially slower?  If not, it's at least something
> that should be documented.
>
> Duncan Murdoch
>
> ______________________________________________
> [hidden email] mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>
>

--
Brian D. Ripley,                  [hidden email]
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

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel
Reply | Threaded
Open this post in threaded view
|

Re: Named parameters in optim()

Duncan Murdoch
On 3/2/2006 2:52 PM, Prof Brian Ripley wrote:
> I think the cost is small, and have just added this.

Thank you!

Duncan Murdoch

>
> On Thu, 2 Mar 2006, Duncan Murdoch wrote:
>
>> If I name the elements of the vector of initial values passed to
>> optim(), then it attaches the names to the final result, e.g.
>>
>> > f <- function(parms) (parms[1]-1)^2+(parms[2]-2)^2
>> > optim(c(x=3,y=4), f)
>> $par
>>         x         y
>> 0.9999635 2.0003241
>>
>> $value
>> [1] 1.063637e-07
>>
>> $counts
>> function gradient
>>       65       NA
>>
>> $convergence
>> [1] 0
>>
>> $message
>> NULL
>>
>> However, the vector that gets passed to f doesn't have its names attached:
>>
>> > f <- function(parms) {
>> +  print(parms)
>> +  (parms["x"]-1)^2+(parms["y"]-2)^2
>> + }
>> > optim(c(x=3,y=4), f)
>> [1] 3 4
>> Error in optim(c(x = 3, y = 4), f) : function cannot be evaluated at
>> initial parameters
>>
>> Is this something that should be fixed, i.e. could it be fixed without
>> making optim() substantially slower?  If not, it's at least something
>> that should be documented.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> [hidden email] mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>>
>

______________________________________________
[hidden email] mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel