Hi: integrate() is not a vectorized function. This appears to work:
sapply(1:2, function(x) func(x, 0.1, 0.1, sad = Exp)) [1] 0.250 0.125 In this case, sapply() is a disguised for loop. HTH, Dennis On Mon, Aug 29, 2011 at 9:45 AM, . . <xkzi...@gmail.com> wrote: > Ooops, > > sorry! > > The problem occurs when > > func(1:2,0.1,0.1,sad=Exp) > > On Mon, Aug 29, 2011 at 12:27 PM, R. Michael Weylandt > <michael.weyla...@gmail.com> wrote: >> Can't help, code runs fine on my machine once you change "valu" to "value." >> Are you sure it fails in a vanilla run of R and isn't caused by any other >> choices you have made along the way? >> >> Michael >> >> PS -- Here's the code >> >> func <- function(y, a, rate, sad){ >> f3 <- function(z){ >> f1 <- function(y,a,n){ >> dpois(y,a*n) >> } >> >> f2 <- function(n,rate){ >> dexp(n,rate) >> } >> >> f <- function(n){ >> f1(y,a,n)*f2(n,rate) >> } >> >> r <- 0 >> r1 <- 1 >> x1 <- 0 >> dx <- 20 >> >> while(r1 > 10e-500){ >> r1 <- integrate(f,x1,x1+dx)$value >> r <- r + r1 >> x1 <- x1 + dx >> } >> >> r + integrate(f,x1,Inf)$value >> } >> sapply(y,f3) >> } >> >> V = func(200,0.1,0.1,sad=Exp) >> >> On Mon, Aug 29, 2011 at 11:16 AM, . . <xkzi...@gmail.com> wrote: >>> >>> Why I am getting >>> >>> Error in integrate(f, x1, x1 + dx) : >>> maximum number of subdivisions reached >>> >>> and can I avoid this? >>> >>> func <- function(y, a, rate, sad){ >>> f3 <- function(z){ >>> f1 <- function(y,a,n){ >>> dpois(y,a*n) >>> } >>> f2 <- function(n,rate){ >>> dexp(n,rate) >>> } >>> f <- function(n){ >>> f1(y,a,n)*f2(n,rate) >>> } >>> r <- 0 >>> r1 <- 1 >>> x1 <- 0 >>> dx <- 20 >>> while(r1 > 10e-500){ >>> r1 <- integrate(f,x1,x1+dx)$value >>> r <- r + r1 >>> x1 <- x1 + dx >>> } >>> r + integrate(f,x1,Inf)$valu >>> } >>> sapply(y,f3) >>> } >>> func(200,0.1,0.1,sad=Exp) >>> >>> Thanks in advance. >>> >>> ______________________________________________ >>> 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. >> >> > > ______________________________________________ > 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. > ______________________________________________ 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.