Compute the **Highest Density Interval (HDI)** of posterior distributions.
All points within this interval have a higher probability density than points
outside the interval. The HDI can be used in the context of uncertainty
characterisation of posterior distributions as **Credible Interval (CI)**.

hdi(x, ...) # S3 method for numeric hdi(x, ci = 0.95, verbose = TRUE, ...) # S3 method for data.frame hdi(x, ci = 0.95, verbose = TRUE, ...) # S3 method for MCMCglmm hdi(x, ci = 0.95, verbose = TRUE, ...) # S3 method for sim.merMod hdi( x, ci = 0.95, effects = c("fixed", "random", "all"), parameters = NULL, verbose = TRUE, ... ) # S3 method for sim hdi(x, ci = 0.95, parameters = NULL, verbose = TRUE, ...) # S3 method for emmGrid hdi(x, ci = 0.95, verbose = TRUE, ...) # S3 method for stanreg hdi( x, ci = 0.95, effects = c("fixed", "random", "all"), component = c("location", "all", "conditional", "smooth_terms", "sigma", "distributional", "auxiliary"), parameters = NULL, verbose = TRUE, ... ) # S3 method for brmsfit hdi( x, ci = 0.95, effects = c("fixed", "random", "all"), component = c("conditional", "zi", "zero_inflated", "all"), parameters = NULL, verbose = TRUE, ... ) # S3 method for BFBayesFactor hdi(x, ci = 0.95, verbose = TRUE, ...)

x | Vector representing a posterior distribution, or a data frame of such
vectors. Can also be a Bayesian model ( |
---|---|

... | Currently not used. |

ci | Value or vector of probability of the (credible) interval - CI
(between 0 and 1) to be estimated. Default to |

verbose | Toggle off warnings. |

effects | Should results for fixed effects, random effects or both be returned? Only applies to mixed models. May be abbreviated. |

parameters | Regular expression pattern that describes the parameters
that should be returned. Meta-parameters (like |

component | Should results for all parameters, parameters for the conditional model or the zero-inflated part of the model be returned? May be abbreviated. Only applies to brms-models. |

A data frame with following columns:

`Parameter`

The model parameter(s), if`x`

is a model-object. If`x`

is a vector, this column is missing.`CI`

The probability of the credible interval.`CI_low`

,`CI_high`

The lower and upper credible interval limits for the parameters.

Unlike equal-tailed intervals (see `eti()`

) that typically exclude `2.5%`

from each tail of the distribution and always include the median, the HDI is
*not* equal-tailed and therefore always includes the mode(s) of posterior
distributions.

The `95%`

or `89%`

Credible Intervals (CI)
are two reasonable ranges to characterize the uncertainty related to the estimation (see here for a discussion about the differences between these two values).

The `89%`

intervals (`ci = 0.89`

) are deemed to be more stable than, for
instance, `95%`

intervals (Kruschke, 2014). An effective sample size
of at least 10.000 is recommended if one wants to estimate `95%`

intervals
with high precision (Kruschke, 2014, p. 183ff). Unfortunately, the
default number of posterior samples for most Bayes packages (e.g., `rstanarm`

or `brms`

) is only 4.000 (thus, you might want to increase it when fitting
your model). Moreover, 89 indicates the arbitrariness of interval limits -
its only remarkable property is being the highest prime number that does not
exceed the already unstable `95%`

threshold (McElreath, 2015).

However, `95%`

has some advantages too. For instance, it
shares (in the case of a normal posterior distribution) an intuitive
relationship with the standard deviation and it conveys a more accurate image
of the (artificial) bounds of the distribution. Also, because it is wider, it
makes analyses more conservative (i.e., the probability of covering 0 is
larger for the `95%`

CI than for lower ranges such as `89%`

), which is a good
thing in the context of the reproducibility crisis.

A `95%`

equal-tailed interval (ETI) has `2.5%`

of the distribution on either
side of its limits. It indicates the 2.5th percentile and the 97.5h
percentile. In symmetric distributions, the two methods of computing credible
intervals, the ETI and the HDI, return similar results.

This is not the case for skewed distributions. Indeed, it is possible that
parameter values in the ETI have lower credibility (are less probable) than
parameter values outside the ETI. This property seems undesirable as a summary
of the credible values in a distribution.

On the other hand, the ETI range does change when transformations are applied
to the distribution (for instance, for a log odds scale to probabilities):
the lower and higher bounds of the transformed distribution will correspond
to the transformed lower and higher bounds of the original distribution.
On the contrary, applying transformations to the distribution will change
the resulting HDI.

There is also a `plot()`

-method implemented in the see-package.

Kruschke, J. (2014). Doing Bayesian data analysis: A tutorial with R, JAGS, and Stan. Academic Press.

McElreath, R. (2015). Statistical rethinking: A Bayesian course with examples in R and Stan. Chapman and Hall/CRC.

Credits go to ggdistribute and HDInterval.

#> 89% HDI: [-1.58, 1.67]#> Highest Density Interval #> #> 80% HDI | 90% HDI | 95% HDI #> --------------------------------------------- #> [-1.34, 1.30] | [-1.60, 1.74] | [-2.03, 1.99]#> Highest Density Interval #> #> Parameter | 95% HDI #> ------------------------- #> X1 | [-1.67, 2.37] #> X2 | [-1.91, 1.93] #> X3 | [-2.20, 2.03] #> X4 | [-1.77, 2.03]#> Highest Density Interval #> #> Parameter | 80% HDI | 90% HDI | 95% HDI #> --------------------------------------------------------- #> X1 | [-1.36, 1.36] | [-1.36, 2.05] | [-1.67, 2.37] #> X2 | [-0.99, 1.49] | [-1.63, 1.53] | [-1.91, 1.93] #> X3 | [-1.50, 1.22] | [-1.50, 1.95] | [-2.20, 2.03] #> X4 | [-0.75, 1.39] | [-1.48, 1.39] | [-1.77, 2.03]if (FALSE) { library(rstanarm) model <- stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0) hdi(model) hdi(model, ci = c(.80, .90, .95)) library(emmeans) hdi(emtrends(model, ~1, "wt")) library(brms) model <- brms::brm(mpg ~ wt + cyl, data = mtcars) hdi(model) hdi(model, ci = c(.80, .90, .95)) library(BayesFactor) bf <- ttestBF(x = rnorm(100, 1, 1)) hdi(bf) hdi(bf, ci = c(.80, .90, .95)) }