dagR: Directed Acyclic Graph with R

Directed Acyclic Graph (DAG)

A tool often used in epidemiology to better understand causal/non-causal relationships between multiple variables.

Load package

library(dagR)

Built in examples

junk <- dag.draw(demo.dag1())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag2())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag3())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag4())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag5())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag6())

plot of chunk unnamed-chunk-3

junk <- dag.draw(demo.dag7())

plot of chunk unnamed-chunk-3

Simple common cause example

dag.dat <-
    dag.init(outcome = NULL, exposure = NULL, covs = c(1),
             arcs = c(1,0, 1,-1),
             assocs = c(0,0), xgap = 0.04, ygap = 0.05, len = 0.1,
             x.name = "Preterm birth",
             cov.names = c("Maternal Age"),
             y.name = "Later Life Maternal CVD"
             )

junk <- dag.draw(dag.dat, noxy = T)

plot of chunk unnamed-chunk-4

More complex with a common effect

dag.dat <-
    dag.init(outcome = NULL, exposure = NULL,
             covs = c(1,1), arcs = c(0,2, 1,2, 1,-1),
             assocs = c(0,0), xgap = 0.04, ygap = 0.05, len = 0.1,
             x.name = "Birth characteristics (preterm/SGA)",
             cov.names = c("Socioeconomic status (unobserved)","Emigration status"),
             y.name = "Later Life Maternal CVD",
             symbols = c("A", "U", "L", "Y"))
junk <- dag.draw(dag.dat, noxy = T)

plot of chunk unnamed-chunk-5

Fine tune by directly manipulating XY positions

## Direct manipulation of XY positions
dag.dat$x <- c(0.00, 0.25, 0.75, 1.00)
dag.dat$y <- c(0.25, 0.00, 0.9, 0.25)

## Draw a DAG without X->Y arrow
junk <- dag.draw(dag.dat, noxy = T)

## Add a conditioning box using rect()
rect(xleft   = 0.75-0.05, xright = 0.75+0.05,
     ybottom = 0.90-0.05, ytop   = 0.90+0.05)

plot of chunk unnamed-chunk-6