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 ks::kde() or a data frame or matrix with numerical columns that can be passed to as_kde().

prob

Probability of the HDR

h

Bandwidth for univariate kernel density estimate. Default is kde_bandwidth.

H

Bandwidth for multivariate kernel density estimate. Default is kde_bandwidth.

...

If y is supplied, other arguments are passed to kde. Otherwise, additional arguments are passed to as_kde.

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)

[Package weird version 1.0.2 Index]