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

support dagitty objects #183

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Suggests:
ape,
covr,
data.tree,
dagitty,
graph,
influenceR,
methods,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ S3method(as.data.frame,tbl_graph)
S3method(as.igraph,tbl_graph)
S3method(as.list,tbl_graph)
S3method(as_tbl_graph,Node)
S3method(as_tbl_graph,dagitty)
S3method(as_tbl_graph,data.frame)
S3method(as_tbl_graph,default)
S3method(as_tbl_graph,dendrogram)
Expand Down
44 changes: 44 additions & 0 deletions R/dagitty.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#' @describeIn tbl_graph Method to deal with dagitty objects from the dagitty package
#' @export
#' @importFrom rlang is_empty check_installed
as_tbl_graph.dagitty <- function(x, directed = TRUE, node_attr = NULL, edge_attr = NULL, ...) {
check_installed('dagitty', 'in order to coerce a dagitty object to tbl_graph')
if (!is_empty(intersect(node_attr, c('adjusted', 'latent', 'exposure', 'outcome')))) stop('node_attr cannot be adjusted, exposure, outcome, or latent, as these are automatically added if present')
if (is_empty(names(x))) return(create_empty(0))

nodes <- tibble::tibble(
name = names(x)
)
adjusted <- dagitty::adjustedNodes(x)
if (!is_empty(adjusted)) nodes$adjusted <- nodes$name %in% adjusted
exposure <- dagitty::exposures(x)
if (!is_empty(exposure)) nodes$exposure <- nodes$name %in% exposure
outcome <- dagitty::outcomes(x)
if (!is_empty(outcome)) nodes$outcome <- nodes$name %in% outcome
latent <- dagitty::latents(x)
if (!is_empty(latent)) nodes$latent <- nodes$name %in% latent
coords <- coordinates(x)
if (!all(is.na(coords$x))) {
nodes$x <- coords$x
nodes$y <- coords$y
}

for (a in node_attr){
if (vctrs::vec_as_names(a, repair = 'unique') != a) stop('each node_attr must be a string of length > 0')
nodes[a] <- dagitty:::.vertexAttributes(x, a)$a
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If there is no way to extract these without using a non-exported function we should simply ignore any additional attributes all-together. Same with edge attributes

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except that the "beta" edge attributes are important. Perhaps @jtextor, maintainer of the dagitty package, can suggest a solution, e.g., modifying the edges function to also extract the beta values.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, that would be necessary on daggity's side. Using internal functions from other packages is not allowed, nor wise


edges <- dagitty::edges(x)
if (is_empty(edges)){
edges <- tibble::tibble(from = int(), to = int())
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
edges <- tibble::tibble(from = int(), to = int())
edges <- tibble::tibble(from = integer(), to = integer())

} else {
edges <- edges[c('v', 'w', 'e')]
names(edges) <- c('from', 'to', 'type')
for (a in edge_attr){
if (vctrs::vec_as_names(a, repair = 'unique') != a) stop('each edge_attr must be a string of length > 0')
edges[a] <- dagitty:::.edgeAttributes(x, a)$a
}
}

tbl_graph(nodes = nodes, edges = edges, directed = directed)
}
13 changes: 9 additions & 4 deletions man/tbl_graph.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.