Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrapper around ggdag and dagitty #761

Merged
merged 29 commits into from
Aug 3, 2024
Merged

Wrapper around ggdag and dagitty #761

merged 29 commits into from
Aug 3, 2024

Conversation

strengejacke
Copy link
Member

@strengejacke strengejacke commented Aug 2, 2024

library(performance)

dag <- check_dag(
  y ~ x + b,
  outcome = "y",
  exposure = "x"
)
dag
#> # Correct adjustments for identifying direct effects
#> 
#> Model is correctly specified.
#> - Outcome: y
#> - Exposure: x
#> 
#> No adjustment needed to estimate the direct effect of x on y.
#> 
#> # Correct adjustments for identifying total effects
#> 
#> Model is correctly specified.
#> - Outcome: y
#> - Exposure: x
#> 
#> No adjustment needed to estimate the total effect of x on y.
plot(dag)

dag <- check_dag(
  y ~ x + b,
  outcome = "y",
  exposure = "x",
  adjusted = "b"
)
dag
#> # Correct adjustments for identifying direct effects
#> 
#> Model is correctly specified.
#> - Outcome: y
#> - Exposure: x
#> 
#> All minimal sufficient adjustments to estimate the direct effect were done.
#> 
#> # Correct adjustments for identifying total effects
#> 
#> Model is correctly specified.
#> - Outcome: y
#> - Exposure: x
#> 
#> All minimal sufficient adjustments to estimate the total effect were done.
plot(dag)

dag <- check_dag(
  y ~ x + b + c,
  x ~ b,
  outcome = "y",
  exposure = "x"
)
dag
#> # Correct adjustments for identifying direct effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the direct effect, *also* adjust for: b.
#> Currently, the model does not adjust for any variables.
#> 
#> # Correct adjustments for identifying total effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the total effect, *also* adjust for: b.
#> Currently, the model does not adjust for any variables.
plot(dag)

dag <- check_dag(
  y ~ x + b + c,
  x ~ b,
  outcome = "y",
  exposure = "x",
  adjusted = "c"
)
dag
#> # Correct adjustments for identifying direct effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the direct effect, *also* adjust for: b and c.
#> Currently, the model currently only adjusts for c.
#> 
#> # Correct adjustments for identifying total effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the total effect, *also* adjust for: b and c.
#> Currently, the model currently only adjusts for c.
plot(dag)

Created on 2024-08-03 with reprex v2.1.1

@strengejacke
Copy link
Member Author

@easystats/core-team What do you think? A "check" function for DAGs in performance? Plots still needs some improvements (e.g. get rid of the "adjusted line" legend).

Benefits over ggdag:

  • you can include "adjusted" variables in the dag-definition, using the adjusted argument
  • plot() is intended to compare current to required model, not to create final plots (though we could add this, but ggdag is sufficient here, or not?)

@mattansb
Copy link
Member

mattansb commented Aug 3, 2024

Can you give an example of where this would fit in the model-building workflow?

@strengejacke
Copy link
Member Author

library(performance)
# error for cyclic model
dag <- check_dag(
  y ~ x + b + c + d,
  x ~ c + d,
  b ~ x,
  b ~ y,
  outcome = "y",
  exposure = "x",
  adjusted = "c"
)
#> Error: Model is cyclic. Causal effects can't be determined for cyclic models.
#>   Please remove cycles from the model. To do so, check following
#>   variables: b and y

# collider-bias
dag <- check_dag(
  y ~ x + c + d,
  x ~ c + d,
  b ~ x,
  b ~ y,
  outcome = "y",
  exposure = "x",
  adjusted = "c"
)
dag
#> # Correct adjustments for identifying direct effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the direct effect, also adjust for: c and d.
#> Currently, the model currently only adjusts for c.
#> 
#> # Correct adjustments for identifying total effects
#> 
#> Incorrectly adjusted!
#> - Outcome: y
#> - Exposure: x
#> 
#> To estimate the total effect, also adjust for: c and d.
#> Currently, the model currently only adjusts for c.
plot(dag)

Created on 2024-08-03 with reprex v2.1.1

@strengejacke
Copy link
Member Author

Can you give an example of where this would fit in the model-building workflow?

What do you exactly mean? You can use this at the point where you involve DAGs. Like you would do with the online tool dagitty.net, you can easily specify your "dependencies" (causal paths) via familiar formula-syntax and then check if you adjusted correctly or not.

The "check" here is that you get the corrected DAG, telling you what you need to adjust for.

@strengejacke
Copy link
Member Author

not sure why the geoms look so ugly in the reprex, though

@strengejacke strengejacke merged commit b2be78b into main Aug 3, 2024
21 checks passed
@strengejacke strengejacke deleted the check_dag branch August 3, 2024 19:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants