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
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 >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments, ...{{dropped}}
On Mon, 21 Jul 2003 15:54:24 -0400, ximing wu <xiwu at uoguelph.ca> wrote :>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?I'd just have one function, with a definition like this: f <- function(x, t, g1, g2) exp(-t[1]-t[2]*g1(x)-t[3]*g2(1+x)) and pass different functions to it, e.g. f(x, t, function(x) x, function(x) x^2) If you really need separate functions, then still do what I suggest above, but have your individual functions defined in terms of one, e.g. f1 <- function(x,t) f(x, t, function(x) x, function(x) x^2) Duncan Murdoch
ximing wu <xiwu at uoguelph.ca> wrote: > 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? Unfortunately, outer() doesn't like function elements. Let's suppose you have vectors g.1[1:n] and g.2[1:n], and that you want to construct a list f[1:n] of functions. f <- lapply(1:n, function(i) { p <- g.1[i] q <- g.2[i] function (x, t) exp(-t[1] - t[2]*p(x) - t[3]*q(x)) }) will make a list of n functions. Note that the function passed to lappy() returns a function. In R, it returns a *different* function each time, and each of these points to an environment with its own copy of p and q.
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 aboveshould> 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