[R] Problem implementing adapt()
Sergey Goriatchev
sergeyg at gmail.com
Sun Nov 4 18:43:12 CET 2007
Hello, I am trying to compute rectangle probability of bivariate
normal distribution with the following function:
bvnrectangle <- function(mu, Sig, xmin, xmax, ymin, ymax){
library(adapt)
Siginv <- solve(Sig)
detSig <- det(Sig)
areal <- adapt(ndim=2, lower=c(xmin,ymin), upper=c(xmax,ymax),
minpts=100, maxpts=1000, functn=bvnpdf, mu, Siginv, detSig)$value
areal
}
bvnpdf <- function(xy, mu, Siginv, detSig){
f<-numeric()
for(xloop in 1:length(xy[1])){
x <- xy[[1]][xloop]
if(xy[[1]]>xy[[2]] & (1==2)) {
f[xloop] <- 0} else {
v <- rbind(xy[1], xy[2])
e <- t(v-mu) %*% Siginv %*% (v-mu)
f[xloop] <- exp(-e/2)/(2*pi)/sqrt(detSig)
}
}
f
}
bvnpdf works correctly (tested),
ex.: bvnpdf(c(0,0), c(0,0), solve( matrix(c(1, 0.5, 0.5, 1), nrow=2,
byrow=FALSE)), det( matrix(c(1, 0.5, 0.5, 1), nrow=2, byrow=FALSE)))
but when I run bvnrectangle, ex.: bvnrectangle(c(0,0), matrix(c(1,
0.5, 0.5, 1), nrow=2, byrow=FALSE), 0, 8, 0, 8)
I get the following error message:
Error in v - mu : non-conformable arrays
Why do I get this error message. I just can't understand what I am
doing wrong... Please, help.
Thanks in advance,
Regards,
Sergey
More information about the R-help
mailing list