library(dagitty) #Library to create and analyze causal graphs
library(ggplot2) #Plotting
suppressWarnings(suppressMessages(library(ggdag))) #library to plot causal graphs
repexdag<-dagify(Y3~X3+Y2+X2+Y1+X1,Y2~X2+Y1+X1,Y1~X1) #create graph
#Set position of nodes so they lie on a straight line
coords<-list(x=c(X1 = 0, Y1 = 0, X2=1, Y2=1, X3=2, Y3=2),
y=c(X1 = 1, Y1 = 0, X2=1, Y2=0, X3=1, Y3=0))
coords_df<-coords2df(coords)
coordinates(repexdag)<-coords2list(coords_df)
ggdag(repexdag, edge_type = "arc")+theme_dag_blank()+labs(title="X randomized in every time period") #Plot causal graph
seqdag<-dagify(Y4~X4+Y3+X3+Y2+X2+Y1+X1,Y3~X3+Y2+X2+Y1+X1,Y2~X2+Y1+X1,Y1~X1,
X4~Y3+X3+Y2+X2+Y1+X1,X3~Y2+X2+Y1+X1, X2~Y1+X1) #create graph
#Set position of nodes so they lie on a straight line
coords<-list(x=c(X1 = 0, Y1 = 0, X2=1, Y2=1, X3=2, Y3=2, X4=3, Y4=3),
y=c(X1 = 1, Y1 = 0, X2=1, Y2=0, X3=1, Y3=0, X4=1, Y4=0))
coords_df<-coords2df(coords)
coordinates(seqdag)<-coords2list(coords_df)
ggdag(seqdag, edge_type = "arc")+theme_dag_blank()+labs(title="X may depend on all past variables") #Plot causal graph
library(gridExtra) #Graph Display
splitgraphs<-list()
predag<-dagify(Y3~X3+Y2+X2+Y1+X1,Y2~X2+Y1+X1,Y1~X1,
X3~Y2+X2+Y1+X1, X2~Y1+X1) #create graph
#Set position of nodes so they lie on a straight line
coords<-list(x=c(X1 = 0, Y1 = 0, X2=1, Y2=1, X3=2, Y3=2),
y=c(X1 = 1, Y1 = 0, X2=0.8, Y2=0.2, X3=1, Y3=0))
coords_df<-coords2df(coords)
coordinates(predag)<-coords2list(coords_df)
splitgraphs[[1]]<-ggdag(predag)+theme_dag_blank()+labs(title="DAG without intervention",subtitle="X may depend on all past variables") #Plot causal graph
swig<-dagify(Y3_x2~X3_x2+Y2_x2+x2+Y1+X1,Y2_x2~x2+Y1+X1,Y1~X1,
X3_x2~Y2_x2+x2+Y1+X1, X2~Y1+X1) #create graph
#Set position of nodes so they lie on a straight line
coords<-list(x=c(X1 = 0, Y1 = 0, X2=0.8, x2=1.2, Y2_x2=1, X3_x2=2, Y3_x2=2),
y=c(X1 = 1, Y1 = 0, X2=0.8, x2=0.8, Y2_x2=0.2, X3_x2=1, Y3_x2=0))
coords_df<-coords2df(coords)
coordinates(swig)<-coords2list(coords_df)
splitgraphs[[2]]<-ggdag(swig)+theme_dag_blank()+labs(title="SWIG with intervention on X2",subtitle="X may depend on all past variables") #Plot causal graph
grid.arrange(grobs=splitgraphs,nrow=1,ncol=2) #Arrange In 2x2 grid
#Test that effect of X2 on Y2, Y3 identifiable by adjustment
dseparated(swig,c("Y3_x2","Y2_x2"),"X2",c("Y1","X1"))
## [1] TRUE
markovdag<-dagify(Y4~X4+Y3+X3,Y3~X3+Y2+X2,Y2~X2+Y1+X1,Y1~X1,
X4~Y3+X3,X3~Y2+X2, X2~Y1+X1) #create graph
#Set position of nodes so they lie on a straight line
coords<-list(x=c(X1 = 0, Y1 = 0, X2=1, Y2=1, X3=2, Y3=2, X4=3, Y4=3),
y=c(X1 = 1, Y1 = 0, X2=1, Y2=0, X3=1, Y3=0, X4=1, Y4=0))
coords_df<-coords2df(coords)
coordinates(markovdag)<-coords2list(coords_df)
ggdag(markovdag, edge_type = "arc")+theme_dag_blank()+labs(title="X and Y depend on immediate past only") #Plot causal graph
Propensity Time Series
Impulse Response Functions