Additional R resources
DAGitty does not display noncausal paths, which can often be helpful for trying to approximately block noncausal paths in the presence of unmeasured variables.
The code below shows a way to do this. For now, you have to define the DAG using a list of edges. (The example below shows the classic confounder triangle.)
If you’re feeling adventurous, learn about the dagitty
R package to see if you can write a function that takes the “Model code” from the DAGitty web interface (right side of the window) to directly create the igraph
object.
library(igraph)
# Create the edge list
rbind(
el <-c("C", "A"),
c("A", "Y"),
c("C", "Y")
)# Create the graph
graph_from_edgelist(el, directed = TRUE)
dag <-
# Helper functions for displaying paths
function(g, paths) {
display_paths <- as_adjacency_matrix(g)
adj <-for (i in seq_along(paths)) {
paths[[i]]
path <-cat("Path ", i, ": ", sep = "")
draw_edge(adj_mat = adj, path = path)
}
}
function(adj_mat, path) {
draw_edge <- as_ids(path)
path_ids <- rep("", length(path_ids))
arrow_vec <-for (i in seq_len(length(path_ids)-1)) {
path_ids[i]
node1 <- path_ids[i+1]
node2 <-if (adj_mat[node1,node2]==1) {
## Arrow is 1 -> 2
"->"
arrow_vec[i] <-else {
} ## Arrow is 1 <- 2
"<-"
arrow_vec[i] <-
}
} paste(path_ids, arrow_vec)
path_string <- paste(path_string, collapse = " ")
path_string <-cat(path_string, "\n")
}
display_paths(dag, all_simple_paths(dag, from = "A", to = "Y", mode = "all"))