[R] Model Formulae Evaluation

Gabor Grothendieck ggrothendieck at gmail.com
Mon Jun 20 16:28:08 CEST 2011


On Mon, Jun 20, 2011 at 9:46 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
> On Mon, Jun 20, 2011 at 9:08 AM, albeam <beam.andrew at gmail.com> 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.
>
> fn in gsubfn, when used to preface a function like this, will convert
> certain formula arguments to functions.  Here we preface the identity
> function so that we just get back the converted argument directly:
>
>> library(subfn)
>> fo <- ~ x + 1 # or fo <- x ~ x + 1
>> fn$identity(fo)
> function (x)
> x + 1
>
> See
> http://gsubfn.googlecode.com
> ?fn
> vignette("gsubfn")
>

Also note that converting to a function only has to be done once so it
can be factored out of the inner loop of the optimization.


-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.com



More information about the R-help mailing list