Refits each equation once without each fold of the levels of a grouping, and scores every held-out level from that refit with all of its own deviations integrated out.
kfold_groups(
x,
group = NULL,
K = 10,
folds = NULL,
fold = NULL,
iter = NULL,
warmup = NULL,
seed = 1L
)A bayesnecfit or bayesmanecfit.
The grouping whose levels are held out, as a column name or
an interaction such as "plate:conc_rank". By default, the one
grouping with a deviation in the fit whose levels are each observed at a
single concentration. Name it where its levels have no deviation in the
equation, such as a cell identifier in an equation with only a plate term.
Its column may then be one kept in x$retained_data rather than in
the fitted data.
The number of folds. Ignored when folds is supplied.
A named integer vector assigning every level of the grouping to a fold, numbered consecutively from one. By default the levels at each concentration are assigned to the folds in turn, in random order, so that each fold spans the concentration series.
One fold to compute in this process. By default every fold is computed in sequence.
The iterations and warm-up of each refit. By default, those of the fit.
The seed of the fold assignment and of the draws that start the integration over each held-out level's deviations. The refits use the seed the fit recorded, and this one only where it recorded none.
Where every fold is computed and scored, a criterion of class
c("kfold", "loo") with one pointwise row per level, or for a model
set a named list of them, one per equation. Its attributes group,
folds, refit_settings and refit_diagnostics record the
grouping, the realised assignment, the refits' iter, warmup
and seed, and each refit's diagnostics. Where fold is given,
or any fold has no score, a kfold_groups_part for
kfold_combine.
Each level of the grouping must be observed at a single
concentration. Each equation may have up to two deviations on the grouping.
Each must be a normal intercept, independent of the other, as for an
ogl() term together with one (par | group) term. Every other
grouping must keep all its levels in each training set, so a fold that
removes a whole plate is refused.
Each refit takes the fit's formula, priors, family, backend, chains,
sampler control and seed, with the fit's initial values and its
group-level deviations started at zero. As in brms::update(), its
chains run on getOption("mc.cores", 1) cores. A held-out level is
scored by the log of the mean, over the refit's draws, of the likelihood of
its observations with its deviations integrated out by Gauss–Legendre
quadrature, nested where there are two. The other group-level deviations
are taken at each draw. The likelihood is evaluated by
brms::log_lik(), so censoring, the transform of an ogl() term
and a disp() model are taken from the fit.
To compute the folds in separate R processes, read the same saved fit in
each, pass the same folds (or K and seed), iter
and warmup, and one fold, and save the returned part.
kfold_combine joins the parts.
The result records each refit's largest R-hat, number of divergent transitions, share of transitions at the maximum tree depth, elapsed seconds and any error. A fold whose refit fails, or which gives a held-out level no finite log predictive density, leaves its levels without a score and gives a warning.
loo::loo_compare() compares the criteria of two fits to the same
data and levels. The pointwise rows are named by level, so that the
differences can be summed within a coarser grouping, such as plate, for
its standard error. The p_kfold column is NA, because the log
predictive density of the full fit is not computed.