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), whereri(cluster)is a random intercept initialized viari()andxis either a covariate directly, or a smooth term.The
scaleargument of this method is the random intercept’s scale, it is passed tori(). The same goes forpenaltyandfactor_scale.- Parameters:
x (
str|StrctTerm|LinTerm) – Name of input variable, or a smooth represented by aStrctTerm.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 thisTermBuilderinstance. 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 theinferenceattribute 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 usingScaleIG. 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 theVarIGPriorobject. A fitting Gibbs kernel will be set up automatically to sample \(\tau^2\) in this case, seeScaleIGfor 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 theTermBuilder’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. SeeStrctTerm.factor_scale()for details.
See also
riRandom intercept.
BasisBuilderInitializes the basis and penalty.
- Return type:
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)")