People are more prone to think of confounders and mediators when it comes to these situations, in the sense that by adjusting for them you're blocking paths, removing indirect associations and therefore getting the direct correlation between the two variables of interest. The issue is that sometimes the variable we are adjusting for is neither a confounder or a mediator: It is a collider. Run the R code below:
N = 1000
X <- rnorm(N)
Y <- rnorm(N)
Z <- X + Y + rnorm(N)
In the code above, we're describing X and Y as independent variables but that, together, cause Z. The respective causal diagram would be the one below:
As the name implies, a collider is a node whose incoming arrows collide with it. This path is blocked. You do not need to adjust for Z to measure the direct correlation between X and Y. But if you adjust, you open the path and you add spurious correlation to your estimate. Let's check this with some R code.
library(ppcor)
cor.test(X,Y)
You will get a correlation of 0.04571798.
pcor.test(X,Y,Z)
You will get a partial correlation of -0.4641393 of X and Y adjusting for Z. We had barely anything, and now we have a reasonable correlation. One may think: Ok, but can we have barely anything and then a strong positive correlation with collider-adjustment? Sure!
N <- 1000
X <- rnorm(N)
Y <- rnorm(N)
Z <- X - Y + rnorm(N)
cor.test(X,Y)
pcor.test(X,Y,Z)
THe correlation between X and Y is -0.01578202 and the partial correlation of X and Y, given Z, is 0.5008563.
We could check what happens with mediators and confounders. Ready?
Let's make Z a confounder variable in respect to the association between X and Y.
N = 1000
Z <- rnorm(N)
X <- Z + rnorm(N)
Y <- Z + rnorm(N)
X <- Z -> Y
cor.test(X,Y)
0.524119
library(ppcor)
pcor.test(X,Y,Z)
-0.02022428
See? You blocked the confounding path, in a way that there is practically no relationship between X and Y (which is expected, based on the way we created such relationships). Now for a mediator:
N = 1000
X <- rnorm(N)
Z <- X + rnorm(N)
Y <- Z + rnorm(N)
X -> Z -> Y
cor.test(X,Y)
0.577676
pcor.test(X,Y,Z)
-0.01984836
The take away message is that in such simple cases of unshielded triples, if we know the causal model, we should not adjust for the collider Z if we want the direct association between X and Y. We should adjust for the confounder Z if we want the direct association of X and Y and we could adjust for the mediator if we want the direct association, but sometimes we want the TOTAL association and therefore we could adjust for the mediator Z or not, depending on what is our goal.