| glmbayes_insight_methods | R Documentation |
Methods implementing the insight accessor generics for objects of
class "glmb" (which includes lmb, rglmb,
rlmb, and rGamma_reg fits, since these all
inherit "glmb" in their class vector). This lets insight-based
tooling (including bayestestR) recognize glmbayes fits
as Bayesian models and extract posterior draws in the standard
insight shape (one row per draw, one column per parameter).
model_info.glmb() always reports is_bayesian = TRUE and
derives family/link-specific flags (is_linear, is_count,
is_poisson, is_binomial, is_trial, is_logit,
is_probit, is_exponential) from x$family (defaulting
to gaussian when absent, which is the case for
lmb fits). All other boolean fields reported by
model_info (e.g. is_mixed,
is_zero_inflated, is_ordinal, is_gam,
is_multivariate) are explicitly set to FALSE rather than
omitted, since some insight/bayestestR internals test these
fields with if() and error on NULL.
get_parameters.glmb() returns x$coefficients (the posterior
draws matrix) as a data frame, with a dispersion column appended
only when the dispersion parameter was actually sampled (i.e. varies
across draws) rather than held fixed. dGamma with
Inv_Dispersion = TRUE (an "rGamma_reg" fit) is a special
case: that prior is on the inverse dispersion (precision) only
– beta is a fixed, known input (typically held fixed in a Gibbs
step), not itself a modeled/estimated quantity – so
get_parameters.glmb() reports only a dispersion
column and omits the coefficients entirely, rather than recycling the
fixed row as if it were a (degenerate) posterior draw.
dGamma with Inv_Dispersion = FALSE is the mirror
image: there, the prior is on the coefficient (the conjugate
Gamma-Poisson/Gamma-Gamma rate), which is genuinely sampled, and
dispersion is fixed – so that case reports coefficients as usual and
omits dispersion.
find_parameters.glmb() returns the same parameter grouping as a
named list of parameter names (not values): conditional for
the regression coefficients, plus dispersion only when sampled –
or, for dGamma(Inv_Dispersion = TRUE), dispersion only,
with no conditional entry at all (mirroring
get_parameters.glmb() above).
find_algorithm.glmb() reports the estimation method as
"iid rejection sampling" rather than falling through to the
inherited "glm"/"lm" methods (which would otherwise report
"ML"/"OLS" — incorrect for a Bayesian iid-sampling fit).
There is no chains/warmup concept (draws are independent,
not an MCMC chain), so only algorithm and iterations are
reported, mirroring find_algorithm's treatment of
other non-MCMC Bayesian samplers (e.g. bayesQR).
get_data.glmb() returns x$data (or x$model, the model
frame, when x$data is not itself a data frame – e.g. when
glmb()/lmb() was called without an explicit data
argument, in which case x$data is the calling environment, as for
glm). This avoids a spurious "Could not recover
model data from environment" warning that get_data's
default method emits for glmb fits before falling back to
(successfully) reconstructing the same data from the model frame.
get_priors.glmb() does not translate the pfamily
prior specification into insight's usual
Parameter/Distribution/Location/Scale shape
(unlike, say, get_priors.stanreg/get_priors.brmsfit). That
shape has one row per parameter with only a scalar
Location/Scale, so it cannot represent off-diagonal
covariance for a genuinely multivariate normal coefficient prior.
Instead, get_priors.glmb() returns pfamily(x)
directly, so it prints the exact same Call/Prior
Family/Prior List report as pfamily(x) (via the package's
existing print.pfamily() method) – including the complete
covariance matrix, not just its diagonal. See
glmbayes_bayestestR_prior_methods's
simulate_prior.glmb() for a consumer that draws from this true
joint distribution, and describe_prior.glmb(), which returns the
same thing under bayestestR's naming.
## S3 method for class 'glmb'
model_info(x, ...)
## S3 method for class 'glmb'
get_parameters(x, ...)
## S3 method for class 'glmb'
find_parameters(x, ...)
## S3 method for class 'glmb'
find_algorithm(x, ...)
## S3 method for class 'glmb'
get_data(x, ...)
## S3 method for class 'glmb'
get_priors(x, verbose = TRUE, ...)
x |
An object of class |
... |
Not used; included for S3 signature compatibility with the insight generics. |
verbose |
Not used; included for S3 signature compatibility with
|
model_info.glmb() returns a named list of model
metadata matching the shape of model_info's output
for other model classes.
get_parameters.glmb() returns a data.frame with one
row per posterior draw and one column per parameter (plus a
dispersion column when sampled).
find_parameters.glmb() returns a named list of
character vectors (parameter names), with components conditional
and (when sampled) dispersion.
find_algorithm.glmb() returns a named list with
components algorithm and iterations.
get_data.glmb() returns a data.frame (the model data).
get_priors.glmb() returns the fit's "pfamily" object (see
pfamily).
glmb, lmb, rglmb,
rlmb, glmbayes-package, pfamily;
model_info, get_parameters,
find_parameters, find_algorithm,
get_data, get_priors.
## insight accessor methods for glmb/lmb fits, covering all 16 of insight's
## documented "core" functions (see ?glmbayes_insight_methods and insight's
## own JOSS paper for this list): model_info, get_parameters, find_parameters,
## get_priors, find_algorithm, get_data, get_response, get_predictors,
## get_random, get_variance, find_formula, find_variables, find_terms,
## find_predictors, find_random, find_response.
##
## 6 of these (model_info, get_parameters, find_parameters, find_algorithm,
## get_data, get_priors) have custom glmb methods (R/insight-methods.R) and
## are re-exported by glmbayes, so they can be called unqualified below. The
## other 10 already work correctly via glmb/lmb's inherited "glm"/"lm" class
## entries or insight's own defaults, are NOT re-exported by glmbayes, and
## are called below with an explicit insight:: prefix.
## ----dobson-------------------------------------------------------------------
## Dobson (1990) Page 93: Randomized Controlled Trial :
counts <- c(18, 17, 15, 20, 10, 20, 25, 13, 12)
outcome <- gl(3, 1, 9)
treatment <- gl(3, 3)
ps <- Prior_Setup(counts ~ outcome + treatment, family = poisson())
glmb.D93 <- glmb(
n = 1000,
counts ~ outcome + treatment,
family = poisson(),
pfamily = dNormal(mu = ps$mu, Sigma = ps$Sigma)
)
## ----custom glmb methods: model_info, get_parameters--------------------------
model_info(glmb.D93)$is_bayesian ## TRUE
model_info(glmb.D93)$is_poisson ## TRUE
draws <- get_parameters(glmb.D93)
dim(draws) ## one row per posterior draw, one column per coefficient
head(draws)
## ----custom glmb methods: find_parameters, find_algorithm---------------------
find_parameters(glmb.D93) ## list(conditional = <coefficient names>)
find_algorithm(glmb.D93) ## "iid rejection sampling", not "ML"
## ----custom glmb methods: get_data, get_priors---------------------------------
get_data(glmb.D93) ## model data frame, no environment-recovery warning
## get_priors() returns pfamily(glmb.D93) directly -- the same Call / Prior
## Family / Prior List report pfamily() itself prints, including the
## complete covariance matrix for this multivariate normal coefficient
## prior (not just its diagonal, which insight's usual per-parameter
## Location/Scale table for other model classes could not represent).
get_priors(glmb.D93)
identical(get_priors(glmb.D93), pfamily(glmb.D93)) ## TRUE
## ----already work via inherited glm methods: response/predictor discovery-----
insight::get_response(glmb.D93) ## the observed counts
insight::get_predictors(glmb.D93) ## data frame of predictor columns (no response)
insight::find_response(glmb.D93) ## "counts"
insight::find_predictors(glmb.D93) ## list(conditional = c("outcome", "treatment"))
insight::find_formula(glmb.D93) ## list(conditional = counts ~ outcome + treatment)
insight::find_variables(glmb.D93) ## response + conditional predictor names
insight::find_terms(glmb.D93) ## response/conditional terms, incl. "1" for intercept
## ----already correctly NULL/empty: no random-effects structure----------------
## glmbayes fits have no random effects, so these correctly report "none".
## get_variance()'s default fallback also warns "not supported" (accurate,
## since it targets variance-decomposition models); verbose = FALSE silences
## that expected warning for this clean demo run.
insight::get_random(glmb.D93) ## NULL
insight::get_variance(glmb.D93, verbose = FALSE) ## NULL (no variance components)
insight::find_random(glmb.D93) ## NULL
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.