Message-ID: <43F1FFF6.7000702@lancaster.ac.uk>
Date: 2006-02-14T16:06:14Z
From: Barry Rowlingson
Subject: Using optim() with a function which returns more than a scalar - alternatives?
In-Reply-To: <C83C5E3DEEE97E498B74729A33F6EAEC038781C2@DJFPOST01.djf.agrsci.dk>
S??ren H??jsgaard wrote:
> I want to numerically maximize a function with optim (maximization
> over several arguments). optim() needs a function which returns a
> scalar only. However, it could be nice to be able to "take other
> things out" from the function as well. I'tried to create an attribute
> to the scalar with what I want to take out, but that attribute
> disappears in optim(). I looked into the code to see if it could
> (easily) be modified such that it could work on a function which
> returns e.g. a list or a vector (and then it should be maximized over
> the first element). But I gave up... Any suggestion will be
> appreciated...
Have your function return a scalar value and any additional data in an
attribute, and then optimise. Then call your function one more time
using the resulting parameters found by optim(), and you'll get the
attributes.
Pro: No need to mess with the code of optim()
Con: One more function call required. Probably not a problem since
optim() will have called it a few times anyway.
BArry