2  DAGs and adjustment sets

3 Intro

R can determine the adjustment set of confounders for you if you specify the DAG. Description of function adjustmentSets {dagitty}: 

Enumerates sets of covariates that (asymptotically) allow unbiased estimation of causal effects from observational data, assuming that the input causal graph is correct

4 Load libraries

library(tidyverse)
library(ggdag)

5 set up dag

dag<-dagify(
  y~ x + a + b,
  x~ a,
  b~ a,
  exposure="x",
  outcome="y"
)

tidy_dagitty(dag)|>
  dag_adjustment_sets()
# A DAG with 4 nodes and 5 edges
#
# Exposure: x
# Outcome: y
#
# A tibble: 6 × 10
  name        x       y direction to       xend    yend circular adjusted  set  
  <chr>   <dbl>   <dbl> <fct>     <chr>   <dbl>   <dbl> <lgl>    <chr>     <chr>
1 a     -0.335   1.01   ->        b     -0.0937 -0.0531 FALSE    adjusted  {a}  
2 a     -0.335   1.01   ->        x      0.329   1.88   FALSE    adjusted  {a}  
3 a     -0.335   1.01   ->        y      0.568   0.811  FALSE    adjusted  {a}  
4 b     -0.0937 -0.0531 ->        y      0.568   0.811  FALSE    unadjust… {a}  
5 x      0.329   1.88   ->        y      0.568   0.811  FALSE    unadjust… {a}  
6 y      0.568   0.811  <NA>      <NA>  NA      NA      FALSE    unadjust… {a}  
ggdag(dag)

ggdag_adjustment_set(dag)

# What if x causes b? 
dag2<-dagify(
  y~ x + a + b,
  x~ a,
  b~ a+x,
  exposure="x",
  outcome="y"
)
ggdag_adjustment_set(dag2)

6 Session info

sessionInfo()
R version 4.5.0 (2025-04-11 ucrt)
Platform: x86_64-w64-mingw32/x64
Running under: Windows 11 x64 (build 26100)

Matrix products: default
  LAPACK version 3.12.1

locale:
[1] LC_COLLATE=English_United States.utf8 
[2] LC_CTYPE=English_United States.utf8   
[3] LC_MONETARY=English_United States.utf8
[4] LC_NUMERIC=C                          
[5] LC_TIME=English_United States.utf8    

time zone: America/New_York
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] ggdag_0.2.13    lubridate_1.9.4 forcats_1.0.0   stringr_1.5.1  
 [5] dplyr_1.1.4     purrr_1.0.4     readr_2.1.5     tidyr_1.3.1    
 [9] tibble_3.2.1    ggplot2_3.5.2   tidyverse_2.0.0

loaded via a namespace (and not attached):
 [1] viridis_0.6.5      utf8_1.2.4         generics_0.1.3     stringi_1.8.7     
 [5] hms_1.1.3          digest_0.6.37      magrittr_2.0.3     evaluate_1.0.3    
 [9] grid_4.5.0         timechange_0.3.0   fastmap_1.2.0      jsonlite_2.0.0    
[13] ggrepel_0.9.6      gridExtra_2.3      dagitty_0.3-4      viridisLite_0.4.2 
[17] scales_1.3.0       tweenr_2.0.3       cli_3.6.4          graphlayouts_1.2.2
[21] rlang_1.1.6        polyclip_1.10-7    tidygraph_1.3.1    munsell_0.5.1     
[25] cachem_1.1.0       withr_3.0.2        yaml_2.3.10        tools_4.5.0       
[29] tzdb_0.5.0         memoise_2.0.1      colorspace_2.1-1   boot_1.3-31       
[33] curl_6.2.2         vctrs_0.6.5        R6_2.6.1           lifecycle_1.0.4   
[37] V8_6.0.3           htmlwidgets_1.6.4  MASS_7.3-65        ggraph_2.2.1      
[41] pkgconfig_2.0.3    pillar_1.10.2      gtable_0.3.6       glue_1.8.0        
[45] Rcpp_1.0.14        ggforce_0.4.2      xfun_0.52          tidyselect_1.2.1  
[49] knitr_1.50         farver_2.1.2       htmltools_0.5.8.1  igraph_2.1.4      
[53] labeling_0.4.3     rmarkdown_2.29     compiler_4.5.0