r-causal / ggdag

:arrow_lower_left: :arrow_lower_right: An R package for working with causal directed acyclic graphs (DAGs)
https://r-causal.github.io/ggdag/
Other
433 stars 28 forks source link

DAGs with multiple exposures #127

Open lorenzoFabbri opened 8 months ago

lorenzoFabbri commented 8 months ago

dagitty, both the web interface and the R package, now supports the selection of multiple nodes as exposure. ggdag seems to work out-of-the box for e.g., finding the adjustment sets.

Using dagitty:

mult_exposures <- dagitty::dagitty(
  'dag {
    bb="0,0,1,1"
    E1 [exposure,pos="0.263,0.295"]
    E2 [exposure,pos="0.269,0.481"]
    W [pos="0.413,0.186"]
    Y [outcome,pos="0.496,0.411"]
    E1 -> Y
    E2 -> Y
    W -> E1
    W -> E2
    W -> Y
  }'
)
# Prints: `{ W }`
dagitty::adjustmentSets(mult_exposures)

Using ggdag:

tidy <- ggdag::tidy_dagitty(mult_exposures)
# Prints:
# # A DAG with 4 nodes and 5 edges
# #
# # Exposure: E1, E2
# # 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 E1    0.263 0.295 ->        Y      0.496  0.411 FALSE    unadjusted {W}  
# 2 E2    0.269 0.481 ->        Y      0.496  0.411 FALSE    unadjusted {W}  
# 3 W     0.413 0.186 ->        E1     0.263  0.295 FALSE    adjusted   {W}  
# 4 W     0.413 0.186 ->        E2     0.269  0.481 FALSE    adjusted   {W}  
# 5 W     0.413 0.186 ->        Y      0.496  0.411 FALSE    adjusted   {W}  
# 6 Y     0.496 0.411 NA        NA    NA     NA     FALSE    unadjusted {W}
ggdag::dag_adjustment_sets(tidy)

Using ggdag with tidy interface:

tidy_raw <- ggdag::dagify(
  Y ~ E1 + E2 + W,
  E1 ~ W,
  E2 ~ W,
  exposure = c("E1", "E2"),
  outcome = "Y"
)
ggdag::dag_adjustment_sets(tidy_raw)
# Prints:
# # A DAG with 4 nodes and 5 edges
# #
# # Exposure: E1, E2
# # 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 E1    -0.239  0.606  ->        Y      0.616 -0.0778 FALSE    unadjusted {W}  
# 2 E2     1.69   0.129  ->        Y      0.616 -0.0778 FALSE    unadjusted {W}  
# 3 W      0.837  0.813  ->        E1    -0.239  0.606  FALSE    adjusted   {W}  
# 4 W      0.837  0.813  ->        E2     1.69   0.129  FALSE    adjusted   {W}  
# 5 W      0.837  0.813  ->        Y      0.616 -0.0778 FALSE    adjusted   {W}  
# 6 Y      0.616 -0.0778 NA        NA    NA     NA      FALSE    unadjusted {W}

So apparently everything works just fine. Perhaps it would be nice to upload the documentation and/or add a vignette.

System information:

> Sys.info()
                                                           sysname 
                                                           "Linux" 
                                                           release 
                                                "6.2.0-39-generic" 
                                                           version 
"#40~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Nov 16 10:53:04 UTC 2"

> R.version
               _                           
platform       x86_64-pc-linux-gnu         
arch           x86_64                      
os             linux-gnu                   
system         x86_64, linux-gnu           
status                                     
major          4                           
minor          3.2                         
year           2023                        
month          10                          
day            31                          
svn rev        85441                       
language       R                           
version.string R version 4.3.2 (2023-10-31)
nickname       Eye Holes

> packageVersion("ggdag")
[1] ‘0.2.10’

Original discussion here.