[R] Alternatives to integrate?

Berend Hasselman bhh at xs4all.nl
Wed Sep 7 09:47:53 CEST 2011


. wrote:
> 
> Hi, continuing the improvements...
> 
> I've prepared a new code:
> 
> ddae <- function(individuals, frac, sad, samp="pois", trunc=0, ...) {
>   dots <- list(...)
>   Compound <- function(individuals, frac, n.species, sad, samp, dots) {
>     print(c("Size:", length(individuals), "Compound individuals:",
> individuals, "End."))
>     RegDist <- function(n.species, sad, dots) {  # "RegDist" may be
> Exponential, Gamma, etc.
>       dcom <- paste("d", as.name(sad), sep="")
>       dots <- as.list(c(n.species, dots))
>       ans <- do.call(dcom, dots)
>       return(ans)
>     }
>     SampDist <- function(individuals, frac, n.species, samp) {  #
> "SampDist" may be Poisson or Negative Binomial
>       dcom <- paste("d", samp, sep="")
>       lambda <- frac * n.species
>       dots <- as.list(c(individuals, lambda))
>       ans <- do.call(dcom, dots)
>       return(ans)
>     }
>     ans <- RegDist(n.species, sad, dots) * SampDist(individuals, frac,
> n.species, samp)
>     return(ans)
>   }
>   IntegrateScheme <- function(Compound, individuals, frac, sad, samp,
> dots) {
>     print(c("Size:", length(individuals), "Integrate individuals:",
> individuals))
>     ans <- integrate(Compound, 0, 2000, individuals, frac, sad, samp,
> dots)$value
>     return(ans)
>   }
>   ans <- IntegrateScheme(Compound, individuals, frac, sad, samp, dots)
>   return(ans)
> }
> 
> ddae(2, 0.05, "exp")
> 
> Now I can't understand what happen to "individuals", why is it
> changing in value and size? I've tried to "traceback()" and "debug()",
> but I was not smart enough to understand what is going on.
> 
> Could you, please, give some more help?
> 

>From the help for integrate argument f :

an R function taking a numeric first argument and returning a numeric vector
of the same length. .....

Function Compound is passed to integrate. First argument is "individuals"
and integrate is integrating over individuals. That is why it is changing in
value and size: integrate is only doing what you asked it do.

The code is too uncommented and convoluted to supply further comments.
You really should simplify this 

Berend


--
View this message in context: http://r.789695.n4.nabble.com/Alternatives-to-integrate-tp3783624p3795491.html
Sent from the R help mailing list archive at Nabble.com.



More information about the R-help mailing list