[R] Retrieving value computed in inner function call

Pablo Lewinger lewinger at usc.edu
Wed Sep 13 10:20:27 CEST 2006


Though not obvious at first the posting you pointed me too is very helpful 
indeed. Thanks a lot Gabor.

Juan Pablo

At 08:48 PM 9/12/2006 -0400, Gabor Grothendieck wrote:
>Check out:
>
>http://finzi.psych.upenn.edu/R/Rhelp02a/archive/83547.html
>
>On 9/12/06, Juan Pablo Lewinger <lewinger at usc.edu> wrote:
>>Dear R users,
>>
>>Consider the following example function:
>>
>>f = function(a,b) {
>>    g = function(x) a*x + b
>>    h = function(x) g(x)^2 + x^2
>>    opt = optimize(h,lower = -1, upper = 1)
>>    x.min = opt$minimum
>>    h.xmin = opt$objective
>>    g.xmin = g(x.min)
>>    return(c(x.min, h.xmin, g.xmin))
>>}
>>
>>In my real problem the function that plays the role of "g" is costly
>>to compute. Now, to minimize "h", "optimize" calls "h" with different
>>values of x. In particular, at the end of the optimization, "h" would
>>be called with argument x.min, the minimizer of h(x). Therefore,
>>buried somewhere, there has to be a call to "g" with argument x=x.min
>>which I would like to retrieve in order to avoid the extra call to
>>"g" in the line before the return. Can this be done without too much pain?
>>
>>I'd very much appreciate any help.
>>
>>
>>
>>Juan Pablo Lewinger
>>Department of Preventive Medicine
>>University of Southern California
>>
>>______________________________________________
>>R-help at stat.math.ethz.ch mailing list
>>https://stat.ethz.ch/mailman/listinfo/r-help
>>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list