Formula
Model formula
Usage
Formula(
formula,
*additionals,
nlpars=None,
)Allows to describe a model with multiple formulas. The first formula describes the response variable and its predictors. The following formulas describe predictors for other parameters of the response distribution, allowing distributional models.
Parameters
formula: str-
A model description written using the formula syntax from the
formulaelibrary. *additionals: tuple of str-
Additional formulas that describe model parameters rather than a response variable.
nlpars: list or tuple of str = None-
Names of coefficients used in nonlinear expressions. Terms that reference modeled parameters use arithmetic syntax; other terms retain ordinary Formulae syntax. Use
nl(expression)to add an arithmetic expression directly to the predictor, without an estimated multiplier. For example,y ~ nl(exp(b * x) + x) + treatmentestimatesband the usual intercept and treatment effects, but usesxdirectly inside the wrapper. Use0 + nl(expression)to omit the additive intercept. Parentheses outsidenlretain ordinary formula grouping semantics. A formula consisting entirely of unwrapped arithmetic terms and numeric constants retains its expression-only interpretation, with no implicit intercept. An additional formula can describe how a nonlinear coefficient varies. Coefficients without an additional formula have a direct prior and are sampled under their own names. Coefficients with a formula use priors for its terms, including intercept-only formulas. Additional formulas can also describe ordinary auxiliary likelihood parameters, such assigma ~ z. Each expression is on its parameter’s link scale. The family’s inverse link is applied once to the complete predictor, including auxiliary parameters. An additional formula that references a modeled parameter is evaluated after its dependencies, using their values on the response scale. For example, with the log link for Gaussiansigma, writesigma ~ 0 + nl(0.5 * log(variance))for a known variance column. The inverse link produces its square root. Separately modeled nonlinear coefficients use identity links.
Notes
Nonlinear coefficients are scalar by default and do not inherit dimensions from the likelihood parameters that use them. Additive coefficient formulas produce one value per observation, shared across response categories. Nonlinear coefficient expressions can reference vector-valued modeled parameters and retain those dimensions.
Nonlinear formulas support categorical, count-vector, ordinal, and compatible custom numeric vector families. Weighted, censored, truncated, and constrained scalar responses are supported where the ordinary family supports the wrapper. New-data weighted predictions use unit weights unless a weight column is supplied. See the nonlinear expressions guide for supported dimensions, competing risks, and remaining restrictions.
Examples
Model an exponential decay with three separately modeled parameters:
>>> Formula("y ~ a + b * exp(-k * x)", "a ~ 1 + z", nlpars=("a", "b", "k"))
Formula('y ~ a + b * exp(-k * x)', 'a ~ 1 + z', nlpars=('a', 'b', 'k'))Combine a nonlinear contribution with ordinary additive effects:
>>> Formula("y ~ nl(exp(b * x) + x) + treatment", nlpars=("b",))
Formula('y ~ nl(exp(b * x) + x) + treatment', nlpars=('b',))Attributes
| Name | Description |
|---|---|
| is_nonlinear | Whether the formulas declare nonlinear coefficients or explicit contributions. |
is_nonlinear
Whether the formulas declare nonlinear coefficients or explicit contributions.
is_nonlinear
Methods
| Name | Description |
|---|---|
| check_additional() | Check if an additional formula matches the expected format |
| get_all_formulas() | Get all the model formulas |
check_additional()
Check if an additional formula matches the expected format
Usage
check_additional(additional)Parameters
additional: str- A model formula that describes a model parameter.
Returns
str- The parameter name on the left-hand side.
Raises
ValueError-
If the formula does not contain a response term.
ValueError- If the response term is not a plain name.
get_all_formulas()
Get all the model formulas
Usage
get_all_formulas()Returns
list of str- All the formulas in the instance.