jtextor / dagitty

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

ancestors() and descendants() return input variable #60

Closed behrman closed 2 years ago

behrman commented 2 years ago

The functions ancestors() and descendants() return the input variable.

library(dagitty)

sessioninfo::package_info("dagitty", dependencies = FALSE)
#>  package * version date (UTC) lib source
#>  dagitty * 0.3-2   2022-04-06 [1] Github (jtextor/dagitty@b31c919)
#> 
#>  [1] /Library/Frameworks/R.framework/Versions/4.1/Resources/library

graph <- dagitty('dag { X -> Y -> Z } ')

ancestors(graph, "Y")
#> [1] "Y" "X"

descendants(graph, "Y")
#> [1] "Y" "Z"

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

This does not follow the usual definitions; see, for example, (Pearl, 2016), p. 25.

jtextor commented 2 years ago

Hi Bill, these definitions are quite common in the literature, and defining it this way makes some things easier. (See e.g. https://www.jmlr.org/papers/volume18/16-319/16-319.pdf) I would not want to change the default behavior but I can add an explanation and a parameter that you can use to turn this off?

behrman commented 2 years ago

Hi Johannes,

First, thanks for creating a very useful package. If the concepts of ancestors and descendants have different definitions, I think it would be helpful to add a parameter to these functions to let users choose the desired behavior. I agree that you would not want to change the default behavior, as that likely break others' code, but being able to choose the behavior would be helpful.

I just finished writing some dagitty examples using study questions from (Pearl, 2016) and would have welcomed that option.

jtextor commented 2 years ago

Hi Bill,

Thanks, I added an option "proper" to the ancestors and descendants functions. With proper=TRUE, you will not get the node itself. See last commit

behrman commented 2 years ago

Hi Johannes,

Perfect! That's just what was needed. I've streamlined my code to use proper = TRUE. Thanks for being so responsive.

By the way, my students love using dagitty.