Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

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 http://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
)

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

Value

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

Author(s)

Ben Bolker and Frank Harrell


rmsb

Bayesian Regression Modeling Strategies

v0.0.2
GPL (>= 3)
Authors
Frank Harrell [aut, cre] (<https://orcid.org/0000-0002-8271-5493>), Ben Goodrich [ctb] (contributed Stan code), Ben Bolker [ctb] (wrote original code that is folded into the pdensityContour function), Doug Bates [ctb] (write original code for highest posterior density interval that is folded into the HPDint function)
Initial release
2021-02-27

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.