Reports, per group of the predictor, the observed location and scale of the response against what the fitted model simulates, with a posterior predictive p-value for each. Sits alongside check_chains, which checks the sampler, and check_priors, which checks the priors: this checks the fit against the data.

check_fit(x, group = NULL, ndraws = 1000, seed = 10, ...)

# S3 method for class 'bayesnecfit'
check_fit(x, group = NULL, ndraws = 1000, seed = 10, ...)

# S3 method for class 'bayesmanecfit'
check_fit(x, group = NULL, ndraws = 1000, seed = 10, ...)

# S3 method for class 'bayesnecgroupfit'
check_fit(x, group = NULL, ndraws = 1000, seed = 10, ...)

# S3 method for class 'bayesnechurdlefit'
check_fit(x, group = NULL, ndraws = 1000, seed = 10, combined = TRUE, ...)

# S3 method for class 'checkfit_group'
print(x, ...)

# S3 method for class 'checkfit_group'
plot(x, ...)

Arguments

x

An object of class bayesnecfit or bayesmanecfit as returned by bnec, a bayesnechurdlefit returned by bnec_hurdle, or a bayesnecgroupfit returned by bnec_group.

group

A numeric, character or factor vector of the same length as the data giving the group each row belongs to, or a single whole number giving the number of bins to cut the predictor into. A vector with a missing value is refused. If NULL (the default) the distinct values of the predictor are used where they are replicated, and the predictor is binned otherwise, with a warning. For a bayesnecgroupfit, an explicit vector has one value per row of the original data passed to bnec_group(), in its original order. It is split by fitted level before the checks. The fitted-level factor is not the predictor grouping used within each level. For a bayesnechurdlefit, an explicit vector has one value per exposed row; the growth check uses the values for surviving rows.

ndraws

A numeric vector of length 1, the number of posterior draws to simulate from. Reduced silently to the number the fit actually holds where that is smaller.

seed

A numeric vector of length 1. Passed to set.seed before simulating, so the result is reproducible. The caller's random number stream is restored before the check returns, so running it does not change what the next random operation in the session returns. NULL is refused rather than passed on, because set.seed(NULL) re-initialises the stream from the clock.

...

Unused.

combined

A logical vector of length 1. If TRUE (the default) a third table is returned checking the two components against the response they jointly predict, which is what the user actually observed. The growth component is predicted onto the full exposed set rather than onto its own data, since it is fitted on survivors only. Where either component is a model set, the third table has one block of rows per pair of growth and survival equations, named "growth / survival" in the column model. When one component has one equation, wi is the other component's model weight. With model sets in both components, wi is available under pseudo-BMA through crossed_weights; it is omitted for other weighting methods.

Value

A data.frame with one row per group (per model, for a bayesmanecfit), carrying the group, the number of observations, the observed and simulated mean and standard deviation, their ratios, a posterior predictive p-value for each, and a logical flag marking the control group. For the mixture families it also carries the observed and simulated proportion of zeros. A group with one observation has no standard deviation; its standard deviation statistics are NA, while its mean statistics remain available. Where the formula includes a cens() term, the column n_cens gives the number of censored readings in each group. A bayesnecgroupfit returns a checkfit_group object containing the original checkfit table for each fitted level in $fits. Each table retains its attributes, including any stored Pareto diagnostics. print() labels and prints each level; plot() returns a named list of plots with one plot per fitted level. A bayesnechurdlefit returns a plain list of such tables, named growth, survival and, unless combined = FALSE, combined. Plot each element separately, as in plot(checks$growth).

The separate "pareto" attribute is a data frame with one row per equation. It names the stored criterion used for model weights, its Pareto cutoff, the count and IDs of held-out units at or above that cutoff, the largest k, and any successful or failed group refits. flagged_ids, refitted_ids, and refit_failed_ids are list columns of character vectors. A missing criterion has status = "unavailable" and missing counts, not a clean result. Single-equation fits have no model-weight comparison. For hurdle fits the growth and survival tables have their own attributes; the combined predictive table has no leave-one-out criterion.

Pareto k assesses the approximation to a leave-one-out score. It does not assess sampler convergence or the posterior predictive discrepancies in the concentration-level table. Grouped scores use the package's 0.7 cutoff; observation-wise scores use the applicable loo cutoff.

A checkfit table for this single fit, with model and Pareto diagnostic attributes.

A checkfit table with one row block per model and columns for model weights, with Pareto diagnostics as an attribute.

Details

Why this is not dispersion

dispersion reports one global statistic, and for any family with a free dispersion parameter that parameter absorbs exactly the discrepancy the global statistic measures. On the packaged manec_example the global Pearson ratio is 1.011, with a 95% interval of 0.71 to 1.44 — a clean bill of health — while the same fit simulates about 27% more variability than the data show in the control region. A global summary is structurally incapable of showing that, which is why this check is local.

Why the control row matters

nsec sets its reference from the sig_val quantile of the posterior of the control mean, so the width of that posterior sets the reference. Overstate control variability and the reference falls, the curve crosses it later, and NSEC moves right — a less protective number. Understate it and NSEC moves left. A curve pulled away from the control data biases the same reference in location rather than spread. The control row is flagged for that reason.

Residuals, not raw values

The scale statistic is computed on residuals. Within a group the raw standard deviation of the response mixes residual variability with the slope of the curve across the group, which would make every steep-region group look overdispersed.

Model averaging does not protect you here

Model weights come from a global elpd, whether they are the pseudo-BMA weights bnec computes by default or stacking weights requested through loo_controls. The control is a handful of rows out of many, so a candidate model can hold high weight while fitting the control badly — it wins on the bulk of the curve and pays almost nothing for the control. For a bayesmanecfit the per-model rows are therefore reported alongside the averaged rows, or the table could not say which model is doing the damage.

Posterior predictive p-values give half weight to simulated statistics that tie the observed statistic. Ties are common for count families because their statistics take few distinct values. When both an observed statistic and the median simulated statistic are zero, their ratio is reported as 1 to denote agreement in the control check.

Censored readings

Where the formula includes a cens() term, a censored reading is recorded at its bound, whereas the responses simulated from the fit are not censored. In each group that includes a censored reading, the observed and the simulated responses are therefore both held at one bound before the statistics are computed: values below the largest left-censoring bound in the group are raised to it, and values above the smallest right-censoring bound are lowered to it. An interval-censored reading is treated as left-censored at the upper end of its interval. A group whose lower holding bound exceeds its upper holding bound cannot be compared this way and is refused. The true value of every censored reading is known to be beyond the bound, so its held value is known exactly, and the observed and simulated statistics are computed from the same quantity. Where the observed statistic equals the median simulated one, as when every value is held, its ratio is 1. A structural zero of a hurdle or zero-inflated family stays at zero, unless a left-censored reading in the group could itself be a zero; the zeros are then held with the other values and the group's zero proportions are NA.

Holding the values discards what the data show beyond the bound. The column n_cens gives the number of censored readings in each group, and where it approaches n the statistics of that group show little more than whether the fitted model predicts readings beyond the bound. pp_check instead leaves censored readings out, which in a partly censored group compares the uncensored readings, all on the measured side of the bound, with simulations that fall on either side.

For a bayesnechurdlefit one table is returned per component, following the precedent dispersion sets. The two carry different response vectors — growth is fitted on survivors only, survival on every individual — so there is no single table that describes both.

Examples

if (FALSE) { # \dontrun{
library(bayesnec)
nec4param <- pull_out(manec_example, model = "nec4param")
check_fit(nec4param)
data(nec_data)
nec_data$site <- rep(c("a", "b"), length.out = nrow(nec_data))
grouped <- bnec_group(y ~ crf(x, "nec3param"), data = nec_data,
                      group_var = "site")
checks <- check_fit(grouped)
plot(checks)
} # }