hdi: Compute highest density intervals

Description Usage Arguments Value Examples

View source: R/hdi.R

Description

Compute highest density intervals

Usage

1
2
3
4
5
6
hdi(object, prob = 0.95, pars = NULL, regex_pars, ...)

## Default S3 method:
hdi(object, prob = 0.95, pars = NULL, regex_pars = NULL, ...)

hdi_from_grid(object, pars = NULL, prob = 0.95, posterior = "posterior")

Arguments

object

an object describing a posterior distribution

prob

the desired mass of the HDI region.

pars

a vector of parameter names

regex_pars

a regular expression for selecting parameter names

...

additional arguments (ignored for now)

Value

a data frame with 1 row per paramter and variables

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# Determining HDI of a beta(30,12) distribution
# Create a grid
Grid <-
  expand.grid(x = seq(0, 1, length.out = 201)) %>%
    mutate(posterior = dbeta(x, 30, 12))
hdi_from_grid(Grid, "x", prob = 0.9)
Grid %>% dplyr::slice_sample(n = 1E4, weight_by = posterior, replace = TRUE) %>%
  pull(x) %>%
  hdi(prob = 0.9)
x <- rnorm(25, 3, 2)  # some random data
Grid <-
  expand.grid(
    mean = seq(0, 10, length.out = 101),
    sd   = seq(0, 10, length.out = 101)
    ) %>%
    mutate(
      prior = 1,
      loglik =
        purrr::map2_dbl(mean, sd, ~ sum(dnorm(x, .x, .y, log = TRUE)), x = x),
      likelihood = exp(loglik),
      posterior = prior * likelihood
    )
 hdi_from_grid(Grid, pars = c("mean", "sd"))

rpruim/CalvinBayes documentation built on April 12, 2021, 1:49 p.m.