[R] Errors in Integrate
David Winsemius
dwinsemius at comcast.net
Thu Jan 27 21:03:35 CET 2011
On Jan 27, 2011, at 2:38 PM, mcitkowicz wrote:
>
> Hello,
>
> I have written the function I would like to integrate in two ways:
>
> denfxn <- function(yy,vv,a2,b2,mu2) {
> pp <- 1-pnorm(yy/sqrt(vv))
> part1 <- pp^(a2-1)
> part2 <- (1-pp)^(b2-1)
> part3 <- dnorm(yy,mu2,sqrt(vv))
> return(part1*part2*part3) }
>
> denfxnorg <- function(yy,vv,a2,b2,mu2) {
> pp <- 1-pnorm(yy/sqrt(vv))
> pp <- if (pp < .001) .001
> else if (pp > .999) .999
> else pp
> part1 <- pp^(a2-1)
> part2 <- (1-pp)^(b2-1)
> part3 <- dnorm(yy,mu2,sqrt(vv))
> return(part1*part2*part3) }
>
> The first function is the original function and the second one
> allows for
> restrictions around pp.
>
> When I integrate my function using the first version, it works just
> fine:
>> a2 <- .9
>> b2 <- .9
>> mu2 <- .1
>>
>> vv = v[i]
>> yy = y[i]
>> lower <- mu2-6*sqrt(vv)
>> upper <- mu2+6*sqrt(vv)
>> integrate(denfxn,lower,upper,vv=vv,a2=a2,b2=b2,mu2=mu2)
> 1.327635 with absolute error < 1.2e-08
>
> However, when I integrate my function using the second version (with
> restrictions), I obtain the following error messages:
You should learn to distinguish error messages from warning messages.
This warning is telling you that you have chosen the wrong function
for your efforts at truncation.
You perhaps should have used ifelse which is designed for vector
manipulation rather than if() {}else{} which is acontrol structure not
appropriate to that purpose:
?Control
?ifelse
>> a2 <- .9
>> b2 <- .9
>> mu2 <- .1
>>
>> vv = v[i]
>> yy = y[i]
>> lower <- mu2-6*sqrt(vv)
>> upper <- mu2+6*sqrt(vv)
>> integrate(denfxnorg,lower,upper,vv=vv,a2=a2,b2=b2,mu2=mu2)
> 1.588887 with absolute error < 1.2e-07
> Warning messages:
> 1: In if (pp < 0.001) 0.001 else if (pp > 0.999) 0.999 else pp :
> the condition has length > 1 and only the first element will be used
> 2: In if (pp > 0.999) 0.999 else pp :
> the condition has length > 1 and only the first element will be used
> 3: In if (pp < 0.001) 0.001 else if (pp > 0.999) 0.999 else pp :
> the condition has length > 1 and only the first element will be used
> 4: In if (pp > 0.999) 0.999 else pp :
> the condition has length > 1 and only the first element will be used
> 5: In if (pp < 0.001) 0.001 else if (pp > 0.999) 0.999 else pp :
> the condition has length > 1 and only the first element will be used
>
> I do not understand why integrate crashes
Please don't use the work "crash" to refer to a warning message. And
please read the error message. This message says nothing about
integrate. It refers only to the "if" function.
> when I include those restrictions,
> as pp does not have length greater than 1. When I simply try to
> obtain the
> estimate using both ways, I do not obtain the error message:
>> denfxn(yy,vv,a2,b2,mu2)
> [1] 0.001433364
>> denfxnorg(yy,vv,a2,b2,mu2)
> [1] 0.001433364
>
> Thus, I believe the problem is coming from the integrate function,
> rather
> than from my function. Any ideas of what could happening?
No.
>
> Thank you in advance,
> Martyna
> --
> View this message in context: http://r.789695.n4.nabble.com/Errors-in-Integrate-tp3242986p3242986.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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.
David Winsemius, MD
West Hartford, CT
More information about the R-help
mailing list