Skip to contents

Does data simulated from the fitted model look like the data it was fitted to? Each check compares the observed outcomes with replicated data, one replicated dataset per posterior draw.

Usage

bayes_ppc(model, statistics = c("mean", "sd", "min", "max"), seed = 0)

bayes_loo_pit(model, seed = 0)

bayes_replicate(model, seed = 0)

ppc_check(
  observed,
  replicated,
  statistics = c("mean", "sd", "min", "max"),
  groups = NULL
)

loo_pit(observed, replicated, log_lik, r_eff = NULL, seed = 0)

Arguments

model

A bayes_glm_model; the checks run on its training data.

statistics

Test statistics: "mean", "sd", "min", "max", "proportion_zero", or numbers (probabilities) for quantiles.

seed

Seed of the replicates (and of the randomization over ties).

observed

The observed outcomes.

replicated

A matrix, one replicated dataset per row (posterior draw), one column per observation.

groups

Each observation's group, from 1, to check each statistic within each group; NULL for none.

log_lik

The pointwise log-likelihood, draws by observations.

r_eff

Each observation's relative efficiency; NULL for 1.

Value

bayes_ppc() and ppc_check(): a data frame with name, observed and p_value, and an attribute replicated, a matrix of each statistic of each replicate. bayes_loo_pit() and loo_pit(): a list with pit and pareto_k per observation and ks, the Kolmogorov–Smirnov distance from the uniform. bayes_replicate(): a matrix, draws by observations.

Details

  • bayes_ppc() and ppc_check(): a test statistic of the observed data against its distribution over the replicates, with the posterior predictive p-value P(T(y_rep) >= T(y)) (ties count half). A p-value near 0 or 1 says the model cannot reproduce that feature of the data.

  • bayes_loo_pit() and loo_pit(): each observation's leave-one-out probability integral transform, its place among its replicates under the Pareto-smoothed importance weights for leaving it out (as R loo's E_loo of y_rep <= y), randomized over ties. Uniform for a calibrated model; U-shaped when the predictive distributions are too narrow, humped when too wide.

  • bayes_replicate(): the replicated training datasets.

Examples

d <- data.frame(claims = rep(c(1, 2, 3, 5), 10), x = rep(c(-1.5, -0.5, 0.5, 1.5), 10))
m <- bayes_glm_fit(claims ~ x, d, family = "poisson", chains = 2, tune = 300, draws = 300)
bayes_ppc(m, c("mean", "sd", 0.9))
#>   name observed   p_value
#> 1 mean 2.750000 0.5058333
#> 2   sd 1.497862 0.9933333
#> 3 q0.9 5.000000 0.8375000
summary(bayes_loo_pit(m)$pit)
#>    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#>  0.3537  0.4663  0.5522  0.5447  0.5972  0.6899