MyNixOS website logo
Description

Mode Estimation.

Determines single or multiple modes (most frequent values). Checks if missing values make this impossible, and returns 'NA' in this case. Dependency-free source code. See Franzese and Iuliano (2019) <doi:10.1016/B978-0-12-809633-8.20354-3>.

Mode estimation in R

R-CMD-check

The moder package determines single or multiple modes (most frequent values). By default, its functions check whether missing values make this impossible, and return NA in this case. They have no dependencies.

Mode functions fill a gap in measures of central tendency in R. mean() and median() are built into the standard library, but there is a lack of properly NA-sensitive functions for calculating the mode. Use moder for this!

Installation

You can install the development version of moder like so:

remotes::install_github("lhdjung/moder")

Get started

library(moder)

Find the first mode with mode_first()

Everything is fine here:

mode_first(c(7, 8, 8, 9, 9, 9))
#> [1] 9

But what if some values are missing? Maybe there are so many missings that it’s impossible to tell which value is the most frequent one. If both NAs below are secretly 2, then 2 is the (first) mode. Otherwise, 1 is. The mode is unclear, so the function returns NA:

mode_first(c(1, 1, 2, NA, NA))
#> [1] NA

Ignore NAs using na.rm = TRUE if there is a strong rationale for it:

mode_first(c(1, 1, 2, NA, NA), na.rm = TRUE)
#> [1] 1

The next example is different. Even if the NA stands in for 8, there will only be three instances of 8 but four instances of 7. The mode is 7, independent of the true value behind NA.

mode_first(c(7, 7, 7, 7, 8, 8, NA))
#> [1] 7

Find all modes with mode_all()

This function captures multiple modes:

mode_all(c("a", "a", "b", "b", "c", "d", "e"))
#> [1] "a" "b"

If some values are missing but there would be multiple modes when ignoring NAs, mode_all() returns NA. That’s because missings can easily create an imbalance between the equally-frequent known values:

mode_all(c(1, 1, 2, 2, NA))
#> [1] NA

If NA masks either 1 or 2, that number is the (single) mode. As before, if the mode depends on missing values, the function returns NA.

Yet na.rm = TRUE makes the function ignore this:

mode_all(c(1, 1, 2, 2, NA), na.rm = TRUE)
#> [1] 1 2

Find the single mode (or NA) with mode_single()

mode_single() is stricter than mode_first(): It returns NA by default if there are multiple modes. Otherwise, it works the same way.

mode_single(c(3, 4, 4, 5, 5, 5))
#> [1] 5
mode_single(c("x", "x", "y", "y", "z"))
#> [1] NA

Find possible modes

These minimal and maximal sets of modes are possible given the missing value:

mode_possible_min(c("a", "a", "a", "b", "b", "c", NA))
#> [1] "a"
mode_possible_max(c("a", "a", "a", "b", "b", "c", NA))
#> [1] "a" "b"

Acknowledgements

Ken Williams’ mode functions on Stack Overflow were pivotal to moder.

Metadata

Version

0.2.1

License

Unknown

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows