A SEM user's guide to dagitty for R

knitr::opts_chunk$set(comment = "")
library(dagitty)

What is dagitty

Dagitty is a software to analyze causal diagrams, also known as directed acyclic graphs (DAGs). Structural equation models (SEMs) can be viewed as a parametric form of DAGs, which encode linear functions instead of arbitrary nonlinear functions.

Because every SEM is a DAG, much of the methodology developed for DAGs is of potentially great interest for SEM users as well. In this vignette, I am going to show some possibilities. This follows the structure of Kyono's "Commentator" program (http://ftp.cs.ucla.edu/pub/stat_ser/r364.pdf), and thereby also shows how the tasks implemented in that program can be solved using the dagitty package.

g1 <- dagitty( "dag {
    W1 -> Z1 -> X -> Y
    Z1 <- V -> Z2
    W2 -> Z2 -> Y
    X <-> W1 <-> W2 <-> Y
}")

g2 <- dagitty( "dag {
    Y <- X <- Z1 <- V -> Z2 -> Y
    Z1 <- W1 <-> W2 -> Z2
    X <- W1 -> Y
    X <- W2 -> Y
}")

plot(graphLayout(g1))

List testable implications of a structural equation model

print( impliedConditionalIndependencies( g1 ) )

List adjustment sets for specific path coefficients

print( adjustmentSets( g1, "Z1", "X", effect="direct" ) )
print( adjustmentSets( g2, "X", "Y", effect="direct" ) )

List path coefficients that are identifiable by regression

for( n in names(g1) ){
    for( m in children(g1,n) ){
        a <- adjustmentSets( g1, n, m, effect="direct" )
        if( length(a) > 0 ){
            cat("The coefficient on ",n,"->",m,
                " is identifiable controlling for:\n",sep="")
            print( a, prefix=" * " )
        }
    }
}

List adjustment sets for specific total effects

print( adjustmentSets( g1, "X", "Y" ) )
print( adjustmentSets( g2, "X", "Y" ) )

List total effects that are identifiable by regression

for( n in names(g1) ){
    for( m in setdiff( descendants( g1, n ), n ) ){
        a <- adjustmentSets( g1, n, m )
        if( length(a) > 0 ){
            cat("The total effect of ",n," on ",m,
                " is identifiable controlling for:\n",sep="")
            print( a, prefix=" * " )
        }
    }
}

List path coefficients that are identifiable through instrumental variables

```r for( n in names(g1) ){ for( m in children(g1,n) ){ iv <- instrumentalVariables( g1, n, m ) if( length( iv ) > 0 ){ cat( n, m, "\n" ) print( iv , prefix=" * " ) } } }



Try the dagitty package in your browser

Any scripts or data that you put into this service are public.

dagitty documentation built on Jan. 21, 2021, 5:07 p.m.