cleverly.datasets.make_longitudinal#
- cleverly.datasets.make_longitudinal(n: int = 2000, *, seed: int | Generator | None = None, censoring: bool = True, cluster_size: int | None = None, backend: Literal['pandas', 'polars'] | str | None = None) tuple[Any, dict[str, float]][source]#
Two time points, a binary outcome and (optionally) monotone censoring.
Returns
(frame, truth). The frame is wide – a row per unit, a column per node – with columnsW1,W2,A1,C1,L2,A2,C2,Yin time order, and the nodes after a unit’s censoring time set tonan, which is the formLongitudinalDatarequires.truthholdsey_regimen[...]for the four static regimens, for the dynamic regimenRULE_LABEL, and the contrasts ofalwaysand of that rule againstnever– under the labels a fit reports them by, so a test can look up the truth with the name it read off the result. A rule is in there because a coverage study keys intotruthby reported name and so cannot supply its own.cluster_sizeadds anidcolumn and, with it, a per-cluster component ofL2– so the influence curves are correlated within a cluster and ignoringid=understates the standard error. There is anidcolumn over independent rows otherwise, which makes a cluster-robust variance equal the plain one and tests nothing.truthholds on a clustered draw, and for a reason rather than by assertion: the sharing preservesL2’s standard normal noise marginal exactly, which is the lawlongitudinal_truth()integrates over. See_shared_within_clusters(), which also says why the hidden-variable construction this replaced did not.- Parameters:
- n
int Number of units.
- seed
int,Generator,orNone Seed or NumPy random generator.
- censoringbool
Whether to generate monotone censoring. False leaves every unit observed.
- cluster_size
intorNone Rows per cluster.
Noneleaves the rows independent.- backend{“pandas”, “polars”, “pyarrow”}
orNone, default=None Dataframe backend.
Noneuses pandas when installed, then the first available backend.
- n
- Returns:
See also
cleverly.LongitudinalTreatmentThe design declaration for the nodes above.
cleverly.longitudinal.LongitudinalResultWhat a fit on this frame returns.
cleverly.datasets.make_longitudinal_survivalThe same shape with an event outcome.
Examples
>>> from cleverly.datasets import make_longitudinal >>> frame, truth = make_longitudinal(n=200, seed=0) >>> list(frame.columns) ['W1', 'W2', 'A1', 'C1', 'L2', 'A2', 'C2', 'Y'] >>> round(truth["ate_regimen[always vs never]"], 3) 0.362