TermBuilder.rs()

Contents

TermBuilder.rs()#

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

Random slope.

Create a Liesel variable that evaluates to x * ri(cluster), where ri(cluster) is a random intercept initialized via ri() and x is either a covariate directly, or a smooth term.

The scale argument of this method is the random intercept’s scale, it is passed to ri(). The same goes for penalty and factor_scale.

Parameters:
  • x (str | StrctTerm | LinTerm) – Name of input variable, or a smooth represented by a StrctTerm.

  • 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.

See also

ri

Random intercept.

BasisBuilder

Initializes the basis and penalty.

Return type:

Var

Examples

Random slope:

>>> import liesel_gam as gam
>>> df = gam.demo_data(100)
>>> tb = gam.TermBuilder.from_df(df)
>>> tb.rs(x="x_lin", cluster="x_cat")
Var(name="rs(x_lin|x_cat)")

Random scaling of a smooth term:

>>> import liesel_gam as gam
>>> df = gam.demo_data(100)
>>> tb = gam.TermBuilder.from_df(df)
>>> psx = tb.ps("x_nonlin", k=20)
>>> tb.rs(x=psx, cluster="x_cat")
Var(name="rs(ps(x_nonlin)|x_cat)")