Hello All, I have searched but haven't been able to find an answer to this question. I'm writing a function that needs to be able evaluate the right-hand side of a general non-linear formula for different parameter values, similar to what nls() does. I looked through the nls() code but it isn't clear what is going on. Could someone point me to a reference or a way I might do this? Thanks -- View this message in context: http://r.789695.n4.nabble.com/Model-Formulae-Evaluation-tp3610328p3610328.html Sent from the R help mailing list archive at Nabble.com.
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. -- View this message in context: http://r.789695.n4.nabble.com/Model-Formulae-Evaluation-tp3610328p3611329.html Sent from the R help mailing list archive at Nabble.com.
On Mon, Jun 20, 2011 at 1:31 PM, albeam <beam.andrew at gmail.com> wrote:> Hi everyone, > > Thank you for the help, I apologize for not "providing commented, minimal, > self-contained, reproducible code." I was looking for some pointers about > how to do this in general, but it would have been helpful for me to post a > specific example. Anyway, after the feedback this is the solution I have > settled on for future reference: > > ## Simple Linear Model Example ## > X <- c(1,2,3,4,5) > user.formula <- Y ~ a + b*X > > param.values <- list(a=1.5, b=3, x=X) > eval(user.formula[[3]], envir = param.values) > > #Output# > [1] ?4.5 ?7.5 10.5 13.5 16.5 > > ## Something a little more complicated ## > X <- c(.01,.1,1,10,25,50,100) > > user.formula <- Y ~ Top - (Top-Bot)/(1+(X/b)^W) > param.values <- list(Top=100, Bot=1, b=.05, W=2, x=X) > > eval(user.formula[[3]], envir = param.values) > > #Output# > [1] ?1.039584 ?4.807692 80.200000 99.753117 99.960416 99.990101 99.997525 > > Thanks again. >Also try this:> library(gsubfn) > X <- c(.01,.1,1,10,25,50,100) > fo <- Y ~ Top - (Top-Bot)/(1+(X/b)^W) > FUN <- fn$identity(fo[-2]) # drop LHS> # and then in your inner loop:> param.values <- list(Top=100, Bot=1, b=.05, W=2, X=X) > do.call(FUN, param.values)[1] 4.807692 80.200000 99.753117 99.997525 99.999604 99.999901 99.999975 where we note that we only had to create FUN once. -- Statistics & Software Consulting GKX Group, GKX Associates Inc. tel: 1-877-GKX-GROUP email: ggrothendieck at gmail.com