TermBuilder.ri()

Contents

TermBuilder.ri()#

TermBuilder.ri(cluster, scale='default', inference='default', penalty=None, factor_scale=False, prefix='', name=None)[source]#

Random intercept.

Parameters:
  • cluster (str) – Name of the cluster variable.

  • 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 \(\tau\), with an iverse Gamma prior on its square, i.e. \(\tau^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 \(\tau^2\) in this case, see ScaleIG for details.

  • inference (Any | None | Literal['default'], default: 'default') – Inference specification for this term’s coefficient. Note that this inference is only used for the coefficient variables of the terms created by this builder (StrctTerm.coef), not for the scale variables (StrctTerm.scale). The default ("default") uses the TermBuilder’s default inference specification defined during initialization. Please refer to the TermBuilder documentation for more information.

  • penalty (Array | ndarray | bool | number | bool | int | float | complex | None, default: None) – Custom penalty matrix to use. Default is an iid penalty.

  • factor_scale (bool, default: False) – Whether to factor out the scale in the prior for this term, turning it into a partially (or fully) standardized form. See StrctTerm.factor_scale() for details.

  • 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

rs

Random slope.

BasisBuilder

Initializes the basis and penalty.

Return type:

RITerm

Notes

If the penalty is iid, then each column of the basis consists only of binary (0/1) entries, and each row has only one non-zero entry. In this case it is not necessary to store the full matrix in memory and evaluate the term as a dot product basis @ coef.

Instead, we can simply store a 1d array of indices, identifying the nonzero column for each row of the basis matrix, and use this index to access the corresponding coefficient. This scenario is common for independent random intercepts.

This method returns such a sparse representation of the random intercept basis if penalty=None.

Examples

>>> import liesel_gam as gam
>>> df = gam.demo_data(100)
>>> tb = gam.TermBuilder.from_df(df)
>>> tb.ri("x_cat")
RITerm(name="ri(x_cat)")