Skip to content

Commit c4f2125

Browse files
authored
Slowed down all graphs, added collider graph
1 parent 474156d commit c4f2125

7 files changed

+64
-11
lines changed

Animation of Collider.R

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
library(tidyverse)
2+
library(gganimate)
3+
library(ggthemes)
4+
5+
#Probably try a few times until the raw correlation looks nice and low
6+
df <- data.frame(X = rnorm(200)+1,Y=rnorm(200)+1,time="1") %>%
7+
mutate(C = as.integer(X+Y+rnorm(200)/2>2)) %>%
8+
group_by(C) %>%
9+
mutate(mean_X=mean(X),mean_Y=mean(Y)) %>%
10+
ungroup()
11+
12+
cor(df$X,df$Y)
13+
14+
#Calculate correlations
15+
before_cor <- paste("1. Start with raw data, ignoring C. Correlation between X and Y: ",round(cor(df$X,df$Y),3),sep='')
16+
after_cor <- paste("7. Analyze what's left! Correlation between X and Y controlling for C: ",round(cor(df$X-df$mean_X,df$Y-df$mean_Y),3),sep='')
17+
18+
19+
20+
21+
#Add step 2 in which X is demeaned, and 3 in which both X and Y are, and 4 which just changes label
22+
dffull <- rbind(
23+
#Step 1: Raw data only
24+
df %>% mutate(mean_X=NA,mean_Y=NA,C=0,time=before_cor),
25+
#Step 2: Raw data only
26+
df %>% mutate(mean_X=NA,mean_Y=NA,time='2. Separate data by the values of C.'),
27+
#Step 3: Add x-lines
28+
df %>% mutate(mean_Y=NA,time='3. Figure out what differences in X are explained by C'),
29+
#Step 4: X de-meaned
30+
df %>% mutate(X = X - mean_X,mean_X=0,mean_Y=NA,time="4. Remove differences in X explained by C"),
31+
#Step 5: Remove X lines, add Y
32+
df %>% mutate(X = X - mean_X,mean_X=NA,time="5. Figure out what differences in Y are explained by C"),
33+
#Step 6: Y de-meaned
34+
df %>% mutate(X = X - mean_X,Y = Y - mean_Y,mean_X=NA,mean_Y=0,time="6. Remove differences in Y explained by C"),
35+
#Step 7: Raw demeaned data only
36+
df %>% mutate(X = X - mean_X,Y = Y - mean_Y,mean_X=NA,mean_Y=NA,time=after_cor))
37+
38+
p <- ggplot(dffull,aes(y=Y,x=X,color=as.factor(C)))+geom_point()+
39+
geom_vline(aes(xintercept=mean_X,color=as.factor(C)))+
40+
geom_hline(aes(yintercept=mean_Y,color=as.factor(C)))+
41+
guides(color=guide_legend(title="C"))+
42+
scale_color_colorblind()+
43+
labs(title = 'Inventing a Correlation Between X and Y by Controlling for Collider C \n{next_state}')+
44+
transition_states(time,transition_length=c(1,12,32,12,32,12,12),state_length=c(160,125,100,75,100,75,160),wrap=FALSE)+
45+
ease_aes('sine-in-out')+
46+
exit_fade()+enter_fade()
47+
48+
animate(p,nframes=200)

Animation of Controlling for Z.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,14 @@ dffull <- rbind(
3131
#Step 6: Raw demeaned data only
3232
df %>% mutate(X = X - mean_X,Y = Y - mean_Y,mean_X=NA,mean_Y=NA,time=after_cor))
3333

34-
ggplot(dffull,aes(y=Y,x=X,color=as.factor(W)))+geom_point()+
34+
p <- ggplot(dffull,aes(y=Y,x=X,color=as.factor(W)))+geom_point()+
3535
geom_vline(aes(xintercept=mean_X,color=as.factor(W)))+
3636
geom_hline(aes(yintercept=mean_Y,color=as.factor(W)))+
3737
guides(color=guide_legend(title="W"))+
3838
scale_color_colorblind()+
3939
labs(title = 'The Relationship between Y and X, Controlling for a Binary Variable W \n{next_state}')+
40-
transition_states(time,transition_length=c(6,16,6,16,6,6),state_length=c(50,22,12,22,12,50),wrap=FALSE)+
40+
transition_states(time,transition_length=c(12,32,12,32,12,12),state_length=c(160,100,75,100,75,160),wrap=FALSE)+
4141
ease_aes('sine-in-out')+
4242
exit_fade()+enter_fade()
43+
44+
animate(p,nframes=200)

Animation of DID.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ dffull <- rbind(
4040

4141

4242

43-
ggplot(dffull,aes(y=Y,x=xaxisTime,color=as.factor(Control)))+geom_point()+
43+
p <- ggplot(dffull,aes(y=Y,x=xaxisTime,color=as.factor(Control)))+geom_point()+
4444
guides(color=guide_legend(title="Group"))+
4545
geom_vline(aes(xintercept=1.5),linetype='dashed')+
4646
scale_color_colorblind()+
@@ -82,4 +82,4 @@ ggplot(dffull,aes(y=Y,x=xaxisTime,color=as.factor(Control)))+geom_point()+
8282
ease_aes('sine-in-out')+
8383
exit_fade()+enter_fade()
8484

85-
85+
animate(p,nframes=150)

Animation of Fixed Effects.R

+4-2
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ dffull <- rbind(
2828
#Step 6: Raw demeaned data only
2929
df %>% mutate(X = X - mean_X,Y = Y - mean_Y,mean_X=NA,mean_Y=NA,time=after_cor))
3030

31-
ggplot(dffull,aes(y=Y,x=X,color=as.factor(Person)))+geom_point()+
31+
p <- ggplot(dffull,aes(y=Y,x=X,color=as.factor(Person)))+geom_point()+
3232
geom_vline(aes(xintercept=mean_X,color=as.factor(Person)))+
3333
geom_hline(aes(yintercept=mean_Y,color=as.factor(Person)))+
3434
guides(color=guide_legend(title="Individual"))+
3535
scale_color_colorblind()+
3636
labs(title = 'The Relationship between Y and X, with Individual Fixed Effects \n{next_state}')+
37-
transition_states(time,transition_length=c(6,16,6,16,6,6),state_length=c(50,22,12,22,12,50),wrap=FALSE)+
37+
transition_states(time,transition_length=c(12,32,12,32,12,12),state_length=c(160,100,75,100,75,160),wrap=FALSE)+
3838
ease_aes('sine-in-out')+
3939
exit_fade()+enter_fade()
40+
41+
animate(p,nframes=200)

Animation of IV.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ endpts <- df %>%
3333
group_by(Z) %>%
3434
summarize(mean_X=mean(mean_X),mean_Y=mean(mean_Y))
3535

36-
ggplot(dffull,aes(y=Y,x=X,color=as.factor(Z)))+geom_point()+
36+
p <- ggplot(dffull,aes(y=Y,x=X,color=as.factor(Z)))+geom_point()+
3737
geom_vline(aes(xintercept=mean_X,color=as.factor(Z)))+
3838
geom_hline(aes(yintercept=mean_Y,color=as.factor(Z)))+
3939
guides(color=guide_legend(title="Z"))+
@@ -46,3 +46,4 @@ ggplot(dffull,aes(y=Y,x=X,color=as.factor(Z)))+geom_point()+
4646
ease_aes('sine-in-out')+
4747
exit_fade()+enter_fade()
4848

49+
animate(p,nframes=175)

Animation of Matching.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ dffull <- rbind(
4242
df %>% mutate(Y = NA,bins=NA,state="6. The treatment effect is the remaining difference."))
4343

4444

45-
ggplot(dffull,aes(y=Y,x=xaxisTime,color=Treated,size=Treated))+geom_point()+
45+
p <- ggplot(dffull,aes(y=Y,x=xaxisTime,color=Treated,size=Treated))+geom_point()+
4646
geom_vline(aes(xintercept=bins))+
4747
geom_hline(aes(yintercept=mean_Y,color=Treated))+
4848
geom_segment(aes(x=.5,xend=.5,
@@ -57,4 +57,4 @@ ggplot(dffull,aes(y=Y,x=xaxisTime,color=Treated,size=Treated))+geom_point()+
5757
ease_aes('sine-in-out')+
5858
exit_fade()+enter_fade()
5959

60-
60+
animate(p,nframes=200)

Animation of RDD.R

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ dffull <- rbind(
2727
df %>% mutate(mean_Y = ifelse(xaxisTime > 9 & xaxisTime < 11,mean_Y,NA),Y=ifelse(xaxisTime > 9 & xaxisTime < 11,mean_Y,NA),groupLine=NA,state="5. The jump at the cutoff is the effect of treatment."))
2828

2929

30-
ggplot(dffull,aes(y=Y,x=xaxisTime))+geom_point()+
30+
p <- ggplot(dffull,aes(y=Y,x=xaxisTime))+geom_point()+
3131
geom_vline(aes(xintercept=10),linetype='dashed')+
3232
geom_point(aes(y=mean_Y,x=groupX),color="red",size=2)+
3333
geom_vline(aes(xintercept=groupLine))+
@@ -46,4 +46,4 @@ ggplot(dffull,aes(y=Y,x=xaxisTime))+geom_point()+
4646
ease_aes('sine-in-out')+
4747
exit_fade()+enter_fade()
4848

49-
49+
animate(p,nframes=175)

0 commit comments

Comments
 (0)