jtextor / dagitty

Graphical analysis of structural causal models / graphical causal models.
GNU General Public License v2.0
255 stars 47 forks source link

Is there a way to extract all of the covariates rolls for a dag? #61

Closed uriahf closed 2 years ago

uriahf commented 2 years ago

Hi, thanks for this package!

I wonder if there's a way to extract all of the covariates rolls for a dag.

Ideally, I would like to have a data frame that looks like this:

Covariate Roll
x Exposure
m Mediator
w Confounder
y Outcome
malcolmbarrett commented 2 years ago

ggdag, which works with dagitty objects, might do something close to what you're looking for:

library(ggdag)
dag <- dagify(l ~ x + y,
              y ~ x,
              exposure = "x",
              outcome = "y",
              latent = "l")

node_status(dag)
#> # A DAG with 3 nodes and 3 edges
#> #
#> # Exposure: x
#> # Outcome: y
#> # Latent Variable: l
#> #
#> # A tibble: 4 × 9
#>   name      x     y direction to     xend  yend circular status  
#>   <chr> <dbl> <dbl> <fct>     <chr> <dbl> <dbl> <lgl>    <fct>   
#> 1 x      3.34  4.22 ->        l      3.01  3.28 FALSE    exposure
#> 2 x      3.34  4.22 ->        y      2.36  4.04 FALSE    exposure
#> 3 y      2.36  4.04 ->        l      3.01  3.28 FALSE    outcome 
#> 4 l      3.01  3.28 <NA>      <NA>  NA    NA    FALSE    latent

Created on 2022-06-10 by the reprex package (v2.0.1)

uriahf commented 2 years ago

That's really cool! Does node_status supports statuses such as "Collider", "Mediator" and "Confounder"?

I can iterate over the variables with dagitty::isCollider over the variables, but I'm not familiar with something like isConfounder or isMediator. I wonder if there's a good reason for that or maybe I didn't look well enough.

malcolmbarrett commented 2 years ago

ggdag also has node_collider() and node_instrumental() but not the others you mention. "confounder" and "mediator" are a little wishy-washy because they really depend on your research question and analysis.

uriahf commented 2 years ago

Thank you, It was really helpful!