ehrapy.tools.causal_inference

ehrapy.tools.causal_inference(adata, graph, treatment, outcome, estimation_method, refute_methods=None, print_causal_estimate=False, print_summary=True, return_as='estimate', show_graph=False, show_refute_plots=None, attempts=10, *, identify_kwargs=None, estimate_kwargs=None, refute_kwargs=None)[source]

Performs causal inference on an AnnData object using the specified causal model and returns a tuple containing the causal estimate and the results of any refutation tests.

Parameters:
  • adata – An AnnData object containing the input data.

  • graph – A str representing the causal graph to use.

  • treatment – A str representing the treatment variable in the causal graph.

  • outcome – A str representing the outcome variable in the causal graph.

  • estimation_method – An optional Literal specifying the estimation method to use.

  • refute_methods (default: None) – An optional List of Literal specifying the methods to use for refutation tests.

  • print_causal_estimate (default: False) – Whether to print the causal estimate or not.

  • print_summary (default: True) – Whether to print the causal model summary or not.

  • return_as (default: 'estimate') – An optional Literal specifying the type of output to return.

  • show_graph (default: False) – Whether to display the graph or not.

  • show_refute_plots (default: None) – Whether to display the refutation plots or not.

  • attempts (default: 10) – Number of attempts to try to generate a valid causal estimate.

  • identify_kwargs (default: None) – Optional keyword arguments for dowhy.CausalModel.identify_effect().

  • estimate_kwargs (default: None) – Optional keyword arguments for dowhy.CausalModel.estimate_effect().

  • refute_kwargs (default: None) – Optional keyword arguments for dowhy.CausalModel.refute_estimate().

Returns:

A tuple containing the causal estimate and a dictionary of the results of any refutation tests.

Raises:
  • TypeError – If adata, graph, treatment, outcome, refute_methods, estimation_method, or return_as is not of the expected type.

  • ValueError – If refute_methods or estimation_method contains an unknown value, or if return_as is an unknown value.

Examples

>>> data = dowhy.datasets.linear_dataset(
...     beta=10,
...     num_common_causes=5,
...     num_instruments=2,
...     num_samples=1000,
...     treatment_is_binary=True,
... )
>>> ci = ep.tl.causal_inference(
...     adata=anndata.AnnData(data["df"]),
...     graph=data["gml_graph"],
...     treatment="v0",
...     outcome="y",
...     estimation_method="backdoor.propensity_score_stratification",
... )
>>> estimate = ep.tl.causal_inference(
...     adata=ci.linear_data,
...     graph=ci.linear_graph,
...     treatment="treatment",
...     outcome="outcome",
...     estimation_method="backdoor.linear_regression",
...     return_as="estimate",
...     show_graph=True,
...     show_refute_plots=True,
... )
... ep.tl.plot_causal_effect(estimate)