[R] Optim stripping attributes from relistable objects

Prof Brian Ripley ripley at stats.ox.ac.uk
Mon Aug 18 14:21:50 CEST 2008


On Mon, 18 Aug 2008, rhelp.20.trevva at spamgourmet.com wrote:

> Ok -  there is an easy work-around by just to passing the skeleton to
> the objective function. But would it be possible to clarify this point
> in the relist() help file please (specificaly the example I would
> think), implies that attributes are copied through by optim and that
> this is expected behaviour?

Already done.  You can always check the current version on the SVN server, 
here

https://svn.r-project.org/R/branches/R-2-7-branch/src/library/utils/man/relist.Rd


>
> Thanks,
>
> Mark
>
>
>
> 2008/8/18 Prof Brian Ripley - ripley at stats.ox.ac.uk
> <>:
>> On Mon, 18 Aug 2008, Katharine Mullen wrote:
>>
>>>>
>>>> The following code is inspired by the help file for the relist()
>>>> function (see?relist), which explicitly details how you can use a
>>>
>>> The example is in the 'Details' section and, indeed, it looks like it no
>>> longer works.
>>
>> I don't think it ever did (after all there is a comment that dnorm does not
>> have the parameters stated).
>>
>> The subject line here illustrates the misunderstanding.  Optim *strips*
>> nothing -- it creates a new object to pass to the function.  The help says
>>
>>     Any names given to 'par' will be copied to the vectors passed to
>>     'fn' and 'gr'.
>>
>> Note the use of 'copy' here.  You cannot assume that any other attributes
>> will be copied, and this behaviour has not been changed (except to add names
>> in March 2006, well before relist() was added), AFAICS.
>>
>>>> relistable object in conjunction with optim to pass and reconstruct
>>>> complex parameter structures/groupings. The idea is that the optim()
>>>> function can only work with vectors, but in many cases you would like
>>>> to use a complex structure inside the objective function- relist is
>>>> one way to do that. The problem is that optim appears to be stripping
>>>> the attributes and therefore the example doesn't seem to run, giving
>>>> the error at the bottom.
>>>
>>> You can get around this by specifying skeleton for relist:
>>>
>>> rb.banana <- function(params) {
>>>   params <- relist(params, skeleton=list(x=NA,y=NA))
>>>   return( (1-params$x)^2 + 100*(params$y - params$x^2)^2)
>>> }
>>>
>>> ipar <-  as.relistable(list(x=5,y=0))
>>>
>>> initial.params <- unlist(ipar)
>>>
>>> xx <- optim(unlist(initial.params), rb.banana)
>>>
>>>>
>>>>> rb.banana <- function(params) {
>>>>
>>>> +   #Params is initially a vector
>>>> +   cat("Params initially has the attributes:\n")
>>>> +   print(names(attributes(params)))
>>>> +   #Relisting it turns it into a list...
>>>> +   params <- relist(params)
>>>> +   cat("---------\n")
>>>> +   #..which can then be called in the standard list manner
>>>> +   return( (1-params$x)^2 + 100*(params$y - params$x^2)^2)
>>>> + }
>>>>>
>>>>> ipar            <-  as.relistable(list(x=5,y=0))
>>>>> initial.params  <-  unlist(ipar)
>>>>>
>>>>> #Test to see if rb.banana works properly in the "normal" case
>>>>> rb.banana(initial.params)
>>>>
>>>> Params initially has the attributes:
>>>> [1] "names"    "skeleton"
>>>> ---------
>>>> [1] 62516
>>>>>
>>>>> #OK, that's good. How about with optim though?
>>>>> optim(initial.params,rb.banana)
>>>>
>>>> Params initially has the attributes:
>>>> [1] "names"
>>>> Error in relist(params) :
>>>>  The flesh argument does not contain a skeleton attribute.
>>>> Either ensure you unlist a relistable object, or specify the skeleton
>>>> separately.
>>>>>
>>>>
>>>> What's going on here? Has this functionality been removed and the
>>>> documentation in relist() not updated? Or has the feature been broken?
>>>> Or have I misinterpreted something here (it wouldn't be the first
>>>> time!!)
>>>>
>>>> Am running R version 2.7.1 (2008-06-23) under windows.
>>>>
>>>> Cheers,
>>>>
>>>> Mark
>>>>
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>>>
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>> PLEASE do read the posting guide
>>> http://www.R-project.org/posting-guide.html
>>> and provide commented, minimal, self-contained, reproducible code.
>>>
>>
>> --
>> 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
>>
>

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



More information about the R-help mailing list