You may perhaps want to be more specific about your problem then "First option don't gave results as good. May another method ... ?" if you want help...
I'm not entirely sure what your problem with this sort of discrete numerical integration is, other than you are making it too hard: rectIntegrate <- function(f, from, to, ...) { from <- floor(from); to <- ceiling(to) n = seq(from, to) return(sum(do.call(f, c(list(n),...)))) } > rectIntegrate(dpois,0,20,3) [1] 1 > rectIntegrate(function(x,...){dpois(x,...)*x},0,20,3) [1] 3 So it seems to work pretty well. What was the problem again? Michael On Sun, Sep 18, 2011 at 11:40 AM, . . <xkzi...@gmail.com> wrote: > Hello all, > > Returning to this question, I would like to ask for help on how to > integrate the PMF of a discrete distribution - Michael's guess about > the most immediate problem (Sep 01, 2011; 2:44pm). > > I was thinking in two ways to do this: First is numerically integrate > the function but sampling it only at integer values. For this, I've > used the rectangle method of integration: > > leftrect <- function(f, from, to, n, ...) { > h <- (to - from) / n > sum <- 0 > d <- list(...) > for(i in seq(from, to-h, h)) { > dots <- c(list(i), d) > sum <- sum + do.call(f, dots) > } > return(h * sum) > } > > Second option in my mind is to, find and replace the discrete function > by a continuous version, but I've no idea how to do this. > > First option don't gave results as good. May another method ... ? Is > it possible to specify the sampling points in "Integrate"? > > Any comments are welcome. > > Thanks in advanve. > > > On Wed, Sep 7, 2011 at 4:47 AM, Berend Hasselman <b...@xs4all.nl> wrote: > > > > . 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. > > > > ______________________________________________ > > R-help@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. > > > [[alternative HTML version deleted]] ______________________________________________ R-help@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.