Skip to content

Commit 17d3223

Browse files
committed
Deprecate rhat() and neff_ratio() in favor of extract_rhat() and extract_neff_ratio()
1 parent 9d6a95b commit 17d3223

10 files changed

Lines changed: 152 additions & 76 deletions

NAMESPACE

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,17 @@ S3method(apply_transformations,array)
66
S3method(apply_transformations,matrix)
77
S3method(diagnostic_factor,neff_ratio)
88
S3method(diagnostic_factor,rhat)
9+
S3method(extract_neff_ratio,CmdStanMCMC)
10+
S3method(extract_neff_ratio,stanfit)
11+
S3method(extract_neff_ratio,stanreg)
12+
S3method(extract_rhat,CmdStanMCMC)
13+
S3method(extract_rhat,stanfit)
14+
S3method(extract_rhat,stanreg)
915
S3method(log_posterior,CmdStanMCMC)
1016
S3method(log_posterior,stanfit)
1117
S3method(log_posterior,stanreg)
1218
S3method(melt_mcmc,matrix)
1319
S3method(melt_mcmc,mcmc_array)
14-
S3method(neff_ratio,CmdStanMCMC)
15-
S3method(neff_ratio,stanfit)
16-
S3method(neff_ratio,stanreg)
1720
S3method(num_chains,data.frame)
1821
S3method(num_chains,mcmc_array)
1922
S3method(num_iters,data.frame)
@@ -33,9 +36,6 @@ S3method(pp_check,default)
3336
S3method(print,bayesplot_function_list)
3437
S3method(print,bayesplot_grid)
3538
S3method(print,bayesplot_scheme)
36-
S3method(rhat,CmdStanMCMC)
37-
S3method(rhat,stanfit)
38-
S3method(rhat,stanreg)
3939
export(abline_01)
4040
export(available_mcmc)
4141
export(available_ppc)
@@ -53,6 +53,8 @@ export(example_mcmc_draws)
5353
export(example_x_data)
5454
export(example_y_data)
5555
export(example_yrep_draws)
56+
export(extract_neff_ratio)
57+
export(extract_rhat)
5658
export(facet_bg)
5759
export(facet_text)
5860
export(grid_lines)

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# bayesplot (development version)
22

3+
* Deprecated `rhat()` and `neff_ratio()` in favor of `extract_rhat()` and `extract_neff_ratio()` to avoid masking the same-named (but different) functions in the **posterior** package. The old names still work but emit a deprecation warning. (#295)
34
* Fixed `validate_chain_list()` colnames check to compare all chains, not just the first two.
45
* Added test verifying `legend_move("none")` behaves equivalently to `legend_none()`.
56
* Added singleton-dimension edge-case tests for exported `_data()` functions.

R/bayesplot-extractors.R

Lines changed: 55 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,16 @@
2424
#' The data frame should have columns `"Parameter"` (factor), `"Iteration"`
2525
#' (integer), `"Chain"` (integer), and `"Value"` (numeric). See **Examples**, below.
2626
#' }
27-
#' \item{`rhat()`, `neff_ratio()`}{
27+
#' \item{`extract_rhat()`, `extract_neff_ratio()`}{
2828
#' Methods return (named) vectors.
2929
#' }
30+
#' \item{`rhat()`, `neff_ratio()`}{
31+
#' `r lifecycle::badge("deprecated")` Use `extract_rhat()` and
32+
#' `extract_neff_ratio()` instead. These names clashed with
33+
#' [posterior::rhat()] and [posterior::ess_basic()] (and other ESS
34+
#' functions), which caused confusion because the bayesplot versions
35+
#' *extract* already-computed values rather than compute them.
36+
#' }
3037
#' }
3138
#'
3239
#' @seealso [MCMC-nuts], [MCMC-diagnostics]
@@ -60,17 +67,50 @@ log_posterior <- function(object, ...) {
6067
nuts_params <- function(object, ...) {
6168
UseMethod("nuts_params")
6269
}
70+
# extract_rhat -------------------------------------------------------------
71+
#' @rdname bayesplot-extractors
72+
#' @export
73+
extract_rhat <- function(object, ...) {
74+
UseMethod("extract_rhat")
75+
}
76+
# extract_neff_ratio -------------------------------------------------------
77+
#' @rdname bayesplot-extractors
78+
#' @export
79+
extract_neff_ratio <- function(object, ...) {
80+
UseMethod("extract_neff_ratio")
81+
}
82+
6383
# rhat -------------------------------------------------------------
6484
#' @rdname bayesplot-extractors
6585
#' @export
6686
rhat <- function(object, ...) {
67-
UseMethod("rhat")
87+
lifecycle::deprecate_warn(
88+
when = "1.16.0",
89+
what = "rhat()",
90+
with = "extract_rhat()",
91+
details = paste(
92+
"bayesplot's rhat() was renamed to extract_rhat() to make clear",
93+
"it only extracts already-computed R-hat values (and to avoid",
94+
"masking posterior::rhat(), which computes R-hat)."
95+
)
96+
)
97+
extract_rhat(object, ...)
6898
}
6999
# neff_ratio -------------------------------------------------------------
70100
#' @rdname bayesplot-extractors
71101
#' @export
72102
neff_ratio <- function(object, ...) {
73-
UseMethod("neff_ratio")
103+
lifecycle::deprecate_warn(
104+
when = "1.16.0",
105+
what = "neff_ratio()",
106+
with = "extract_neff_ratio()",
107+
details = paste(
108+
"bayesplot's neff_ratio() was renamed to extract_neff_ratio() to",
109+
"make clear it only extracts already-computed effective sample",
110+
"size ratios (and to avoid masking posterior's ESS functions)."
111+
)
112+
)
113+
extract_neff_ratio(object, ...)
74114
}
75115

76116

@@ -195,9 +235,9 @@ nuts_params.CmdStanMCMC <- function(object, pars = NULL, ...) {
195235

196236
#' @rdname bayesplot-extractors
197237
#' @export
198-
#' @method rhat stanfit
238+
#' @method extract_rhat stanfit
199239
#'
200-
rhat.stanfit <- function(object, pars = NULL, ...) {
240+
extract_rhat.stanfit <- function(object, pars = NULL, ...) {
201241
suggested_package("rstan")
202242
s <- if (!is.null(pars)) {
203243
rstan::summary(object, pars = pars, ...)
@@ -210,10 +250,10 @@ rhat.stanfit <- function(object, pars = NULL, ...) {
210250

211251
#' @rdname bayesplot-extractors
212252
#' @export
213-
#' @method rhat stanreg
253+
#' @method extract_rhat stanreg
214254
#' @template args-regex_pars
215255
#'
216-
rhat.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
256+
extract_rhat.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
217257
suggested_package("rstanarm")
218258
r <- summary(object, pars = pars, regex_pars = regex_pars, ...)[, "Rhat"]
219259
r <- validate_rhat(r)
@@ -226,8 +266,8 @@ rhat.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
226266

227267
#' @rdname bayesplot-extractors
228268
#' @export
229-
#' @method rhat CmdStanMCMC
230-
rhat.CmdStanMCMC <- function(object, pars = NULL, ...) {
269+
#' @method extract_rhat CmdStanMCMC
270+
extract_rhat.CmdStanMCMC <- function(object, pars = NULL, ...) {
231271
.rhat <- utils::getFromNamespace("rhat", "posterior")
232272
s <- object$summary(pars, rhat = .rhat)[, c("variable", "rhat")]
233273
r <- setNames(s$rhat, s$variable)
@@ -238,9 +278,9 @@ rhat.CmdStanMCMC <- function(object, pars = NULL, ...) {
238278

239279
#' @rdname bayesplot-extractors
240280
#' @export
241-
#' @method neff_ratio stanfit
281+
#' @method extract_neff_ratio stanfit
242282
#'
243-
neff_ratio.stanfit <- function(object, pars = NULL, ...) {
283+
extract_neff_ratio.stanfit <- function(object, pars = NULL, ...) {
244284
suggested_package("rstan")
245285
s <- if (!is.null(pars)) {
246286
rstan::summary(object, pars = pars, ...)
@@ -254,9 +294,9 @@ neff_ratio.stanfit <- function(object, pars = NULL, ...) {
254294

255295
#' @rdname bayesplot-extractors
256296
#' @export
257-
#' @method neff_ratio stanreg
297+
#' @method extract_neff_ratio stanreg
258298
#'
259-
neff_ratio.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
299+
extract_neff_ratio.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
260300
suggested_package("rstanarm")
261301
s <- summary(object, pars = pars, regex_pars = regex_pars, ...)
262302
ess <- s[, "n_eff"]
@@ -272,8 +312,8 @@ neff_ratio.stanreg <- function(object, pars = NULL, regex_pars = NULL, ...) {
272312

273313
#' @rdname bayesplot-extractors
274314
#' @export
275-
#' @method neff_ratio CmdStanMCMC
276-
neff_ratio.CmdStanMCMC <- function(object, pars = NULL, ...) {
315+
#' @method extract_neff_ratio CmdStanMCMC
316+
extract_neff_ratio.CmdStanMCMC <- function(object, pars = NULL, ...) {
277317
s <- object$summary(pars, "n_eff" = "ess_basic")[, c("variable", "n_eff")]
278318
ess <- setNames(s$n_eff, s$variable)
279319
tss <- prod(dim(object$draws())[1:2])

R/mcmc-diagnostics.R

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@
105105
#' # intentionally use small 'iter' so there are some
106106
#' # problems with rhat and neff for demonstration
107107
#' fit <- stan_glm(mpg ~ ., data = mtcars, iter = 50, refresh = 0)
108-
#' rhats <- rhat(fit)
109-
#' ratios <- neff_ratio(fit)
108+
#' rhats <- extract_rhat(fit)
109+
#' ratios <- extract_neff_ratio(fit)
110110
#' mcmc_rhat(rhats)
111111
#' mcmc_neff(ratios, size = 3)
112112
#'
@@ -120,8 +120,8 @@
120120
#'
121121
#' # increase number of iterations and plots look much better
122122
#' fit2 <- update(fit, iter = 500)
123-
#' mcmc_rhat(rhat(fit2))
124-
#' mcmc_neff(neff_ratio(fit2))
123+
#' mcmc_rhat(extract_rhat(fit2))
124+
#' mcmc_neff(extract_neff_ratio(fit2))
125125
#' mcmc_acf(as.array(fit2), pars = c("wt", "cyl"), lags = 10)
126126
#' }
127127
#'
@@ -220,7 +220,7 @@ mcmc_rhat_data <- function(rhat, ...) {
220220
#' @rdname MCMC-diagnostics
221221
#' @export
222222
#' @param ratio A vector of *ratios* of effective sample size estimates to
223-
#' total sample size. See [neff_ratio()].
223+
#' total sample size. See [extract_neff_ratio()].
224224
#'
225225
mcmc_neff <- function(ratio, ..., size = NULL) {
226226
check_ignored_arguments(...)

R/mcmc-intervals.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
#' @param rhat An optional numeric vector of R-hat estimates, with one element
3232
#' per parameter included in `x`. If `rhat` is provided, the intervals/areas
3333
#' and point estimates in the resulting plot are colored based on R-hat value.
34-
#' See [rhat()] for methods for extracting R-hat estimates.
34+
#' See [extract_rhat()] for methods for extracting R-hat estimates.
3535
#' @template args-density-controls
3636
#'
3737
#' @template return-ggplot-or-data
@@ -180,7 +180,7 @@
180180
#' color_scheme_set("teal")
181181
#' mcmc_intervals(x, point_est = "mean", prob = 0.8, prob_outer = 0.95)
182182
#' mcmc_areas(x, regex_pars = "cyl", bw = "SJ",
183-
#' rhat = rhat(fit, regex_pars = "cyl"))
183+
#' rhat = extract_rhat(fit, regex_pars = "cyl"))
184184
#' }
185185
#'
186186
#' \dontrun{

man/MCMC-diagnostics.Rd

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/MCMC-intervals.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/bayesplot-extractors.Rd

Lines changed: 26 additions & 13 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)