compute_stat_mode: Compute statistical mode of sample data

View source: R/compute_stat_mode.R

compute_stat_modeR Documentation

Compute statistical mode of sample data

Description

Computes the statistical mode of the supplied vector under some simplifying assumptions (see 'Details' for more information).

Usage

compute_stat_mode(x)

Arguments

x

A vector (integer, numeric, logical, character or factor) for which the mode should be computed.

Details

If x is numeric and assumes integer values then the mode is computed by tabulating its frequencies: the maximum value (or, if there are ties, maximum values) would then be the mode (or modes). If all values are unique then there is no mode and compute_stat_mode returns NA.

If x is numeric and assumes real values, it is assumed to be a sample from a continuous random variable. In this case the mode is estimated by computing the kernel density function of x and returning the (single) value which corresponds to the maximum of the estimated kernel density. Note that in this case only one modal value - i.e. the first - will be returned.

Value

The statistical mode computed from the supplied vector.

Examples

compute_stat_mode(c(1, 1, 3, 5, 1, 3, 1, 2)) # => 1
compute_stat_mode(c(1, 3, 5, 2)) # => NA
compute_stat_mode(c(4, 4, 4, 4, 4, 4)) # => NA
compute_stat_mode(c(63, 62, 66, 67, 63, 70, 67, 68, 61)) # => 63, 67

compute_stat_mode(c("a", "b", "c", "c")) # => "c"

set.seed(10)
compute_stat_mode(rnorm(100)) # => -0.1795327

set.seed(100)
mean(vapply(1:5, function(x) compute_stat_mode(rnorm(100)),
  FUN.VALUE = numeric(1)
)) # => -0.0069


toniprice/jute documentation built on Jan. 11, 2023, 8:23 a.m.