[R] generate a series of fucntion

Jim Rogers jrogers at cantatapharm.com
Tue Jul 22 14:41:15 CEST 2003


You need to be careful if you take the approach below. Try it:

> g1 <- list(function(x) log(x), function(x) log(x+1), function(x) x,
function(x) x^2)
> g2 <- lapply(g1, function(g) {
+   function(x,t) exp(-t[1]-t[2]*g(x)-t[3]*g(1+x))
+ }
+              )
> lapply(g2, function(f) get("g", environment(f)))
[[1]]
function(x) x^2

[[2]]
function(x) x^2

[[3]]
function(x) x^2

[[4]]
function(x) x^2

Because of lazy evaluation + lexical scoping, "g" doesn't get evaluated
where you would naturally think.

To avoid this, you can do:

> g2 <- lapply(g1, function(g) {
+   g  # or, for readability, force(g), as of R 1.7.0 
+   function(x,t) exp(-t[1]-t[2]*g(x)-t[3]*g(1+x))
+ }
+              )
> lapply(g2, function(f) get("g", environment(f)))
[[1]]
function(x) log(x)

[[2]]
function(x) log(x+1)

[[3]]
function(x) x

[[4]]
function(x) x^2

I was first confused by this a few months back, and Luke Tierney, Robert
Gentleman, and Thomas Lumley were kind enough to explain it to me. If
you want to find that thread, do an R site search for "lexical scoping
Jim Rogers".

Cheers,
Jim

> Message: 43
> Date: Mon, 21 Jul 2003 16:15:35 -0400
> From: "Liaw, Andy" <andy_liaw at merck.com>
> Subject: RE: [R] generate a series of fucntion
> To: "'ximing wu'" <xiwu at uoguelph.ca>
> Cc: "'r-help at stat.math.ethz.ch'" <r-help at stat.math.ethz.ch>
> Message-ID:
> 	<3A822319EB35174CA3714066D590DCD50205C8E3 at usrymx25.merck.com>
> Content-Type: text/plain
> 
> Does this do sort of what you want?
> 
> > g1 <- list(function(x) log(x), function(x) log(x+1), function(x) x,
> function(x) x^2)
> > g1
> [[1]]
> function(x) log(x)
> 
> [[2]]
> function(x) log(x+1)
> 
> [[3]]
> function(x) x
> 
> [[4]]
> function(x) x^2
> 
> > lapply(g1, function(g) {function(x,t)
exp(-t[1]-t2*g(x)-t[3]*g(1+x))})
> [[1]]
> function(x,t) exp(-t[1]-t2*g(x)-t[3]*g(1+x))
> <environment: 022BCBD8>
> 
> [[2]]
> function(x,t) exp(-t[1]-t2*g(x)-t[3]*g(1+x))
> <environment: 022BBD08>
> 
> [[3]]
> function(x,t) exp(-t[1]-t2*g(x)-t[3]*g(1+x))
> <environment: 022BBDCC>
> 
> [[4]]
> function(x,t) exp(-t[1]-t2*g(x)-t[3]*g(1+x))
> <environment: 022BBE90>
> 
> You did not say what g1 and g2 actually are, but I suppose the above
should
> give you enough hint.
> 
> Andy
> 
> > -----Original Message-----
> > From: ximing wu [mailto:xiwu at uoguelph.ca] 
> > Sent: Monday, July 21, 2003 3:54 PM
> > To: r-help at stat.math.ethz.ch
> > Subject: [R] generate a series of fucntion
> > 
> > 
> > Hi there,
> > 
> > I want to generate a large amount of functions,
> > 
> > say f=function(x,t) exp(-t[1]-t[2]*g_1(x)-t[3]*g_2(1+x))
> > 
> > where g_1(x) and g_2(x) are from a long list of moments, such 
> > as x, x^2, 
> > log(x), log(1+x) .. and so on.
> > 
> > Any suggestions on how to do this efficiently?
> > 
> > thanks a lot.
> > 
> > x.w
> > 
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list 
> > https://www.stat.math.ethz.ch/mailman/listinfo> /r-help
> > 


James A. Rogers, Ph.D. <rogers at cantatapharm.com>
Statistical Scientist
Cantata Pharmaceuticals
300 Technology Square, 5th floor
Cambridge, MA  02139
617.225.9009 x312
Fax 617.225.9010




More information about the R-help mailing list