cleverly.plotting.forest#

cleverly.plotting.forest(*items: Any, labels: Sequence[str] | 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, outcome_label: str | None = None, units: str | None = None, truth: float | Mapping[str, float] | None = None, include_null: bool | None = None, ax: Axes | None = None) → Figure[source]#

Plot estimates with their intervals, one row each (a forest plot).

Use it to compare several estimates, contrasts or methods on one scale and to see at a glance which intervals cross the no-effect line. Each row draws the estimate and its confidence interval; the numbers are printed in a column on the right with the decimals the printed summary uses. Estimates on different scales go in separate panels: differences around 0, ratios on a log axis around 1, and arm means with no null line. The no-effect line is drawn when it lies near the intervals; when it lies far from them, the axis keeps to the intervals and notes() says on which side no effect lies.

An estimate whose inference the package withholds is drawn as a hollow point with no interval, and its value column reads “(no interval)”; notes() gives the reason.

Parameters:
*itemsTMLEResult, TMLEResultSet, LongitudinalResult, ParameterEstimate or mapping

What to draw. A fitted result contributes every estimate it reports; a mapping contributes each value under its key as the label.

labelssequence of str or None, default=None

One label per item. A result’s default label is its method’s short name when several items are drawn, and nothing when one is.

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.

outcome_labelstr or None, default=None

Words for the quantity on the axis of differences and means, such as "orders in the third month": the labels read "Difference in orders in the third month" and "orders in the third month". Without it they read "Difference in mean" and "Mean" the outcome column’s name when every row shares one.

unitsstr or None, default=None

Units of the outcome, added to the axis label of differences and means.

truthfloat, mapping of str to float, or None, default=None

The known true values of simulated data, drawn as an ink cross on each row it belongs to and named “True value (simulated data only)” in the key. Real data have none. A number is the true value of every row, and needs rows that all estimate one quantity (one estimate, or one estimate from each of several fits). A mapping gives the value of each estimate by the name the fit reports ("ate") or by the row’s label; a row it does not name gets no cross. A forest draws every estimate of each fit it is given, so a name that matches no row is refused.

include_nullbool or None, default=None

Whether the axis reaches the no-effect value (0 for a difference, 1 for a ratio) and draws its line. None draws it when it lies no further from the drawn intervals than their own span (a ratio’s measured on the log axis), so the rows keep at least half the axis; otherwise it leaves no effect off the axis and notes() says on which side it lies. A panel with a row that has no finite interval always draws it under None. True draws it wherever the estimates’ scale has a no-effect value (a transform with no finite null draws no line); False leaves it off the axis unless the intervals cross it.

axAxes or None, default=None

Axes to draw on. Only a plot with one scale can draw on a caller’s axes.

Returns:
Figure

The figure drawn on.

Raises:
ValueError

When labels does not match items, when nothing is given, or when ax is passed for estimates on more than one scale.

CapabilityError

When truth names no drawn row, or is one number for rows that estimate different quantities.

See also

cleverly.estimators.TMLEResult.plot

This plot for one fit.

cleverly.plotting.sensitivity

One estimate across a declared sensitivity parameter.

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 give the reason for each withheld interval, which rows are experimental and why, and on which side a no-effect line left off the axis lies.

Examples

>>> import matplotlib
>>> matplotlib.use("Agg")
>>> from sklearn.linear_model import LinearRegression, LogisticRegression
>>> from cleverly import ATE, CausalStudy, PointTreatment
>>> from cleverly.datasets import make_linear_ate
>>> from cleverly.plotting import forest
>>> frame, _ = make_linear_ate(n=200, seed=0)
>>> study = CausalStudy(
...     frame,
...     design=PointTreatment(outcome="Y", treatment="A", adjustment=("W1", "W2", "W3", "W4")),
... )
>>> learners = dict(
...     outcome_learner=LinearRegression(),
...     treatment_learner=LogisticRegression(max_iter=1000),
...     cross_fit=False,
...     random_state=0,
... )
>>> tmle = study.identify(ATE()).estimate(**learners)
>>> fig = forest(tmle["ate"], labels=["TMLE"])
>>> fig.axes[0].get_title(loc="left")
'Estimates and intervals'
>>> [text.get_text() for text in fig.axes[0].get_yticklabels()]
['TMLE']
>>> import matplotlib.pyplot as plt
>>> plt.close(fig)