[R] integral depending on a parameter

peter dalgaard pdalgd at gmail.com
Sun Aug 7 23:44:41 CEST 2011


On Aug 7, 2011, at 21:25 , Carl Witthoft wrote:

> Just chiming in here...
> 
> Granted some of the help files are a bit cryptic until you get used to them (but still much easier to understand than the Unix man pages :-) ).  This sort of thing does provide a chance to learn about the power of R.
> 
> From Rolf's example,  suppose that  integrate() did NOT have any additional arguments.  Then, do this:
> 
> bar <- function(x)  foo(x,y=pi/4)
> 
> integrate(bar, 0, 2*pi)
> 
> 
> As an exercise to the reader :-) , write a function which takes an input for the value of y and uses some subset of "paste, eval, do.call, parse" etc., to build the function  bar()   on the fly.
> 

> fortune("rethink")

If the answer is parse() you should usually rethink the question.
   -- Thomas Lumley
      R-help (February 2005)

To programmatically define the body of a function, consider something like

body(bar) <- bquote(foo(x,y=.(y))) 

but actually plain old lexical scoping does the trick

> g <- function(y) {
+       foo <- function(x){sin(x^2 + y^2)}
+       integrate(foo,0,2*pi)
+ }
> g(pi/4)
0.9314315 with absolute error < 1.5e-05
> g(pi/3)
0.920137 with absolute error < 2.7e-05
> g(pi/8)
0.7427584 with absolute error < 8.3e-06

as does various other varations over the "unbound variable" theme, e.g.

>  f <- local(function(x){sin(x^2 + y^2)})
> g <- function(y) {
+   assign("y", y, environment(f))
+   integrate(f, 0, 2*pi)
+ }
> g(pi/8)
0.7427584 with absolute error < 8.3e-06


> Carl
> 
> 
> <quote>
> From: Rolf Turner <rolf.turner_at_xtra.co.nz>
> Date: Sat, 06 Aug 2011 11:00:11 +1200
> On 06/08/11 08:17, brunero liseo wrote:
> > Is it possible to use the "integrate" function when it depends on a
> > parameter?
> > in other words can i get something like
> >
> > f(y) = integrate (g(x,y))?
> > If not, how else can I get that?
> > thanks in advance
> This really boils down to ``RTFM''.
> 
> The help for "integrate" ``clearly'' ( :-) ) states that there is a "..." argument
> which consists of ``additional arguments passed to f''. So use it.
> 
> E.g.:
> 
>     foo <- function(x,y){sin(x^2 + y^2)}
>     integrate(foo,0,2*pi,y=pi/4)
>     0.9314315 with absolute error < 1.5e-05
> 
> Most of the time, what you need to know is in the help. You just need to learn to read the help *carefully* and think along the lines: ``This *does* mean something useful and is actually expressed in a rational albeit it cryptic manner. Given that fact, what could this *possibly* mean?''
> That will usually get you there. Then *try* what you think you gleaned from the help using a *simple* example.
> 
>     cheers,
> 
>         Rolf Turner
> </quote>
> -- 
> -----
> Sent from my Cray XK6
> 
> ______________________________________________
> R-help at r-project.org 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.

-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com
"Døden skal tape!" --- Nordahl Grieg



More information about the R-help mailing list