Hi all, I have a sample of n values from a bivariate distribution (from a MCMC procedure). How could I draw a contour plot of "the joint density" based on that sample ? Sorry if I was not too clear. Thans in advance, Regards, Caio [[alternative HTML version deleted]]
On Thu, 4 Oct 2007, Caio Azevedo wrote:> Hi all, > > I have a sample of n values from a bivariate distribution (from a MCMC > procedure). How could I draw a contour plot of "the joint density" based on > that sample ?You need to estimate the density, and contour that estimate. MASS has examples using its kde2d function. There are other methods available, e.g. in package sm.> > Sorry if I was not too clear. > > Thans in advance, > > Regards, > > Caio > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >-- Brian D. Ripley, ripley at stats.ox.ac.uk Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/ University of Oxford, Tel: +44 1865 272861 (self) 1 South Parks Road, +44 1865 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On 10/4/07, Prof Brian Ripley <ripley at stats.ox.ac.uk> wrote:> On Thu, 4 Oct 2007, Caio Azevedo wrote: > > > Hi all, > > > > I have a sample of n values from a bivariate distribution (from a MCMC > > procedure). How could I draw a contour plot of "the joint density" based on > > that sample ? > > You need to estimate the density, and contour that estimate. MASS has > examples using its kde2d function. There are other methods available, > e.g. in package sm.If you want something ready made, ggplot2 uses kde2d to draw contours from a kernel density estimate - http://had.co.nz/ggplot2/stat_density_2d.html Hadley -- http://had.co.nz/
> I have a sample of n values from a bivariate distribution (from a MCMC > procedure). How could I draw a contour plot of "the joint density" based on > that sample ?here is a fast 2D density estimator. Not very sophisticated, but works. The function assumes that data are in the form of a matrix with (first) two columns containing x and y coordinates. To plot the result: image(dens2d(x)) or contour(dens2d(x)) Play with the h parameter to change the smoothness of the surface.>dens2dfunction(x, nx = 20, ny = 20, margin = 0.05, h = 1) { xrange <- max(x[, 1]) - min(x[, 1]) yrange <- max(x[, 2]) - min(x[, 2]) xmin <- min(x[, 1]) - xrange * margin xmax <- max(x[, 1]) + xrange * margin ymin <- min(x[, 2]) - yrange * margin ymax <- max(x[, 2]) + yrange * margin xstep <- (xmax - xmin)/(nx - 1) ystep <- (ymax - ymin)/(ny - 1) xx <- xmin + (0:(nx - 1)) * xstep yy <- ymin + (0:(ny - 1)) * ystep g <- matrix(0, ncol = nx, nrow = ny) n <- dim(x)[[1]] for(i in 1:n) { coefx <- dnorm(xx - x[i, 1], mean = 0, sd = h) coefy <- dnorm(yy - x[i, 2], mean = 0, sd = h) g <- g + coefx %*% t(coefy)/n } return(list(x = xx, y = yy, z = g)) }