Skip to contents

Credible interval for a unimodal density, usually a posterior distribution in a Bayesian framework.

Usage

credInt(dist, level = 0.95,
        type = c("HPD", "eqtail", "both"),
        plot = FALSE, ...)

Arguments

dist

Either a character describing a distribution such as "norm" or "gamma", or an object with class "density" or a numeric vector assumed to provide a sample of the distribution.

level

Numeric value specifying the probability level. A vector of length \(> 1\) is not accepted yet.

type

When set to "HPD", the credible interval is Highest Posterior Density interval. When set to "eqtail" the bounds are found by using the cumulative distribution function and by allocating the same probability to the left and right tails.

plot

Logical. If TRUE a plot will be shown.

...

Other arguments to be passed to the distribution functions: density, quantile.

Value

A numeric vector with the two bounds or an array when type is "both".

Details

In some applications, dist will be a specific distribution within a parametric family. This can occur for instance when a conjugacy property holds.

Note

When dist is intended to describe a distribution within a parametric family, it must be taken as the character string which is prefixed by "d", "p" and "q" to get the density, the distribution and the quantile functions. The values of the parameters can passed through the dots ... See Examples.

Examples

credInt("gumbel", level = 0.95, plot = TRUE)

#>             L        U
#> HPD -1.561441 3.161364
credInt("gamma", level = 0.95, plot = TRUE, shape = 4)

#>             L        U
#> HPD 0.7122661 7.948062
credInt("gumbel", level = 0.95, plot = TRUE, type = "both")

#>                L        U
#> HPD    -1.561441 3.161364
#> eqtail -1.305323 3.676247
x <- rgamma(n = 6000, shape = 4)
credInt(x, level = 0.95, plot = TRUE)
#>            L        U
#> HPD 0.730383 7.847553
## nearly the same
d <- density(x)
credInt(x, level = 0.95, plot = TRUE)

#>            L        U
#> HPD 0.730383 7.847553