[R] Errors in Integrate
mcitkowicz
mcitkowicz at ucmerced.edu
Thu Jan 27 20:38:29 CET 2011
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:
> 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 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?
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.
More information about the R-help
mailing list