Skip to contents

Estimate the mode of a numeric distribution using kernel density estimation.

Usage

estimate_mode(x, na_rm = TRUE, ...)

Arguments

x

Numeric vector.

na_rm

Logical value. If TRUE, missing values are removed before estimation.

...

Further arguments passed to stats::density().

Value

A numeric value giving the estimated mode of the distribution.

Details

The function computes a kernel density estimate with stats::density() and returns the value of x at which the estimated density is largest.

Missing values are removed by default. Additional arguments are passed to stats::density(), for example bw, kernel, or n.

Examples

x <- c(1, 1, 2, 2, 2, 3, 4, NA)

estimate_mode(x)
#> [1] 1.956091
estimate_mode(x, na_rm = TRUE)
#> [1] 1.956091
estimate_mode(x, bw = "nrd0")
#> [1] 1.956091

set.seed(123)
y <- rnorm(100, mean = 5)
estimate_mode(y)
#> [1] 4.807602