Skip to content

Commit 2227e7d

Browse files
jgabrysims1253
andcommitted
Use expand.grid + left_join
Co-Authored-By: Maximilian Scholz <6530123+sims1253@users.noreply.github.com>
1 parent 4147555 commit 2227e7d

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

R/mcmc-traces.R

+9-5
Original file line numberDiff line numberDiff line change
@@ -313,11 +313,15 @@ mcmc_rank_overlay <- function(x,
313313

314314
# Now ensure that all combinations of parameter, chain, and bin_start exist
315315
# even if no counts are present (https://github.com/stan-dev/bayesplot/issues/331)
316-
all_params_chains <- dplyr::distinct(data, .data$parameter, .data$chain)
317-
all_bins <- dplyr::distinct(histobins, .data$bin_start, .data$cut)
318-
combos <- dplyr::cross_join(all_params_chains, all_bins)
319-
d_bin_counts <- full_join(combos, d_bin_counts, by = c("parameter", "chain", "bin_start")) %>%
320-
mutate(n = dplyr::coalesce(n, 0L))
316+
all_combos <- dplyr::as_tibble(expand.grid(
317+
parameter = unique(data$parameter),
318+
chain = unique(data$chain),
319+
bin_start = unique(histobins$bin_start),
320+
stringsAsFactors = FALSE
321+
))
322+
d_bin_counts <- all_combos %>%
323+
left_join(d_bin_counts, by = c("parameter", "chain", "bin_start")) %>%
324+
mutate(n = dplyr::if_else(is.na(n), 0L, n))
321325

322326
# Duplicate the final bin, setting the left edge to the greatest x value, so
323327
# that the entire x-axis is used,

0 commit comments

Comments
 (0)