Package 'SBC'

Title: Simulation Based Calibration for rstan/cmdstanr models
Description: SBC helps perform Simulation Based Calibration on Bayesian models. SBC lets you check for bugs in your model code and/or algorithm that fits the model. SBC focuses on models built with 'Stan' <https://mc-stan.org>, but can support other modelling languages as well.
Authors: Shinyoung Kim [aut], Angie.H Moon [aut, cre], Martin Modrák [aut] , Teemu Säilynoja [aut], Luna Fazio [ctb]
Maintainer: Angie.H Moon <[email protected]>
License: MIT + file LICENSE
Version: 0.3.0.9000
Built: 2024-06-22 09:46:06 UTC
Source: https://github.com/hyunjimoon/SBC

Help Index


Subset an SBC_datasets object.

Description

Subset an SBC_datasets object.

Usage

## S3 method for class 'SBC_datasets'
x[indices]

Subset the results.

Description

Subset the results.

Usage

## S3 method for class 'SBC_results'
x[indices]

Arguments

indices

integer indices or a binary vector of the same length as the number fits, selecting which fits to keep.


Combine multiple datasets together.

Description

Combine multiple datasets together.

Usage

bind_datasets(...)

Arguments

...

datasets to bind


Combine two lists of derived quantities

Description

Combine two lists of derived quantities

Usage

bind_derived_quantities(dq1, dq2)

Combine two sets globals for use in derived quantities or backend

Description

Combine two sets globals for use in derived quantities or backend

Usage

bind_globals(globals1, globals2)

See Also

compute_SBC(), derived_quantities()


Combine multiple SBC results together.

Description

Primarily useful for iteratively adding more simulations to your SBC check.

Usage

bind_results(...)

Arguments

...

objects of type SBC_results to be combined.

Details

An example usage can be found in the small_model_workflow vignette.


Calculate prior standard deviation of a dataset

Description

Calculate prior standard deviation of a dataset

Usage

calculate_prior_sd(datasets)

Value

a named vector of prior SDs


Calculate ranks given variable values within a posterior distribution.

Description

When there are ties (e.g. for discrete variables), the rank is currently drawn stochastically among the ties. NA is assumed to be potentially equal to any value (Issue #78)

Usage

calculate_ranks_draws_matrix(variables, dm, params = NULL)

Arguments

variables

a vector of values to check

dm

draws_matrix of the fit (assumed to be already thinned if that was necessary)

params

DEPRECATED. Use variables instead.


Check diagnostics and issue warnings when those fail.

Description

Check diagnostics and issue warnings when those fail.

Usage

check_all_SBC_diagnostics(x)

## Default S3 method:
check_all_SBC_diagnostics(x)

## S3 method for class 'SBC_results'
check_all_SBC_diagnostics(x)

Value

TRUE if all checks are OK, FALSE otherwise.


Cumulative Jensen-Shannon divergence

Description

Computes the cumulative Jensen-Shannon distance between two samples.

Usage

cjs_dist(
  x,
  y,
  x_weights = rep(1/length(x), length(x)),
  y_weights = rep(1/length(y), length(y)),
  ...
)

Arguments

x

numeric vector of draws from first distribution

y

numeric vector of draws from second distribution

x_weights

numeric vector of weights of first distribution

y_weights

numeric vector of weights of second distribution

...

unused

Details

The Cumulative Jensen-Shannon distance is a symmetric metric based on the cumulative Jensen-Shannon divergence. The divergence CJS(P || Q) between two cumulative distribution functions P and Q is defined as:

CJS(PQ)=P(x)logP(x)0.5(P(x)+Q(x))+12ln2(Q(x)P(x))CJS(P || Q) = \sum P(x) \log \frac{P(x)}{0.5 (P(x) + Q(x))} + \frac{1}{2 \ln 2} \sum (Q(x) - P(x))

The symmetric metric is defined as:

CJSdist(PQ)=CJS(PQ)+CJS(QP)CJS_{dist}(P || Q) = \sqrt{CJS(P || Q) + CJS(Q || P)}

This has an upper bound of \sqrt \sum (P(x) + Q(x))

Value

distance value based on CJS computation.

References

Nguyen H-V., Vreeken J. (2015). Non-parametric Jensen-Shannon Divergence. In: Appice A., Rodrigues P., Santos Costa V., Gama J., Jorge A., Soares C. (eds) Machine Learning and Knowledge Discovery in Databases. ECML PKDD 2015. Lecture Notes in Computer Science, vol 9285. Springer, Cham. doi:10.1007/978-3-319-23525-7_11


Helper functions to be passed to ECDF-plots to combine variables in a single panel.

Description

combine_all_variables will merge all variables in a single plot, while combine_array_elements will merge all elements of any array into a single panel of the plot

Usage

combine_all_variables(x)

combine_array_elements(x)

Arguments

x

parameter names


Combine two named lists and overwrite elements with the same name using the value from args2

Description

Combine two named lists and overwrite elements with the same name using the value from args2

Usage

combine_args(args1, args2)

Compute derived quantities based on given data and posterior draws.

Description

Compute derived quantities based on given data and posterior draws.

Usage

compute_dquants(draws, generated, dquants, gen_quants = NULL)

Arguments

gen_quants

Deprecated, use dquants


Fit datasets and evaluate diagnostics and SBC metrics.

Description

Performs the main SBC routine given datasets and a backend.

Usage

compute_SBC(
  datasets,
  backend,
  cores_per_fit = default_cores_per_fit(length(datasets)),
  keep_fits = TRUE,
  thin_ranks = SBC_backend_default_thin_ranks(backend),
  ensure_num_ranks_divisor = 2,
  chunk_size = default_chunk_size(length(datasets)),
  dquants = NULL,
  cache_mode = "none",
  cache_location = NULL,
  globals = list(),
  gen_quants = NULL
)

Arguments

datasets

an object of class SBC_datasets

backend

the model + sampling algorithm. The built-in backends can be constructed using SBC_backend_cmdstan_sample(), SBC_backend_cmdstan_variational(), SBC_backend_rstan_sample(), SBC_backend_rstan_optimizing() and SBC_backend_brms(). (more to come: issue 31, 38, 39). The backend is an S3 class supporting at least the SBC_fit(), SBC_fit_to_draws_matrix() methods.

cores_per_fit

how many cores should the backend be allowed to use for a single fit? Defaults to the maximum number that does not produce more parallel chains than you have cores. See default_cores_per_fit().

keep_fits

boolean, when FALSE full fits are discarded from memory - reduces memory consumption and increases speed (when processing in parallel), but prevents you from inspecting the fits and using recompute_SBC_statistics(). We recommend to set to TRUE in early phases of workflow, when you run just a few fits. Once the model is stable and you want to run a lot of iterations, we recommend setting to FALSE (even for quite a simple model, 1000 fits can easily exhaust 32GB of RAM).

thin_ranks

how much thinning should be applied to posterior draws before computing ranks for SBC. Should be large enough to avoid any noticeable autocorrelation of the thinned draws See details below.

ensure_num_ranks_divisor

Potentially drop some posterior samples to ensure that this number divides the total number of SBC ranks (see Details).

chunk_size

How many simulations within the datasets shall be processed in one batch by the same worker. Relevant only when using parallel processing. The larger the value, the smaller overhead there will be for parallel processing, but the work may be distributed less equally across workers. We recommend setting this high enough that a single batch takes at least several seconds, i.e. for small models, you can often reduce computation time noticeably by increasing this value. You can use options(SBC.min_chunk_size = value) to set a minimum chunk size globally. See documentation of future.chunk.size argument for future.apply::future_lapply() for more details.

dquants

Derived quantities to include in SBC. Use derived_quantities() to construct them.

cache_mode

Type of caching of results, currently the only supported modes are "none" (do not cache) and "results" where the whole results object is stored and recomputed only when the hash of the backend or dataset changes.

cache_location

The filesystem location of cache. For cache_mode = "results" this should be a name of a single file. If the file name does not end with .rds, this extension is appended.

globals

A list of names of objects that are defined in the global environment and need to present for the backend to work ( if they are not already available in package). It is added to the globals argument to future::future(), to make those objects available on all workers.

gen_quants

Deprecated, use dquants instead

Value

An object of class SBC_results().

Paralellization

Parallel processing is supported via the future package, for most uses, it is most sensible to just call plan(multisession) once in your R session and all cores your computer will be used. For more details refer to the documentation of the future package.

Thinning

When using backends based on MCMC, there are two possible moments when draws may need to be thinned. They can be thinned directly within the backend and they may be thinned only to compute the ranks for SBC as specified by the thin_ranks argument. The main reason those are separate is that computing the ranks requires no or negligible autocorrelation while some autocorrelation may be easily tolerated for summarising the fit results or assessing convergence. In fact, thinning too aggressively in the backend may lead to overly noisy estimates of posterior means, quantiles and the posterior::rhat() and posterior::ess_tail() diagnostics. So for well-adapted Hamiltonian Monte-Carlo chains (e.g. Stan-based backends), we recommend no thinning in the backend and even value of thin_ranks between 6 and 10 is usually sufficient to remove the residual autocorrelation. For a backend based on Metropolis-Hastings, it might be sensible to thin quite aggressively already in the backend and then have some additional thinning via thin_ranks.

Backends that don't require thining should implement SBC_backend_iid_draws() or SBC_backend_default_thin_ranks() to avoid thinning by default.

Rank divisors

Some of the visualizations and post processing steps we use in the SBC package (e.g. plot_rank_hist(), empirical_coverage()) work best if the total number of possible SBC ranks is a "nice" number (lots of divisors). However, the number of ranks is one plus the number of posterior samples after thinning - therefore as long as the number of samples is a "nice" number, the number of ranks usually will not be. To remedy this, you can specify ensure_num_ranks_divisor - the method will drop at most ensure_num_ranks_divisor - 1 samples to make the number of ranks divisible by ensure_num_ranks_divisor. The default 2 prevents the most annoying pathologies while discarding at most a single sample.


Maybe not export in the end? Useful for debugging

Description

Maybe not export in the end? Useful for debugging

Usage

data_for_ecdf_plots(x, ..., prob = 0.95, gamma = NULL, K = NULL)

Determines the default chunk size.

Description

By default will make every worker process a single chunk. You can set the options(SBC.min_chunk_size = value) to enforce a minimum chunk size globally (chunk size can still be larger if you have substantially more fits to run than workers.

Usage

default_chunk_size(n_fits, n_workers = future::nbrOfWorkers())

Determines the default cores per single fit.

Description

When parallel processing is disabled, this just returns the number of available cores. Otherwise, it chooses the largest integer that keeps cores_per_fit * (n_fits / chunk_size) <= total_cores, i.e. it avoids running so many chains in parallel that there will be more chains than cores.

Usage

default_cores_per_fit(
  n_fits,
  total_cores = future::availableCores(),
  chunk_size = default_chunk_size(n_fits)
)

Create a definition of derived quantities evaluated in R.

Description

When the expression contains non-library functions/objects, and parallel processing is enabled, those must be named in the .globals parameter (hopefully we'll be able to detect those automatically in the future). Note that recompute_SBC_statistics() currently does not use parallel processing, so .globals don't need to be set.

Usage

derived_quantities(..., .globals = list())

Arguments

...

named expressions representing the quantitites

.globals

A list of names of objects that are defined in the global environment and need to present for the gen. quants. to evaluate. It is added to the globals argument to future::future(), to make those objects available on all workers.

Examples

# Derived quantity computing the total log likelihood of a normal distribution
# with known sd = 1
normal_lpdf <- function(y, mu, sigma) {
 sum(dnorm(y, mean = mu, sd = sigma, log = TRUE))
}

# Note the use of .globals to make the normal_lpdf function available
# within the expression
log_lik_dq <- derived_quantities(log_lik = normal_lpdf(y, mu, 1),
                                .globals = "normal_lpdf" )

Compute observed coverage of posterior credible intervals.

Description

Uses ranks to compute coverage and surrounding uncertainty of posterior credible intervals. The uncertainty is only approximate (treating coverage for each interval as a set of independent Bernoulli trials, while in fact they are not independent), so for making claims on presence/ absence of detectable discrepancies we strongly recommend using plot_ecdf() or plot_ecdf_diff(). The uncertainty about the coverage can however be useful for guiding decisions on whether more SBC steps should be performed (i.e. whether we can rule out that the coverage of the given backend differs too much for our purposes from the optimal value).

Usage

empirical_coverage(stats, width, prob = 0.95, interval_type = "central")

Arguments

stats

a data.frame of rank statistics (e.g. as returned in the ⁠$stats⁠ component of SBC_results), at minimum should have at least variable, rank and max_rank columns)

width

a vector of values between 0 and 1 representing widths of credible intervals for which we compute coverage.

prob

determines width of the uncertainty interval around the observed coverage

inteval_type

"central" to show coverage of central credible intervals or "leftmost" to show coverage of leftmost credible intervals (i.e. the observed CDF).

Details

Note that while coverage of central posterior intervals (with the default type = "central") is often of the biggest practical interest, perfect calibration of central intervals still leaves space for substantial problems with the model (e.g. if the posterior 25% - 50% intervals contain 50% of the true values and the posterior 50% - 75% interval never contains the true value, the central 50% interval still has the ideal 50% coverage), so investigating central intervals should always be accompanied by checks with plot_ecdf() or plot_ecdf_diff() or by using type = "leftmost", because if all leftmost credible intervals are well calibrated, then all intervals are well calibrated.

Value

A data.frame with columns variable, width (width of the interval as given in the width parameter), width_represented the closest width that can be represented by the ranks in the input (any discrepancy needs to be judged against this rather than width), estimate - observed coverage for the interval, ci_low, ci_high the uncertainty interval around estimate (width of the interval is given by the prob argument).

See Also

plot_coverage()


Generate datasets.

Description

Generate datasets.

Usage

generate_datasets(generator, n_sims, n_datasets = NULL)

Arguments

generator

a generator object - build e.g. via SBC_generator_function or SBC_generator_brms.

n_sims

the number of simulated datasets to use

n_datasets

DEPRECATED, use n_sims instead.

Value

object of class SBC_datasets TODO: seed


Get diagnostic messages for SBC_results or other objects.

Description

Get diagnostic messages for SBC_results or other objects.

Usage

get_diagnostic_messages(x)

Value

An object of class SBC_diagnostic_messages, inheriting a data.frame.


Guess the number of bins for plot_rank_hist().

Description

Guess the number of bins for plot_rank_hist().

Usage

guess_rank_hist_bins(max_rank, N)

Arguments

max_rank

the maximum rank observed

N

the number of ranks observed


Max difference between binned samples with the same length

Description

Max difference between binned samples with the same length

Usage

max_diff(x, y)

Arguments

x

numeric vector of density from first distribution

y

numeric vector of density from second distribution

...

unused

Value

distance value based on max difference


Prior/posterior contraction plot.

Description

The rationale for this plot and its interpretation is explained in Mike Betancourt's Towards A Principled Bayesian Workflow.

Usage

plot_contraction(
  x,
  prior_sd,
  variables = NULL,
  scale = "sd",
  alpha = 0.8,
  parameters = NULL
)

Arguments

x

object containing results (a data.frame or SBC_results() object).

prior_sd

a named vector of prior standard deviations for your variables. Either pass in analytically obtained values or use calculate_prior_sd() to get an empirical estimate from an SBC_datasets object.

variables

variables to show in the plot or NULL to show all must correspond a field already computed in the results (most likely "mean" and "median").

scale

which scale of variability you want to see - either "sd" for standard deviation or "var" for variance.

alpha

the alpha for the points

parameters

DEPRECATED, use variables instead.

Value

a ggplot2 plot object


Plot the observed coverage and its uncertainty.

Description

plot_coverage will plot the observed coverage, while plot_coverage_diff will show the difference between observed and expected coverage. Please refer to empirical_coverage() for details on computation and limitations of this plot as well as details on the arguments. See vignette("rank_visualizations") for more details.

Usage

plot_coverage(
  x,
  variables = NULL,
  prob = 0.95,
  interval_type = "central",
  parameters = NULL,
  max_points = NULL
)

plot_coverage_diff(
  x,
  variables = NULL,
  prob = 0.95,
  interval_type = "central",
  parameters = NULL,
  max_points = NULL
)

Arguments

x

object containing results (a data.frame or SBC_results() object).

variables

variables to show in the plot or NULL to show all

prob

the with of the uncertainty interval to be shown

parameters

DEPRECATED. Use variables instead.

max_points

maximum number of points where to evaluate the coverage. If set to NULL, coverage is evaluated across the whole range of ranks. Setting to some smaller number may reduce memory footprint and increase speed.

Value

a ggplot2 plot object

See Also

empirical_coverage


Plot the ECDF-based plots.

Description

See vignette("rank_visualizations") for more details. See the methods for data_for_ecdf_plots() for available data formats.

Usage

plot_ecdf(
  x,
  variables = NULL,
  K = NULL,
  gamma = NULL,
  prob = 0.95,
  size = 1,
  alpha = 0.33,
  combine_variables = NULL,
  ecdf_alpha = NULL,
  ...,
  parameters = NULL
)

plot_ecdf_diff(
  x,
  variables = NULL,
  K = NULL,
  gamma = NULL,
  prob = 0.95,
  size = 1,
  alpha = 0.33,
  combine_variables = NULL,
  ecdf_alpha = NULL,
  ...,
  parameters = NULL
)

Arguments

x

object supporting the data_for_ecdf_plots() method.

variables

optional subset of variables to show in the plot

K

number of uniformly spaced evaluation points for the ECDF or ECDFs. Affects the granularity of the plot and can significantly speed up the computation of the simultaneous confidence bands. Defaults to the smaller of number of ranks per variable and the maximum rank.

gamma

TODO

prob

the width of the plotted confidence interval for the ECDF.

size

size passed to ggplot2::geom_ribbon() for the confidence band

alpha

alpha level of the confidence band

combine_variables

optionally specify a named list where each entry is a character vectors which specifies a group of variables that will be displayed in the same panel. Panel title will be the name of the list element. A function that takes a character vector as an input and produces a list can also be specified (see combine-functions).

ecdf_alpha

the alpha level of the empirical CDF. Can be either a single number or a function taking the number of variables that were combined (when combine_variables is specified) and returns a number. By default, plots showing many ECDFs will have reduced alpha.

...

additional arguments passed to data_for_ecdf_plots(). Most notably, if x is matrix, a max_rank parameter needs to be given.

parameters

DEPRECATED, use variables instead.

Details

arxiv::1903.08008 by A. Vehtari et al.

See Also

plot_coverage()


Plot rank histogram of an SBC results.

Description

The expected uniform distribution and an approximate confidence interval is also shown. The confidence interval cannot be taken too seriously as it is derived assuming the bins are independent (which they are not). The plot_ecdf() and plot_ecdf_diff() plots provide better confidence interval but are somewhat less interpretable. See vignette("rank_visualizations") for more details.

Usage

plot_rank_hist(
  x,
  variables = NULL,
  bins = NULL,
  prob = 0.95,
  ...,
  parameters = NULL
)

Arguments

x

Object supporting the plotting method.

variables

Names of variables to show

bins

number of bins to be used in the histogram, if left unspecified, it is determined by guess_rank_hist_bins().

prob

The width of the approximate confidence interval shown.

Details

By default the support is for SBC_results objects and data frames in the same format as the ⁠$stats⁠ element of SBC_results.


Plot the simulated "true" values versus posterior estimates

Description

Plot the simulated "true" values versus posterior estimates

Usage

plot_sim_estimated(
  x,
  variables = NULL,
  estimate = "mean",
  uncertainty = c("q5", "q95"),
  alpha = NULL,
  parameters = NULL
)

Arguments

x

object containing results (a data.frame or SBC_results() object).

variables

variables to show in the plot or NULL to show all

estimate

which estimate to use for the central tendency, must correspond a field already computed in the results (most likely "mean" and "median").

uncertainty

which estimates to use for uncertainty (a character vector of length 2) must correspond a field already computed in the results. Pass NULL to avoid showing uncertainty at all.

alpha

the alpha for the points and uncertainty intervals

parameters

DEPRECATED, use variables instead

Value

a ggplot2 plot object


Distance between binned draws (rank for SBC) and discrete uniform

Description

Distance between binned draws (rank for SBC) and discrete uniform

Usage

rank2unif(results, par, bins = 20)

Arguments

par

names of parameter to plot

bins

number of bins to use for summary

ranks

array of dimension (n_iter, n_pars) where n_iter=number of posterior draw iterations, n_pars the number of parameters of interest

thin

integer in which thinning was applied


Recompute SBC statistics without refitting models.

Description

Useful for example to recompute SBC ranks with a different choice of thin_ranks or added derived quantities.

Usage

recompute_SBC_statistics(
  old_results,
  datasets,
  backend,
  thin_ranks = SBC_backend_default_thin_ranks(backend),
  ensure_num_ranks_divisor = 2,
  dquants = NULL,
  gen_quants = NULL
)

Arguments

datasets

an object of class SBC_datasets

backend

backend used to fit the results. Used to pull various defaults and other setting influencing the computation of statistics.

thin_ranks

how much thinning should be applied to posterior draws before computing ranks for SBC. Should be large enough to avoid any noticeable autocorrelation of the thinned draws See details below.

ensure_num_ranks_divisor

Potentially drop some posterior samples to ensure that this number divides the total number of SBC ranks (see Details).

dquants

Derived quantities to include in SBC. Use derived_quantities() to construct them.

gen_quants

Deprecated, use dquants instead

Value

An S3 object of class SBC_results with updated ⁠$stats⁠ and ⁠$default_diagnostics⁠ fields.


Build a backend based on the brms package.

Description

Build a backend based on the brms package.

Usage

SBC_backend_brms(
  ...,
  template_data,
  out_stan_file = NULL,
  template_dataset = NULL
)

Arguments

...

arguments passed to brm.

template_data

a representative value for the data argument in brm that can be used to generate code.

out_stan_file

A filename for the generated Stan code. Useful for debugging and for avoiding unnecessary recompilations.

template_dataset

DEPRECATED. Use template_data


Build a brms backend, reusing the compiled model from a previously created SBC_generator_brms object.

Description

Build a brms backend, reusing the compiled model from a previously created SBC_generator_brms object.

Usage

SBC_backend_brms_from_generator(generator, ...)

Backend based on sampling via cmdstanr.

Description

Backend based on sampling via cmdstanr.

Usage

SBC_backend_cmdstan_sample(model, ..., init_factory = NULL)

Arguments

model

an object of class CmdStanModel (as created by cmdstanr::cmdstan_model)

...

other arguments passed to the ⁠$sample()⁠ method of the model. The data and parallel_chains arguments cannot be set this way as they need to be controlled by the SBC package.

init_factory

an optional function that takes in a dataset and returns a value that can be passed to the init argument of ⁠$sample()⁠. This allows for data-dependent inits. The caching mechanism in compute_SBC() ignores the environment of the function, i.e. if the init factory takes values from its environment and those values change between runs, this will not by itself cause cached results to be recomputed.


Backend based on variational approximation via cmdstanr.

Description

Backend based on variational approximation via cmdstanr.

Usage

SBC_backend_cmdstan_variational(model, ..., n_retries_init = 1)

Arguments

model

an object of class CmdStanModel (as created by cmdstanr::cmdstan_model)

...

other arguments passed to the ⁠$variational()⁠ method of the model. The data argument cannot be set this way as they need to be controlled by the SBC package.

n_retries_init

number of times to retry the variational fit if the algorithm has trouble initializing (e.g. too many dropped evaluations (see https://discourse.mc-stan.org/t/advi-too-many-dropped-evaluations-even-for-well-behaved-models/24338), or "cannot compute ELBO using the initial variational distribution")


S3 generic to get backend-specific default thinning for rank computation.

Description

The default implementation plays it relatively safe and returns 10, unless SBC_backend_iid_draws() returns TRUE in which case it returns 1.

Usage

SBC_backend_default_thin_ranks(backend)

## Default S3 method:
SBC_backend_default_thin_ranks(backend)

Get hash used to identify cached results.

Description

S3 generic that allows backends to override how a hash is computed. By default rlang::hash() is used.

Usage

SBC_backend_hash_for_cache(backend)

S3 generic to let backends signal that they produced independent draws.

Description

Most backends (e.g. those based on variatns of MCMC) don't produce independent draws and thus diagnostics like Rhat and ESS are important and draws may need thinning. Backends that already produce independent draws (e.g. ADVI/optimizing) can implement this method to return TRUE to signal this is the case. If this method returns TRUE, ESS and Rhat will always attain their best possible values and SBC_backend_default_thin_ranks() will return 1. The default implementation returns FALSE.

Usage

SBC_backend_iid_draws(backend)

## Default S3 method:
SBC_backend_iid_draws(backend)

Arguments

backend

to check


A mock backend.

Description

Mock backend is useful for testing the package. It will ignore all data passed to it and always provide result as the draws generated by the backend.

Usage

SBC_backend_mock(
  result = posterior::draws_matrix(a = rnorm(100)),
  output = NULL,
  message = NULL,
  warning = NULL,
  error = NULL
)

Arguments

result

a draws_matrix that will be returned regardless of the data


Create a JAGS backend using rjags

Description

Create a JAGS backend using rjags

Usage

SBC_backend_rjags(
  file,
  n.iter,
  n.burnin,
  variable.names,
  thin = 1,
  na.rm = TRUE,
  ...
)

Arguments

file

model file or connection to model code (passed to rjags::jags.model())

n.iter

number of iterations for sampling (passed to [rjags::coda.samples())

n.burnin

number of iterations used for burnin

variable.names

names of variables to monitor (passed to rjags::coda.samples())

thin

thinning (passed to rjags::coda.samples())

na.rm

whether to omit variables containing NA (passed to rjags::coda.samples())

...

additional optional arguments passed to rjags::jags.model()

  • most notably n.chains, n.adapt and inits.


SBC backend using the optimizing method from rstan.

Description

SBC backend using the optimizing method from rstan.

Usage

SBC_backend_rstan_optimizing(model, ..., n_retries_hessian = 1)

Arguments

model

a stanmodel object (created via rstan::stan_model)

...

other arguments passed to optimizing (number of iterations, ...). Argument data cannot be set this way as they need to be controlled by the package.

n_retries_hessian

the number of times the backend is allow to retry optimization (with different seeed) to produce a usable Hessian that can produce draws. In some cases, the Hessian may be numerically unstable and not be positive definite.


SBC backend using the sampling method from rstan.

Description

SBC backend using the sampling method from rstan.

Usage

SBC_backend_rstan_sample(model, ...)

Arguments

model

a stanmodel object (created via rstan::stan_model)

...

other arguments passed to sampling (number of iterations, ...). Arguments data and cores cannot be set this way as they need to be controlled by the package.


Create new SBC_datasets object.

Description

In most cases, you may want to use generate_datasets to build the object, but for full control, you can also create datasets directly via this function.

Usage

SBC_datasets(variables, generated, parameters = NULL)

Arguments

variables

draws of "true" values of unobserved parameters or other derived variables. An object of class draws_matrix (from the posterior package)

generated

a list of objects that can be passed as data to the backend you plan to use. (e.g. list of values for Stan-based backends, a data frame for SBC_backend_brms)

parameters

DEPRECATED. Use variables instead.


Construct a backend to be used in the examples.

Description

Note that this will involve compiling a Stan model and may take a while.

Usage

SBC_example_backend(
  example = c("normal_sd", "normal_bad"),
  interface = c("rstan", "cmdstanr", "rjags")
)

Arguments

example

name of the example model. normal_sd is a simple model fitting a normal distribution parametrized as mean and standard deviation. normal_bad is a model that tries to implement the normal_sd model, but assumes an incorrect parametrization of the normal distribution. For Stan-based backends, the model is written as if Stan parametrized normal distribution with precision (while Stan uses sd), for JAGS-based backends the model is written as if JAGS parametrized normal distribution with sd (while JAGS uses precision).

interface

name of the interface to be used to fit the model


Construct a generator used in the examples.

Description

Construct a generator used in the examples.

Usage

SBC_example_generator(example = c("normal"), N = 100)

Arguments

example

name of example

N

size of the dataset the generator should simulate

Value

an object that can be passed to generate_datasets()


Combine an example backend with an example generator to provide full results that can be used to test other functions in the package.

Description

Except for example = "visualizations", all examples will actually compile and fit Stan models and thus may take a while to complete.

Usage

SBC_example_results(
  example = c("normal_ok", "normal_bad", "visualizations"),
  interface = c("rstan", "cmdstanr", "rjags"),
  N = 100,
  n_sims = 50
)

Arguments

example
  • name of the example. normal_ok is an example where the generator matches the model (using the normal generator and normal_sd backend), while normal_bad is an example with a mismatch between the generator and backend that manifests in SBC (normal_bad combines the normal generator with normal_bad backend). visualizations creates a purely artificial results that are meant to showcase the built-in plots (the interface parameter will be ignored).

interface

name of the interface to be used for the backend

N

number of datapoints to simulate from the generator for each simulation

n_sims

number of simulations to perform


S3 generic using backend to fit a model to data.

Description

Needs to be implemented by all backends. All implementations have to return an object for which you can safely call SBC_fit_to_draws_matrix() and get some draws. If that's not possible an error should be raised.

Usage

SBC_fit(backend, generated, cores)

S3 generic to get backend-specific diagnostics.

Description

The diagnostics object has to be a data.frame but may inherit additional classes - in particular it may be useful for the returning object to implement get_diagnostic_messages().

Usage

SBC_fit_to_diagnostics(fit, fit_output, fit_messages, fit_warnings)

Arguments

fit

The fit returned by SBC_fit

fit_output

a character string capturing what the backend wrote to stdout

fit_messages

a character vector of messages the backend raised

fit_warnings

a character vector of warnings the backend raised

Value

an single row data.frame that includes diagnostics or NULL, if no diagnostics available.


S3 generic converting a fitted model to a draws_matrix object.

Description

Needs to be implemented for all types of objects the backend can return from SBC_fit(). Default implementation just calls, posterior::as_draws_matrix(), so if the fit object already supports this, it will work out of the box.

Usage

SBC_fit_to_draws_matrix(fit)

## Default S3 method:
SBC_fit_to_draws_matrix(fit)

Create a brms generator.

Description

Brms generator uses a brms model with sample_prior = "only" to generate new datasets.

Usage

SBC_generator_brms(
  formula,
  data,
  ...,
  generate_lp = TRUE,
  generate_lp_chunksize = 5000/nrow(data),
  out_stan_file = NULL
)

Arguments

formula, data, ...

arguments passed to brms::brm

generate_lp

whether to compute the overall log-likelihood of the model as an additional variable. This can be somewhat computationally expensive, but improves sensitivity of the SBC process.

generate_lp_chunksize

Will determine whether the chunk size used with future.apply::future_mapply(). Set quite high by default as the parallelism only benefits for large individual datasets/number of simulations.

out_stan_file

A filename for the generated Stan code. Useful for debugging and for avoiding unnecessary recompilations.


Wrap a function the creates a complete dataset.

Description

This creates a very thin wrapper around the function and can store additional arguments, but does not do anything more..

Usage

SBC_generator_custom(f, ...)

Arguments

f

function accepting at least an n_sims argument and returning and SBC_datasets object

...

Additional arguments passed to f

Details

Running:

gen <- SBC_generator_custom(f, <<some other args>>)
datasets <- generate_datasets(gen, n_sims = my_n_sims)

is equivalent to just running

datasets <- f(<<some other args>>, n_sims = my_n_sims)

So whenever you control the code calling generate_datasets, it usually makes more sense to just create an SBC_datasets object directly and avoid using SBC_generator_custom and generate_datasets at all. SBC_generator_custom can however be useful, when a code you do not control calls generate_datasets for you and the built-in generators do not provide you with enough flexibility.


Generate datasets via a function that creates a single dataset.

Description

Generate datasets via a function that creates a single dataset.

Usage

SBC_generator_function(f, ...)

Arguments

f

function returning a list with elements variables (prior draws, a list or anything that can be converted to draws_rvars) and generated (observed dataset, ready to be passed to backend)

...

Additional arguments passed to f


Print the Stan code of a model used in the examples.

Description

Print the Stan code of a model used in the examples.

Usage

SBC_print_example_model(
  example = c("normal_sd", "normal_bad"),
  interface = c("rstan", "cmdstanr", "rjags")
)

Arguments

example

name of the example model.


Create an SBC_results object

Description

This will build and validate an SBC_results object from its constituents.

Usage

SBC_results(
  stats,
  fits,
  backend_diagnostics,
  default_diagnostics,
  outputs,
  messages,
  warnings,
  errors
)

Details

The SBC_results contains the following fields:

  • ⁠$stats⁠ statistics for all variables and fits (one row per variable-fit combination)

  • ⁠$fits⁠ the raw fits (unless keep_fits = FALSE) or NULL if the fit failed

  • ⁠$errors⁠ error messages that caused fit failures

  • ⁠$outputs⁠, ⁠$messages⁠, ⁠$warnings⁠ the outputs/messages/warnings written by fits

  • ⁠$default_diagnostics⁠ a data frame of default convergence/correctness diagnostics (one row per fit)

  • ⁠$backend_diagnostics⁠ a data frame of backend-specific diagnostics (one row per fit)


Recompute SBC statistics given a single fit.

Description

Potentially useful for doing some advanced stuff, but should not be used in regular workflow. Use recompute_SBC_statistics() to update an ⁠[SBC_results]⁠ objects with different thin_ranks or other settings.

Usage

SBC_statistics_from_single_fit(
  fit,
  variables,
  generated,
  thin_ranks,
  ensure_num_ranks_divisor,
  dquants,
  backend,
  gen_quants = NULL
)

Arguments

thin_ranks

how much thinning should be applied to posterior draws before computing ranks for SBC. Should be large enough to avoid any noticeable autocorrelation of the thinned draws See details below.

ensure_num_ranks_divisor

Potentially drop some posterior samples to ensure that this number divides the total number of SBC ranks (see Details).

dquants

Derived quantities to include in SBC. Use derived_quantities() to construct them.

backend

the model + sampling algorithm. The built-in backends can be constructed using SBC_backend_cmdstan_sample(), SBC_backend_cmdstan_variational(), SBC_backend_rstan_sample(), SBC_backend_rstan_optimizing() and SBC_backend_brms(). (more to come: issue 31, 38, 39). The backend is an S3 class supporting at least the SBC_fit(), SBC_fit_to_draws_matrix() methods.

gen_quants

Deprecated, use dquants instead

See Also

recompute_SBC_statistics()


Summarize relational property of overall prior and posterior draws

Description

Summarize relational property of overall prior and posterior draws

Usage

set2set(priors, posteriors, par, bins = 20)

Arguments

priors

A posterior::draws_rvars of dimension(n_iterations=1, n_chains=n_sbc_iterations, n_variables=n_variables) which stores prior draws

posteriors

A posterior::draws_Rvars of dimension(n_iterations=n_posterior_draws, n_chains=n_sbc_iterations, n_variables=n_variables), which stores fitted posterior draws

par

names of parameter to summarize

bins

number of bins for prior and post density


Validate a definition of derived quantities evaluated in R.

Description

Validate a definition of derived quantities evaluated in R.

Usage

validate_derived_quantities(x)

wasserstein distance between binned samples

Description

wasserstein distance between binned samples

Usage

wasserstein(x, y)

Arguments

x

numeric vector of density from first distribution

y

numeric vector of density from second distribution

...

unused

Value

distance value based on max difference