prior
Classes¶
Functions¶
prior(prior, class_=None, coef=None, group=None, dpar=None, resp=None, nlpar=None, lb=None, ub=None, **kwargs)
¶
Create a brms-style prior specification.
This function mirrors the behavior of brms::prior_string() and allows
specifying priors for regression parameters, group-level effects, nonlinear
parameters, distributional parameters, and more — using a typed Python
interface. All arguments correspond directly to the parameters of
prior_string() in brms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prior
|
str
|
The prior definition as a string, exactly as brms expects it. Examples include :: |
required |
class_
|
str
|
Parameter class (e.g. |
None
|
coef
|
str
|
Coefficient name for class-level effects. |
None
|
group
|
str
|
Grouping variable for hierarchical/multilevel effects. |
None
|
dpar
|
str
|
Distributional parameter (e.g. |
None
|
resp
|
str
|
Response variable name for multivariate models. |
None
|
nlpar
|
str
|
Nonlinear parameter name if using nonlinear formulas. |
None
|
lb
|
float
|
Lower bound for truncated priors. |
None
|
ub
|
float
|
Upper bound for truncated priors. |
None
|
**kwargs
|
Any
|
Any additional keyword arguments supported by |
{}
|
Returns:
| Type | Description |
|---|---|
PriorSpec
|
A typed prior specification object used by |
Notes
This function does not validate the prior expression string itself — validation occurs inside brms. Its purpose is to construct a structured, editor-friendly representation that seamlessly maps to rpy2 calls.
Examples:
Prior on the intercept ::
p = prior("student_t(3, 0, 1.95)", class_="Intercept")
Prior on a coefficient ::
p = prior("normal(0, 1)", class_="b", coef="age")
Group-level (hierarchical) SD prior ::
p = prior("exponential(2)", class_="sd", group="region")
Truncated prior ::
p = prior("normal(0, 1)", class_="b", coef="income", lb=0)
Multiple priors passed to fit ::
from brmspy import prior
priors = [
prior("student_t(3, 0, 2)", class_="b", coef="zAge"),
prior("exponential(2)", class_="sd", group="patient"),
]
model = brms.fit("y ~ zAge + (1|patient)", data=df, priors=priors)
get_prior(formula, data=None, family='gaussian', **kwargs)
¶
Get default priors for all model parameters.
Returns a DataFrame with default priors for each parameter class in the specified brms model. Useful for reviewing and customizing priors before fitting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
formula
|
str or FormulaResult
|
Model formula (e.g., "y ~ x + (1|group)") or FormulaResult object |
required |
data
|
DataFrame or dict
|
Dataset containing model variables. Required for data-dependent priors |
None
|
family
|
str or ListVector
|
Distribution family (e.g., "gaussian", "poisson", "binomial") |
"gaussian"
|
**kwargs
|
Additional arguments passed to brms::get_prior() (e.g., autocor, data2, knots, drop_unused_levels) |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: prior, class, coef, group, resp, dpar, nlpar, lb, ub, source. Each row represents a parameter or parameter class that can have a custom prior. |
See Also
default_prior : Generic function for getting default priors prior : Create custom prior specifications brms::get_prior : R documentation https://paulbuerkner.com/brms/reference/get_prior.html
Examples:
Review default priors for a model:
from brmspy import brms
priors = brms.get_prior(
formula="count ~ zAge + zBase * Trt + (1|patient)",
data=epilepsy,
family="poisson"
)
print(priors)
# prior class coef group ...
# student_t() Intercept ... ... ...
# (flat) b zAge ... ...
Customize and use priors:
default_prior(object, data=None, family='gaussian', **kwargs)
¶
Get default priors for brms model parameters (generic function).
Generic function to retrieve default prior specifications for all parameters in a brms model. Accepts formula objects, brmsformula objects, or other model specification objects. This is the generic version of get_prior().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
object
|
str, FormulaResult, or ListVector
|
Model specification: formula string, brmsformula object, mvbrmsformula, or any object that can be coerced to these classes |
required |
data
|
DataFrame or dict
|
Dataset containing model variables. Required for data-dependent priors |
None
|
family
|
str or ListVector
|
Distribution family (e.g., "gaussian", "poisson", "binomial"). Can be a list of families for multivariate models |
"gaussian"
|
**kwargs
|
Additional arguments passed to brms::get_prior() (e.g., autocor, data2, knots, drop_unused_levels, sparse) |
{}
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with columns: prior, class, coef, group, resp, dpar, nlpar, lb, ub, source. Each row specifies a parameter class with its default prior. The 'prior' column is empty except for internal defaults. |
See Also
get_prior : Convenience function with formula parameter prior : Create custom prior specifications brms::default_prior : R documentation https://paulbuerkner.com/brms/reference/get_prior.html
Examples:
Get default priors for a Poisson model:
from brmspy import brms
priors = brms.default_prior(
object="count ~ zAge + zBase * Trt + (1|patient)",
data=epilepsy,
family="poisson"
)
print(priors)
Use with formula object: