TermBuilder.lin()#
- TermBuilder.lin(formula, prior=None, inference='default', context=None, prefix='', name=None)[source]#
Linear term.
- Parameters:
formula (
str) – Right-hand side of a model formula, as understood by formulaic. Most of formulaic’s grammar is supported. See notes for details.prior (
Dist|None, default:None) – An optional prior for this term’s coefficient. The default is a constant prior.inference (
Any|None|Literal['default'], default:'default') – An optionalliesel.goose.MCMCSpecinstance (or other valid inference object). The default ("default") uses theTermBuilder’s default inference specification defined during initialization. Please refer to the TermBuilder documentation for more information.context (
dict[str,Any] |None, default:None) – Dictionary of additional Python objects that should be made available to formulaic when constructing the design matrix. Gets passed toformulaic.ModelSpec.get_model_matrix().prefix (
str, default:'') – A string prefix to be added to the returned term’s name.name (
str|None, default:None) – Manually defined name of the term. If a prefix is specified, the prefix will be added to this name.
See also
slinLinear term with identity penalty matrix, leading to a ridge prior.
- Return type:
Notes
This term evaluates to \(\mathbf{X}\boldsymbol{\beta}\), where \(\mathbf{X}\) is a linear-effect design matrix. The coefficient vector receives a constant prior by default, \(\boldsymbol{\beta} \sim \text{const}\), but a custom prior can be passed in the argument
prioras aliesel.model.Dist.The following formulaic syntax is supported:
+for adding a terma:bfor simple interactionsa*bfor expanding toa + b + a:b(a + b)**nfor n-th order interactionsa / bfor nestingC(a, ...)for categorical effects (see formulaic_categorical for details)b %in% afor inverted nesting{a+1}for quoted Python code to be executed`weird name`backtick-strings for weird namesOther transformations like
center(a),scale(a), orlag(a), see grammar.Python functions
Not supported:
String literals
Numeric literals
Wildcard
"."\|for splitting a formula"~"in formula, since this method supports only the right-hand side of a Wilkinson formula.1 +,0 +, or-1in formula, since intercept addition is handled via the argumentinclude_intercept.
References
Python library formulaic: https://matthewwardrop.github.io/formulaic/latest/
Examples
Simple example:
>>> import liesel_gam as gam >>> df = gam.demo_data(n=100) >>> registry = gam.PandasRegistry(df) >>> bb = gam.BasisBuilder(registry) >>> bb.lin("x_lin + x_nonlin + x_cat") LinBasis(name="X")
Customized categorical encoding:
>>> import liesel_gam as gam >>> df = gam.demo_data(n=100) >>> registry = gam.PandasRegistry(df) >>> bb = gam.BasisBuilder(registry) >>> bb.lin("x_lin + x_nonlin + C(x_cat, contr.sum)") LinBasis(name="X")
Interaction:
>>> import liesel_gam as gam >>> df = gam.demo_data(n=100) >>> registry = gam.PandasRegistry(df) >>> bb = gam.BasisBuilder(registry) >>> bb.lin("x_lin * x_cat") LinBasis(name="X")