Today

Review of Control

Structural Equations

Structural Equation Models

Causal Graphs (Code)

#Library to create and analyze causal graphs
library(dagitty) 
library(ggdag) #library to plot causal graphs
yxdag<-dagify(Y~X) #create graph with arrow from X to Y
#Set position of nodes so they lie on a straight line
  coords<-list(x=c(X = 0, Y = 1),y=c(X = 0, Y = 0)) 
  coords_df<-coords2df(coords)
  coordinates(yxdag)<-coords2list(coords_df)
#Plot causal graph
ggdag(yxdag)+theme_dag_blank()+
  labs(title="X causes Y",
  subtitle="X is a parent of Y, Y is a child of X") 

Causal Graphs

Implications of structural equations models

Example: Solving a structural model (Code)

examplegraph<-dagify(Y3~Y2,Y4~Y3+Y2,Y2~Y1) #create graph
#Set position of nodes 
coords<-list(x=c(Y1 = 0, Y2 = 1, Y3 = 2, Y4 = 3),
         y=c(Y1 = 0, Y2 = 0, Y3 = -0.1, Y4 = 0)) 
coords_df<-coords2df(coords)
coordinates(examplegraph)<-coords2list(coords_df)

#Plot causal graph  
ggdag(examplegraph)+theme_dag_blank()
  +labs(title="Y1 causes Y2, 
        Y2 causes Y3, Y3 and Y2 cause Y4") 

Example: Solving a structural model

Interpreting a structural equation model

impliedConditionalIndependencies(examplegraph)
## Y1 _||_ Y3 | Y2
## Y1 _||_ Y4 | Y2

Every SEM tells a story

“Do” operation and causal interventions

Example: Experiments (Code)

#Set position of nodes so they lie on a straight line
  coords<-list(x=c(X = 0, Y = 1),y=c(X = 0, Y = 0)) 
  coords_df<-coords2df(coords)
  coordinates(yxdag)<-coords2list(coords_df)
ggdag(yxdag)+theme_dag_blank() #Plot causal graph

Example: Experiments

Example 2: Confounding (Code 1)

confoundgraph<-dagify(Y~X+W,X~W) #create graph
#Set position of nodes 
coords<-list(x=c(X = 0, W = 1, Y = 2),
        y=c(X = 0, W = -0.1, Y = 0)) 
coords_df<-coords2df(coords)
coordinates(confoundgraph)<-coords2list(coords_df)
#Plot causal graph
ggdag(confoundgraph)+theme_dag_blank()
  labs(title="Confounding of effect of X on Y by W") 

Example 2: Confounding (Code 2)

perturbedgraph<-dagify(Y~x+W) #create graph
#Set position of nodes 
coords<-list(x=c(x = 0, W = 1, Y = 2),
            y=c(x = 0, W = -0.1, Y = 0)) 
coords_df<-coords2df(coords)
coordinates(perturbedgraph)<-coords2list(coords_df)

#Plot causal graph  
ggdag(perturbedgraph)+theme_dag_blank()+
  labs(title="Perturbed Graph") 

Example 2: Confounding

Recovering the adjustment formula using causal graphs

What have we learned so far?

Mediators

mediatorgraph<-dagify(Y~W,W~X) #create graph
#Set position of nodes 
coords<-list(x=c(X = 0, W = 1, Y = 2),
          y=c(X = 0, W = 0, Y = 0)) 
coords_df<-coords2df(coords)
coordinates(mediatorgraph)<-coords2list(coords_df)

#Plot causal graph
ggdag(mediatorgraph)+theme_dag_blank()+
  labs(title="Mediator structure") 

Mediators

Colliders (Code)

collidergraph<-dagify(W~Y,W~X) #create graph
#Set position of nodes 
coords<-list(x=c(X = 0, W = 1, Y = 2),
               y=c(X = 0, W = 0, Y = 0)) 
coords_df<-coords2df(coords)
coordinates(collidergraph)<-coords2list(coords_df)
  
#Plot causal graph  
ggdag(collidergraph)+theme_dag_blank()+
  labs(title="Collider structure") 

Colliders

Summary

References

    install.packages("dagitty")
    install.packages("ggdag")