Skip to content

Instantly share code, notes, and snippets.

@thlytras
Created April 5, 2023 08:40
Show Gist options
  • Save thlytras/2357c7931ef59d536cad6c6c1f1a573a to your computer and use it in GitHub Desktop.
Save thlytras/2357c7931ef59d536cad6c6c1f1a573a to your computer and use it in GitHub Desktop.
Minimal example of collider bias, using R
set.seed(42)
dat <- data.frame(
A = rbinom(500, 1, 0.5),
B = rbinom(500, 1, 0.5)
)
dat$C = rbinom(500, 1, 0.1 + 0.3*dat$A + 0.4*dat$B)
tb <- with(dat, table(A,B))
tb0 <- with(subset(dat, C==0), table(A,B))
tb1 <- with(subset(dat, C==1), table(A,B))
print(tb)
print(chisq.test(tb))
print(tb0)
print(chisq.test(tb0))
print(tb1)
print(chisq.test(tb1))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment