r-causal / causal-inference-in-R

Causal Inference in R: A book!
https://www.r-causal.org/
176 stars 45 forks source link

Check multiple adjustment sets where possible #116

Open malcolmbarrett opened 1 year ago

malcolmbarrett commented 1 year ago

I'm not exactly sure where this should go (likely either DAGs or sensitivity analysis). I'm putting it in sensitivity analysis for now.

If your DAG is right, and you have multiple adjustments, assuming all are well measured and modeled, you should get the same answer. Also dependent on sample size:

library(ggdag, warn.conflicts = FALSE)
set.seed(1234)
dag  <- dagify(
  y ~ x + z2 + w2 + w1,
  x ~ z1 + w1,
  z1 ~ w1 + v,
  z2 ~ w2 + v,
  w1 ~~ w2
)

.df_200 <- simulate_data(dag, N = 200)

.df_100000 <- simulate_data(dag, N = 100000)

pull_estimate <- function(.fmla, .df) {
  lm(.fmla, data = .df) |> 
    broom::tidy() |> 
    dplyr::filter(term == "x") |> 
    dplyr::pull(estimate)
}

fmlas <- list(
  y ~ x + w1 + w2 + z2,
  y ~ x + w1 + v,
  y ~ x + w1 + z1
)

fmlas |> 
  purrr::map_dbl(pull_estimate, .df = .df_200)
#> [1] 0.1276502 0.1670121 0.1137664

fmlas |> 
  purrr::map_dbl(pull_estimate, .df = .df_100000)
#> [1] -0.1292621 -0.1272877 -0.1277341

Created on 2022-09-28 with reprex v2.0.2