cleverly.TrialWithExternalData#

class cleverly.TrialWithExternalData(outcome: str, treatment: str, adjustment: Sequence[str], source: str, trial: Any = 1, missingness: str | None = None, outcome_family: Literal['gaussian', 'binomial', 'auto'] = 'auto')[source]#

Declare a randomized trial pooled with external data on the same outcome.

The pooled frame holds the trial’s rows and the external rows, such as the controls of a registry or an earlier study, with one column marking trial membership. The design claims two things about the trial and nothing about the external rows: the treatment was randomized in the trial given the adjustment columns, and every trial row could get either arm. The adaptive TMLE (method="atmle") learns how the external outcomes differ from the trial’s and removes that bias. After the fit it reports how well the trial covers the external rows’ covariates.

Parameters:
outcomestr

Outcome column.

treatmentstr

Binary treatment column. A treatment with more than two levels, a continuous dose and a vector treatment are refused, because the estimator is derived for a binary treatment only.

adjustmentsequence of str

Baseline covariate columns. At least one is required: the bias of the external rows is learned as a function of them.

sourcestr

The column that marks trial membership.

trialAny, default=1

The value of source on trial rows. Every other value marks an external row.

missingnessstr or None, default=None

Outcome-observation indicator. One means observed. The design then assumes the outcome is missing at random given the covariates and the arm in the trial, and assumes nothing about why external outcomes are missing.

outcome_family{“auto”, “gaussian”, “binomial”}, default=”auto”

Outcome family. "auto" infers it from observed values.

Raises:
DataError

When two roles name one column, or adjustment is empty or repeats a column.

CapabilityError

When treatment names more than one column.

See also

TrialATE

The effect this design identifies.

AdaptiveTMLEMethod

The method that estimates it.

PointTreatment

The design for the trial rows alone.

Examples

>>> from cleverly import TrialWithExternalData
>>> design = TrialWithExternalData(
...     outcome="Y", treatment="A", adjustment=("W1", "W2"), source="S"
... )
>>> design.source, design.trial
('S', 1)

Methods#

TrialWithExternalData.prepare(data: Any) → CausalData[source]#

Validate the pooled data and encode the declared column roles.

Parameters:
datadataframe or CausalData

Pandas, Polars, or prepared causal data.

Returns:
CausalData

Validated internal representation, with trial membership in source.

Raises:
CapabilityError

When the treatment takes more than two values.

DataError

When the data hold no trial row, no external row, or a trial with one arm, or when a supplied CausalData was built with other roles.

Attributes#

TrialWithExternalData.missingness: str | None = None#
TrialWithExternalData.outcome_family: Literal['gaussian', 'binomial', 'auto'] = 'auto'#
TrialWithExternalData.treatment_name#

The treatment’s column.

TrialWithExternalData.trial: Any = 1#
TrialWithExternalData.outcome: str#
TrialWithExternalData.treatment: str#
TrialWithExternalData.adjustment: Sequence[str]#
TrialWithExternalData.source: str#