jtextor / dagitty

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

Unclear error message when variable name not in graph #15

Closed malcolmbarrett closed 4 years ago

malcolmbarrett commented 6 years ago

Hi Johannes,

I've found myself occasionally entering an incorrect variable name into the analysis functions. When one does that, dagitty produces a bit of a mysterious error message, e.g.

library(dagitty)
g <- dagitty("dag{x <- z -> y}")

#  accidentally capitalize "x"
ancestors(g, "X")
#> Error in context_eval(join(src), private$context): TypeError: Cannot read property 'traversal_info' of undefined

I think it would be nice if it were clearer, e.g. in ancestors, do something like

check_dag_names <- function(x, v) {
  if (!(v %in% names(x))) 
    stop(paste(v, "is not a variable in `x`"))
}

check_dag_names(g, "X")
#> Error in check_dag_names(g, "X"): X is not a variable in `x`

ancestors <- function(x, v) {
  check_dag_names(x, v)
  .kins(x, v, "ancestors")
}

What do you think? I could send a PR for the functions that accept variable names, if you'd like.

jtextor commented 4 years ago

Hi Malcolm, I finally got to implementing your suggestion, hope this was what you had in mind!

malcolmbarrett commented 4 years ago

Great, thanks, Johannes!