Skip to content

Commit bf1dec2

Browse files
authored
Merge pull request #199 from stan-dev/rank-hotfix
Rank hotfix
2 parents 6dacc42 + 1499635 commit bf1dec2

File tree

5 files changed

+18
-15
lines changed

5 files changed

+18
-15
lines changed

NEWS.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@
77
<!-- Items for next release go here* -->
88

99
* 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
10+
Rank histograms were introduced by the Stan team's [new paper on
1111
MCMC diagnostics](https://arxiv.org/abs/1903.08008). (#178, #179)
1212

1313
`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.
14+
sampled values the MCMC chains mix over the course of sampling. A rank
15+
histogram (`mcmc_rank_hist()`) visualizes how the *ranks* of values from the
16+
chains mix together. An ideal plot would show the ranks mixing or overlapping
17+
in a uniform distribution.
1818

1919
`mcmc_rank_overlay()`: Instead of drawing each chain's histogram in a separate
2020
panel, this plot draws the top edge of the chains' histograms in a single

R/mcmc-intervals.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mcmc_intervals <- function(x,
165165

166166
data <- mcmc_intervals_data(x, pars, regex_pars, transformations,
167167
prob = prob, prob_outer = prob_outer,
168-
point_est = point_est, rhat = rhat)
168+
point_est = point_est, rhat = rhat)
169169

170170
color_by_rhat <- rlang::has_name(data, "rhat_rating")
171171
no_point_est <- all(data$point_est == "none")

R/mcmc-traces.R

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#' }
5555
#' \item{`mcmc_rank_hist()`}{
5656
#' Whereas traditional trace plots visualize how the chains mix over the
57-
#' course of sampling, rank-normalized histograms visualize how the values
57+
#' course of sampling, rank histograms visualize how the values
5858
#' from the chains mix together in terms of ranking. An ideal plot would
5959
#' show the rankings mixing or overlapping in a uniform distribution.
6060
#' See Vehtari et al. (2019) for details.
@@ -469,7 +469,9 @@ mcmc_trace_data <- function(x,
469469
first_cols <- syms(c("parameter", "value", "value_rank"))
470470
data <- data %>%
471471
group_by(.data$parameter) %>%
472-
mutate(value_rank = dplyr::row_number(.data$value)) %>%
472+
mutate(
473+
value_rank = rank(.data$value, ties.method = "average")
474+
) %>%
473475
ungroup() %>%
474476
select(!!! first_cols, dplyr::everything())
475477

man/MCMC-traces.Rd

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-mcmc-intervals.R

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,10 @@ test_that("mcmc_intervals/areas with rhat", {
8585
expect_error(expect_warning(mcmc_intervals(arr, rhat = rbad)))
8686

8787
expect_gg(g <- mcmc_intervals(arr, rhat = r))
88+
8889
if (utils::packageVersion("ggplot2") >= "3.0.0") {
8990
rhat_map <- g$layers[[3]][["mapping"]]
90-
expect_identical(as.character(rhat_map[["colour"]]), c("~", "rhat_rating"))
91+
expect_identical(rlang::as_name(rhat_map[["colour"]]), "rhat_rating")
9192
}
9293

9394
# areas with rhat.
@@ -98,18 +99,18 @@ test_that("mcmc_intervals/areas with rhat", {
9899
expect_gg(g2 <- mcmc_areas(arr, rhat = r))
99100
if (utils::packageVersion("ggplot2") >= "3.0.0") {
100101
rhat_map2 <- g2$layers[[2]][["mapping"]]
101-
expect_identical(as.character(rhat_map2$fill), c("~", "rhat_rating"))
102-
expect_identical(as.character(rhat_map2$colour), c("~", "rhat_rating"))
102+
expect_identical(rlang::as_name(rhat_map2$fill), "rhat_rating")
103+
expect_identical(rlang::as_name(rhat_map2$colour), "rhat_rating")
104+
103105
# layer 3 is point estimate. manually colored. [skip]
104106

105107
# layer 4 is outer interval.
106108
rhat_map4 <- g2$layers[[4]][["mapping"]]
107-
expect_identical(as.character(rhat_map4$colour), c("~", "rhat_rating"))
109+
expect_identical(rlang::as_name(rhat_map4$colour), "rhat_rating")
108110

109111
# layer 5 is bottom line.
110-
111112
rhat_map5 <- g2$layers[[5]][["mapping"]]
112-
expect_identical(as.character(rhat_map5$colour), c("~", "rhat_rating"))
113+
expect_identical(rlang::as_name(rhat_map5$colour), "rhat_rating")
113114
}
114115
})
115116

0 commit comments

Comments
 (0)