Skip to content

Instantly share code, notes, and snippets.

@malcolmbarrett
Last active August 7, 2018 21:41
Show Gist options
  • Save malcolmbarrett/6604f357e06181b8cf421b3c68b78130 to your computer and use it in GitHub Desktop.
Save malcolmbarrett/6604f357e06181b8cf421b3c68b78130 to your computer and use it in GitHub Desktop.
# install.packages(c("devtools", "tidyverse"))
# devtools::install_github("malcolmbarrett/ggdag")
library(ggdag)
library(tidyverse)
dag <- dagify(D ~ ABU1 + AEU2 + BEU3,
ABU1 ~ A + B + U1,
AEU2 ~ A + E + U2,
BEU3 ~ B + E + U3)
set.seed(5080)
dag <- dag %>%
tidy_dagitty() %>%
mutate(
sc = case_when(
name == "ABU1" ~ "A = 0,\nB = 1,\nU1 = ?",
name == "AEU2" ~ "A = 0,\nE = 1,\nU2 = ?",
name == "BEU3" ~ "B = 1,\nE = 1,\nU3 = ?",
TRUE ~ NA_character_
),
is_sc = if_else(is.na(sc), NA_character_, "Sufficient\nCause"),
blank_names = if_else(is.na(sc), name, NA_character_)
)
dag %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(aes(col = is_sc)) +
geom_dag_text(aes(label = blank_names)) +
geom_dag_text_repel(aes(label = sc), force = 3, max.iter = 10000, nudge_x = c(1, -1, 1), nudge_y = -.3) +
geom_dag_edges_link() +
scale_color_manual("", breaks = "Sufficient\nCause", values = "#56B4E9", na.value = "#000000") +
theme_dag()
ggsave("sc_dag.png", dpi = 320, width = 6, height = 5)
dag %>%
adjust_for(c("D", "ABU1", "AEU2", "BEU3")) %>%
filter(!collider_line) %>%
ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_point(aes(col = is_sc, shape = adjusted)) +
geom_dag_text(aes(label = blank_names)) +
geom_dag_text_repel(aes(label = sc), force = 3, max.iter = 10000, nudge_x = c(1, -1, 1), nudge_y = -.3) +
geom_dag_edges_link(aes(
start_cap = ggraph::circle(10, "mm"),
end_cap = ggraph::circle(10, "mm")
)) +
scale_color_manual("", breaks = "Sufficient\nCause", values = "#56B4E9", na.value = "#000000") +
theme_dag() +
scale_adjusted() +
guides(shape = FALSE)
ggsave("sc_dag_sq.png", dpi = 320, width = 6, height = 5)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment