The frequentist analog of brms's pp_check(): responses are
simulated from the fitted model (marginally over the random effects)
and handed to the corresponding bayesplot ppc_* function
(bayesplot must be installed, but not necessarily attached).
Usage
pp_check(object, ...)
# S3 method for class 'frmtmb_fit'
pp_check(
object,
type = "dens_overlay",
ndraws = 10,
prefix = c("ppc", "ppd"),
group = NULL,
x = NULL,
newdata = NULL,
resp = NULL,
...,
re_formula = NA
)Arguments
- object
A
frmtmb_fitfor a univariate model.- ...
Passed to the
ppc_*function.- type
The bayesplot check, i.e. the part after
ppc_("dens_overlay","hist","stat","stat_grouped","scatter_avg", ...). Withprefix = "ppc"a name thatbayesplot::available_ppc()does not list is refused, as brms refuses it.- ndraws
Number of simulated response vectors.
- prefix
"ppc"(the default) plots the observed response against the simulated ones;"ppd"plots the simulated ones alone, through bayesplot'sppd_*function of the same name.- group
The name of a model variable to stratify by, for the
*_groupedtypes, which need it. The name is looked up in the model frame, as brms looks it up in the model's data, so a column the model does not use is refused.- x
The name of a model variable for the types that take an
x("intervals","ribbon","error_scatter_avg_vs_x", ...), looked up asgroupis.- newdata, resp
brms's arguments. With
newdatathe check is on its rows:simulate()draws for them, the observed series is its response column (needed forprefix = "ppc"only), andgroupandxare read from it, as brms reads them. A row with a missing response is dropped with brms's warning. On a multivariate fitrespnames the one response to check, as in brms, which refuses the call without it: that response's observed values against its own simulated replicates, on its own rows (asubset()response on the rows of its subset). On a model with one responserespis accepted and ignored, which is what brms does with it there.- re_formula
The random-effect switch, in brms's spelling (
pp_check()is a brms function). On a fit it is passed tosimulate()and defaults toNA, which redraws every group-level effect in each replicate and holds every SMOOTH at its estimate, a smooth indexed by a grouping factor included;~0and~1mean the same, and a one-sided formula keeps the terms it names and redraws the rest (seesimulate.frmtmb_fit()). On draws it is passed toposterior_predict()and defaults toNULL, because a draw already carries its own. lme4'sre.formis refused. brms honors it onpp_check()and warns that it ignored it, which is a leak through its dots rather than a decision to copy.
Types a fit cannot draw
The loo_* types weight posterior draws by Pareto-smoothed importance
sampling, and a maximum-likelihood fit has no posterior draws, so
they are refused on a fit. pp_check() on the draws of
frmtmb.sample::frm_sample() is the route to them.
"error_binned" is refused on an ordinal, categorical or multinomial
fit, as brms refuses it.
Examples
if (requireNamespace("bayesplot", quietly = TRUE)) {
set.seed(1)
dd <- data.frame(x = rnorm(100), g = factor(rep(1:10, 10)))
dd$y <- rpois(100, exp(0.3 + 0.4 * dd$x + rnorm(10, 0, 0.6)[dd$g]))
fit <- frm(bf(y ~ x + (1 | g)) + poisson(), data = dd)
# the observed density against draws from the fit
pp_check(fit, ndraws = 20)
# any bayesplot ppc_* check, named by its suffix. A statistic the
# model was not fitted to is the informative one: here, the share
# of zeros, which is how zero inflation shows up.
pp_check(fit, type = "stat", stat = function(y) mean(y == 0),
ndraws = 50)
}
#> `stat_bin()` using `bins = 30`. Pick better value `binwidth`.