Skip to content

brm

Model fitting wrappers.

This module contains the brms::brm() wrapper used by brmspy.brms.fit() / brmspy.brms.brm().

Notes

This code executes inside the worker process (the process that hosts the embedded R session).

Classes

Functions

brm(formula, data, priors=None, family='gaussian', sample_prior='no', sample=True, backend='cmdstanr', formula_args=None, cores=2, *, return_idata=True, **brm_args)

brm(formula: FormulaConstruct | ProxyListSexpVector | str, data: dict | pd.DataFrame, priors: Sequence[PriorSpec] | None = ..., family: str | ListSexpVector | None = ..., sample_prior: str = ..., sample: bool = ..., backend: str = ..., formula_args: dict | None = ..., cores: int | None = ..., *, return_idata: Literal[True] = True, **brm_args: Any) -> FitResult
brm(formula: FormulaConstruct | ProxyListSexpVector | str, data: dict | pd.DataFrame, priors: Sequence[PriorSpec] | None = ..., family: str | ListSexpVector | None = ..., sample_prior: str = ..., sample: bool = ..., backend: str = ..., formula_args: dict | None = ..., cores: int | None = ..., *, return_idata: Literal[False], **brm_args: Any) -> ProxyListSexpVector

Fit a Bayesian regression model with brms.

This is a thin wrapper around R brms::brm() that returns a structured FitResult (including an ArviZ InferenceData).

Parameters:

Name Type Description Default
formula str or FormulaConstruct

Model formula. Accepts a plain brms formula string (e.g. "y ~ x + (1|g)") or a composed formula created via brmspy.brms.bf() / brmspy.brms.lf() (typically imported as from brmspy.brms import bf, lf).

required
data dict or DataFrame

Model data.

required
priors Sequence[PriorSpec] or None

Optional prior specifications created via brmspy.brms.prior().

None
family str or ListSexpVector or None

brms family specification (e.g. "gaussian", "poisson").

"gaussian"
sample_prior str

Passed to brms. Common values: "no", "yes", "only".

"no"
sample bool

If False, compile the model without sampling (brms empty=TRUE).

True
backend str

Stan backend. Common values: "cmdstanr" or "rstan".

"cmdstanr"
formula_args dict or None

Reserved for future use. Currently ignored.

None
cores int or None

Number of cores for brms/cmdstanr.

2
return_idata bool

When working with large datasets, you might not want the full idata. when False, you get the R object proxy which can be forwarded to posterior_epred or other functions

True
**brm_args

Additional keyword arguments passed to R brms::brm() (e.g. chains, iter, warmup, seed).

{}

Returns:

Type Description
FitResult

Result object with idata (ArviZ InferenceData) and an underlying R handle.

See Also

brms::brm : R documentation

Warnings

Using cores <= 1 can be unstable in embedded R sessions and may crash the worker process. Prefer cores >= 2.

Examples:

from brmspy import brms

fit = brms.brm("y ~ x + (1|g)", data=df, family="gaussian", chains=4, cores=4)

fit.idata.posterior