Reading a fit, for packages that extend flexyBayes
Source:R/extension_api.R, R/fixed_design.R
fb_extension.RdA package built on flexyBayes reads a fitted model through these
functions, fb_refit(), fb_marginal_cov(), fb_isolated_lapply() and
the refusal registry (fb_register_refusals(), fb_refuse()), and never
through the package's internal functions. fb_extension_api() gives the
version of that set. A package checks it in its .onLoad() and refuses to
load against a version it was not written for.
Builds the fixed-effect design matrix of formula over data, as
flexyBayes builds it for its aliasing check, with the term of each column
and the order in which ASReml 4.2 enters the columns.
Usage
fb_extension_api()
fb_model_terms(fit)
fb_response_values(response, data)
fb_offset_values(offsets, data, newdata = FALSE)
fb_model_weights(terms)
fb_known_variance(terms)
fb_engine_settings(engine)
fb_convergence(fit)
fb_inla_posterior_sample(fit, n, ...)
fb_check_refit(fit, caller = "")
fb_fixed_design(formula, data, train_rows = NULL)Arguments
- fit
A flexyBayes fit.
- response
The response as written in the model: a column name or an R expression in the columns of
data.- data
A data frame.
- offsets
The offset terms of a model as written, for example
"offset(log(area))",fb_model_terms(fit)$offsets.- newdata
TRUEwhendataholds new rows for a prediction, so that a missing offset column is refused with a message that says so.- terms
A model description from
fb_model_terms(), or a fit.- engine
NULL, anfb_engineobject, or a named list of sampler settings.- n
The number of posterior samples.
- ...
Further arguments to
INLA::inla.posterior.sample(), for exampleseed,num.threadsorselection.- caller
Text placed in front of a refusal's message, naming the function that asked.
- formula
A one-sided formula of fixed terms.
- train_rows
Row numbers of the training rows, or
NULL(default) to learn transforms from every row.
Value
fb_extension_api(): an integer, the version of the set. It is 1.fb_model_terms(): a list, orNULLfor an object that carries none.fb_response_values(): a vector with one value per row ofdata, orNULLwhen the response cannot be evaluated there.fb_offset_values(): a numeric vector with one value per row ofdata, orNULLwhen the model has no offset.fb_model_weights(): the numeric weights, orNULLfor an unweighted model.fb_known_variance():NULLfor a model without a known variance. Otherwise a list withvalues(one variance per row of the fitted data),matrix(the known covariance, orNULLfor a vector),column(the data column that carries each row's variance) andinla_index(the index of the row effect that carries it in an INLA formula).fb_engine_settings(): a named list, empty forNULL.fb_convergence(): a list withengine,ok(TRUE,FALSEorNA),reasonsandevidence.fb_inla_posterior_sample(): the listINLA::inla.posterior.sample()returns, with the linear predictor and the hyperparameters on the scale of the data.fb_check_refit():fit, invisibly.
A list: X (the matrix), terms, frame (the model frame),
xlev (the factor levels), assign (the term index of each column),
column_terms (the term label of each column) and asreml_order (the
column positions in ASReml's order: term by term, and within a term by
descending factor levels with the first factor varying slowest).
Details
fb_model_terms()returns the model description of a fit: the parsed fixed, random, residual and addition terms, the response, the family and the link, as the engines received them.fb_response_values()evaluates a response, a column name or an expression such as"log(y)", overdata.fb_offset_values()sums the offsets of a model over the rows ofdata, refusing an offset that is not a finite number on every row.fb_model_weights()returns the observation weights a model was fitted with.fb_known_variance()describes the known variance or covariance of a model and where it is carried in the data.fb_engine_settings()checks and returns sampler settings.fb_convergence()returns the convergence verdict thatsummary(fit)reports, with its evidence.fb_inla_posterior_sample()draws joint posterior samples of an INLA fit on the scale of the data.fb_check_refit()refuses a fit that cannot be refitted as the model it was fitted as.fb_fixed_design()builds the design matrix of a fixed formula.
Factor levels and contrasts are those of all of data. With train_rows,
a transform that learns from the rows it is evaluated on (scale(),
poly(), splines::ns(), splines::bs()) learns from the training rows
alone and is applied with those parameters to every row, the rule of
stats::predict(). Missing values are kept as rows of NA.
Examples
fb_extension_api()
#> [1] 1
d <- data.frame(y = c(1, 4, 9), area = c(1, 2, 3))
fb_response_values("sqrt(y)", d)
#> [1] 1 2 3
fb_offset_values("offset(log(area))", d)
#> [1] 0.0000000 0.6931472 1.0986123
fb_engine_settings(list(chains = 2L))
#> $chains
#> [1] 2
#>
fb_fixed_design(~ area, d)$X
#> (Intercept) area
#> 1 1 1
#> 2 1 2
#> 3 1 3
#> attr(,"assign")
#> [1] 0 1