[R] Model Formulae Evaluation
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jun 20 21:13:34 CEST 2011
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
More information about the R-help
mailing list