[Rd] inconsistent results when calling functions with other func (PR#869)

Peter Dalgaard BSA p.dalgaard@biostat.ku.dk
08 Mar 2001 10:23:50 +0100


Paul.White@dsto.defence.gov.au writes:

> Hello Bug people,
> 
> I have an unexpected behavior and am unsure whether the problem is in my
> thinking, my implementation or the program R.
> 
> Basically I get two different answers depending on how I call a function
> which takes other functions as arguments as indicated below.
> To me it should make no difference if f is a function that returns the
> function g then z(f(x)) whould give the same as
> y<- f(x)
> z(y)
> but I appear to get different answers as below. I have not distilled the
> problem down to something you can replicate in total but have I done
> something obviously wrong ?

Lazy evaluation sometimes plays tricks on the unsuspecting. In the
first case, eifs.at.time.using.density(5000,eifs,1e-4,2) is evaluated
when adapt first calls prob.of.fracture, in the other it is of course
evaluated from the start. If eifs.at.time.using.density is dependent
on values that might have changed...

You're not showing us eifs.at.time.using.density, so it is difficult
to say whether you have a bug or not.

> > risk <- function(f,g,H,lower.limits,upper.limits) 
> + {   prob.of.fracture <-  function(x)
> +       { a  <- x[1] 
> +         kc <- x[2] 
> +         f(a) * g(kc) * H(a,kc) }
> +
> adapt(2,lower.limits,upper.limits,minpts=200,maxpts=20000,prob.of.fracture,e
> ps=1e-3) }
> >
> risk(eifs.at.time.using.density(5000,eifs,1e-4,2),g,H,c(0.001,20),c(.5,60))
> [1] 1.999471
>        value       relerr       minpts       lenwrk        ifail 
>     28.39852 0.0003455776          627         2803            0 
> > 
> > eifs.5000 <- eifs.at.time.using.density(5000,eifs,1e-4,2)
> [1] 1.999471
> > risk(eifs.5000,g,H,c(0.001,20),c(.5,60))
>        value       relerr       minpts       lenwrk        ifail 
> 5.461088e-13 0.0006449058          741         2803            0 
> 
> 
> -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
> r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
> Send "info", "help", or "[un]subscribe"
> (in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
> _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
> 

-- 
   O__  ---- Peter Dalgaard             Blegdamsvej 3  
  c/ /'_ --- Dept. of Biostatistics     2200 Cph. N   
 (*) \(*) -- University of Copenhagen   Denmark      Ph: (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)             FAX: (+45) 35327907
-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-devel mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-devel-request@stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._