TermBuilder.slin()

TermBuilder.slin()#

TermBuilder.slin(formula, scale='default', inference='default', context=None, factor_scale=False, prefix='', name=None)[source]#

Linear term with an identity penalty matrix, leading to a ridge prior.

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.

  • scale (ScaleIG | Var | float | VarIGPrior | Literal['default'], default: 'default') –

    Scale parameter passed to the coefficient prior, StrctTerm.scale.

    • If "default", the scale will be initialized according to the default scale function defined for this TermBuilder instance. Please refer to the TermBuilder documentation for more information.

    • If you pass a float, this will be taken as the constant value of the scale, and the scale will not be estimated as part of the model without further action.

    • If you pass a liesel.model.Var, this will be used as the scale. Make sure to define the inference attribute of your custom scale variable (or a latent, transformed version of it).

    • If you pass a VarIGPrior, a scale variable will be set up for you using ScaleIG. This means, the scale will be :math:` au`, with an iverse Gamma prior on its square, i.e. :math:` au^2 sim operatorname{InverseGamma}(a, b)`, where a and b are taken from the VarIGPrior object. A fitting Gibbs kernel will be set up automatically to sample :math:` au^2` in this case, see ScaleIG for details.

  • inference (Any | None | Literal['default'], default: 'default') – An optional liesel.goose.MCMCSpec instance (or other valid inference object). The default ("default") uses the TermBuilder’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 to formulaic.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

lin

Linear term with constant prior.

Return type:

StrctLinTerm

Notes

The following formulaic syntax is supported:

  • + for adding a term

  • a:b for simple interactions

  • a*b for expanding to a + b + a:b

  • (a + b)**n for n-th order interactions

  • a / b for nesting

  • C(a, ...) for categorical effects (see formulaic_categorical for details)

  • b %in% a for inverted nesting

  • {a+1} for quoted Python code to be executed

  • `weird name` backtick-strings for weird names

  • Other transformations like center(a), scale(a), or lag(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 -1 in formula, since intercept addition is handled via the argument include_intercept.

References

Examples

Simple example:

>>> import liesel_gam as gam
>>> df = gam.demo_data(n=100)
>>> tb = gam.TermBuilder.from_df(df)
>>> tb.slin("x_lin")
StrctLinTerm(name="slin(X)")