Learn R Programming

sjstats (version 0.11.0)

hdi: Compute high density intervals (HDI) for MCMC samples

Description

hdi() computes the high density interval for values from MCMC samples.

Usage

hdi(x, prob = 0.9, trans = NULL)

Arguments

x

A vector of values from a probability distribution (e.g., posterior probabilities from MCMC sampling), or a stanreg-object.

prob

Scalar between 0 and 1, indicating the mass within the credible interval that is to be estimated.

trans

Name of a function or character vector naming a function, used to apply transformations on the returned HDI-values.

Value

If x is a vector, hdi() returns a vector of length two with the lower and upper limit of the HDI; if x is a stanreg-object, returns a tibble with lower and upper HDI-limits for each predictor.

Details

Computation is based on the code from Kruschke 2015, pp. 727f.

References

Kruschke JK. Doing Bayesian Data Analysis: A Tutorial with R, JAGS, and Stan. 2nd edition. Academic Press, 2015

Examples

Run this code
# NOT RUN {
library(rstanarm)
fit <- stan_glm(mpg ~ wt + am, data = mtcars, chains = 1)
hdi(fit)

# fit logistic regression model
fit <- stan_glm(
  vs ~ wt + am,
  data = mtcars,
  family = binomial("logit"),
  chains = 1
)
# compute hdi, transform on "odds ratio scale"
hdi(fit, trans = exp)

# }

Run the code above in your browser using DataLab