RandomEffect#
- class superglm.RandomEffect(
- *,
- levels=None,
- unseen: Literal['population', 'error'] = 'population',
- missing: Literal['error'] = 'error',
- lambda_policy: LambdaPolicy | None = None,
- nested_in: Hashable | None = None,
Bases:
objectAll-level categorical effect with a REML-estimated variance component.
Every level keeps its own coefficient, shrunk toward the population value by the variance component
tau_squared = phi / lambdathatfit_remlestimates; no level is dropped as a reference.- Parameters:
- levelslist | tuple | Series | ndarray | CategoricalDtype, optional
The level universe to bind to. Without it the universe is the column’s categorical dtype, else its sorted training levels. A declared level with no training rows is not pinned the way an unpenalized dummy is: it keeps its own coefficient and shrinks to the population value, as a thinly observed level does. Training rows outside the universe are an error.
- unseen{“population”, “error”}, default “population”
Predict-time policy for levels outside the universe.
"population"gives those rows the population value (a level effect of zero);"error"raises.- missing{“error”}, default “error”
Missing values (NaN or None) in the column raise, at fit and at predict.
- lambda_policyLambdaPolicy or None
Control of the term’s smoothing parameter. None estimates it by REML;
LambdaPolicy.fixed(value)holds it atvalue.- nested_inhashable or None
Another
RandomEffectfeature of the same model that this one is nested in: every level of this effect belongs to exactly one of its levels (a region within a country). Nesting is also detected from the level codes without it; the declaration is checked on the training rows, and a row that breaks it is an error naming the row and both of the parent’s levels. When this effect is the structured solver’s leaf (the random effect with the most levels, with noFactorSmoothin the model), the declared parent is in its elimination chain unless its smoothing parameter is zero, which moves it to the border; otherwise the declaration is only validated.
Notes
When a REML-estimated
RandomEffectis fitted beside an unpenalisedCategoricalwhose levels include some with exposure but no positive response (under a log link with a zero-mass family such as Tweedie or Poisson), those levels separate and the marginal likelihood becomes nearly flat in this term’s variance. The fitted variance component is then poorly determined, and for the estimated-scale Tweedie criterion it is also biased upward.fit_remlwarns on that configuration; treat the publishedtau_squaredwith care there.- adopt_dtype_categories(categories: list) None#
Adopt a dtype-declared universe unless one is already declared.
- apply_level_binding(binding) None#
Adopt a full-frame universe when nothing more specific declared one.
Only the levels are read: a penalized term has no base level, so its bindings carry
base=Noneand there is nothing to pin.
- resolve_binding(
- values: NDArray,
- sample_weight=None,
Compute this spec’s full-frame binding without mutating the spec.
- build( ) GroupInfo#
Factorize all fitted levels without dropping a reference category.
- validate_prediction_values(x: NDArray) None#
Reject missing values without applying the unseen-level policy.