cleverly.plotting.curve#
- cleverly.plotting.curve(obj: Any, *, scale: str | None = None, measure: str = 'curve', band: str | None = None, cause: str | None = None, regimens: Sequence[str] | None = None, labels: Mapping[str, str] | None = None, msm: Any = None, means: Any = None, levels: Mapping[str, Any] | None = None, modifier: str | None = None, by: str | None = None, at: Mapping[str, Any] | None = None, alpha: float | None = None, outcome_label: str | None = None, treatment_label: str | None = None, units: str | None = None, time_label: str | None = None, truth: Any = None, max_rug_points: 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, ax: Axes | None = None) Figure[source]#
Plot a result as a curve: over time, over a treatment level, or over a modifier.
The object decides the plot. A longitudinal fit with a report at each time point draws its survival, risk or mean-outcome curve for each regimen, the difference between regimens in a second panel, and the number of units still following each regimen under observation below. A point-treatment fit of a family of policies (a sequence of shifts, scale factors or incremental odds multipliers) draws the effect against the policy’s parameter. A point-treatment fit of per-level means draws the means against the treatment level, and the working model of a marginal structural model fit beside them. A conditional effect curve draws the effect over one modifier. A smoothed dose-response curve draws one series per declared bandwidth, at most three, with its pointwise intervals and the band over every declared pair. A continuous-time survival fit draws each arm’s risk, or cumulative incidence by cause in one column per cause, and their difference below, at its target times only: a point with its interval bar and band brackets at each target time, and nothing between them, where the fit estimates no risk.
A band is drawn only when the fit computed one. A fit that reports every time point gets step ribbons, because the curve is constant between time points, and the last time point, where a step has no width, gets a bar and bracket marks; a fit that reports only some horizons gets points and bracket marks at those horizons. A result that withholds inference draws points only, and
notes()gives the reason.- Parameters:
- obj
LongitudinalResult,TMLEResult,EffectCurveResultoranotherresult What to draw: one of those, a
DoseResponseResultor aContinuousSurvivalResult.- scale{“risk”, “survival”, “mean”}
orNone, default=None Longitudinal and continuous-time survival fits: the view of the outcome.
Noneis"risk"for a survival outcome and"mean"for a repeated one. The levels and differences are read fromobj.curve(scale=...).- measure{“curve”, “rmst”, “rmtl”}, default=”curve”
Longitudinal fits: the curve over time, or the restricted mean survival time or time lost up to each horizon (points joined by a thin line, pointwise intervals only). A continuous-time survival fit draws the restricted means at its
restricted_horizons, as points with pointwise intervals.- band{“multiplier”, “bootstrap”, “none”},
FalseorNone, default=None Which simultaneous band to draw.
Nonedraws the multiplier band when the fit holds one, else the bootstrap band;notes()names the other."none"(orFalse) draws no band, so a figure need not show a band its page does not discuss. A point-treatment fit of per-level means or of policies, and a conditional effect curve, hold a multiplier band at most.- cause
strorNone, default=None Longitudinal fits with several causes: the cause to draw. A continuous-time survival fit draws one column per cause when omitted.
- regimenssequence
ofstrorNone, default=None Longitudinal fits: the regimens to draw, at most three. A continuous-time survival fit: its arm labels.
- labels
mappingofstrtostrorNone, default=None Display names for regimens.
- msm
TMLEResultorNone, default=None Per-level means (a fit of
RegimeMeanorCounterfactualMean): the point-treatment fit of a working model to draw beside them.- means
TMLEResultorNone, default=None Working model fit: the fit of per-level means to draw beside it.
- levels
mappingorNone, default=None Per-level means: the numeric treatment level of each regime name or arm label, when the names are not the levels themselves. It names every regime or arm the fit reports and no other. The working model’s levels are mapped the same way, so labelled arms such as
"low"and"high"need it once.- modifier
strorNone, default=None Conditional effect curve: the modifier on the x axis; the only continuous modifier the curve reads when omitted.
- by
strorNone, default=None Conditional effect curve: a categorical modifier, drawn as one panel per level. Dose-response curve with baseline strata: the stratum label to draw, such as
"V=1".- at
mappingorNone, default=None Conditional effect curve: the value at which each other modifier the curve reads is held.
- alpha
floatorNone, default=None Conditional effect curve: the pointwise interval holds at
1 - alpha.Noneuses the level of the curve’s band when it has one, so the two are read at one level, and the level of the fit’s coefficients otherwise.- outcome_label
strorNone, default=None Words for the quantity on the y axis of a mean or a difference in means, such as
"order share": the label reads"order share"and"Difference in order share". Without it the label is the mean of the outcome column,"Mean Y"and"Difference in mean Y". A conditional effect curve reads"Effect on"the words.- treatment_label
strorNone, default=None Words for the treatment on the x axis.
- units
strorNone, default=None Units of the quantity on the y axis, added to its label: the outcome of a mean view, a dose, policy or effect plot, or time for a restricted mean. A risk or a survival probability has none, and this is ignored there.
- time_label
strorNone, default=None Words for the time axis of a longitudinal fit, with its units, such as
"Days since enrolment".- truth
mapping, sequence,callable()orNone, default=None The known true values of simulated data, drawn as ink crosses beside the estimates and named “True value (simulated data only)” in the key. Real data have none. A mapping of estimate name to value (
{"ate_regimen[a vs b @ t=1]": 0.1}) works on every plot of named estimates; a name the fit reports but the plot does not draw is passed over. A figure of one series also takes one value per x level, as a sequence in increasing order of the level or a mapping of level to value. A conditional effect curve takes a function of the modifier’s values, drawn as a dash-dot line. Values are on the scale the plot draws.- max_rug_points
intorNone, default=None The most ticks the rug of a conditional effect curve draws in each panel (the short marks along the bottom at the rows’ modifier values). With a number, a panel with more rows draws that many values evenly spaced in rank, from the smallest to the largest, so the ticks follow the modifier’s distribution whatever the row order;
0draws no rug. When omitted, a panel with more than 20,000 rows draws every k-th row in row order. The curve and its interval use every row either way, and the notes say how many ticks were drawn. Other views draw no rug and ignore it.- title
str,FalseorNone, default=None The title; a short description of the figure when omitted, and none when
False.- subtitle
str,FalseorNone, 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.- xlabel
str, sequenceofstrorNone, 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.
- ylabel
str, sequenceofstrorNone, default=None The y-axis label, given as for
xlabel.- legend{“top”, “right”, “bottom”}
orFalse, default=”top” Where the key goes: above the axes, right of them, or under the figure;
Falsedraws no key.- figsize
tupleoffloatorNone, default=None Width and height in inches of a figure the plot creates.
- ax
AxesorNone, default=None Axes to draw on. Only a one-panel plot can use it.
- obj
- Returns:
FigureThe figure drawn on.
- Raises:
TypeErrorWhen
objis not a result this function draws.ValueErrorWhen an argument does not match the object: too many regimens or contrasts for one axes, an unknown modifier, a held value that is missing, or a policy family with no single parameter.
CapabilityErrorWhen the fit refuses the view (for example the survival view of a fit with several causes), with the fit’s own reason, or when
truthdoes not match what the plot draws.
See also
cleverly.plotting.forestThe estimates of one or several fits as a forest plot.
cleverly.longitudinal.estimator.LongitudinalResult.plotThis plot for a longitudinal fit.
cleverly.effect_curves.EffectCurveResult.plotThis plot for a conditional effect curve.
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 estimates or bands are experimental, why an interval is withheld, which band the fit also holds, the values other modifiers are held at, which horizons a fit reports, and what a working model or projection means for the drawn curve.Examples
>>> import matplotlib >>> matplotlib.use("Agg") >>> import matplotlib.pyplot as plt >>> from sklearn.linear_model import LinearRegression, LogisticRegression >>> from cleverly.datasets import make_longitudinal_survival >>> from cleverly.longitudinal import LTMLE >>> from cleverly.plotting import curve >>> frame, _ = make_longitudinal_survival(n=300, seed=0) >>> result = LTMLE( ... {"always": 1, "never": 0}, ... n_folds=1, ... outcome_learner=LinearRegression(), ... treatment_learner=LogisticRegression(max_iter=1000), ... censoring_learner=LogisticRegression(max_iter=1000), ... ).fit( ... frame, ... outcome=["Y1", "Y2"], ... treatment=["A1", "A2"], ... baseline=["W1", "W2"], ... time_varying=[[], ["L2"]], ... censoring=["C1", "C2"], ... ) >>> fig = curve(result, scale="survival") >>> fig.axes[0].get_title(loc="left") 'Survival over time' >>> plt.close(fig)