causalis.data_contracts.iv_causal_data

Causalis dataclass for storing cross-sectional DataFrame and column metadata for instrumental variables causal inference.

Module Contents

Classes

IVCausalData

Container for instrumental variables causal inference datasets.

API

class causalis.data_contracts.iv_causal_data.IVCausalData(/, **data: Any)

Bases: causalis.data_contracts.causaldata.CausalData

Container for instrumental variables causal inference datasets.

Extends :class:CausalData with exactly one instrument column. The stored DataFrame is restricted to outcome, treatment, instrument, confounder, and optional user_id columns.

Attributes

df : pd.DataFrame DataFrame restricted to the columns used by the IV analysis. treatment_name : str Column name representing the endogenous treatment variable. outcome_name : str Column name representing the outcome variable. instruments_names : List[str] Name of the instrument column, stored as a single-item list. confounders_names : List[str] Names of the confounder columns (may be empty). user_id_name : str, optional Column name representing the unique identifier for each observation/user.

Initialization

Create a new model by parsing and validating input data from keyword arguments.

Raises [ValidationError][pydantic_core.ValidationError] if the input data cannot be validated to form a valid model.

self is explicitly positional-only to allow self as a field name.

instruments_names: List[str]

‘Field(…)’

classmethod from_df(df: pandas.DataFrame, treatment: str, outcome: str, instruments: Union[str, List[str]], confounders: Optional[Union[str, List[str]]] = None, user_id: Optional[str] = None, **kwargs: Any) causalis.data_contracts.iv_causal_data.IVCausalData

Friendly constructor for IVCausalData.

Parameters

df : pd.DataFrame The DataFrame containing the data. treatment : str Column name representing the endogenous treatment variable. outcome : str Column name representing the outcome variable. instruments : Union[str, List[str]] Column name(s) representing the instrumental variable(s). confounders : Union[str, List[str]], optional Column name(s) representing the observed confounders/covariates. user_id : str, optional Column name representing the unique identifier for each observation/user. **kwargs : Any Additional arguments passed to the Pydantic model constructor.

Returns

IVCausalData A validated IVCausalData instance.

property instruments: List[str]

List of instrument column names.

Returns

List[str] Names of the instrument columns.

property Z: pandas.DataFrame

Design matrix of instruments.

Returns

pd.DataFrame The DataFrame containing only instrument columns.

get_df(columns: Optional[List[str]] = None, include_treatment: bool = True, include_outcome: bool = True, include_confounders: bool = True, include_user_id: bool = False, include_instruments: bool = True) pandas.DataFrame

Get a DataFrame with specified columns.

Parameters

columns : List[str], optional Specific column names to include. include_treatment : bool, default True Whether to include the treatment column. include_outcome : bool, default True Whether to include the outcome column. include_confounders : bool, default True Whether to include confounder columns. include_user_id : bool, default False Whether to include the user_id column. include_instruments : bool, default True Whether to include instrument columns.

Returns

pd.DataFrame A copy of the internal DataFrame with selected columns.

Raises

ValueError If any specified columns do not exist.

__repr__() str