5
$\begingroup$

Point 1: I'm not sure if this question could be asked here, as it is may not seem to be about the "science" itself at the first glance! At the second glance though, I think in practice several newbies would face this question and it is a public benefit to have it for reference of people

Point 2: If the community think that this is an inappropriate place for this question, I would delete the whole question when I get an answer. Otherwise, I would edit the question and remove these two points and let the question be here for the reference of others.

So I'm new to the world of causal inference. I am learning some elementary concepts such as DAG, matching on covariates/propensity score, etc. The problem is that I don't know where to start in terms of available packages in python? I have found several packages DoWhy, EconML, Causalib, CausalML, CausalNex, etc... . The point is that I don't know where to start for these simple tasks such as building a DAG, or matching in python? Does anyone know about a "meta-reference" to know how to work thorough among these libraries?

$\endgroup$

4 Answers 4

6
$\begingroup$

Here are a few good websites/books that I am fond of that use DAGs, and have code examples in R, Python, and Stata on github or packaged up.

This is not exactly the cutting-edge stuff, but the foundation you need to get started.

I am an economist at a tech company who uses and teaches these methods.

$\endgroup$
3
$\begingroup$

Not exactly a meta-reference, but I have been working on a causal inference library, zEpid. It is mainly from the epidemiology perspective, since that is what my training is in. (I also disagree that causal inference is "mostly connected to econometrics").

My goal was to develop a cohesive library of the various estimators common in epidemiology, as well as other common tools. So you can build a DAG, estimate causal effects for time-fixed exposures, there is some support for time-varying exposures, and other more general functionalities. I also have tried to provide detailed tutorials that demonstrate the code and motivate the ideas behind the approaches. Most of them are located here.

As Dimitriy said, there are several good books that have code available. Another book to consider is What If. I also demonstrate the examples from that book here (there is also code linked on their site).

I used to keep more up-to-date with the other libraries, but have fallen behind. I think a resource that compares some of the available libraries, but I am unaware of such a thing. One major difference between the libraries is the field they come from. For example, I don't have any support for IV analyses, but econ causal inference libraries have much more support. There is also different value judgements from each of the fields about what quantities are important and what methods should be considered. A good starting place would be the field your background is closest to.

$\endgroup$
1
  • $\begingroup$ (+1) for CI being important outside econometrics. $\endgroup$
    – dimitriy
    Sep 18, 2021 at 18:29
3
$\begingroup$

Take a look at https://github.com/gmgeorg/pypsps for a ready-to-go TensorFlow keras implementation for causal inference using computational graphs. See README and notebooks for code examples and case studies and the article for details, Kelly, Kong, and Goerg (2022).

For example here is an end-to-end example of simulating data, building a model, training & evaluating the model, and -- last but not least -- estimate the ATE & unit level treatment effects.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

from pypsps.keras import models
from pypsps import datasets, inference, utils

np.random.seed(10)
ks_data = datasets.KangSchafer(n_samples=1000, true_ate=20).run()
tf.random.set_seed(10)
model = models.build_toy_model(
    n_states=4, n_features=ks_data.n_features, compile=True, alpha=10.
)
inputs, outputs = ks_data.to_keras_inputs_outputs()
history = model.fit(inputs,
                    outputs,
                    epochs=250,
                    batch_size=64,
                    verbose=2,
                    validation_split=0.2,
                    callbacks=models.recommended_callbacks(),
                    )
preds = model.predict(inputs)
outcome_pred, scale, propensity_score, weights = utils.split_y_pred(preds)

pred_ate = inference.predict_ate(model, ks_data.features)
print("ATE\n\t true: %.1f \n\tnaive: %.1f \n\t PSPS: %.1f" % (
    ks_data.true_ate, ks_data.naive_ate(), pred_ate)
    )
pd.DataFrame(history.history)[["loss", "val_loss"]].plot(logy=True); plt.grid()
ATE
     true: 20.0 
    naive: -1.3 
     PSPS: 17.3

pspsloss

$\endgroup$
3
  • $\begingroup$ Is this based on propensity score matching? $\endgroup$
    – Galen
    Sep 20, 2023 at 20:25
  • $\begingroup$ Conceptually yes, but it's not an adhoc 2 step manual procedure. Instead the propensity scores, the right bucketing of similar scores, and the outcomes are all trained simultaneously in one step to optimize (out of sample ) joint likelihood. $\endgroup$ Sep 22, 2023 at 11:54
  • 1
    $\begingroup$ @Galen added paper reference in post $\endgroup$ Sep 22, 2023 at 12:01
0
$\begingroup$

From what I see causal inference (CI) is mostly connected to econometrics, and vastly to its most basic concepts. Also it is mostly academic domain, since the companies are usually not interested in any inference at all.

The tools to use in applied CI are often limited to the basic regressions, panels, IV. The DAG's, understanding endogeneity and some other CI concepts complement those tools. There are some sophisticated tricks to help with the identification of the parameters, but the tools themselves are mostly very basic.

The statsmodels package with pandas and something to make charts, like plotly may be more than enough to start and finish your PhD in applied CI.

However, it really depends on what you aim for. CI is quite a big field, and there are many sophisticated areas within it. But from my perspective - they are not the core.

$\endgroup$
2
  • $\begingroup$ Thanks @cure .. So my two cents: * I think in academia, causal inference has been historically studied very seriously in Economics, but I'm not sure if it has been limited to econometrics. Let's skip this point though! $\endgroup$
    – Ehsan Sh
    Sep 17, 2021 at 20:49
  • 1
    $\begingroup$ * About application in industry; first; I see that all these packages have been developed by companies like Microsoft, Uber and IBM! So why have they developed them if they have no use for them?! *second; I see that many questions are "causal inference" questions in nature! So in my current project, we are interested to see WHY some bad outcomes has happened in our past plans in order to prevent them, and I'm looking to see whether causal inference tools can help me address those... $\endgroup$
    – Ehsan Sh
    Sep 17, 2021 at 20:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.