hdi.Rd
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.89, verbose = TRUE, ...) # S3 method for data.frame hdi(x, ci = 0.89, verbose = TRUE, ...) # S3 method for MCMCglmm hdi(x, ci = 0.89, verbose = TRUE, ...) # S3 method for sim.merMod hdi( x, ci = 0.89, effects = c("fixed", "random", "all"), parameters = NULL, verbose = TRUE, ... ) # S3 method for sim hdi(x, ci = 0.89, parameters = NULL, verbose = TRUE, ...) # S3 method for emmGrid hdi(x, ci = 0.89, verbose = TRUE, ...) # S3 method for stanreg hdi( x, ci = 0.89, effects = c("fixed", "random", "all"), parameters = NULL, verbose = TRUE, ... ) # S3 method for brmsfit hdi( x, ci = 0.89, effects = c("fixed", "random", "all"), component = c("conditional", "zi", "zero_inflated", "all"), parameters = NULL, verbose = TRUE, ... ) # S3 method for BFBayesFactor hdi(x, ci = 0.89, verbose = TRUE, ...)
x  Vector representing a posterior distribution. Can also be a


...  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. Metaparameters (like 
component  Should results for all parameters, parameters for the conditional model or the zeroinflated part of the model be returned? May be abbreviated. Only applies to brmsmodels. 
A data frame with following columns:
Parameter
The model parameter(s), if x
is a modelobject. 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 equaltailed intervals (see eti()
) that typically exclude 2.5%
from each tail of the distribution and always include the median, the HDI is
not equaltailed and therefore always includes the mode(s) of posterior
distributions.
By default, hdi()
and eti()
return the 89% intervals (ci = 0.89
),
deemed to be more stable than, for instance, 95% intervals (Kruschke, 2014).
An effective sample size of at least 10.000 is recommended if 95% intervals
should be computed (Kruschke, 2014, p. 183ff). 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).
A 90% equaltailed interval (ETI) has 5% of the distribution on either
side of its limits. It indicates the 5th percentile and the 95h 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.
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.
#> # Highest Density Interval #> #> 89% HDI #>  #> [1.46, 1.50] #>#> # Highest Density Intervals #> #> 80% HDI #>  #> [1.05, 1.30] #> #> #> 90% HDI #>  #> [1.39, 1.67] #> #> #> 95% HDI #>  #> [1.86, 1.83] #> #>#> # Highest Density Interval #> #> Parameter  89% HDI #>  #> X1  [1.43, 1.27] #> X2  [1.29, 1.68] #> X3  [1.35, 1.93] #> X4  [1.63, 1.83] #>#> # Highest Density Intervals #> #> Parameter  80% HDI #>  #> X1  [1.00, 1.16] #> X2  [1.10, 1.35] #> X3  [1.32, 1.12] #> X4  [1.44, 1.28] #> #> #> Parameter  90% HDI #>  #> X1  [1.43, 1.31] #> X2  [1.29, 1.71] #> X3  [1.35, 2.00] #> X4  [1.72, 1.83] #> #> #> Parameter  95% HDI #>  #> X1  [1.88, 1.31] #> X2  [1.67, 2.11] #> X3  [1.65, 2.03] #> X4  [1.87, 2.30] #> #>library(rstanarm) model < stan_glm(mpg ~ wt + gear, data = mtcars, chains = 2, iter = 200, refresh = 0)#> Warning: The largest Rhat is 1.07, indicating chains have not mixed. #> Running the chains for more iterations may help. See #> http://mcstan.org/misc/warnings.html#rhat#> Warning: Bulk Effective Samples Size (ESS) is too low, indicating posterior means and medians may be unreliable. #> Running the chains for more iterations may help. See #> http://mcstan.org/misc/warnings.html#bulkess#> Warning: Tail Effective Samples Size (ESS) is too low, indicating posterior variances and tail quantiles may be unreliable. #> Running the chains for more iterations may help. See #> http://mcstan.org/misc/warnings.html#tailesshdi(model)#> # Highest Density Interval #> #> Parameter  89% HDI #>  #> (Intercept)  [31.74, 50.13] #> wt  [6.78, 4.38] #> gear  [1.94, 1.32] #>#> # Highest Density Intervals #> #> Parameter  80% HDI #>  #> (Intercept)  [32.50, 47.06] #> wt  [6.54, 4.65] #> gear  [1.89, 0.71] #> #> #> Parameter  90% HDI #>  #> (Intercept)  [28.74, 47.58] #> wt  [6.78, 4.34] #> gear  [2.28, 1.11] #> #> #> Parameter  95% HDI #>  #> (Intercept)  [28.90, 50.13] #> wt  [7.02, 4.13] #> gear  [2.28, 1.84] #> #>#> # Highest Density Interval #> #> Parameter  89% HDI #>  #> overall  [6.78, 4.38] #>