Skip to content

Instantly share code, notes, and snippets.

@florianhartig
Last active December 2, 2020 20:54
Show Gist options
  • Save florianhartig/8583c771c3e63c2e255fe281463e622f to your computer and use it in GitHub Desktop.
Save florianhartig/8583c771c3e63c2e255fe281463e622f to your computer and use it in GitHub Desktop.
Collider bias example.R
n = 1000 # sample size
# generating data according to a collider structure
x1 = runif(n) # random values for explenatory variable x1
y = 1.4 * x1 + rnorm(n,sd = 0.1) # y is causally influences by x1, effect size 0.4
x2 = 2 * x1 + 2 * y + rnorm(n,sd = 0.1) # x2 is a collider, no influence on y, but influenced by y and x1
# ignoring the collider results in the correct regression estimate of approx 1.4
summary(lm(y ~ x1))
# including the colider in the regression prduces an estimate of -0.5 for x1
# so we see a collider can even flip the sign of another variable, as in Simpson's paradoxon
summary(lm(y ~ x1 + x2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment