validate_inla_vs_brms: Spot-check INLA posterior estimates against brms/Stan

View source: R/diagnostics.R

validate_inla_vs_brmsR Documentation

Spot-check INLA posterior estimates against brms/Stan

Description

Generates a small number of synthetic datasets using the same internal data-generator as brms_inla_power(), fits each dataset with both INLA (as the package does) and brms::brm() (via Stan), and compares the posterior mean, posterior SD, and 95 % credible-interval bounds for the primary effect parameter.

This is a qualitative spot-check, not a formal equivalence test. Differences are expected due to the Laplace approximation used by INLA versus full MCMC in Stan, and will vary across models and priors.

Usage

validate_inla_vs_brms(
  formula,
  family = gaussian(),
  priors = NULL,
  data_generator = NULL,
  effect_name,
  effect_value = 0.5,
  sample_size = 100L,
  n_check = 5L,
  brms_iter = 2000L,
  brms_chains = 4L,
  tolerance = NULL,
  seed = 42L,
  error_sd = 1,
  group_sd = 0.5,
  obs_per_group = 10,
  predictor_means = NULL,
  predictor_sds = NULL,
  family_args = list(),
  inla_num_threads = NULL
)

Arguments

formula

Model formula (same as brms_inla_power()).

family

GLM family object (default gaussian()).

priors

A brms::prior() specification or NULL for defaults. Supported priors are translated to INLA controls where possible and audited in the returned settings.

data_generator

Optional function f(n, effect) returning a data frame. If NULL, the automatic generator is used.

effect_name

Character vector of fixed-effect names (same as brms_inla_power()).

effect_value

Numeric value (or named vector) of the true effect size used when generating data for the comparison. Default 0.5.

sample_size

Integer sample size for each comparison dataset. Default 100.

n_check

Number of independent datasets to compare. Default 5.

brms_iter

Total MCMC iterations per chain passed to brms::brm(). Default 2000.

brms_chains

Number of MCMC chains. Default 4.

tolerance

Numeric threshold: the flag is set to TRUE when the maximum absolute difference in posterior means exceeds tolerance. If NULL (default), ⁠0.1 * mean(INLA posterior SD)⁠ is used.

seed

Integer random seed. Default 42.

error_sd, group_sd, obs_per_group, predictor_means, predictor_sds, family_args

Passed to the automatic data generator when data_generator = NULL (see brms_inla_power() for details).

inla_num_threads

INLA threading string (e.g. "4:1"). Auto- detected when NULL.

Value

A list with components:

comparisons

Data frame with one row per dataset and columns sim, inla_ok, brms_ok, inla_mean, brms_mean, diff_mean, inla_sd, brms_sd, inla_ci_lower, inla_ci_upper, brms_ci_lower, brms_ci_upper.

flag

TRUE if any ⁠|diff_mean|⁠ exceeds tolerance.

max_abs_diff

Maximum observed ⁠|diff_mean|⁠.

tolerance

The tolerance value actually used.

settings

List of key settings for reproducibility.

Performance warning

This function runs n_check full brms/Stan fits. Even with brms_chains = 2 and brms_iter = 1000, this can take several minutes for non-trivial models. Use n_check = 3 and brms_chains = 2 for quick sanity checks.


powerbrmsINLA documentation built on July 2, 2026, 5:07 p.m.