mmy:::warn_debug_makevars_flags()
require(mmy, quietly = TRUE)

Modes

Some alternative functions written in R to find the modes.

#' @references 
#' \url{https://stackoverflow.com/a/8189441}
modes.sof <- function(x) {
  ux <- unique(x)
  tab <- tabulate(match(x, ux))
  ux[tab == max(tab)]
}

find.modes <- function(x) {
  tbl <- table(x)
  as.numeric(names(tbl[tbl == max(tbl)]))
}

Some mini tests:

all.same.values <- c(1, 1, 1, 1, 1)
single.mode <- c(11, 12, 12, 12, 13, 14, 15)
multiple.mode <- c(25, 26, 26, 27, 27, 28, 29)

stopifnot(identical(modes.sof(all.same.values), 1))
stopifnot(identical(modes.sof(single.mode), 12))
stopifnot(identical(modes.sof(multiple.mode), c(26, 27)))

stopifnot(identical(find.modes(all.same.values), 1))
stopifnot(identical(find.modes(single.mode), 12))
stopifnot(identical(find.modes(multiple.mode), c(26, 27)))
uniform.sample <- sample(1:1000, 1e6, replace = TRUE)
results <- microbenchmark::microbenchmark(
  modes.sof = modes.sof(uniform.sample),
  find.modes = find.modes(uniform.sample),
  check = "identical"
)
results

Looks like find.modes() is the most inefficient.

Plot timings:

ggplot2::autoplot(results)

group_sequence

long_seq <- c(seq(1e3), seq(1e4), seq(1e5))

profmem::profmem({
  group_sequence(long_seq)
})

File last created on r Sys.Date()



strboul/mmy documentation built on Sept. 24, 2021, 12:08 p.m.