Compute the nested Rhat convergence diagnostic for a single variable as proposed in Margossian et al. (2023).

rhat_nested(x, ...)

# S3 method for default
rhat_nested(x, superchain_ids, ...)

# S3 method for rvar
rhat_nested(x, superchain_ids, ...)

Arguments

x

(multiple options) One of:

...

Arguments passed to individual methods (if applicable).

superchain_ids

(numeric) Vector of length nchains specifying which superchain each chain belongs to. There should be equal numbers of chains in each superchain. All chains within the same superchain are assumed to have been initialized at the same point.

Value

If the input is an array, returns a single numeric value. If any of the draws is non-finite, that is, NA, NaN, Inf, or -Inf, the returned output will be (numeric) NA. Also, if all draws within any of the chains of a variable are the same (constant), the returned output will be (numeric) NA

as well. The reason for the latter is that, for constant draws, we cannot distinguish between variables that are supposed to be constant (e.g., a diagonal element of a correlation matrix is always 1) or variables that just happened to be constant because of a failure of convergence or other problems in the sampling process.

If the input is an rvar, returns an array of the same dimensions as the rvar, where each element is equal to the value that would be returned by passing the draws array for that element of the rvar to this function.

Details

Nested Rhat is a convergence diagnostic useful when running many short chains. It is calculated on superchains, which are groups of chains that have been initialized at the same point.

Note that there is a slight difference in the calculation of Rhat and nested Rhat, as nested Rhat is lower bounded by 1. This means that nested Rhat with one chain per superchain will not be exactly equal to basic Rhat (see Footnote 1 in Margossian et al. (2023)).

References

Charles C. Margossian, Matthew D. Hoffman, Pavel Sountsov, Lionel Riou-Durand, Aki Vehtari and Andrew Gelman (2023). Nested R-hat: Assessing the convergence of Markov chain Monte Carlo when running many short chains. arxiv:arXiv:2110.13017 (version 4)

See also

Other diagnostics: ess_basic(), ess_bulk(), ess_quantile(), ess_sd(), ess_tail(), mcse_mean(), mcse_quantile(), mcse_sd(), rhat_basic(), rhat(), rstar()

Examples

mu <- extract_variable_matrix(example_draws(), "mu")
rhat_nested(mu, superchain_ids = c(1, 1, 2, 2))
#> [1] 1.002846

d <- as_draws_rvars(example_draws("multi_normal"))
rhat_nested(d$Sigma, superchain_ids = c(1, 1, 2, 2))
#>          [,1]     [,2]     [,3]
#> [1,] 1.000511 1.001590 1.000154
#> [2,] 1.001590 1.001037 1.000071
#> [3,] 1.000154 1.000071 1.000469