Takes what a factorised fit favoured and refits it as one model expressing
structure the factorisation could not. For a
bayesnechurdlefit that is the model combination
crossed_weights favours, refitted with a two-block
(hurdle_gamma, zero_inflated_beta, hurdle_poisson or
hurdle_negbinomial) family. For a bayesnecgroupfit it
is the equation each level favours, composed into one model in which every
curve parameter takes a separate value per level of the grouping factor,
estimated in a single posterior.
bnec_joint(object, ...)
# Default S3 method
bnec_joint(object, ...)
# S3 method for class 'bayesnechurdlefit'
bnec_joint(
object,
model = NULL,
model_survival = NULL,
formula = NULL,
...,
prior_type = NULL,
asymptote_observed = NULL,
predictor_scale = NULL,
x_control = NULL
)
# S3 method for class 'bayesnecgroupfit'
bnec_joint(
object,
model = NULL,
formula = NULL,
disp_by_level = TRUE,
...,
prior_type = NULL,
asymptote_observed = NULL,
predictor_scale = NULL,
x_control = NULL
)An object of class bayesnechurdlefit returned by
bnec_hurdle, or of class bayesnecgroupfit
returned by bnec_group.
Further arguments passed to brm.
An optional character string naming the
equation to fit. For a hurdle fit it is the response block's equation and
defaults to the highest-weighted growth model in object. For a
grouped fit it forces one equation at every level; the default is instead
the equation each level's own weights favour, which may differ between
levels.
An optional character string naming
the equation for the survival block. Defaults to the highest-weighted
survival model in object. Hurdle fits only; refused for a grouped
fit.
An optional bayesnecformula to fit instead of
the one held in object. Supply this to add structure the factorised
fit cannot carry, for example a group-level term. The crf model
argument is overwritten by model either way.
The arguments of
bnec that build the default priors. Each defaults to the value
object was fitted with, and to the bnec default where
object does not record it, as a fit made by an earlier version of
bayesnec does not. Each must be named in full.
The control concentration on the recorded predictor scale. Defaults to the value recorded by the factorised fit.
A logical. Should the family's
dispersion parameter also take a separate value per level? Defaults to
TRUE. Grouped fits only; refused for a hurdle fit.
For a bayesnechurdlefit, an object of class
bayesnecfit; for a bayesnecgroupfit, an object
of class bayesnecjointfit.
Why refit at all
The factorised and joint routes give equivalent inference where the two components are independent, so a joint refit of the same pair of equations adds nothing on its own – the estimates differ only by Monte Carlo noise. What it adds is the ability to write structure that spans the two blocks, which the factorisation cannot express by construction: a group-level effect shared between response and survival makes the two posteriors dependent, and a dependent posterior cannot be assembled from two fits run separately.
The division of labour is therefore: fit the model sets with
bnec_hurdle, where all n_growth * n_survival
combinations can be compared from two fits; choose a combination from
crossed_weights; then refit that one jointly here, adding the
shared structure. Model averaging happens in the first step, because the
joint route can only average over the response block.
What is carried over
The response variable, predictor and data are taken from object, and
the family follows the growth component: hurdle_gamma where that was
fitted as a Gamma, zero_inflated_beta where it was a Beta, and the
corresponding count hurdle where it was Poisson or negative binomial.
A left-censored survivor in a Gamma growth component is refitted as an
interval from zero to its recorded bound. This conditions its response on
known survival, as the factorised fit does. The recorded response stays
positive in the returned data and in the package's checks and plots.
The corresponding zero-inflated Beta joint refit is refused, because Stan
fails to initialise when it evaluates the beta CDF at zero with sampled
shape parameters. The factorised fit remains available. Supplying a
formula without cens() instead fits those values as exact
observations at their recorded bounds.
For either class, the default priors are built with the
prior_type, asymptote_observed and predictor_scale
object was fitted with, unless those arguments are supplied. A grouped
fit made with asymptote_observed = FALSE is therefore refitted under
the same declaration. The recorded control concentration is carried over;
priors and brms control arguments are defaults again unless supplied.
A disp() term in the formula held in object is kept, and in
the joint model it applies to the dispersion parameter of the positive
block. The variance function is written in the component mean mu of
that block and centred on the positive responses, as it is for the growth
component, so the joint model fits the same variance function (see
bnec). A formula supplied here replaces the held one,
so a disp() term is fitted only where it is written in that formula.
A negative binomial growth component fitted with a disp() term is
refitted as a hurdle_negbinomial whose shape follows the same
sub-model.
The joint refit of a grouped fit
bnec_group fits each level of a factor separately, and a
group-level term such as ogl() or (nec | site) pools the
levels towards a common curve. Neither fits one model in which every level
has its own curve parameters estimated together, which is what this route
adds. Where the levels share a parameter, through a group-level term in
formula or through disp_by_level = FALSE, their posteriors are
dependent, and a contrast between two levels is taken between their draws
from the same posterior iteration, as compare_posterior takes
it. Under the defaults the levels share no parameter,
so their posteriors are independent, as they are in a
bnec_group fit.
Each level is fitted the equation its own model weights favour, which is the
bayesnecgroupfit analogue of what best_crossed
does for a hurdle fit: that function returns the best growth equation and
the best survival equation separately, and the two blocks of one joint
hurdle model then carry different equations. One brms model can carry
a different functional form per level, because the mean is arbitrary
arithmetic over data columns and parameters: each level's equation is
multiplied by an indicator that is one on that level's rows and zero
elsewhere, and the terms are summed.
Where every level favours the same equation the sum reduces to that equation
with each parameter's ~ 1 replaced by ~ 0 + <group_var>, which
is dummy coding of the factor onto the curve, and that is what is built.
model forces one equation at every level, which is the same reduced
form.
A group-level term cannot be added to a refit whose levels chose different
equations, because the levels then share no parameter for it to be written
on. Pass model to fit one equation everywhere, which is the form such
a term applies to.
disp_by_level decides whether the family's dispersion parameter is
also estimated per level. FALSE shares one dispersion across the
levels, which is the model for a measurement error that is a property of the
assay rather than of the level.
ecx, nsec, nec,
ecnsec and exceedance on the returned object
give one row per level, and autoplot draws one panel per
level. ecx, nsec, ecnsec and
exceedance return the data.frame a
bayesnecgroupfit returns, and nec adds columns
naming each level's equation and the type of its estimate. Each level is
estimated on its own prediction grid, so the estimate for a level is read
off that level's curve alone.
The returned bayesnecjointfit has the other methods of a
bayesnecfit as well, such as summary(),
plot(), check_fit, rhat and
update(), each returning one result per level where it returns one
per fit; bayesnecjointfit lists them. amend,
pull_out, c() and + stop on it with a message
naming what to use instead, because each of its levels has one equation
and it is not a model set.
if (FALSE) { # \dontrun{
library(bayesnec)
data(nec_data)
nec_data$y[nec_data$x > 2.5] <- 0
nec_data$tank <- factor(rep(1:10, length.out = nrow(nec_data)))
fit <- bnec_hurdle(y ~ crf(x, c("nec3param", "nec4param", "ecx4param")),
data = nec_data)
# the pair crossed_weights() favours, refitted as one model
fit_j <- bnec_joint(fit)
# the same pair with a group-level effect the factorisation cannot carry
fit_r <- bnec_joint(fit, formula = y ~ crf(x, "nec3param") + (nec | tank))
# a grouped fit, refitted as one model with a value per level
data(nec_data)
nec_data$site <- factor(rep(c("a", "b"), length.out = nrow(nec_data)))
fits <- bnec_group(y ~ crf(x, c("nec3param", "nec4param")),
data = nec_data, group_var = "site")
joint <- bnec_joint(fits)
} # }