Computes coordinates of a highest density contour containing a given probability volume given a sample from a continuous bivariate distribution, and optionally plots. The default method assumes an elliptical shape, but one can optionally use a kernel density estimator.
Code adapted from embbook::HPDregionplot
. See https://www.sumsar.net/blog/2014/11/how-to-summarize-a-2d-posterior-using-a-highest-density-ellipse/.
pdensityContour(
x,
y,
method = c("ellipse", "kernel"),
prob = 0.95,
otherprob = c(0.01, 0.1, 0.25, 0.5, 0.75, 0.9),
h = c(1.3 * MASS::bandwidth.nrd(x), 1.3 * MASS::bandwidth.nrd(y)),
n = 70,
pl = FALSE
)
a 2-column matrix with x and y coordinates unless pl=TRUE
in which case a ggplot2
graphic is returned
a numeric vector
a numeric vector the same length of x
defaults to 'ellipse'
, can be set to 'kernel'
main probability coverage (the only one for method='ellipse'
)
vector of other probability coverages for method='kernel'
vector of bandwidths for x and y. See MASS::kde2d()
.
number of grid points in each direction, defaulting to normal reference bandwidth (see bandwidth.nrd
).
set to TRUE
to plot contours
Ben Bolker and Frank Harrell