Compute a table of highest density regions (HDR) for a distributional object.
The HDRs are returned as a tibble with one row per interval and columns:
prob
(giving the probability coverage),
density
(the value of the density at the boundary of the HDR),
For one dimensional density functions, the tibble also has columns
lower
(the lower ends of the intervals), and
upper
(the upper ends of the intervals).
Arguments
- object
Distributional object such as that returned by
dist_kde()
- prob
Vector of probabilities giving the HDR coverage (between 0 and 1)
Examples
# Univariate HDRs
c(dist_normal(), dist_kde(c(rnorm(100), rnorm(100, 3, 1)))) |>
hdr_table(c(0.5, 0.95))
#> # A tibble: 5 × 5
#> prob distribution lower upper density
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 0.5 N(0, 1) -0.674 0.674 0.213
#> 2 0.95 N(0, 1) -1.96 1.96 0.0521
#> 3 0.5 kde[1d, h=0.65] -0.672 1.06 0.213
#> 4 0.5 kde[1d, h=0.65] 2.00 3.11 0.213
#> 5 0.95 kde[1d, h=0.65] -1.69 5.04 0.0521
dist_kde(oldfaithful$duration) |> hdr_table(0.95)
#> # A tibble: 2 × 5
#> prob distribution lower upper density
#> <dbl> <chr> <dbl> <dbl> <dbl>
#> 1 0.95 kde[1d, h=5] 115. 155. 0.00161
#> 2 0.95 kde[1d, h=5] 197. 268. 0.00161
# Bivariate HDRs
dist_kde(oldfaithful[, c("duration", "waiting")]) |> hdr_table(0.90)
#> # A tibble: 1 × 3
#> distribution prob density
#> <chr> <dbl> <dbl>
#> 1 kde[2d, H={(40, 3.3e+02)', (3.3e+02, 2.1e+04)'}] 0.9 0.000000902