Specify mixed models using ASReml formula syntax and estimate them on one of two active engines: INLA (integrated nested Laplace approximation) or brms (the Stan passthrough). Returns a three-part result: a GLM-compatible object for use with standard R packages (emmeans, marginaleffects, etc.), the native backend object for engine-specific diagnostics, and extras for secondary analyses.
Usage
flexybayes(
fixed,
random = NULL,
residual = NULL,
data,
family = "gaussian",
link = NULL,
known_matrices = list(),
weights = NULL,
na_action = c("augment", "omit", "fail"),
n_samples = 1000,
warmup = 500,
chains = 4,
seed = NULL,
control = NULL,
prior = NULL,
prior_fixed_sd = 100,
prior_vc_sd = 1,
verbose = TRUE,
mcmc_verbose = TRUE,
return_code = FALSE,
review_code = FALSE,
backend = c("auto", "inla", "brms"),
aggregate = "auto",
plan = FALSE,
syntax = c("auto", "asreml", "brms"),
known_variance = NULL,
fixed_alias = c("refuse", "drop"),
rcov
)
fb(
fixed,
random = NULL,
residual = NULL,
data,
family = "gaussian",
link = NULL,
known_matrices = list(),
weights = NULL,
na_action = c("augment", "omit", "fail"),
n_samples = 1000,
warmup = 500,
chains = 4,
seed = NULL,
control = NULL,
prior = NULL,
prior_fixed_sd = 100,
prior_vc_sd = 1,
verbose = TRUE,
mcmc_verbose = TRUE,
return_code = FALSE,
review_code = FALSE,
backend = c("auto", "inla", "brms"),
aggregate = "auto",
plan = FALSE,
syntax = c("auto", "asreml", "brms"),
known_variance = NULL,
fixed_alias = c("refuse", "drop"),
rcov
)Arguments
- fixed
Two-sided formula
response ~ fixed_effects. This is the universal entry's model slot: it accepts the ASRemlfixedform (paired withrandom/residual) or a brms / lme4-style bar-grouped formula such asresponse ~ x + (1 | g)(in which case the grouping lives in the formula andrandom/residualmust be leftNULL). The grammar is detected from the call shape. Usesyntaxto force it.Offsets.
offset()terms in the fixed formula enter the linear predictor with a fixed coefficient of one on both engines, and several add. Each is evaluated againstdataand has to be a finite number on every row (fixed_offset_invalidotherwise). brms receives the terms in its formula; INLA receives the evaluated offset throughinla()'soffsetargument, and a Gaussian identity-link fit reads its centre, scale and default priors from the response minus the offset. The aggregated route carries an offset exactly on the Gaussian family (subtracted before the cell statistics are taken) and the Poisson family (as exposure), and refuses one on the binomial family (fixed_offset_unsupported), whereaggregate = "auto"takes the per-row route.predict()on new data evaluates the offset there and refuses when a column it reads is absent (newdata_offset_missing). Anoffset()inrandomorresidualis refused.- random
One-sided formula:
~ random_termsusing ASReml syntax. "Parsed" and "fitted" are not the same claim, so the term catalogue is two lists.Parsed and fitted (emitted by at least one active engine):
vm(),at(),diag(),idh(),us(),ar1(),spl(),ped(),dsum(),id(), and nested colon terms, including the per-trial separable AR1 field described under "Spatial and temporal autoregressive fields" below.Parsed and refused by name.
fa(f, k):g, the factor-analytic genotype-by-environment covariance, is refused on both engines (factor_analytic_not_repeatable). On simulated trials with a known covariance, the genetic-correlation intervals of INLA fits covered the true value less often than required (134 and 125 of 150 for k = 1 and 2, against 136), and flexyBayes has no factor-analytic lowering for brms. Writediag(f):g, which fits on both engines, orus(f):gon brms. The bare spellingfa(v, k), with no grouping factor for the covariance to apply to, keeps its own refusal (fa_not_representable).corh(f):gis likewise ASReml syntax that flexyBayes parses and refuses by name. It is described just below.Heterogeneous variances.
diag(f):g,idh(f):gandat(f):gall fit one variance per level offwith no covariance between levels: the usual multi-environment structure, where the genotype variance differs by site. ASReml treatsdiag()andidh()as the same structure, and so does flexyBayes. The three spellings emit identical code.us(f):gestimates every pairwise correlation instead, atk(k+1)/2parameters againstdiag()'sk.corh(f):g, heterogeneous variances with a single shared correlation, is refused on both engines: flexyBayes has no lowering for it, and approximating it with either neighbour would change the parameter count. With one variance for every level and a correlation of zero or more,random = ~ g + f:gis the same covariance and fits on both engines.Selected levels.
at(f, level):ggives the random effectgonly to the rows whereftakes one of the named levels, with one variance per named level, as in ASReml.at(design, "split"):mainplotis a main-plot effect in the split-plot trials alone. Levels are written as values,at(f, c("a", "b")), or as positions inlevels(f),at(f, 2). A level the data do not have is refused (at_level_not_in_data). It fits on both engines and reportssd_<g>__<f><level>, the nameat(f):ggives the same level.diag(f):gfits on both engines.us(f):gfits on brms, and INLA refuses it, soautoroutes such a model to brms.Spatial and temporal autoregressive fields.
ar1(t)and the separablear1(row):ar1(col)are written here, on the random side, and emitted on INLA as a latent autoregressive field plus the Gaussian observation nugget. Four hyperparameters on a field grid: the row correlation, the column correlation, the field SD and the nugget SD, all four printed bysummary(). Each correlation is named after the factor it runs along,rho_<factor>:rho_rowandrho_colforar1(row):ar1(col), and the same two names forar1(col):ar1(row). The field is faithful only with one observation per grid node, so an incomplete or replicated grid refuses (keep the unobserved nodes as design cells with the defaultna_action = "augment"). flexyBayes has no brms lowering for the field, so brms refuses.That four-parameter model is not ASReml's separable residual, which is one correlated residual with no independent plot error: three parameters, nested inside this one as the nugget goes to zero. On data with real plot-to-plot noise the two return different correlations, because a nugget-free model must absorb independent noise into the correlated structure and is pulled towards zero by it. The gap grows with the share of the total variance the nugget holds, and it falls on the correlations a reader acts on. Neither model is wrong. The residual form is written on the residual side,
residual = ~ ar1(row):ar1(col), described underresidualbelow.Per-trial nested field.
id(trial):ar1(row):ar1(col)fits one separable AR1 field per level oftrialon INLA, via thereplicate =mechanism: one complete grid per trial, each with its own realised field. The row correlation, column correlation and field SD are shared across every level oftrial, not estimated per trial. This is the model ASReml fits under the same spelling.A field for each trial.
at(trial):ar1(row):ar1(col)fits a separate field on each trial, with its own SD and correlations, as ASReml does: three parameters per trial, reported assd_spatial__trial<level>,rho_row__trial<level>andrho_col__trial<level>forar1(row):ar1(col), plus the shared Gaussian nugget.at(trial, level):ar1(row):ar1(col)fits the field on the named trials only. Each trial's plots must fill its own grid. brms refuses both.Units followed through time.
ar1(t):g, or the same term writteng:ar1(t), fits one AR1 series alongtwithin each level ofg, independent across levels, with one correlation and one field SD shared by every level and the Gaussian nugget on top, three hyperparameters in all. This is the units-by-occasions repeated-measures covariance of Verbyla and Cullis (1992) written on the random side:gindexes the plots, animals or series andtthe occasions, whose levels must be in time order. An occasion a unit missed stays as a design row with the responseNAunder the defaultna_action = "augment", and thetbyggrid must be complete in that sense, one row per node. brms refuses, as for the other fields.- residual
One-sided formula:
~ residual_structure. Default~ units(iid residuals).~ dsum(~ units | env), and the equivalent~ at(env):units, give a separate residual variance per level ofenv. Only families that carry a residual scale parameter can have one made heterogeneous. A Poisson's dispersion is a function of its mean, so there is nothing there to vary, and the request is refused.~ ar1(row):ar1(col)is ASReml's separable residual: one correlated residual with no nugget, three parameters (sd_spatial,rho_row,rho_col), and nosigmarow.~ dsum(~ ar1(row):ar1(col) | trial)fits it separately in each trial, with names suffixed__trial<level>. Both fit on INLA for a Gaussian response with the identity link and one plot per grid cell, where the observation error is fixed at a variance ofexp(-12)on the standardised response; brms refuses them. They cannot be combined with a random-side field.~ ar1(t)and~ ar1(row):colare refused on both engines, with a pointer to the random-side field described underrandomabove.- data
A data.frame containing all variables referenced in the formulas.
- family
Character:
"gaussian","binomial","poisson","negative_binomial","gamma","beta","cumulative","cratio","hurdle_gamma"or"lognormal". The last two are brms only: INLA's likelihood roster has no hurdle gamma, and no INLA lognormal fit is validated, so the family gate refuses both there andbackend = "auto"routes them to brms. A family object (binomial(),gaussian(),brms::lognormal()) is also accepted, goes through the same gate as its name, and supplies its own link, soGamma()means the inverse link it names, not the log linkfamily = "gamma"defaults to. Passing a family object together with a contradictinglinkis refused, not resolved. A family outside this set is refused at the door. Where the boundary is flexyBayes's own and not the engines', the refusal says so andinst/KNOWN_ISSUES.mdrecords it. A response outside the family's domain (0 or negative for"gamma"or"lognormal", negative for"hurdle_gamma", at or outside [0, 1] for"beta") is refused by name (family_response_domain_violation) before any engine runs. A missing (NA) response isna_action's concern, not a domain violation. The ordinal families are"cumulative"(proportional odds) and"cratio"(continuation ratio, the sequential model of Cao et al. 2026). The response is an ordered factor or a whole-number vector, its categories are the values observed, in order, and thresholds follow brms's convention:P(Y <= k) = F(tau_k - eta)for"cumulative"andP(Y > k | Y >= k) = F(eta - tau_k)for"cratio". The model keeps its intercept, whose place the thresholds take. INLA fits both families with the random structures it fits for a binomial response, thear1(row):ar1(col)field included, and the cumulative family with at most 11 categories. brms fits both without the field. A factor that is not ordered, or a character or logical response, is refused (ordinal_response_unordered), and so is an offset (ordinal_offset_not_supported). Category probabilities come fromfitted()andpredict(), andfb_kfold()cross-validates the fit.- link
Character or NULL: override the default link function (e.g.,
"probit"for binomial). Leave it unset whenfamilyis a family object, which carries its own link. The links fitted, default first: gaussianidentity,log,inverse,logit,probit,cloglog,cauchit; binomiallogit,probit,probit_approx,cloglog,cauchit,softit,identity,log; poissonlog,identity,sqrt; negative_binomiallog,identity,sqrt,softplus,squareplus; gammalog,identity,inverse; hurdle_gammalog,identity,inverse,softplus,squareplus; betalogit; lognormalidentity; cumulativelogit,probit; cratiologit,probit,cloglog.backend = "inla"takes the default link only, refusing any other by name (inla_gate_refused), andbackend = "auto"fits another link on brms. A link outside the list is refused by name (unsupported_family) on every backend.- known_matrices
Named list of matrices referred to in the random formula (e.g.,
list(Gmat = G_mat, Amat = A_mat)). The carrier is declared with thefb_cov()constructor inside the random term: a dense covariance (vm(group, cov = fb_cov(G, type = "dense")); brms), a user-supplied lower-triangular Cholesky factor (vm(group, cov = fb_cov(L, type = "chol")); brms), or a sparse precision matrixMatrix::dgCMatrix(vm(group, cov = fb_cov(Q, type = "precision")); INLA and brms). INLA takes the precision and pedigree-precision carriers only, and refuses by name a dense or Cholesky carrier, pointing atsolve(V)or at brms, and a block carrier, which INLA would fit with one standard deviation per block, pointing at the precision carrier of the block-diagonal inverse. The bare dense formsvm(group, V = ...)/ped(group, A = ...)remain the default. The legacy v0.3.7 keyword carriers (chol =,precision =,blocks =,low_rank_factor =) are deprecated and emit a migration warning. See the formula-surface vignette for per-type worked examples.- weights
Optional numeric weight vector (length N). Lowered for
family = "gaussian"with the identity link only, in the ASReml / lme4 /glm(weights=)precision sense,Var(y_i) = sigma^2 / w_i: INLA'sscale = wper-observation precision multiplier, and on brms a known offset on the log-link sigma distributional parameter (not brms's ownweights()addition term, which implements a different, likelihood-power quantity). A constant vector (includingrep(1, N)) is the unweighted model under a different spelling and passes through unchanged. A non-constant vector on any other family, or a non-identity link on Gaussian, is refused by name (weights_requires_gaussian) instead of silently producing the unweighted posterior. Likelihood-power, frequency, trials and exposure are different models and cannot share this argument silently.aggregate = TRUEtogether with weights is also refused by name (weights_not_aggregatable): the aggregated route's closed-form sufficient statistics do not carry a per-observation weight through the compression.- na_action
How to treat observations whose response is missing, and, through the list form below, observations whose predictors are missing.
"augment"(default) retains missing-response rows, carrying the missing response as a latent quantity the engine marginalises, and completes the design grid where the absent cells are determinable. This is ASReml'sna.method(y = "include")behaviour, and it matters whenever the model carries a covariance indexed by the design: deleting the row of a lost plot changes the index set a separable AR1 field is built over, so the fitted model is no longer the model that was written down."omit"drops the rows (complete-case), and a structured covariance over the resulting broken grid then refuses downstream."fail"refuses if any response is missing, and leaves the design grid alone.A response value that is
NaNor infinite, aslog(0)gives, is not a missing observation. It refuses by name under every setting (response_not_finite). Set it toNAto treat it as missing.The argument also accepts the object an ASReml user already writes,
asreml::na.method(y = , x = ), and the bare list of the same shape for readers without an asreml licence:The response words map
includeto"augment",omitto"omit",failto"fail". The covariate words are ASReml's own.x = "fail"(the default, and ASReml's) refuses a missing predictor.x = "omit"drops the affected rows with a warning naming the count and the columns.x = "include", ASReml's zero-fill (Reference Manual 4.2, section 3.11), is refused by name, because a zero is a value the plot did not have. A value arriving as an unreducedna.method()default vector is read as its first element, which is the policy ASReml itself would use.Under ignorability, and only when both representations put the same prior on the same model parameters, the posterior for those parameters is the same whether a missing response is augmented or omitted – augmentation preserves the representation, not information. A backend default prior can depend on the realised design matrix, so it does not establish that common-prior condition automatically. The identity is a statement about the posterior and not a promise about what an optimiser returns: the two settings hand the engine an intact design and a ragged one, and on a weakly identified mode they can land in different places. Where missingness depends on the unobserved response itself, both are biased and neither this argument nor any diagnostic here will tell you so.
- n_samples
Integer: number of posterior samples per chain.
- warmup
Integer: number of warmup (burn-in) iterations per chain.
- chains
Integer: number of MCMC chains.
- seed
Integer or
NULL: the seed for the sampler, on the engine that samples.On brms it is forwarded to
brms::brm(seed = )and fixes Stan's draws: two fits of the same model to the same data under the same seed, chains and backend return identical draws. WithNULL(the default) brms passes no seed and Stan runs with one drawn for it. On the cmdstanr backend that draw comes from R's random-number stream, so it followsset.seed()only when nothing else draws from the stream first. Either way the seed Stan ran with is kept atfit$extras$reproducibility$seed$as_run, and a refit under that value returns the same draws. Draws taken later from the fitted object, such asbrms::posterior_epred()on new levels, follow R's random-number stream and not this seed.On INLA the seed has no effect:
INLA::inla()takes no seed, and the argument says once that it was not used. A refit in a fresh R session can reach a different mode whatever the seed (see the section INLA threads). The thread setting an INLA fit ran with is recorded beside the engine versions, in the same record.- control
A named list of sampler control settings or
NULL, forwarded tobrms::brm(control = ). This is the route toadapt_deltaandmax_treedepth:control = list(adapt_delta = 0.95)is the standard first response to divergent transitions.NULL(the default) uses brms's own settings. A no-op on the INLA path, asseedis.- prior
An optional
fb_prior()object specifying priors via the PC-canonical hybrid DSL (preferred). When supplied it overridesprior_vc_sdfor the variance components it covers. Seefb_prior(). On INLA, a random-effect standard deviation the prior does not name takes the package's default,half_normal(scale = s)with the family-awaresdescribed underprior_vc_sd(divided by the covariate's standard deviation for an uncorrelated slope);prior_summary()lists what was filled. A standard deviation the prior does name is left exactly as written.- prior_fixed_sd
Numeric: SD for fixed-effect normal priors, applied uniformly to the intercept, factor contrasts, continuous slopes, factor x continuous interactions, and
I()-expression terms. Default100: weakly informative on the natural response scale for the vast majority of agricultural / clinical responses (covers responses with central tendency up to several hundred without crushing the posterior toward zero), while still regularising at sample sizes below ~ 30 per coefficient. Set wider (e.g.1000) for responses on a larger natural scale, or narrower for explicit shrinkage. A weakly-informative normal prior on the data scale, in the spirit of the weakly-informative-prior literature for regression coefficients (e.g. Gelman et al. 2008, Annals of Applied Statistics 2(4):1360-1383). Applied when you supply it, on every backend: brms receives onenormal(0, sd)row per fixed-effect class, and INLA receivescontrol.fixedwithprec = 1 / sd^2on the slopes and the intercept alike. Left unsupplied, brms keeps its own fixed-effect default (flat coefficients and a response-centredstudent_tintercept), because replacing a response-centred intercept prior withnormal(0, 100)is a different model for any response far from zero. INLA gives each column built from a numeric variable the covariate-scaled defaultnormal(0, 1e5 * sd(y) / sd(x))on an identity link andnormal(0, sqrt(1000) / sd(x))on a log or logit link, withsd(x)read over the rows whose response is observed, so a covariate's units do not change the fit, and keeps its ownprec = 0.001on factor contrasts and its flat intercept.prior_summary()names what a fit ran under. A supplied value is never rescaled by a covariate: it is a standard deviation in response units per covariate unit, so on a covariate recorded in small units (a large coefficient) it can bind. Measure the covariate in natural units, or leave the argument unsupplied on INLA.- prior_vc_sd
Numeric: hyperparameter for the legacy
lognormal(0, prior_vc_sd)variance-component prior. Note: when bothpriorandprior_vc_sdare left at their defaults, the package applies a half-normal default on the SD scale (half_normal(scale = s)with family-awares:5 * sd(y)for Gaussian,5on the logit scale for binomial / beta,3on the log scale for Poisson / negative-binomial / gamma) tosigmaand every named random-effect group, on both engines, with the scale on the standard deviation of an uncorrelated random slope divided by the standard deviation of its numeric covariate. It is weakly informative and has no upper bound. A half-t or half-Cauchy (Gelman 2006, Bayesian Analysis 1(3):515-534) is stated throughfb_prior(). Before 0.13.0 the default wasuniform(0, U)withUequal to the scale above. The legacylognormal(0, 1)default fires only whenprior_vc_sdis passed explicitly. Silence the one-time announcement message viaoptions(flexyBayes.silence_default_prior_note = TRUE). When it is passed explicitly it reaches both active engines as the same density on the standard-deviation scale: brms as alognormal(0, prior_vc_sd)prior row, INLA as the expression prior that writes that density in INLA's log-precision parameterisation. The two engines therefore carry the same variance-component prior.- verbose
Logical: print the generated backend code to the console.
- mcmc_verbose
Logical: show the sampler's progress bar. Has no effect on INLA, which does not sample.
- return_code
Logical: if TRUE, return the generated backend code without fitting the model: the Stan program on
backend = "brms", the INLA formula, family and hyperparameter list onbackend = "inla".- review_code
Logical: if TRUE, do not fit the model immediately; instead return a
<flexybayes_review>deferred-execution object carrying the generated Stan code, the resolved prior, the parsed intermediate representation (IR), the captured call, and a snapshot of.Random.seed. Inspect the code withcat_code()and run the deferred fit withproceed(). A secondproceed()call returns the cached fit. Useful as a teaching / auditing surface before a long MCMC run. DefaultFALSEpreserves the existing run-immediately semantics. A session-level override is available viaoptions(flexyBayes.review_code_default = TRUE). The argument value at call time always wins. Closest published precedent:brms::stancode()plus thechains = 0idiom for "do everything except sampling".review_code = TRUEandreturn_code = TRUEare mutually exclusive. brms is the only engine with a code slot, soreview_code = TRUEis available underbackend = "brms"and under"auto"(which resolves the code modes to brms). Underbackend = "inla"it refuses withreview_code_backend_unsupported: the deferred-execution token would need an INLA-side code slot, which is queued for a later release.- backend
Character: one of
"auto"(default),"inla", or"brms". INLA fits the latent-Gaussian model class by nested Laplace approximation, without sampling. brms is the Stan passthrough. Any other value, including a formerly-registered engine name this package withdrew entirely (seeNEWS.md), refuses withunknown_backendnaming the two active engines.What
"auto"does. The call runslgm_gate()and routes to INLA when the model is latent-Gaussian feasible and INLA is installed. Otherwise it routes to brms when brms is installed and its capability predicate accepts the model. When neither can represent the model, the call refuses withauto_no_active_routeinstead of fitting something else. Nothing is translated silently.autodoes change engine in one case after starting: an INLA program failure is caught and re-routed to brms, and a message reports it. The fit's class andbackendfield always record which engine produced it.backend_decision()surfaces the full dispatch trace (includingrejected_routes) post-fit, andfb_backend_status()reports which engines are usable. The one-time notes on the auto path are silenceable viaoptions(flexyBayes.silence_auto_fallback_note = TRUE)(gate refusal or an INLA numerical failure) andoptions(flexyBayes.silence_auto_inla_missing_note = TRUE)(INLA not installed).What each engine represents. INLA fits: Gaussian LMM, simple random intercept; GLMM (binomial, Poisson, negative binomial, gamma, beta), simple random effect; Ordinal regression (cumulative, continuation ratio); Uncorrelated random slope; Factor-by-numeric fixed interaction; Nested / interaction random effects, multi-stratum; Heterogeneous variance by factor level; Heterogeneous residual by factor level; Baseline heteroscedastic MET: iid genotype, iid genotype-by-environment and a sectioned residual; Known-covariance genomic / pedigree random effect; Separable AR1 spatial field; Per-trial separable AR1 field; Grouped temporal AR1 field; Univariate P-spline; Observation weights (Gaussian, identity link); Exact sufficient-statistic aggregation (binomial, Poisson). INLA refuses: Hurdle gamma (zero mass plus a positive gamma part); Correlated random slope; Unstructured genotype-by-environment covariance; Heterogeneous variances with one shared correlation; Factor-analytic genotype-by-environment covariance; Multi-trait covariance; Exact sufficient-statistic aggregation (Gaussian). brms fits: Gaussian LMM, simple random intercept; GLMM (binomial, Poisson, negative binomial, gamma, beta), simple random effect; Hurdle gamma (zero mass plus a positive gamma part); Ordinal regression (cumulative, continuation ratio); Uncorrelated random slope; Factor-by-numeric fixed interaction; Correlated random slope; Nested / interaction random effects, multi-stratum; Heterogeneous variance by factor level; Unstructured genotype-by-environment covariance; Heterogeneous residual by factor level; Baseline heteroscedastic MET: iid genotype, iid genotype-by-environment and a sectioned residual; Known-covariance genomic / pedigree random effect; Observation weights (Gaussian, identity link). brms refuses: Heterogeneous variances with one shared correlation; Factor-analytic genotype-by-environment covariance; Multi-trait covariance; Separable AR1 spatial field; Per-trial separable AR1 field; Grouped temporal AR1 field; Univariate P-spline. The per-class table is in
README.md, and with a note on each row insystem.file("KNOWN_ISSUES.md", package = "flexyBayes").Caution (small-group random effects). Both engines apply the same default prior, the half-normal on each standard deviation, which the INLA path represents exactly as an expression prior on the log-precision, so they agree on the variance component far more closely than in earlier versions. A model with very few groups nonetheless carries a weakly-identified variance component (the data say little about the between-group spread), and INLA's Laplace approximation is less accurate there than full MCMC. For a flagship random-intercept model with few groups, pin
backend = "brms"or supply an explicit informative prior (a half-Cauchy, per Gelman 2006, as in the priors and regularisation vignette).Convergence. The brms fit emits a warning when the sampler may not have converged (a parameter with Rhat at or above 1.1, or a low effective sample size). Treat such a posterior with caution: increase
warmup/n_samples, simplify the model, or supply a more informative prior, and inspect the full diagnostics withsummary(). Silence the warning (for intentionally short fits) viaoptions(flexyBayes.silence_convergence_warning = TRUE). An INLA fit does not sample and carries no such warning.Code inspection. brms is the only engine with a code slot, so
return_code = TRUE/review_code = TRUEunder"auto"resolve to brms, but only when brms can represent the model. Where it cannot, the call refuses withauto_no_active_routeinstead of returning the code for a different model.backend = "inla"withreturn_code = TRUEreturns the INLA formula, family and hyperparameter list. Withreview_code = TRUEit refuses until the INLA-side code slot lands.- aggregate
One of
"auto"(default),TRUE, orFALSE. Exact sufficient-statistics aggregation gate. INLA is the only engine with an aggregated emit."auto"consults the aggregation plan and routes a binomial-logit or poisson-log model through the per-cell path when the IR is in scope (fixed plus random intercept; productive compression) and the model reaches INLA. A Gaussian model is fitted per row: the aggregated Gaussian route cannot yet carry the residual prior, soTRUEon a Gaussian model refuses by name (aggregation_residual_prior_not_composed).TRUEalso raises a structured refusal when the plan declares ineligibility, or when no active engine offers an aggregated route.FALSEskips the gate entirely. Aggregated fits carry$exactness == "aggregated_exact", the dispatch trace'spathslot reads"aggregated_count", and their summary reports no marginal likelihood (aggregated_evidence_unavailable). For out-of-core datasets that do not fit in memory, seeflexybayes_stream().- plan
Logical: if
TRUE, short-circuit after intermediate representation (IR) build and return a<fb_plan>object carrying the IR, the routing decision, the representation plan, the aggregation plan, and the prediction plan, without emitting backend code or fitting. Equivalent to callingfb_plan()with the same formula triple, exposed inline asflexybayes(plan = TRUE)so users can reach the planning object without re-typing the call. DefaultFALSEpreserves the run- immediately semantics. Mutually exclusive withreturn_codeandreview_code.- syntax
One of
"auto"(default),"asreml", or"brms". Selects howfixedis interpreted."auto"detects the grammar from the call shape (a bar-grouped formula is read as brms, otherwise ASReml). The other values force a grammar.- known_variance
Optional numeric vector of length N, or an N x N covariance matrix (below). A vector is a known variance for each response, added to the estimated residual variance,
Var(y_i) = sigma^2 + v_i. This is the second stage of a two-stage analysis, where each response is a first-stage estimate (a variety mean in one trial, say) andv_iis the variance of that estimate.weightsis a different model,Var(y_i) = sigma^2 / w_i. Given both, the residual variance issigma^2 / w_i + v_i. Lowered forfamily = "gaussian"with the identity link: brms fitsy | se(sqrt(v), sigma = TRUE), and INLA a row effect with known variancev_i. The summaries reportsigmaas the estimated part, and fitted values on both engines leave the known part out. A vector of zeros fits the model without it. Any other family refuses withknown_variance_not_supported, and a vector that is not one finite, non-negative value per row withknown_variance_invalid. A symmetric positive-definite N x N matrixKis a known covariance,Var(y) = sigma^2 I + K, as when the first stage returns the full covariance of its estimates. It is fitted on INLA as a latent effect with precisionK^-1. brms has no term for it and refuses withknown_variance_not_supported. A row and column of zeros marks a row without a known part. A matrix that is not square, symmetric and positive definite over the rows ofdatarefuses withknown_variance_invalid.- fixed_alias
"refuse"(the default) or"drop": what to do with a fixed-effect column that is a linear combination of the columns before it on the rows with a response, as in a factorial with empty cells. INLA and brms would report such a column at its prior, so by default the fit refuses withfixed_effects_aliasedand names the columns."drop"drops them as asreml 4.2 does, on INLA: terms are taken in model order, and within a term by descending factor levels, the term's first factor varying slowest. A column that is a linear combination of the columns before it is fixed at zero.summary()reports it with estimate 0 and no standard error, the columns are listed infit$extras$fixed_alias$dropped, and a prediction whose row is not a combination of the fitting rows isNAwith status"Aliased"(seepredict.flexybayes_inla()).- rcov
Defunct in flexyBayes 0.9.0. This was the ASReml 3 name for the residual-structure argument. ASReml 4 renamed it to
residual, and flexyBayes follows the ASReml 4 name. Supplyingrcovnow raises an error. Useresidualinstead.
Value
An object of class "flexybayes", a list with three
components.
$glmA GLM-compatible object (class
c("flexybayes_glm", "glm", "lm")) with posterior mean coefficients, vcov, residuals, fitted values, etc. Works withsummary(),emmeans()andmarginaleffects().$brmsor$inlaThe native backend object: a live
brmsfiton the brms path, or INLA's own fitted object on the INLA path. Use withbayesplot,posterior::as_draws(), and each engine's own accessors.$extrasAdditional outputs: posterior
summary,convergencediagnostics,variance_comps,blups,predictions, generatedcode,param_names,parse_info,call_info,run_time,model_info, andreproducibility: the R, flexyBayes and engine versions, the thread settings and the seed the fit ran with, whichprint()shows.
If return_code = TRUE, returns the generated backend code instead: a
character string holding the Stan program on the brms path, or a list of
the formula, family and hyperparameter specification on the INLA path.
INLA threads
An INLA fit runs on one thread, num.threads = "1:1", whatever
INLA::inla.setOption() holds. With more threads, identical calls can
return different variance components where the data inform a variance only
weakly. On a 48-trial barley model with 22 localities, eight identical fits
at "4:1" gave the locality variance from 0.58 to 42.3, and at "1:1"
from 0.75 to 0.79, with the fixed effects unchanged. Set
options(flexyBayes.inla_threads = "4:1") for another plan, or "inla"
for INLA's own default. The plan a fit ran with is in
$extras$reproducibility.
One thread does not guarantee the same result in a new R session. Identical calls in fresh R processes can reach different modes. On a 48-trial barley series, four processes reached two modes, 0.10 apart in log marginal likelihood, whose variance component standard deviations differed by at most 3.4%. On six trials of the same series, the modes differed by up to 26%.
Each INLA fit is therefore run three times: once in this session and twice
in fresh R processes started through callr. The fit
with the highest log marginal likelihood is returned (the first of equal
ones). options(flexyBayes.inla_best_of = k) sets the number of fits, a
whole number of one or more; 1 fits once in this session, as 0.16.0 did,
and any other value refuses with inla_best_of_invalid. Above 1 without
callr installed, the fit refuses with isolation_requires_callr. A fit
that fails in a separate process stops the call with that process's
message. $extras$reproducibility$best_of holds each fit's log marginal
likelihood and mode status, the fit kept, the range of the marginal
likelihoods, and each standard deviation per fit with its largest
difference across the fits, relative to the smallest. print() and
summary() add a line when a standard deviation differs by more than 5%.
The restart probe of the numerical-confirm gate examines the fit kept.
The fresh-process fits run beside the session fit, at most
options(flexyBayes.inla_best_of_workers) fits at once (default: all of
them) and fewer, with a message, when the free memory holds fewer. The
record gives each fit's time in seconds (wall_s) and whether they ran at
once (parallel).
Fitting in a separate process
options(flexyBayes.isolation = "process") runs the engine fit in a
separate R process started through callr, which is in Suggests. Without
callr the request refuses with
isolation_requires_callr before anything is fitted. The option applies
to flexybayes(), fb_brms(), fb_inla() and proceed(), and
fb_isolated_lapply() runs any loop of calls the same way. The default,
"none", fits in this session.
If the operating system kills that process, or compiled code crashes inside
it, the call refuses with isolated_process_died, naming the engine, the
function called, and the exit status or signal, and this session carries on.
A failure inside the fit, a refusal for instance, arrives exactly as it would
without the option. The call is not retried.
The separate process loads the flexyBayes code this session loaded and
starts from this session's options, INLA settings, environment variables
and random-number state, so it fits the same model with the same seed,
and the random-number stream moves on as it would have. Messages,
warnings and printed output come back in the order they were raised, and
the fit records the process in $extras$isolation. A brms fit with a
seed returns the same draws either way. INLA does not return identical
numbers for two identical calls in one session, and an isolated INLA fit
agrees with an in-process one as closely as two in-process fits agree.
The separate process starts with an empty global environment, so the
formula finds its variables in data. Starting the process and
moving the data and the fit costs time on every call.
Deprecated
fb() is deprecated in flexyBayes 0.13.0 and will be removed in a later
release. Call flexybayes(), which takes the same arguments. A call to
fb() warns with a condition of class flexybayes_deprecated.
References
Cao Z, Megirian R, Nguyen M, Sparks AH (2026). Bayesian ordinal analysis of cereal growth stage data from oat trials in Western Australia. arXiv:2610.37384.
Examples
if (FALSE) { # \dontrun{
# live fit, needs an active backend (INLA or brms/Stan)
data(met_example, package = "flexyBayes")
# Simple random intercept model (small budget for example purposes)
fit <- flexybayes(
fixed = yield ~ env,
random = ~ geno,
data = met_example$dat,
n_samples = 100, warmup = 100, chains = 1, verbose = FALSE
)
summary(fit)
coef(fit)
} # }