cleverly.plotting.weights#

cleverly.plotting.weights(result: Any, *, regimens: Sequence[str] | None = None, horizon: int | None = None, cause: str | None = None, repeat: int = 1, labels: dict[str, str] | None = None, time_label: str = 'Time point', counterfactual: bool = False, title: str | Literal[False] | None = None, subtitle: str | Literal[False] | None = None, xlabel: str | Sequence[str] | None = None, ylabel: str | Sequence[str] | None = None, legend: str | Literal[False] = 'top', figsize: tuple[float, float] | None = None) → Figure[source]#

Plot how the weights are spread at each time point, and how many units they leave.

Use it to see at which decision point the weights grow too large. The top panel shows, for each time point, the weight each unit still followed carried: the box spans the middle half, the whiskers the 1st to 99th percentile, the cross is the largest. The dotted line marks the largest weight the fit’s limit on the cumulative probability allows, with the share of units the limit moved printed above. The bottom panel shows the effective sample size (solid), the number of equally weighted units that would carry the same information, against the number still followed (dashed). A gap that widens at a time point is where the regimen loses its support.

A fit with several horizons draws the largest, a fit with several cross-fitting repeats the first, and notes() says which. A time point whose weight the limit does not cap has no dotted line: one with an incremental or modified policy at it or at an earlier time point, whose factor the weight carries, or any time point when the data’s weights are not all one. A log axis cannot show a weight of zero, which a modified treatment policy at that or an earlier time point gives a unit when the shifted treatment cannot produce the one it received; notes() counts the units still followed with a weight of zero at each time point, and the boxes describe the other weights.

A continuous-time survival fit draws one panel: at each target time \(t\), each unit’s weight \(1 / (\hat\pi(a \mid L)\hat S_c(t^- \mid a, L))\) for each arm, the inverse of the probability of the arm times the probability of staying uncensored just before \(t\), before the fit’s floor. No weight the fit gives the unit up to that time is larger. Each box holds the units given that arm, or with counterfactual=True the units given the other arm. The dotted line is the largest weight the floor allows. The fit keeps no follow-up times, so no effective sample size is drawn.

Parameters:
resultLongitudinalResult or ContinuousSurvivalResult

A fitted inverse-weighting or targeted longitudinal result, or a continuous-time survival fit.

regimenssequence of str or None, default=None

The labels of the regimens to draw, at most three, in the order given; every regimen of the result, up to three, when omitted. On a continuous-time survival fit, the arm labels.

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.

labelsdict or None, default=None

Words for each regimen, keyed by its label.

time_labelstr, default=”Time point”

Words for a node on the x axis.

counterfactualbool, default=False

On a continuous-time survival fit, draw each arm’s weight over the units given the other arm: the weight each would carry under the arm it did not receive.

titlestr, False or None, default=None

The title; a short description of the figure when omitted, and none when False.

subtitlestr, False or None, default=None

The line under the title; the plot’s own, which names the estimate and its headline number, when omitted, and none when False.

xlabelstr, sequence of str or None, default=None

The x-axis label. A string replaces the first labelled panel’s label and the same label wherever it repeats; a sequence gives each labelled panel its own, in reading order.

ylabelstr, sequence of str or None, default=None

The y-axis label, given as for xlabel.

legend{“top”, “right”, “bottom”} or False, default=”top”

Where the key goes: above the axes, right of them, or under the figure; False draws no key.

figsizetuple of float or None, default=None

Width and height in inches of a figure the plot creates.

Returns:
Figure

Two panels with a shared x axis; one for a continuous-time survival fit.

Raises:
CapabilityError

When the result is not a longitudinal or continuous-time survival fit, or is a g-computation or parametric g-formula fit, which has no weights.

DataError

When the result holds more than three regimens and regimens is omitted, when regimens is a single string, empty, repeats a label or names more than three, or when a regimen, the cause, the horizon or the repeat is not in the result.

ValueError

When counterfactual=True is given for a longitudinal fit, or horizon, cause or repeat for a continuous-time survival fit.

See also

cleverly.longitudinal.node_weights

The same weights as numbers.

Notes

The figure carries no note text. It does not mark an experimental fit or band, one whose interval has no checked coverage because no registered study covers it or a stated condition applies, such as too few clusters: read notes() on the figure before you publish it. For this plot the notes say which horizon and repeat are drawn, where no limit line applies and why, and how many units carry a weight of zero. For a continuous-time survival fit they say which units each box holds and how the share beyond the floor differs from the fit’s own bounded share.

Examples

>>> import matplotlib
>>> matplotlib.use("Agg")
>>> import matplotlib.pyplot as plt
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly.datasets import make_longitudinal
>>> from cleverly.longitudinal import LTMLE
>>> from cleverly.plotting import weights
>>> frame, _ = make_longitudinal(n=200, seed=0)
>>> result = LTMLE(
...     {"always": 1, "never": 0},
...     n_folds=1,
...     random_state=0,
...     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"],
... )
>>> fig = weights(result)
>>> fig.axes[0].get_title(loc="left")
'Weights over time'
>>> plt.close(fig)