Ensure rank overlay plot starts at 0 even if not all bins present#332
Ensure rank overlay plot starts at 0 even if not all bins present#332
Conversation
| # Now ensure that all combinations of parameter, chain, and bin_start exist | ||
| # even if no counts are present (https://github.com/stan-dev/bayesplot/issues/331) | ||
| all_params_chains <- dplyr::distinct(data, .data$parameter, .data$chain) | ||
| all_bins <- dplyr::distinct(histobins, .data$bin_start, .data$cut) | ||
| combos <- dplyr::cross_join(all_params_chains, all_bins) | ||
| d_bin_counts <- full_join(combos, d_bin_counts, by = c("parameter", "chain", "bin_start")) %>% | ||
| mutate(n = dplyr::coalesce(n, 0L)) | ||
|
|
There was a problem hiding this comment.
There may be a more elegant solution, but this seems to get the job done
There was a problem hiding this comment.
Not sure if this is more elegant but an expand.grid with one left join and an if_else to fill the zeros should also work. Something like the following. Reads a little easier for me, though that might just be me. (not tested!)
all_combos <- tibble::as_tibble(expand.grid(
parameter = unique(data$parameter),
chain = unique(data$chain),
bin_start = unique(histobins$bin_start),
stringsAsFactors = FALSE
))
d_bin_counts <- all_combos %>%
left_join(d_bin_counts, by = c("parameter", "chain", "bin_start")) %>%
mutate(n = if_else(is.na(n), 0L, n))
There was a problem hiding this comment.
# Ensure all combinations exist and count values
d_bin_counts <- data %>%
left_join(histobins, by = "value_rank") %>%
count(.data$parameter, .data$chain, .data$bin_start) %>%
right_join(
dplyr::expand(.data$parameter, .data$chain, unique(histobins$bin_start)),
by = c("parameter", "chain", "bin_start")
) %>%
mutate(n = dplyr::coalesce(n, 0L))
Or courtesy of claude, telling me my solution was verbouse and not dplyr-ish enough.
There was a problem hiding this comment.
Thanks to you and Claude, both!
There was a problem hiding this comment.
dplyr::expand
Except Claude didn't realize there is no dplyr::expand . There is tidyr::expand, but we don't already depend on tidyr so probably best not to add another dependency just for this.
|
The failure on macOS-latest (devel) is due to the vdiffr package failing to compile on GitHub actions |
|
Maybe add a |
There's a |
Co-Authored-By: Maximilian Scholz <6530123+sims1253@users.noreply.github.com>
|
@sims1253 Thanks. I just pushed a commit that uses basically your first suggestion of |
|
Going to merge since everything is passing except the unrelated failure on macos-latest (devel) that happens when installing the vdiffr dependency. Thanks again @sims1253. |
fixes #331
Code to test: