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_boundsreplaced a cumulative probability. The weight is the one the fit’s fluctuation used, the data’s weight times the clever covariate, so it equals1 / cumulativeon a static plan with unit weights. Nothing is refit.- Parameters:
- result
LongitudinalResult A fitted inverse-weighting or targeted longitudinal result.
- regimen
strorNone, default=None The regimen’s label. May be omitted when the result holds one.
- horizon
intorNone, default=None The horizon of a curve fit; the largest when omitted.
- cause
strorNone, default=None The cause, under competing events; the first when omitted.
- repeat
int, default=1 The cross-fitting repeat, counted from one.
- result
- Returns:
NodeWeightsThe weights and cumulative probabilities, per unit and node.
- Raises:
CapabilityErrorWhen the result is a g-computation or parametric g-formula fit, which divides by no treatment mechanism.
DataErrorWhen the regimen, cause, horizon or repeat is not in the result.
See also
cleverly.plotting.weightsThe 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