[R] maximum number of subdivisions reached
Dennis Murphy
djmuser at gmail.com
Mon Aug 29 20:03:21 CEST 2011
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, . . <xkziloj at 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.weylandt at 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, . . <xkziloj at 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 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.
>>
>>
>
> ______________________________________________
> 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.
>
More information about the R-help
mailing list