Fits the model set independently within each level of a grouping factor, model-averaging within each level, and returns a bayesnecgroupfit. Crossed model weights across levels are then available from the per-level weights alone.

bnec_group(
  formula,
  data,
  group_var,
  family = NULL,
  predictor_scale = "auto",
  ...
)

Arguments

formula

A bayesnecformula, as passed to bnec.

data

A data.frame containing the data.

group_var

A character string naming the column of data that carries the factor.

family

A family function, string, or NULL. If NULL it is chosen once from the whole response and passed down — see Details.

predictor_scale

The predictor-scale declaration passed to bnec for every level. It is checked against the whole predictor before any level is fitted.

...

Further arguments passed to bnec for every level.

Value

An object of class bayesnecgroupfit.

Details

Reading and pooling levels

ecx, nsec, nec and ecnsec return one row per fitted level by default. With posterior = TRUE and pool = "none", they return a named list of per-level draws. pool = "mixture" combines levels in proportion to their observation counts; "mean_estimate" averages their estimates and "mean_curve" reads the estimate from their weighted mean curves. A NEC parameter has no curve to average, so nec() does not accept "mean_curve". Each level uses the control recorded at fitting, or its own lowest observed concentration if none was stated. x_control = "shared" or a number reads all levels at one concentration and warns where this lies below a level's observations.

Why the levels can be fitted separately

Factor levels partition the data disjointly and share no parameters, so the log likelihood is a sum over levels and the expected log predictive density is additive. For pseudo-BMA weights computed without the Bayesian bootstrap, the crossed model weights are then exactly the outer product of the per-level weight vectors, the identity crossed_weights rests on for the two blocks of a hurdle fit. The default pseudo-BMA weights use the bootstrap, which each level applies to its own observations, so for them the outer product approximates the weight of a combination scored on all levels together. See crossed_group_weights.

The family is chosen once

The family chosen from each subset separately could differ between levels, which would make the levels incomparable — their elpd contributions would not be on the same scale and the crossed weights would be meaningless. So it is chosen from the whole response and passed down.

Dispersion is per level, deliberately

Separate fits give each level its own sigma/shape/phi. That is a feature, not an oversight: a shared dispersion parameter would break the factorisation the crossed weights depend on. A model with structure spanning levels — shared dispersion, a group-level effect crossing levels, an explicit contrast — is a different model, and is not what this function fits. The favoured combination is not refitted jointly here; bnec_joint composes the equation each level favours into one model and fits it.

Fitting the levels in parallel

A grouped call has two loops that could use a future plan: the levels, and the models within a level. One plan drives one of them, because future evaluates a nested future sequentially unless the plan is a list, so bnec_group() decides which and says which.

The two arrangements are counted in rounds of one fit and the smaller is taken, a tie going to the models. Writing L for the levels, M for the equations the formula asks for and W for the workers, the levels take ceiling(L / W) * M rounds and the models L * ceiling(M / W). Seven levels of eleven equations over four workers is 22 against 21, so the levels stay in sequence; over eight workers it is 11 against 14, so they do not. A nested plan — plan(list(tweak(multisession, workers = 2), tweak(multisession, workers = I(4)))) — drives both loops and is honoured without being counted, and plan(list(sequential, ...)) asks for the levels in sequence explicitly.

No argument turns this on or off: the plan already holds that state, as it does for bnec.

A worked example, including the nested plan that drives both loops, is in the article Grouping and factor covariates (https://open-aims.github.io/bayesnec/articles/example8.html) under Fitting the levels in parallel.

What a plan does and does not reproduce

Each level is given its own seed, realised in the calling session before the levels are dispatched and derived from seed where you passed one. The realised seed is passed to every equation fitted for that level and is reapplied when the model-averaging draw is realised. A grouped call therefore gives the same estimates whichever arrangement it took, and the worker count does not change an answer. With a seed it repeats with no set.seed in the session; without one, set.seed before the call fixes it.

What was realised is kept: the returned object carries level_seeds, one per level in the order of levels. A regenerated seed cannot be trusted to be the same one years later — sample.int()'s algorithm changed once already, in R 3.6.0 — and these objects are archived and reopened, so the realisation is stored rather than described. bnec describes the rest of what a plan does to a fit, and none of that changes here.

Examples

if (FALSE) { # \dontrun{
library(bayesnec)
data(nec_data)
nec_data$site <- rep(c("a", "b"), length.out = nrow(nec_data))
fit <- bnec_group(y ~ crf(x, "nec3param"), data = nec_data,
                  group_var = "site")
nec(fit)
crossed_group_weights(fit)
} # }