quivers.formulas.formula¶
Parsed-formula IR: Formula, FixedColumn, RandomTerm, plus the
formula_from_data adapter over formulae.design_matrices.
formula
¶
Parsed-formula IR: a typed didactic.api.Model wrapping
the raw formulae.matrices.DesignMatrices so the rest of
the formula frontend operates on typed values.
The Formula IR is the canonical source representation of the formula→QVR lens. Future versions can register it as a panproto protocol so the lens machinery applies; for now the compiler walks this IR directly.
Convention
Each fixed-effect term may produce one or more design-matrix
columns (a single column for x, two for poly(x, 2), K
for an unordered factor with K + 1 levels, etc.). R / brms
assign one coefficient per column; this IR follows the same
convention by exploding each term into a tuple of
FixedColumn records. Multi-column terms thus produce
multiple named scalar latents downstream, with deterministic naming
{term}_1, {term}_2, ... that mirrors R's
poly(x, 2)1 / poly(x, 2)2 display.
Polynomial default: formulae.design_matrices's poly
transform is orthogonal by default (matches R's
stats::poly). Raw monomials are available via
I(x^2) / I(x**2). Transforms log, exp, sqrt,
abs, sin, cos, tan, log10, log2,
log1p, expm1 are wired through the formulae evaluation
namespace so users coming from R get the expected base R behaviour.
FixedColumn
¶
Bases: Model
One column of the fixed-effects design matrix.
| ATTRIBUTE | DESCRIPTION |
|---|---|
term |
Originating term name (e.g.
TYPE:
|
name |
Per-column label, equal to
TYPE:
|
qvr_name |
QVR-legal identifier derived from
TYPE:
|
is_intercept |
TYPE:
|
RandomTerm
¶
Bases: Model
One random-effect group, e.g. (1 | g) or (x | g).
| ATTRIBUTE | DESCRIPTION |
|---|---|
slope |
TYPE:
|
group |
Grouping factor name.
TYPE:
|
Formula
¶
Bases: Model
A parsed regression formula plus the data it was parsed against.
| ATTRIBUTE | DESCRIPTION |
|---|---|
formula |
Original formula string.
TYPE:
|
response_name |
Name of the response column.
TYPE:
|
fixed_columns |
One entry per design-matrix column (matches R/brms's one-coefficient-per-column convention).
TYPE:
|
random_terms |
Random-effect group specifications.
TYPE:
|
response_values |
Response column values, shape
TYPE:
|
group_levels |
Canonical level ordering per grouping factor, used to derive deterministic plate-index tensors.
TYPE:
|
group_indices |
Per-group integer index array, shape
TYPE:
|
FormulaData
¶
Bases: Model
The complement of a Formula under the
quivers.formulas.compile.FormulaToQVRModule lens.
The emitted QVR quivers.dsl.ast_nodes.Module carries
the structural skeleton of the formula (which columns there are,
keyed by their QVR-legal identifier; whether each is an
intercept; the random-effect group / slope pairs; the family;
the response identifier in its QVR-legal form). It does not
carry:
- the per-row data arrays (those flow through the host-data channel at fit time);
- the per-column / per-group / response original names (the
lens uses
_qvr_nameto normalize identifiers, which replaces non-alphanumeric characters with underscores and is therefore lossy); - the per-column
termlabel (presentation, ungrouped from the lens forward output); - the original formula string (presentation: the lens emits a canonical AST that does not record user whitespace or operator-precedence choices).
Those fields travel in the complement. backward(module,
complement) decodes the structural fields from the Module and
fuses them with this carrier to reproduce the original
Formula verbatim.
| ATTRIBUTE | DESCRIPTION |
|---|---|
formula |
Original formula string.
TYPE:
|
response_name |
Original (pre-
TYPE:
|
response_values |
Response column values, shape
TYPE:
|
fixed_column_names |
Per-column
TYPE:
|
fixed_column_data |
Per-row predictor values, keyed by
TYPE:
|
group_original_names |
Per-group
TYPE:
|
group_levels |
Canonical per-group level ordering. Needed to populate
TYPE:
|
group_indices |
Per-row integer codes for each grouping factor.
TYPE:
|
formula_from_data
¶
formula_from_data(formula: str, data: IntoDataFrame, *, extra_namespace: Mapping[str, object] | None = None) -> Formula
Build a typed Formula IR by lifting
formulae.design_matrices over a dataframe.
This is an adapter, not a parser: the brms-style formula syntax
is parsed by the formulae
library; we lift its formulae.matrices.DesignMatrices
result into a typed didactic record, augmented with deterministic
per-group level orderings and integer-code arrays derived from
the dataframe.
The R-style numeric transforms (log, exp, sqrt,
abs, sin, cos, tan, log10, log2,
log1p, expm1, asin, acos, atan, sinh,
cosh, tanh) are pre-loaded into the formulae evaluation
namespace so users coming from R / brms get the expected base
R behaviour without explicit registration. Polynomial terms via
poly(x, k) are orthogonal by default, matching R's
stats::poly.
| PARAMETER | DESCRIPTION |
|---|---|
formula
|
Formula string in brms / lme4 syntax.
TYPE:
|
data
|
Pandas, polars, or any other Narwhals-compatible dataframe.
TYPE:
|
extra_namespace
|
Additional names visible inside the formula's expression evaluation, merged on top of the R-style transforms.
TYPE:
|
Source code in src/quivers/formulas/formula.py
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 | |