Skip to content

Instantly share code, notes, and snippets.

@NickCH-K
Created April 19, 2024 07:15
Show Gist options
  • Save NickCH-K/da6893442508a7f67c6299f879ff7df8 to your computer and use it in GitHub Desktop.
Save NickCH-K/da6893442508a7f67c6299f879ff7df8 to your computer and use it in GitHub Desktop.
Not-a-collider demonstration
library(tidyverse)
library(broom)
library(purrr)
create_data = function(n, trt) {
tibble(donut = rnorm(n),
sleep = rnorm(n)) %>%
mutate(lifting = rnorm(n) + .1*sleep + .3*donut,
coffee = rnorm(n) + .2*sleep,
happy = rnorm(n) + .3*lifting + .2*sleep + trt*coffee)
}
single_iteration = function(n, trt) {
dat = create_data(n, trt)
no_lifting = lm(happy ~ coffee + sleep, data = dat) %>%
tidy() %>%
filter(term == "coffee") %>%
pull(estimate)
with_lifting = lm(happy ~ coffee + sleep + lifting, data = dat) %>%
tidy() %>%
filter(term == "coffee") %>%
pull(estimate)
tibble(no_lifting = no_lifting,
with_lifting = with_lifting)
}
sim_data = map_dfr(1:10000, ~single_iteration(1000, .3))
summary(sim_data)
# no_lifting with_lifting
# Min. :0.1693 Min. :0.1565
# 1st Qu.:0.2776 1st Qu.:0.2787
# Median :0.2997 Median :0.2999
# Mean :0.3000 Mean :0.3003
# 3rd Qu.:0.3220 3rd Qu.:0.3213
# Max. :0.4218 Max. :0.4343
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment