hdr_table {weird} | R Documentation |
Table of Highest Density Regions
Description
Compute the highest density regions (HDR) for a kernel density estimate. 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),
upper
(the upper ends of the interval),
mode
(the point at which the density is maximized within each interval).
Usage
hdr_table(
y = NULL,
density = NULL,
prob = c(0.5, 0.99),
h = kde_bandwidth(y, method = "double"),
H = kde_bandwidth(y, method = "double"),
...
)
Arguments
y |
Numerical vector or matrix of data |
density |
Probability density function, either estimated by |
prob |
Probability of the HDR |
h |
Bandwidth for univariate kernel density estimate. Default is |
H |
Bandwidth for multivariate kernel density estimate. Default is |
... |
If |
Value
A tibble
Author(s)
Rob J Hyndman
References
Hyndman, R J. (1996) Computing and Graphing Highest Density Regions, The American Statistician, 50(2), 120–126.
Examples
# Univariate HDRs
y <- c(rnorm(100), rnorm(100, 3, 1))
hdr_table(y = y)
hdr_table(density = ks::kde(y))
x <- seq(-4, 4, by = 0.01)
hdr_table(density = data.frame(y = x, density = dnorm(x)), prob = 0.95)
# Bivariate HDRs
y <- cbind(rnorm(100), rnorm(100))
hdr_table(y = y)
grid <- seq(-4, 4, by=0.1)
density <- expand.grid(grid, grid) |>
mutate(density = dnorm(Var1) * dnorm(Var2))
hdr_table(density = density)