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

Reverse edge direction in as_tidy_dagitty() output #177

Open
gomesleduardo opened this issue Feb 2, 2025 · 3 comments
Open

Reverse edge direction in as_tidy_dagitty() output #177

gomesleduardo opened this issue Feb 2, 2025 · 3 comments
Labels
bug Something isn't working
Milestone

Comments

@gomesleduardo
Copy link

Hi, guys!

Description

I encountered an issue with the as_tidy_dagitty() function while following the example in the documentation. The resulting
DAG object appears to store the relationships correctly, but the data tibble has the edges defined in the opposite direction.

Reproducible Example

library(ggdag)
dag <- data.frame(name = c("c", "c", "x"), to = c("x", "y", "y")) %>% 
  as_tidy_dagitty()

print(dag$data)
print(dag$dag)

Observed Behavior

Output of dag$data:

# A tibble: 4 × 8
  name       x       y direction to      xend    yend circular
  <chr>  <dbl>   <dbl> <fct>     <chr>  <dbl>   <dbl> <lgl>   
1 c     -0.572  0.911  ->        x      0.420  0.765  FALSE   
2 c     -0.572  0.911  ->        y     -0.203 -0.0206 FALSE   
3 x      0.420  0.765  ->        y     -0.203 -0.0206 FALSE   
4 y     -0.203 -0.0206 NA        NA    NA     NA      FALSE   

Output of dag$dag:

dag {
c [pos="-0.572,0.911"]
x [pos="0.420,0.765"]
y [pos="-0.203,-0.021"]
x -> c
y -> c
y -> x
}

# ARCS ARE REVERSED!

Environment

# R Version: 4.3.1
# ggdag Version: 0.2.13
# dagitty Version: 0.3-4

Would appreciate any guidance or fixes for this issue.

Thanks for your work on this package!

@malcolmbarrett
Copy link
Collaborator

This may already be fixed in the development version if memory serves correctly. Could you try installing the version on GitHub (see README if you need instructions) and reporting back?

@malcolmbarrett
Copy link
Collaborator

Confirmed this is still the case in dev

@malcolmbarrett malcolmbarrett added this to the ggdag 0.3.0 milestone Feb 4, 2025
@malcolmbarrett
Copy link
Collaborator

This is specifically a bug in as_tidy_dagitty.data.frame(). Other methods work correctly:

library(ggdag, warn.conflicts = FALSE)

dag <- dagify(x ~ c, y ~ x + c) |>
  tidy_dagitty()

dag$data
#> # A tibble: 4 × 8
#>   name       x      y direction to      xend   yend circular
#>   <chr>  <dbl>  <dbl> <fct>     <chr>  <dbl>  <dbl> <lgl>   
#> 1 c     -0.248  0.594 ->        x     -0.717 -0.283 FALSE   
#> 2 c     -0.248  0.594 ->        y      0.277 -0.251 FALSE   
#> 3 x     -0.717 -0.283 ->        y      0.277 -0.251 FALSE   
#> 4 y      0.277 -0.251 <NA>      <NA>  NA     NA     FALSE
dag$dag
#> dag {
#> c [pos="-0.248,0.594"]
#> x [pos="-0.717,-0.283"]
#> y [pos="0.277,-0.251"]
#> c -> x
#> c -> y
#> x -> y
#> }

Created on 2025-02-03 with reprex v2.1.1

@malcolmbarrett malcolmbarrett added the bug Something isn't working label Feb 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants