hdi: Highest density interval

Description Usage Arguments Details Value Examples

Description

This is a function that will calculate the highest density interval from a posterior sample.

Usage

1
hdi(x, prob = 0.95, warn = TRUE)

Arguments

x

Numeric vector of a distribution of data, typically a posterior sample

prob

Width of the interval from some distribution. Defaults to 0.95.

warn

Option to turn off multiple sample warning message Must be in the range of [0,1].

Details

The default is to calcualte the highest 95 percent interval. It can be used with any numeric vector instead of having to use one of the specific MCMC classes. This function has been adapted from John K. Kruschke (2011). Doing Bayesian Data Analaysis: A Tutorial with R and BUGS.

Value

Numeric range

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
x <- qnorm(seq(1e-04, .9999, length.out=1001))
hdi_95 <- hdi(x, .95)
hdi_50 <- hdi(x, .50)

hist(x, br=50)
abline(v=hdi_95, col="red")
abline(v=hdi_50, col="green")

x <- exp(seq(pi * (1 - (1/16)), pi, len = 1000))
x <- c(x, rev(x)[-1])
x <- c(-x, x)
plot(sort(x), type="l")
plot(density(x, adjust=0.25))
abline(v=hdi(x, p=.49), col=2)
abline(v=hdi(x, p=.50), col=3)

ggdistribute documentation built on May 2, 2019, 10:25 a.m.