Skip to content

Instantly share code, notes, and snippets.

@alexpghayes
Created November 13, 2024 15:56
Show Gist options
  • Save alexpghayes/927f1a2a65bf9f8e8edce5ed60ca7372 to your computer and use it in GitHub Desktop.
Save alexpghayes/927f1a2a65bf9f8e8edce5ed60ca7372 to your computer and use it in GitHub Desktop.
library(ggdag)
#> 
#> Attaching package: 'ggdag'
#> The following object is masked from 'package:stats':
#> 
#>     filter
library(tidyverse)
library(tidygraph)
#> 
#> Attaching package: 'tidygraph'
#> 
#> The following object is masked from 'package:stats':
#> 
#>     filter

coords <- tibble(
  name = c("Ci", "Ti", "Xi", "Yi", "Aij", "Xj", "Cj", "Yj", "Tj"),
  x = c(1, 0, 2, 1, 3, 4, 5, 5, 6),
  y = c(2, 1, 1, 0, 0, 1, 2, 0, 1)
)

dag <- dagify(
  Yi ~ Ti + Ci + Xi,
  Aij ~ Xi + Xj,
  Xi ~ Ti + Ci,
  Ti ~ Ci,
  Yj ~ Tj + Cj + Xj,
  Xj ~ Tj + Cj,
  Tj ~ Cj,
  coords = coords,
  latent = c("Xi", "Xj"),
  labels = c(
    Ci = "C[i %.% phantom(j)]",
    Ti = "T[i]",
    Xi = "X[i %.% phantom(j)]",
    Yi = "Y[i]",
    Aij = "A[ij]",
    Xj = "X[j %.% phantom(j)]",
    Cj = "C[j %.% phantom(j)]",
    Yj = "Y[j]",
    Tj = "T[j]"
  ),
  exposure = "Ti",
  outcome = "Yi"
)

dag |>
  tidy_dagitty() |>
  ggplot(aes(x = x, y = y, xend = xend, yend = yend)) +
  geom_dag_point() +
  geom_dag_edges_diagonal() +
  geom_dag_text(aes(label = label), parse = TRUE, size = 5) +
  theme_dag(base_size = 22, base_family = "Computer Modern")

Created on 2024-11-13 with reprex v2.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment