brm
Classes¶
Functions¶
brm(formula, data, priors=None, family='gaussian', sample_prior='no', sample=True, backend='cmdstanr', formula_args=None, cores=2, **brm_args)
¶
Fit Bayesian regression model using brms.
Uses brms with cmdstanr backend for proper parameter naming. Returns FitResult with .idata (arviz.InferenceData) and .r (brmsfit) attributes.
BRMS documentation and parameters
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str
|
brms formula: formula string, e.g "y ~ x + (1|group)" or FormulaResult from formula() |
required |
data
|
dict or DataFrame
|
Model data |
required |
priors
|
list
|
Prior specifications: [("normal(0,1)", "b"), ("cauchy(0,2)", "sd")] |
[]
|
family
|
str
|
Distribution family: "gaussian", "poisson", "binomial", etc. |
"gaussian"
|
sample_prior
|
str
|
Sample from prior: "no", "yes", "only" |
"no"
|
sample
|
bool
|
Whether to sample. If False, returns compiled model with empty=TRUE |
True
|
backend
|
str
|
Stan backend: "cmdstanr" (recommended), "rstan" |
"cmdstanr"
|
**brm_args
|
Additional brms::brm() arguments: chains=4, iter=2000, warmup=1000, cores=4, seed=123, thin=1, etc. |
{}
|
Returns:
| Type | Description |
|---|---|
FitResult
|
Object with .idata (arviz.InferenceData) and .r (brmsfit) attributes |
See Also
brms::brm : R documentation https://paulbuerkner.com/brms/reference/brm.html posterior_epred : Expected value predictions posterior_predict : Posterior predictive samples formula : Create formula object with options
Warnings
cores <= 1 is unsafe in embedded R sessions. The single-process
code path used by brms::brm() can crash the interpreter.
Always use cores >= 2 to force parallel workers and avoid segfaults.
Examples:
Basic Poisson regression:
from brmspy import brms
import arviz as az
epilepsy = brms.get_brms_data("epilepsy")
model = brms.fit(
formula="count ~ zAge + zBase * Trt + (1|patient)",
data=epilepsy,
family="poisson",
chains=4,
iter=2000
)
az.summary(model.idata)
from brmspy import prior
model = brms.fit(
formula="count ~ zAge + zBase * Trt + (1|patient)",
data=epilepsy,
priors=[
prior("normal(0, 0.5)", class_="b"),
prior("exponential(2)", class_="sd", group="patient")
],
family="poisson",
chains=4
)