get_stat_mode: Compute statistical mode of sample data

Description Usage Arguments Details Value Examples

Description

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

Usage

1

Arguments

x

vector: integer, numeric, logical, character or factor vector 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 get_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

numeric: the statistical mode computed from the supplied vector.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
get_stat_mode(c(1, 1, 3, 5, 1, 3, 1, 2))              # => 1
get_stat_mode(c(1, 3, 5, 2))                          # => NA
get_stat_mode(c(63, 62, 66, 67, 63, 70, 67, 68, 61))  # => 63, 67

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

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

toniprice/jutebag documentation built on May 12, 2019, 4:39 a.m.