cleverly.longitudinal.node_weights#

cleverly.longitudinal.node_weights(result: Any, *, regimen: str | None = None, horizon: int | None = None, cause: str | None = None, repeat: int = 1) → NodeWeights[source]#

Read the weights a fitted regimen used at each node, one row per unit.

Use it to see which units carried the weight at each decision point, and to see where the fit’s g_bounds replaced a cumulative probability. The weight is the one the fit’s fluctuation used, the data’s weight times the clever covariate, so it equals 1 / cumulative on a static plan with unit weights. Nothing is refit.

Parameters:
resultLongitudinalResult

A fitted inverse-weighting or targeted longitudinal result.

regimenstr or None, default=None

The regimen’s label. May be omitted when the result holds one.

horizonint or None, default=None

The horizon of a curve fit; the largest when omitted.

causestr or None, default=None

The cause, under competing events; the first when omitted.

repeatint, default=1

The cross-fitting repeat, counted from one.

Returns:
NodeWeights

The weights and cumulative probabilities, per unit and node.

Raises:
CapabilityError

When the result is a g-computation or parametric g-formula fit, which divides by no treatment mechanism.

DataError

When the regimen, cause, horizon or repeat is not in the result.

See also

cleverly.plotting.weights

The same weights as a figure.

Examples

>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly.datasets import make_longitudinal
>>> from cleverly.longitudinal import LTMLE, node_weights
>>> frame, _ = make_longitudinal(n=200, seed=0)
>>> result = LTMLE(
...     {"always": 1},
...     n_folds=1,
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(max_iter=1000),
...     censoring_learner=LogisticRegression(max_iter=1000),
... ).fit(
...     frame,
...     outcome="Y",
...     treatment=["A1", "A2"],
...     baseline=["W1", "W2"],
...     time_varying=[[], ["L2"]],
...     censoring=["C1", "C2"],
... )
>>> weights = node_weights(result)
>>> weights.times
(1, 2)
>>> bool(np.all(weights.used[weights.followed] > 0))
True