R/bnec_hurdle.R
bnec_hurdle.RdFits a two-part concentration-response model as a pair of ordinary
bnec fits: one for the response of the units that gave a
non-zero value, and a bernoulli model for the probability
of being non-zero. The two are returned together so that the combined
endpoint – the expected response per unit exposed – can be derived from
them.
bnec_hurdle(
formula,
data,
model_survival = NULL,
family_growth = NULL,
predictor_scale = "auto",
...
)Either a character string defining an
R formula or an actual formula object. See
bayesnecformula. The response must be untransformed, and zero
values in it are taken to mean the individual did not survive. A
cens() aterm is allowed for continuous growth families. It is refused
for count growth families because brms does not combine left censoring
with zero truncation correctly. Other aterms are refused; see Details. A
disp() term applies to the growth component only; see Details.
A data.frame containing the data to use with
the formula. Every unit that entered the experiment must be present,
with 0 recorded for those that gave no response. Rows omitted rather
than zeroed cannot be distinguished from ones never run, and would be read
as a smaller experiment rather than as zeros.
An optional character vector naming
the model or model group to use for the survival component. Defaults to
whatever crf specifies in formula, i.e. the same set as the
response component.
A family function for the response
of the non-zero subset. Defaults to NULL, in which case it is chosen
from that subset the same way bnec would: Gamma for a
positive continuous response, Beta for one bounded on (0, 1), and
poisson for counts. A
two-block family (hurdle_gamma, zero_inflated_beta) is refused,
because bnec_hurdle is itself the two-part model, and so are the
zero-inflated count families, which are mixtures rather than hurdles – see
bnec. binomial and beta_binomial are refused as
well: each needs the number of trials in a trials() term, and
bnec_hurdle takes none. For poisson and negbinomial, the positive
subset is fitted with the corresponding zero-truncated likelihood. The
truncation is added internally because every row in that subset is known to
be positive; it is not a user-selectable response transformation. This path
requires brms 2.23.2 or later, where the fitted model and log
likelihood use the same inclusive lower bound. bayesnec computes the
exact conditional-positive expected response from the fitted count
parameters rather than using brms's finite-grid approximation.
The predictor-scale declaration passed to both
bnec fits. It is checked against the complete predictor before
either component is fitted. See bnec.
Further arguments passed to both bnec calls.
An object of class bayesnechurdlefit.
This covers the zero-inflated case too. brms names the
equivalent joint families hurdle_gamma and
zero_inflated_beta, but the distinction is nominal: zero-inflation
differs from a hurdle only when the base distribution can itself produce
zeros, which neither the Gamma nor the Beta can. The Stan density
brms generates for zero_inflated_beta is the hurdle form, with
no mixture at zero. One function therefore serves both, and the appropriate
family is chosen from the data – Gamma for a positive continuous
response, Beta for a proportion on (0, 1).
Throughout the documentation the two parts are called "growth" and "survival", after the case they were written for – individuals that die contribute a zero and the survivors contribute a measurement. Nothing in the implementation is specific to that reading: any process producing exact zeros alongside a positive response fits the same structure. Algal growth rate expressed as a proportion of a ceiling, with replicates that failed entirely, is the same model.
Why two fits rather than one
The hurdle log-likelihood separates exactly into a Bernoulli term over all individuals and a zero-bounded term over the survivors only:
log p(y_i) = log Bernoulli(alive_i | 1 - hu_i)
+ 1[y_i > 0] * log Gamma(y_i | mu_i, shape)
The two blocks share no parameters, so with independent priors the posterior
factorises and fitting them separately gives the same inference as fitting
them jointly. Doing so also makes the full crossed model comparison
tractable: elpd(a, b) = elpd_growth(a) + elpd_survival(b), so all
combinations of the two model sets can be compared from the two fits alone
rather than by fitting every pair.
The factorisation does not hold if the two components are coupled, for example by a shared group-level effect. Group-level terms supplied here apply within each component independently.
Coding of deaths
A 0 response means the individual did not survive; it is a label, not
a measurement. Where mortality is instead recorded by omitting rows, those
rows must be reinstated as zeros before calling this function.
Censoring, and which aterms are allowed
cens() is the one aterm accepted on a continuous response.
bnec
itself carries three – trials(), weights() and
cens() – and of those cens() is the only one whose meaning
stays unambiguous once the response is split across two models. It is also
the one with a use here that nothing else covers. A growth endpoint can be
both
zero-bounded with structural zeros and left-censored at the recording
resolution, and only a two-part model with a censored response component can
tell the two apart: a death is a structural zero belonging to the Bernoulli
component, while a survivor measured below the limit is a real observation of
the growth component whose value is known only to lie at or below a bound.
The declaration is routed accordingly. It is passed through to the growth component, where the censoring indicator travels as an ordinary data column and is subset along with everything else, and it is dropped from the survival component, whose alive/dead response is observed exactly. A row that is both zero and censored is refused rather than assigned to one of them.
Under a Gamma growth component the censoring bound cannot be 0 –
bnec rejects that, correctly, because the censored likelihood
contribution there is F(0) = 0. The bound has to be the resolution
limit, so the encoding is "at most the smallest resolvable value" rather than
"at most zero".
The other two are refused by name, with the reason. trials() has no
meaning for either component, and weights() is a modelling decision –
whether a weight applies to the growth component, the survival component or
both – that this function will not make on the user's behalf. Aterms beyond
those three are refused here as well, though they would not reach brms
in any case: model.frame drops them for an ordinary
bnec fit too. Making the two bnec calls directly
remains available for anything outside this set. Count growth is an
exception: combining its required trunc(lb = 1) with left censoring
makes brms subtract the truncation normaliser from an unconditioned
cumulative probability, which can yield a likelihood contribution greater
than one. bnec_hurdle therefore refuses cens() with a count
growth family before fitting.
Non-constant dispersion
A disp() term in formula models the dispersion parameter of
the growth component, as it does for bnec; see
bayesnecformula. It is removed from the survival component,
whose bernoulli likelihood has no dispersion parameter.
The parameters of the dispersion sub-model belong to the growth component
alone, so the factorisation described above still holds. The term is checked
against family_growth before either component is fitted, so a
specification that family cannot take is refused before anything is
sampled: any disp() term on a poisson growth component, for
example, or disp("twosided") on a Gamma one. A variance
function such as disp("power") is written in the mean of the growth
component as brms parameterises it. For a count growth family that is
the mean of the untruncated distribution, not the mean of the positive
counts that bayesnec reports.
A disp(~z) term naming a column other than the predictor is refused
for a negbinomial growth component, before either component is
fitted. The curve of that component is the mean of the positive counts,
mu / (1 - (shape / (mu + shape))^shape), which depends on the shape
parameter, so with the shape modelled on z the curve and every
estimate read from it would depend on the value z is given when
predicting. On a Gamma or Beta growth component the term is
fitted, and z is treated on a prediction grid as
bayesnecformula describes, with the values a predicted
reading takes drawn from the survivors the growth component is fitted to.
bnec for the equivalent joint fit via
family = "hurdle_gamma" or family = "zero_inflated_beta",
bayesnechurdlefit, crossed_weights