Machine learning applications have been growing in volume and scope rapidly over the last few years. What’s Causal inference, how is it different than plain good ole’ ML and when should you consider using it? In this report I try giving a short and concrete answer by using an example.
You can also see the original blog post here.
A typical data science task
Imagine we’re tasked by the marketing team to find the effect of raising marketing spend on sales. We have at our disposal records of marketing spend (mkt), visits to our website (visits), sales, and competition index (comp).
We’ll simulate a dataset using a set of equations (also called structural equations):
All data presented in graphs or used to fit models below is simulated from the above equations.
Below are the first few rows of the dataset:
Our goal is to predict the effect of raising marketing spend on sales which is 0.15 from the set of equations above, using product decomposition:
Common analysis approaches
First approach: plot bi-variate relationship
Many of us would start off by plotting a scatter plot of sales by marketing:
We can see that the relationship seen in the graph is actually the opposite of what we’d expected! It looks like increasing marketing actually decreases sales. Indeed, not only correlation isn’t causation, at times it can show a relation opposite to the true causation.
Fitting a simple linear model:
would yield the following coefficients: (note r is a regression coefficient where’s beta is a true parameter in the structural equations)
Confirming that we get a vastly different effect than the one we were looking for (0.15).
Second approach: Use ML model with all available features
One might postulate that looking on a bi-variate relation amounts to using only 1 predictor variable, but if we were to use all of the available features we might be able to find a more accurate estimate.
When running the regression
We get the following coefficients:
Now it looks like marketing spend has almost no effect at all! Since we simulated the data from a set of linear equations we know that using more sophisticated models (e.g. XGBoost, GAMs) can’t produce better results (I encourage the skeptic reader to try this out by re-running the Rmd script used to produce this report).
Maybe we should consider the relation between features too…
Quite baffled by the results obtained so far we turn to consult the marketing team and we learn that in highly competitive markets the team would usually increase marketing spend (this is reflected in the coefficient above). So it’s possible that competition is a “confounding” factor: when we observe high marketing spend there’s also high competition thus leading to lower sales.
Also, we notice that marketing probably affects visits to our site and those visits in turn affect sales.
We can visualize these feature inter-dependencies with a directed a-cyclic graph (DAG):
So it would make sense to account for the confounding competition by adding it to our regression. Adding visits to our model however somehow “blocks” or “absorbs” the effect of marketing on sales so we should omit it from our model.
Fitting the model
yields the coefficients below:
Now we finally got the right effect estimate!
The way we got there was a bit shaky though. We came up with general concepts such as “confounding” and “blocking” of features. Trying to apply these to datasets consisting of tens of variables with complicated relationships would probably prove tremendously hard.
So now what? Causal inference!
So far we’ve seen that trying to estimate the effect of marketing spend on sales by examining bi-variate plots can fail bad. We’ve also seen that standard ML practices of throwing all available features into our model can fail too. It would seem we need to carefully construct the set of covariates included in our model in order to obtain the true effect.
In causal inference this covariate set is also termed “adjustment set”. Given a model DAG we can utilise various algorithms that rely on rules very much like those mentioned above such as “confounding” and “blocking”, to find the correct adjustment set.
One of the most basic algorithms that can obtain the correct adjustment set is the “Backdoor-criteria” developed by J. Pearl. In a nutshell it seeks adjustment sets that block every “spurious” paths between our “exposure” variable (e.g. marketing) and “outcome” variable (e.g. sales) while keeping directed paths open.
Consider for example the DAG below where we’re interested in finding the effect of x5 on x10:
Using the backdoor-criterion (implemented in the R package “dagitty”) we can find the correct adjustment set:
How to obtain model DAGs?
Finding the model DAG can be admittedly challenging. It can be done using any combination of the following:
- Use domain knowledge
- Given a few candidate model DAGs one can perform statistical tests to compare their fit to the data at hand
- Use search algorithms (e.g. those implemented in the R “mgm” or “bnlearn” packages)
I’ll touch upon this subject in more breadth in a future post.
To anyone curious to learn a bit more about the questions I’ve tried to answer in this report I’ll recommend reading the light-weight Technical Report by Pearl: The Seven Tools of Causal Inference with Reflections on Machine Learning
For a more in-depth introduction to Causal inference and the DAG machinery I’d recommend getting Pearl’s short book: Causal Inference in Statistics — A Primer
For the full script reproducing this post see Rmd script
You can also check out my blog post and join the discussion