rmsb (version 1.1-0)

pdensityContour: Bivariate Posterior Contour

Description

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/.

Usage

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
)

Value

a 2-column matrix with x and y coordinates unless pl=TRUE in which case a ggplot2 graphic is returned

Arguments

x

a numeric vector

y

a numeric vector the same length of x

method

defaults to 'ellipse', can be set to 'kernel'

prob

main probability coverage (the only one for method='ellipse')

otherprob

vector of other probability coverages for method='kernel'

h

vector of bandwidths for x and y. See MASS::kde2d().

n

number of grid points in each direction, defaulting to normal reference bandwidth (see bandwidth.nrd).

pl

set to TRUE to plot contours

Author

Ben Bolker and Frank Harrell