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. Defaults to “backdoor.propensity_score_stratification”.

  • refute_methods – An optional List of Literal specifying the methods to use for refutation tests. Defaults to [“placebo_treatment_refuter”, “random_common_cause”, “data_subset_refuter”].

  • print_causal_estimate – Whether to print the causal estimate or not, default is False.

  • print_summary – Whether to print the causal model summary or not, default is True.

  • return_as – An optional Literal specifying the type of output to return. Defaults to “summary”.

  • show_graph – Whether to display the graph or not, default is False.

  • show_refute_plots – Whether to display the refutation plots or not, default is False.

  • attempts – Number of attempts to try to generate a valid causal estimate, default is 10.

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

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

  • refute_kwargs – 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,
>>> )
>>>
>>> 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=self.linear_data,
>>>     graph=self.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)