Skip to content

formula

Classes

Functions

bf(formula, **formula_args)

Set up a model formula for brms package.

Allows defining (potentially non-linear) additive multilevel models for all parameters of the assumed response distribution.

Parameters:

Name Type Description Default
formula str

brms formula specification, e.g., "y ~ x + (1|group)"

required
**formula_args dict

Additional brms::brmsformula() arguments:

  • decomp : str Decomposition method (e.g., "QR" for QR decomposition)
  • center : bool Whether to center predictors (default True)
  • sparse : bool Use sparse matrix representation
  • nl : bool Whether formula is non-linear
  • loop : bool Use loop-based Stan code
{}

Returns:

Type Description
FormulaResult

Object with .r (R brmsformula object) and .dict (Python dict) attributes

See Also

brms::brmsformula : R documentation https://paulbuerkner.com/brms/reference/brmsformula.html fit : Fit model using formula

Examples:

Basic formula:

    from brmspy import brms
    f = brms.bf("y ~ x1 + x2 + (1|group)")

With QR decomposition for numerical stability:

f = brms.bf(
    "reaction ~ days + (days|subject)",
    decomp="QR"
)

Multivariate formula:

    f = brms.bf(
        "mvbind(y1, y2) ~ x1 + x2",
        center=True
    )

lf(*formulas, flist=None, dpar=None, resp=None, center=None, cmc=None, sparse=None, decomp=None)

Specify linear formulas for distributional / non-linear parameters.

Parameters:

Name Type Description Default
*formulas str or FormulaResult

One or more formulas (e.g. "sigma ~ x", "nu ~ z").

()
flist list

Additional formulas passed as a list.

None
dpar str

Name of the distributional parameter (e.g. "sigma").

None
resp str

Response name in multivariate models.

None
center optional

Passed through to brms::lf().

None
cmc optional

Passed through to brms::lf().

None
sparse optional

Passed through to brms::lf().

None
decomp optional

Passed through to brms::lf().

None

Returns:

Type Description
FormulaResult

Object that can be added to a bf() formula via +.

Examples:

>>> f = bf("y ~ 1") + lf("sigma ~ x", dpar="sigma")

nlf(formula, *extra, flist=None, dpar=None, resp=None, loop=None)

Specify non-linear formulas for distributional parameters.

Parameters:

Name Type Description Default
formula str

Non-linear formula, e.g. "sigma ~ a * exp(b * x)".

required
*extra str or FormulaResult

Additional named parameters or formulas (rarely needed here; typically you use lf() for those).

()
flist list

Additional formulas passed as a list.

None
dpar str

Name of the distributional parameter.

None
resp str

Response name in multivariate models.

None
loop bool

Whether to compute inside a loop (brms::nlf(loop=...)).

None

Returns:

Type Description
FormulaResult

Object that can be added to a bf() formula via +.

Examples:

>>> f = bf("y ~ 1") + nlf("sigma ~ a * exp(b * x)")

acformula(autocor, resp=None)

Specify autocorrelation terms to add to a model.

Parameters:

Name Type Description Default
autocor str

One-sided formula with autocorrelation terms, e.g. "~ arma(p = 1, q = 1)".

required
resp str

Response name in multivariate models.

None

Returns:

Type Description
FormulaResult

Object that can be added to a bf() formula via +.

Examples:

>>> f = bf("y ~ x") + acformula("~ arma(p = 1, q = 1)")

set_rescor(rescor=True)

Control residual correlations in multivariate models.

Parameters:

Name Type Description Default
rescor bool

Whether to model residual correlations.

True

Returns:

Type Description
FormulaResult

Object that can be added to a multivariate formula via +.

Examples:

>>> f = bf("y1 ~ x") + bf("y2 ~ z") + set_rescor(True)

set_mecor(mecor=True)

Control correlations between latent me-terms.

Parameters:

Name Type Description Default
mecor bool

Whether to model correlations between me() latent variables.

True

Returns:

Type Description
FormulaResult

Object that can be added to a formula via +.

Examples:

>>> f = bf("y ~ me(x, sdx)") + set_mecor(True)

set_nl(dpar=None, resp=None)

Mark a formula as non-linear (or parts of it).

Parameters:

Name Type Description Default
dpar str

Distributional parameter name (if only part of the model is non-linear).

None
resp str

Response name in multivariate models.

None

Returns:

Type Description
FormulaResult

Object that can be added to a formula via +.

Examples:

>>> f = bf("y ~ a * inv_logit(x * b)") + lf("a + b ~ z") + set_nl()