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