cleverly.plotting.zipper#

cleverly.plotting.zipper(study: Any, *, estimand: str | Sequence[str] | None = None, labels: dict[str, str] | None = None, max_intervals: int | None = None, 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 every replication’s interval, sorted by centre, with the misses coloured.

Use it to see how a study’s coverage was lost. Each line is one replication’s interval, drawn relative to the true value (the dashed line) and sorted by its centre. Intervals that miss are orange. Misses piled at one end mean the estimator is biased; misses split between the ends mean the intervals are too narrow. The subtitle gives the coverage and the level it was aimed at.

A difference is drawn as the limit minus the true value, a ratio as the limit divided by it on a log axis. Both are drawn from the limits each record holds, and the figure refuses a study whose limits disagree with the coverage it recorded.

Parameters:
studyStudyResult

A finished coverage study.

estimandstr, sequence of str or None, default=None

The estimands to draw, one panel each; every estimand of the study when omitted.

labelsdict or None, default=None

Words for each estimand, keyed by its alias.

max_intervalsint or None, default=None

The most intervals each panel draws, at least 2; every interval when omitted. With a large study the lines merge and the exported file grows with every replication; a number draws that many intervals at evenly spaced places in the sorted order, both ends included, each at its own height. The coverage in the subtitle and the counts in the notes still use every replication, and the notes give how many intervals were drawn and how many of them miss.

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

One panel per estimand, up to three across.

Raises:
DataError

When the study’s records hold no finite interval limits, or the limits disagree with the recorded coverage.

KeyError

When estimand names an estimand the study did not measure.

See also

cleverly.validation.CoverageStudy

The study that produces the records.

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 count the intervals that miss below and above the true value, say which estimates are experimental, and, with max_intervals, how many intervals were drawn and how many of those miss.

Examples

>>> import matplotlib
>>> matplotlib.use("Agg")
>>> import matplotlib.pyplot as plt
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly.datasets import nonlinear_dgp
>>> from cleverly.estimators import TMLE
>>> from cleverly.plotting import zipper
>>> from cleverly.validation import CoverageStudy
>>> study = CoverageStudy(
...     dgp=nonlinear_dgp(),
...     estimator=lambda: TMLE(
...         outcome_learner=LinearRegression(),
...         treatment_learner=LogisticRegression(max_iter=1000),
...         cross_fit=False,
...     ),
...     n=200,
...     n_replicates=12,
...     estimands=("ate",),
...     seed=0,
... ).run()
>>> fig = zipper(study)
>>> fig.axes[0].get_title(loc="left")
'Interval coverage'
>>> plt.close(fig)