Skip to content

Add argument to force continuous or discrete Mode #240

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 21 additions & 7 deletions R/point_interval.R
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,11 @@ globalVariables(c("y", "ymin", "ymax"))
#' and unbounded data.
#' @param n For [hdi()] and [Mode()], the number of points to use to estimate highest-density
#' intervals or modes.
#' @param type For [Mode()], the type of estimator to use. Can be `"discrete"` to compute
#' the discrete mode (the single most frequently occurring value) or `"continuous"`
#' to compute the maximum a posteriori (MAP) estimate (the maximum value of the
#' the estimated density). The default `"auto"` uses `"discrete"` if `x` is
#' an integer or non-numeric (see [`rlang::is_integerish()`]) and `"continuous"` otherwise.
#' @param weights For [Mode()], an optional vector, which (if not `NULL`) is of the same length
#' as `x` and provides weights for each element of `x`.
#' @return A data frame containing point summaries and intervals, with at least one column corresponding
Expand Down Expand Up @@ -522,20 +527,29 @@ hdi_.distribution = function(x, .width = .95, na.rm = FALSE, ..., density = dens
#' @rdname point_interval
#' @importFrom rlang is_integerish
#' @importFrom stats density
Mode = function(x, na.rm = FALSE, ...) {
Mode = function(x, na.rm = FALSE, type = c("auto", "discrete", "continuous"), ...) {
UseMethod("Mode")
}
#' @export
#' @rdname point_interval
Mode.default = function(x, na.rm = FALSE, ..., density = density_bounded(trim = TRUE), n = 2001, weights = NULL) {
Mode.default = function(x, na.rm = FALSE, type = c("auto", "discrete", "continuous"), ..., density = density_bounded(trim = TRUE), n = 2001, weights = NULL) {
type = match.arg(type)
if (na.rm) {
x = x[!is.na(x)]
} else if (anyNA(x)) {
return(NA_real_)
}
density = match_function(density, "density_")

if (is_integerish(x)) {
if (type == "auto") {
if (is_integerish(x) || !is.numeric(x)) {
type = "discrete"
} else {
type = "continuous"
}
}

if (type == "discrete") {
if (is.null(weights)) {
# for the discrete case, based on https://stackoverflow.com/a/8189441
ux = unique(x)
Expand All @@ -553,20 +567,20 @@ Mode.default = function(x, na.rm = FALSE, ..., density = density_bounded(trim =
}
#' @export
#' @rdname point_interval
Mode.rvar = function(x, na.rm = FALSE, ...) {
Mode.rvar = function(x, na.rm = FALSE, type = c("auto", "discrete", "continuous"), ...) {
draws = posterior::draws_of(x)
.dim = dim(draws)
apply(draws, seq_along(.dim)[-1], Mode, na.rm = na.rm, weights = weights(x))
apply(draws, seq_along(.dim)[-1], Mode, na.rm = na.rm, type = type, weights = weights(x))
}
#' @importFrom stats optim
#' @export
#' @rdname point_interval
Mode.distribution = function(x, na.rm = FALSE, ...) {
Mode.distribution = function(x, na.rm = FALSE, type = c("auto", "discrete", "continuous"), ...) {
find_mode = function(x) {
if (anyNA(x)) {
NA_real_
} else if (distr_is_sample(x)) {
Mode(distr_get_sample(x), na.rm = na.rm, weights = distr_get_sample_weights(x))
Mode(distr_get_sample(x), na.rm = na.rm, type = type, weights = distr_get_sample_weights(x))
} else if (distr_is_constant(x)) {
quantile(x, 0.5)[[1]]
} else if (distr_is_discrete(x)) {
Expand Down
13 changes: 10 additions & 3 deletions man/point_interval.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading