fb_select() starts
from a fitted mixed model and a list of candidate covariates, and asks
which candidates improve predictions for new sites. The fixed effects
already in the model are protected: they are never shrunk or
dropped. The candidates share a shrinkage prior, and the choice between
them is scored on folds that hold out whole sites. A shrinkage posterior
has no coefficients exactly equal to zero, so “selected” always means
chosen by a stated rule.
Twenty sites with ten plots each, and eight candidate covariates.
Only x1 and x2 affect the response.
set.seed(20260912)
n_site <- 20L
per_site <- 10L
site <- factor(rep(sprintf("S%02d", seq_len(n_site)), each = per_site))
n <- length(site)
d <- data.frame(site = site)
for (v in paste0("x", 1:8)) {
d[[v]] <- stats::rnorm(n)
}
u <- stats::setNames(stats::rnorm(n_site, sd = 0.8), levels(site))
d$y <- 2 + 1.2 * d$x1 - 0.9 * d$x2 +
as.numeric(u[as.character(d$site)]) + stats::rnorm(n, sd = 0.6)The base model has a random site effect and no fixed effects other than the intercept. This is needed for the regularised horseshoe prior used below, which brms applies to every fixed coefficient except the intercept. A base with protected fixed effects is shown later.
fb_penalty() takes one formula per block of candidates.
p0 is the prior guess at how many candidates matter. The
posterior can disagree with it.
pen <- fb_penalty(term(everything()) ~ regularized_horseshoe(p0 = 2))
pen
#> <fb_penalty> 1 specification(s), resolution = "term", scaling = "train_fold_standardize"
#> 1. term(everything()) ~ regularized_horseshoe(p0 = 2, slab_scale = 2, slab_df = 4, df = 1, df_global = 1, scope = "candidates")
#> route class_wide, hyperparameters learned
#> columns resolve against the model matrix at fit time.plan = TRUE checks the request and reports what would
run, without fitting.
fb_select(
base,
candidates = ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8,
penalty = pen,
resamples = fb_resamples(unit = ~ site, v = 3L),
plan = TRUE,
seed = 20260912L
)
#> == flexyBayes selection plan ============================
#> Projection structure gate:
#> response family supported Gaussian response with an identity link
#> population-level effects supported the fixed part is what the search selects over (probe C2)
#> (1 | site) supported grouped random intercept: get_refmodel() accepted it and a validated grouped k-fold search ran (probes C1-C3)
#> Workflow: bayes
#> Provider: brms
#> Prior: regularized_horseshoe (route class_wide)
#> Decision: projection (resolved from "auto")
#> every structural feature of the reference model has an executed projection gate behind it, so the decision is the projection
#> the search runs on 20 clustered draws and the predictive evaluation on 100
#> Candidates: x1, x2, x3, x4, x5, x6, x7, x8
#> Prior blocks:
#> 1. term(everything()) ~ regularized_horseshoe over 8 coefficient(s), learned hyperparameters
#> stanvars: no
#> Structure gate: supported
#> Folds: 3, prediction new_cluster
#> Cost estimate: 24.6s over 4 fit(s) at 6.15s each
#> the per-fit figure is the base model's own recorded wall time; the reference model carries a wider fixed part and a shrinkage prior, so this is a floor
#> Approximations this route commits to:
#> - calibration_n_at_the_resampling_unit: the prior scales are calibrated against 20 units of site rather than against 200 rows, because rows inside a cluster are not independent pieces of evidence about a coefficient
#> - projection_search_folds_reuse_whole_sample_preprocessing: the projection's k-fold search refits the reference model through brms, which reuses the reference fit's standardised data, compiled prior and calibration, so the constants and prior scales inside the search's folds are the whole sample's and the elpd-by-size curve that chooses the submodel is not strictly out of fold. The benchmark's out-of-fold tables learn every constant on each fold's analysis rows
#> Nothing was fitted.The reference model with all eight candidates is fitted to all the
data and again in each fold. projpred then projects it onto smaller
models, and the rule "one_se" picks the smallest model
whose predictive score is within one standard error of the best. A
horseshoe prior needs adapt_delta = 0.99 here to avoid
divergent transitions, which makes the fit slower.
projpred prints convergence warnings from its submodel fits. They are hidden on this page.
sel <- fb_select(
base,
candidates = ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8,
penalty = pen,
resamples = fb_resamples(unit = ~ site, v = 3L),
decision = fb_decision(method = "projection", rule = "one_se"),
engine = list(chains = 4L, n_samples = 1500L,
warmup = 1500L, adapt_delta = 0.99),
seed = 20260912L
)
#> Warning: 49 of 6000 (1.0%) transitions hit the maximum treedepth limit of 10.
#> See https://mc-stan.org/misc/warnings for details.
#> Setting 'K' to the number of folds (3)
#> Fitting model 1 out of 3
#> Warning: 11 of 6000 (0.0%) transitions hit the maximum treedepth limit of 10.
#> See https://mc-stan.org/misc/warnings for details.
#> Fitting model 2 out of 3
#> Warning: 1 of 6000 (0.0%) transitions hit the maximum treedepth limit of 10.
#> See https://mc-stan.org/misc/warnings for details.
#> Fitting model 3 out of 3
#> Warning: 6 of 6000 (0.0%) transitions hit the maximum treedepth limit of 10.
#> See https://mc-stan.org/misc/warnings for details.
sel
#> <fb_select> workflow bayes / provider brms / prior regularized_horseshoe / decision projection, searched on 20 clustered draws and evaluated on 100
#> Projection structure gate:
#> response family supported Gaussian response with an identity link
#> population-level effects supported the fixed part is what the search selects over (probe C2)
#> (1 | site) supported grouped random intercept: get_refmodel() accepted it and a validated grouped k-fold search ran (probes C1-C3)
#> base : gaussian/identity, 200 rows, convergence ok
#> prior n : calibrated at 20 site, not at 200 rows
#> covariance: none: covariance integrated by the engine / integrated (integrated by the engine, not plugged in)
#> resampling: 3 folds, prediction new_cluster, scored at site
#> reference : adequacy pass -- 49 transition(s) reached the treedepth ceiling of 10; reported, not decisive
#>
#> decision : projection
#> size : 3 ((1 | site), x1, x2)
#>
#> approximations:
#> - calibration_n_at_the_resampling_unit: the prior scales are calibrated against 20 units of site rather than against 200 rows, because rows inside a cluster are not independent pieces of evidence about a coefficient
#> - projection_search_folds_reuse_whole_sample_preprocessing: the projection's k-fold search refits the reference model through brms, which reuses the reference fit's standardised data, compiled prior and calibration, so the constants and prior scales inside the search's folds are the whole sample's and the elpd-by-size curve that chooses the submodel is not strictly out of fold. The benchmark's out-of-fold tables learn every constant on each fold's analysis rowsThe expected log predictive density (elpd) by model size, with standard errors computed over sites:
rnd(sel$projection$elpd_by_size[, c("size", "elpd", "se", "delta_to_ref")])
#> size elpd se delta_to_ref
#> 1 0 -401.561 10.049 -208.038
#> 2 1 -399.124 10.735 -205.600
#> 3 2 -325.288 11.672 -131.765
#> 4 3 -192.557 12.864 0.966
#> 5 4 -192.576 12.785 0.947
#> 6 5 -193.270 12.931 0.253
#> 7 6 -193.470 12.687 0.053
#> 8 7 -193.609 12.468 -0.085
#> 9 8 -193.658 12.591 -0.135
#> 10 9 -193.566 12.520 -0.043tidy() gives the reference posterior for each candidate.
These are not the estimates of the chosen submodel.
rnd(tidy(sel)[, c("term", "role", "estimate", "std.error", "retained")])
#> term role estimate std.error retained
#> 1 (Intercept) protected NA NA TRUE
#> 2 x1 candidate 1.273 0.042 TRUE
#> 3 x2 candidate -0.888 0.040 TRUE
#> 4 x3 candidate -0.006 0.028 FALSE
#> 5 x4 candidate -0.020 0.032 FALSE
#> 6 x5 candidate -0.016 0.032 FALSE
#> 7 x6 candidate 0.040 0.040 FALSE
#> 8 x7 candidate -0.021 0.032 FALSE
#> 9 x8 candidate 0.003 0.028 FALSEMost real models protect some fixed effects, such as a treatment. The
horseshoe on its own covers every fixed effect, so use
laplace(), ridge() or
elastic_net(), which put a prior on each candidate
separately, or regularized_horseshoe(scope = "block"),
which fits the candidates as their own part of the model and leaves the
protected effects outside it.
d$p1 <- stats::rnorm(nrow(d))
d$y_p <- d$y + 0.7 * d$p1
protected_base <- flexybayes(
y_p ~ p1 + (1 | site),
data = d,
backend = "brms",
chains = 2L,
n_samples = 2000L,
warmup = 1000L,
seed = 20260912L,
verbose = FALSE,
mcmc_verbose = FALSE
)
protected_sel <- fb_select(
protected_base,
candidates = ~ x1 + x2 + x3 + x4 + x5 + x6 + x7 + x8,
penalty = fb_penalty(term(everything()) ~ laplace(scale = 0.3)),
resamples = fb_resamples(unit = ~ site, v = 3L),
decision = fb_decision(method = "full"),
engine = list(chains = 4L, n_samples = 1500L, warmup = 1500L),
seed = 20260912L
)
rnd(tidy(protected_sel)[, c("term", "role", "family", "estimate")])
#> term role family estimate
#> 1 (Intercept) protected none NA
#> 2 p1 protected none 0.754
#> 3 x1 candidate laplace 1.266
#> 4 x2 candidate laplace -0.889
#> 5 x3 candidate laplace -0.005
#> 6 x4 candidate laplace -0.041
#> 7 x5 candidate laplace -0.038
#> 8 x6 candidate laplace 0.067
#> 9 x7 candidate laplace -0.044
#> 10 x8 candidate laplace 0.005p1 has no shrinkage prior. The same request with the
horseshoe stops:
fb_select(
protected_base,
candidates = ~ x1 + x2,
penalty = fb_penalty(term(everything()) ~ regularized_horseshoe(p0 = 1)),
resamples = fb_resamples(unit = ~ site, v = 3L),
plan = TRUE,
seed = 20260912L
)
#> Error:
#> ! fb_penalty(): regularized_horseshoe() is a class-wide prior -- brms applies it to every population-level coefficient except the intercept -- and these coefficients are outside the block: p1. Three ways forward: use ridge(), laplace() or elastic_net(), which target blocks exactly; write regularized_horseshoe(scope = "block"), which fits the candidates as their own non-linear part and leaves those coefficients under their own priors, with the full or posterior-threshold decision; or write regularized_horseshoe(scope = "all_fixed"), an explicit opt-in under which those coefficients also sit under the global-local prior, the projection search forces them into every submodel, and the certificate records approximations = "protected_under_global_shrinkage".penalty = NULL fits the base model with and without the
candidates in each fold, and compares their predictions for the held-out
sites. Here the base is fitted with INLA.
d$y2 <- d$y + 0.4 * d$p1
inla_base <- flexybayes(
y2 ~ p1,
random = ~ site,
data = d,
backend = "inla",
verbose = FALSE
)
increment <- fb_select(
inla_base,
candidates = ~ x1 + x2,
resamples = fb_resamples(unit = ~ site, v = 3L),
seed = 20260912L
)
rnd(glance(increment)[, c(
"loss", "loss_protected_only", "loss_full", "loss_reduction", "n_units"
)])
#> loss loss_protected_only loss_full loss_reduction n_units
#> 1 neg_elpd 20.088 13.957 6.131 20The scores are held-out losses, so lower is better.
neg_elpd is minus the log predictive density of a held-out
site, and loss_reduction is the loss without the candidates
minus the loss with them.
A shrinkage prior on an INLA base is fitted with brms, since INLA does not fit these priors.
benchmark = TRUE adds two non-Bayesian comparisons on
the same folds: generalised least squares and glmnet.workflow = "penalized_optimizer" computes a penalised
path directly, which is much faster than sampling.sel$certificate records what was fitted, by which
engine, under which prior and rule.| Prior | Base with protected fixed effects | Projection |
|---|---|---|
ridge(), laplace(),
elastic_net() |
yes | yes |
regularized_horseshoe(), r2d2() |
only with scope = "all_fixed", which shrinks the
protected effects too |
yes |
any prior, on a base with vm() or
ped() |
yes | no (the default decision = "auto" uses
"full") |