| hdi | R Documentation |
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 class 'numeric'
hdi(x, ci = 0.95, verbose = TRUE, ...)
## S3 method for class 'data.frame'
hdi(x, ci = 0.95, rvar_col = NULL, verbose = TRUE, ...)
## S3 method for class 'brmsfit'
hdi(
x,
ci = 0.95,
effects = "fixed",
component = "conditional",
parameters = NULL,
verbose = TRUE,
...
)
## S3 method for class 'get_predicted'
hdi(x, ci = 0.95, use_iterations = FALSE, verbose = TRUE, ...)
x |
Vector representing a posterior distribution, or a data frame of such
vectors. Can also be a Bayesian model. bayestestR supports a wide range
of models (see, for example, |
... |
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. |
rvar_col |
A single character - the name of an |
effects |
Should variables for fixed effects ( For models of from packages brms or rstanarm there are additional options:
|
component |
Which type of parameters to return, such as parameters for the conditional model, the zero-inflated part of the model, the dispersion term, etc. See details in section Model Components. May be abbreviated. Note that the conditional component also refers to the count or mean component - names may differ, depending on the modeling package. There are three convenient shortcuts (not applicable to all model classes):
|
parameters |
Regular expression pattern that describes the parameters
that should be returned. Meta-parameters (like |
use_iterations |
Logical, if |
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. While this can be useful to better represent the credibility
mass of a distribution, the HDI also has some limitations. See spi() for
details.
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.5th 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.
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).
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.
Possible values for the component argument depend on the model class.
Following are valid options:
"all": returns all model components, applies to all models, but will only
have an effect for models with more than just the conditional model
component.
"conditional": only returns the conditional component, i.e. "fixed
effects" terms from the model. Will only have an effect for models with
more than just the conditional model component.
"smooth_terms": returns smooth terms, only applies to GAMs (or similar
models that may contain smooth terms).
"zero_inflated" (or "zi"): returns the zero-inflation component.
"location": returns location parameters such as conditional,
zero_inflated, or smooth_terms (everything that are fixed or random
effects - depending on the effects argument - but no auxiliary
parameters).
"distributional" (or "auxiliary"): components like sigma,
dispersion, beta or precision (and other auxiliary parameters) are
returned.
For models of class brmsfit (package brms), even more options are
possible for the component argument, which are not all documented in detail
here. See also ?insight::find_parameters.
There is also a plot()-method implemented in the see-package.
Credits go to ggdistribute and HDInterval.
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.
Other interval functions, such as hdi(), eti(), bci(),
spi(), si().
Other ci:
bci(),
ci(),
eti(),
si(),
spi()
library(bayestestR)
posterior <- rnorm(1000)
hdi(posterior, ci = 0.89)
hdi(posterior, ci = c(0.80, 0.90, 0.95))
hdi(iris[1:4])
hdi(iris[1:4], ci = c(0.80, 0.90, 0.95))
model <- suppressWarnings(
rstanarm::stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)
)
hdi(model)
hdi(model, ci = c(0.80, 0.90, 0.95))
hdi(emmeans::emtrends(model, ~1, "wt", data = mtcars))
model <- brms::brm(mpg ~ wt + cyl, data = mtcars)
hdi(model)
hdi(model, ci = c(0.80, 0.90, 0.95))
bf <- BayesFactor::ttestBF(x = rnorm(100, 1, 1))
hdi(bf)
hdi(bf, ci = c(0.80, 0.90, 0.95))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.