Skip to content

Commit 5eced5b

Browse files
authored
Merge pull request #179 from stan-dev/rank-histogram
Rank histogram
2 parents fb533f2 + 691fd80 commit 5eced5b

28 files changed

+2016
-186
lines changed

DESCRIPTION

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ Imports:
2929
utils,
3030
rlang (>= 0.3.0),
3131
ggridges,
32+
tibble,
3233
hexbin
3334
Suggests:
3435
gridExtra (>= 2.2.1),

NAMESPACE

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,8 @@ export(mcmc_nuts_treedepth)
7070
export(mcmc_pairs)
7171
export(mcmc_parcoord)
7272
export(mcmc_parcoord_data)
73+
export(mcmc_rank_hist)
74+
export(mcmc_rank_overlay)
7375
export(mcmc_recover_hist)
7476
export(mcmc_recover_intervals)
7577
export(mcmc_recover_scatter)
@@ -78,6 +80,7 @@ export(mcmc_rhat_data)
7880
export(mcmc_rhat_hist)
7981
export(mcmc_scatter)
8082
export(mcmc_trace)
83+
export(mcmc_trace_data)
8184
export(mcmc_trace_highlight)
8285
export(mcmc_violin)
8386
export(neff_ratio)

NEWS.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,23 @@
66

77
<!-- Items for next release go here* -->
88

9+
* Two new plots have been added for inspecting the distribution of ranks.
10+
Rank-normalized histograms were introduced by the Stan team's [new paper on
11+
MCMC diagnostics](https://arxiv.org/abs/1903.08008). (#178, #179)
12+
13+
`mcmc_rank_hist()`: A traditional traceplot (`mcmc_trace()`) visualizes how
14+
sampled values the MCMC chains mix over the course of sampling. A
15+
rank-normalized histogram (`mcmc_rank_hist()`) visualizes how the *ranks* of
16+
values from the chains mix together. An ideal plot would show the ranks mixing
17+
or overlapping in a uniform distribution.
18+
19+
`mcmc_rank_overlay()`: Instead of drawing each chain's histogram in a separate
20+
panel, this plot draws the top edge of the chains' histograms in a single
21+
panel.
22+
23+
* Added `mcmc_trace_data()`, which returns the data used for plotting the trace
24+
plots and rank histograms. (Advances #97)
25+
926
* [ColorBrewer](http://colorbrewer2.org) palettes are now available as color
1027
schemes via
1128
[`color_scheme_set()`](https://mc-stan.org/bayesplot/reference/bayesplot-colors.html).
@@ -49,6 +66,12 @@
4966
* The examples in
5067
[`?ppc_loo_pit_overlay()`](https://mc-stan.org/bayesplot/reference/PPC-loo.html)
5168
now work as expected. (#166, #167)
69+
70+
* Added `"viridisD"` as an alternative name for `"viridis"` to the supported
71+
colors.
72+
73+
* Added `"viridisE"` (the [cividis](https://github.com/marcosci/cividis)
74+
version of viridis) to the supported colors.
5275

5376
* `ppc_bars()` and `ppc_bars_grouped()` now allow negative integers as input.
5477
(#172, @jeffpollock9)

R/bayesplot-colors.R

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
#' * `"teal"`
4646
#' * `"yellow"`
4747
#' * [`"viridis"`](https://CRAN.R-project.org/package=viridis), `"viridisA"`,
48-
#' `"viridisB"`, `"viridisC"`
48+
#' `"viridisB"`, `"viridisC"`, `"viridisD"`, `"viridisE"`
4949
#' * `"mix-x-y"`, replacing `x` and `y` with any two of
5050
#' the scheme names listed above (e.g. "mix-teal-pink", "mix-blue-red",
5151
#' etc.). The order of `x` and `y` matters, i.e., the color schemes
@@ -395,7 +395,12 @@ master_color_list <- list(
395395
viridisB =
396396
list("#FCFFA4FF", "#FCA50AFF", "#DD513AFF", "#932667FF", "#420A68FF", "#000004FF"),
397397
viridisC =
398-
list("#F0F921FF", "#FCA636FF", "#E16462FF", "#B12A90FF", "#6A00A8FF", "#0D0887FF")
398+
list("#F0F921FF", "#FCA636FF", "#E16462FF", "#B12A90FF", "#6A00A8FF", "#0D0887FF"),
399+
# popular form of viridis is viridis option D
400+
viridisD =
401+
list("#FDE725FF", "#7AD151FF", "#22A884FF", "#2A788EFF", "#414487FF", "#440154FF"),
402+
viridisE =
403+
list("#FFEA46FF", "#CBBA69FF", "#958F78FF", "#666970FF", "#31446BFF", "#00204DFF")
399404
)
400405

401406
# instantiate aesthetics --------------------------------------------------

R/bayesplot-package.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#' @aliases bayesplot
66
#'
77
#' @import ggplot2 stats rlang
8-
#' @importFrom dplyr %>%
8+
#' @importFrom dplyr %>% summarise group_by select
99
#'
1010
#' @description
1111
#' \if{html}{

R/helpers-gg.R

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,26 @@ dont_expand_axes <- function() {
6464
}
6565
force_axes_in_facets <- function() {
6666
thm <- bayesplot_theme_get()
67-
annotate("segment",
68-
x = c(-Inf, -Inf), xend = c(Inf,-Inf),
69-
y = c(-Inf,-Inf), yend = c(-Inf, Inf),
70-
color = thm$axis.line$colour %||% "black",
71-
size = thm$axis.line$size %||% 0.5)
67+
annotate(
68+
"segment",
69+
x = c(-Inf, -Inf), xend = c(Inf,-Inf),
70+
y = c(-Inf,-Inf), yend = c(-Inf, Inf),
71+
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
72+
size = thm$axis.line$size %||% thm$line$size %||% 0.5
73+
)
7274
}
75+
76+
force_x_axis_in_facets <- function() {
77+
thm <- bayesplot_theme_get()
78+
annotate(
79+
"segment",
80+
x = -Inf, xend = Inf,
81+
y = -Inf, yend = -Inf,
82+
color = thm$axis.line$colour %||% thm$line$colour %||% "black",
83+
size = thm$axis.line$size %||% thm$line$size %||% 0.5
84+
)
85+
}
86+
7387
no_legend_spacing <- function() {
7488
theme(legend.spacing.y = unit(0, "cm"))
7589
}

R/helpers-ppc.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ validate_x <- function(x = NULL, y, unique_x = FALSE) {
151151
melt_yrep <- function(yrep) {
152152
out <- yrep %>%
153153
reshape2::melt(varnames = c("rep_id", "y_id")) %>%
154-
dplyr::as_data_frame()
154+
tibble::as_tibble()
155155
id <- create_yrep_ids(out$rep_id)
156156
out$rep_label <- factor(id, levels = unique(id))
157157
out[c("y_id", "rep_id", "rep_label", "value")]
@@ -178,7 +178,7 @@ melt_and_stack <- function(y, yrep) {
178178
# Add a level in the labels for the observed y values
179179
levels(molten_yrep$rep_label) <- c(levels(molten_yrep$rep_label), y_text)
180180

181-
ydat <- dplyr::data_frame(
181+
ydat <- tibble::tibble(
182182
rep_label = factor(y_text, levels = levels(molten_yrep$rep_label)),
183183
rep_id = NA_integer_,
184184
y_id = seq_along(y),

R/mcmc-diagnostics.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,7 @@ diagnostic_data_frame <- function(x) {
379379
stopifnot(!anyDuplicated(names(x)))
380380
diagnostic <- class(x)[1]
381381

382-
d <- dplyr::data_frame(
382+
d <- tibble::tibble(
383383
diagnostic = diagnostic,
384384
parameter = factor(seq_along(x), labels = names(x)),
385385
value = as.numeric(x),

0 commit comments

Comments
 (0)