Skip to content
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

Mode calculated incorrectly #236

Open
fkohrt opened this issue Jul 7, 2024 · 1 comment · May be fixed by #240
Open

Mode calculated incorrectly #236

fkohrt opened this issue Jul 7, 2024 · 1 comment · May be fixed by #240

Comments

@fkohrt
Copy link

fkohrt commented Jul 7, 2024

The following code is supposed to display a spike at the density's mode, but that's apparently not the case.

data.frame(x = datasets::AirPassengers) |>
  ggplot2::ggplot(ggplot2::aes(x = x)) +
  ggdist::stat_slab(ggplot2::aes(height = max(ggplot2::after_stat(pdf)))) +
  ggdist::stat_spike(
    ggplot2::aes(height = max(ggplot2::after_stat(pdf))),
    at = ggdist::Mode
  )

Rplot

@bwiernik bwiernik linked a pull request Jul 18, 2024 that will close this issue
@bwiernik
Copy link
Contributor

bwiernik commented Jul 18, 2024

The function is detecting the x data as discrete because the values are integers. As a result, it is computing the discrete mode (single most frequent value), rather than the continuous maximum a posterior (MAP) value (the highest point in the estimated data distribution).

I've opened a PR here that adds an argument to override this default detection and force computing either the discrete or continuous estimator #240

library(tidyverse)
library(ggdist)

dat <- data.frame(x = as.numeric(datasets::AirPassengers))


data.frame(x = datasets::AirPassengers) |>
  ggplot2::ggplot(ggplot2::aes(x = x)) +
  ggdist::stat_slab(ggplot2::aes(height = max(ggplot2::after_stat(pdf)))) +
  ggdist::stat_spike(
    ggplot2::aes(height = max(ggplot2::after_stat(pdf))),
    at = Mode
  )
#> Don't know how to automatically pick scale for object of type <ts>. Defaulting
#> to continuous.

data.frame(x = datasets::AirPassengers) |>
  ggplot2::ggplot(ggplot2::aes(x = x)) +
  ggdist::stat_slab(ggplot2::aes(height = max(ggplot2::after_stat(pdf)))) +
  ggdist::stat_spike(
    ggplot2::aes(height = max(ggplot2::after_stat(pdf))),
    at = \(x) Mode(x, type = "continuous")
  )
#> Don't know how to automatically pick scale for object of type <ts>. Defaulting
#> to continuous.

Created on 2024-07-18 with reprex v2.0.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants