Skip to content
Prev 287122 / 398498 Next

contour for plotting confidence interval on scatter plot of bivariate normal distribution

OK, the following seems to work
still do not understand exactly why...

library(MASS)

# parameters:
n<-100

# generate samples:
set.seed(138813)
#seed <- .Random.seed
x<-rnorm(n); y<-rnorm(n)

# estimate non-parameteric density surface via kernel smoothing
den<-kde2d(x, y, n=n)
# store z values of density estimate in an extra variable
den.z <-den$z

# this is the critical block, which I still do not comprehend in detail
z <- array()
for (i in 1:n){
        z.x <- max(which(den$x < x[i]))
        z.y <- max(which(den$y < y[i]))
        z[i] <- den$z[z.x, z.y]
}

# store class/level borders of confidence interval in variables
confidence.border <- quantile(z, probs=1-0.6827, na.rm = TRUE) # +-1sd

plot(x,y)
par(new=TRUE)
contour(den, levels=confidence.border, col = "red", add = TRUE)

The results look at least plausible. Do not know if they are really true.
Still hoping for some replies ;), Felix


Am 03.03.12 15:54, schrieb drflxms:

Thread (16 messages)

drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Greg Snow contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Peter Dalgaard contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 David Winsemius contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Greg Snow contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Peter Dalgaard contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Greg Snow contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 drflxms contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 Michael Friendly contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3 David Winsemius contour for plotting confidence interval on scatter plot of bivariate normal distribution Mar 3