Skip to content

mode_hdi() returns an error #269

@ASKurz

Description

@ASKurz

This holds for related functions, like hdi().

# Load
library(tidyverse)
library(ggdist)  # Version 3.3.3

# Following a grid approximation example from McElreath (2020, Section 3.2.2)

# How many grid points would you like?
n <- 1000
n_success <- 3
n_trials  <- 3

d <- tibble(p_grid = seq(from = 0, to = 1, length.out = n),
            prior  = 1) |> 
  mutate(likelihood = dbinom(n_success, size = n_trials, prob = p_grid)) |> 
  mutate(posterior  = (likelihood * prior) / sum(likelihood * prior))

# Sample from the grid
n_samples <- 1e4
set.seed(3)
samples <- d |> 
  slice_sample(n = n_samples, weight_by = posterior, replace = TRUE)

# Check the distribution in a plot
samples |> 
  ggplot(aes(x = p_grid)) +
  geom_density()
Image

mode_hdi() returns an error for p_grid.

samples |> 
  mode_hdi(p_grid)
Error in quantile.default(dist_y, probs = 1 - .width, names = FALSE) : 
  missing values and NaN's not allowed if 'na.rm' is FALSE

What's the solution?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions