R/JAGS-formula-scale-transform.R
transform_prior_samples.RdGenerate prior samples and transform them using the same matrix transformation as posterior samples. This is the correct approach for visualizing priors on the original (unscaled) scale, especially for the intercept which depends on contributions from multiple coefficient priors.
transform_prior_samples(
fit,
n_samples = 10000,
seed = NULL,
formula_scale = NULL
)a fitted model object with prior_list and optionally
formula_scale attributes
number of samples to generate (default: 10000)
random seed for reproducibility (optional). The caller's
random-number state (.Random.seed and RNGkind()) is restored
afterwards. With NULL, the caller's random-number stream is used.
optional nested list containing standardization information.
If not provided, extracted from fit attribute. An entry must name
every predictor that fit standardized for that parameter. The
transformation is derived from the fitted structure stored in fit: the
fixed-effect design, a log intercept, and the random-effect SD structure.
A matrix of prior samples on the original (unscaled) scale, with columns matching the structure of posterior samples. Columns that are not mapped by the scale transformation, such as the totals and allocation nodes of ordered priors, keep their fitted-scale values.
When models use auto-scaling (standardizing predictors), the posterior samples are on the standardized scale. To correctly visualize priors on the original scale, we cannot simply apply a linear transformation to individual priors because the intercept on the original scale is a weighted sum of multiple priors:
$$\beta_0^{orig} = \beta_0^* - \sum_i \frac{\mu_i}{\sigma_i} \beta_i^*$$
This function generates samples from ALL priors simultaneously and applies the same matrix transformation used for posterior samples, which correctly handles the intercept and all other parameters.
Monitored random-effect nodes that the model defines deterministically from
other nodes are computed from the prior draws of those nodes with the
model's own definitions: standard deviations derived from a variance
allocation (random_variance_allocation()), scalar correlations
sampled on the Fisher-z or logit scale, and LKJ Cholesky factors,
correlation matrices, and partial correlations. Every parameter-catalog
quantity that depends only on such nodes and on the priors can therefore be
evaluated on the prior draws (see parameter_draws()). The auxiliary
nodes of spike-and-slab and mixture priors are included from the components
of their rng() draws: the component indicator (<parameter>_indicator)
of both, and the inclusion probability (<parameter>_inclusion) and
slab draws (<parameter>_variable) of spike-and-slab priors; the
random-number stream of the other columns is unchanged. The totals of
ordered priors (<parameter>_ordered_total) are included with the
same auxiliary nodes of a spike-and-slab or mixture total; the slices of a
spike-and-slab total of an interaction share its inclusion probability and
indicator, as in the fitted model. Standardized latent random effects,
nodes derived from them, such as realized group coefficients, the
component nodes of mixture priors, and the auxiliary nodes of Dirichlet
priors are not included, so catalog quantities that depend on them cannot
be evaluated on the prior draws.
Variance-allocation inclusion indicators are included, drawn from their
inclusion probabilities.
Valid LKJ concentrations do not guarantee representable primitive Beta draws.
Nonfinite draws or draws outside the open interval (0, 1) stop before
deterministic replay with BayesTools_lkj_rng_unavailable (parent
BayesTools_prior_rng_unavailable). The condition records block,
primitive, K, eta, original-row failed_draws and n_failed.
Choose a concentration whose primitive draws remain representable; no retry,
clamping, endpoint substitution or prior change is performed. Explicit scalar
correlation transforms that numerically saturate a boundary retain their
compiler refusal rather than changing the requested prior or scale.
The conditions of transform_scale_samples() apply
(BayesTools_formula_transform_unavailable and
BayesTools_refit_required). Expression-point priors without a
certified persisted recipe stop with
BayesTools_formula_transform_unavailable and reason
"uncertified_point_replay"; non-representable LKJ primitive draws
signal BayesTools_lkj_rng_unavailable.
# With a fitted model that used formula_scale:
# prior_samples <- transform_prior_samples(fit, n_samples = 10000)
# This can then be used with density() or for custom plotting