Model Formulae Evaluation
On Jun 20, 2011, at 15:08 , albeam wrote:
Please allow me to clarify my original question. What I really need to be
able to do it is to take arbitrary functions and evaluate them for arbitrary
parameter values. I'm doing the optimization myself, so I need to be able to
take a user's function and evaluate them at the current parameter values
during my optimization process. So it would look something like this:
opt.fun <- function(user.formula, param.values)
{
#--- I would do some optimization here ---#
fitted.values <- eval.fun(user.formula, param.values) ##<---- this is
what I need
}
Where fitted.values is a vector of the same size as the x-values in
user.formula. nls() does this somehow. I could do this easily myself if I
have the user pass the formula in reverse polish notation, but I was hoping
there was a more canonical was to do this in R.
The canonical way goes something like
eval(user.formula[[3]], #i.e. the RHS
envir = as.list(param.values),
enclos = environment(user.formula))
or maybe enclos = list2env(data, parent = environment(user.formula)) if there's a data argument.
All untested, of course. If you want us to actually test our suggestions, follow the instructions
and provide commented, minimal, self-contained, reproducible code.
Peter Dalgaard Center for Statistics, Copenhagen Business School Solbjerg Plads 3, 2000 Frederiksberg, Denmark Phone: (+45)38153501 Email: pd.mes at cbs.dk Priv: PDalgd at gmail.com