-
Notifications
You must be signed in to change notification settings - Fork 9
/
10-slides.Rmd
1609 lines (1190 loc) Β· 48.4 KB
/
10-slides.Rmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
---
title: "Regression discontinuity I"
author: "Andrew Heiss"
date: "`r Sys.Date()`"
output:
xaringan::moon_reader:
lib_dir: "libs"
chakra: "libs/remark-latest.min.js"
css: ["default", "css/ath-slides.css", "css/ath-inferno-fonts.css", "css/animate.css"]
seal: false
anchor_sections: false
nature:
highlightStyle: github
highlightLines: true
countIncrementalSlides: false
ratio: "16:9"
navigation:
scroll: false
editor_options:
chunk_output_type: console
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(warning = FALSE, message = FALSE,
fig.retina = 3, fig.align = "center")
```
```{r packages-data, include=FALSE}
library(tidyverse)
library(broom)
library(ggdag)
library(kableExtra)
library(scales)
library(patchwork)
library(latex2exp)
library(rdrobust)
library(rddensity)
# Fake program data
set.seed(1234)
num_students <- 1000
tutoring <- tibble(
id = 1:num_students,
entrance_exam = rbeta(num_students, shape1 = 7, shape2 = 2),
exit_exam = rbeta(num_students, shape1 = 5, shape2 = 3)
) %>%
mutate(entrance_exam = entrance_exam * 100,
tutoring = entrance_exam <= 70) %>%
mutate(exit_exam = exit_exam * 40 + 10 * tutoring + entrance_exam / 2) %>%
mutate(tutoring_fuzzy = ifelse(entrance_exam > 60 & entrance_exam < 80,
sample(c(TRUE, FALSE), n(), replace = TRUE),
tutoring)) %>%
mutate(tutoring_text = factor(tutoring, levels = c(FALSE, TRUE),
labels = c("No tutor", "Tutor")),
tutoring_fuzzy_text = factor(tutoring_fuzzy, levels = c(FALSE, TRUE),
labels = c("No tutor", "Tutor"))) %>%
mutate(entrance_centered = entrance_exam - 70)
```
```{r xaringanExtra, echo=FALSE}
xaringanExtra::use_xaringan_extra(c("tile_view"))
```
class: center middle main-title section-title-7
# Regression discontinuity I
.class-info[
**Session 10**
.light[PMAP 8521: Program evaluation<br>
Andrew Young School of Policy Studies
]
]
---
name: outline
class: title title-inv-8
# Plan for today
--
.box-2.medium.sp-after-half[Arbitrary cutoffs and causal inference]
--
.box-4.medium.sp-after-half[Drawing lines and measuring gaps]
--
.box-6.medium.sp-after-half[Main RDD concerns]
---
name: arbitrary-cutoffs
class: center middle section-title section-title-2 animated fadeIn
# Arbitrary cutoffs<br>and causal inference
---
layout: true
class: title title-2
---
# Quasi-experiments again
--
.box-inv-2.sp-after[Instead of using carefully adjusted DAGs,<br>we can use *context* to isolate/identify the pathway between<br>treatment and outcome in observational data]
--
.box-inv-2[Diff-in-diff was one kind of quasi-experiment]
.box-2.sp-after[Treatment/control + before/after]
--
.box-inv-2[Regression discontinuity designs (RDD) are another]
.box-2[Arbitrary rules determine access to programs]
---
# Rules to access programs
.box-inv-2.medium[Lots of policies and programs are<br>based on arbitrary rules and thresholds]
--
.box-2[If you're above the threshold, you're in the program;<br>if you're below, you're not (or vice versa)]
---
# Key terms
--
.box-inv-2.medium[Running / forcing variable]
.box-2.sp-after[Index or measure that determines eligibility]
--
.box-inv-2.medium[Cutoff / cutpoint / threshold]
.box-2[Number that formally assigns access to program]
---
layout: false
```{r rd-dag, echo=FALSE, fig.width=12, fig.height=6, out.width="100%"}
node_details <- tribble(
~name, ~label, ~x, ~y,
"treatment", "Program", 1, 1,
"outcome", "Outcome", 4, 1,
"cutoff", "Above cutoff", 2, 1.75,
"running", "Running variable", 3, 2
)
node_labels <- node_details$label
names(node_labels) <- node_details$name
rdd_dag <- dagify(outcome ~ treatment + running,
treatment ~ cutoff,
cutoff ~ running,
exposure = c("treatment", "cutoff"),
outcome = "outcome",
coords = node_details,
labels = node_labels) %>%
tidy_dagitty() %>%
node_status()
status_colors <- c(exposure = "#0074D9", outcome = "#FF851B", latent = "grey50")
set.seed(1234)
ggplot(rdd_dag, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_dag_edges(start_cap = ggraph::circle(3, "lines"),
end_cap = ggraph::circle(3, "lines"),
edge_width = 1.5,
arrow_directed = grid::arrow(length = grid::unit(0.75, "lines"), type = "closed")) +
geom_dag_point(aes(color = status), size = 30) +
geom_dag_label_repel(aes(label = label, fill = status), seed = 1234,
color = "white", fontface = "bold", size = 10,
label.padding = grid::unit(0.75, "lines"),
box.padding = grid::unit(2.5, "lines"),
direction = "y") +
scale_color_manual(values = status_colors, na.value = "grey20") +
scale_fill_manual(values = status_colors, na.value = "grey20") +
guides(color = FALSE, fill = FALSE) +
theme_dag(base_size = 20, base_family = "Fira Sans Condensed")
```
---
layout: true
class: title title-2
---
# Discontinuities everywhere!
.pull-left-wide.small[
```{r poverty-table, echo=FALSE}
poverty_line <- tibble::tribble(
~Size, ~Annual, ~Monthly, ~`138%`, ~`150%`, ~`200%`,
1, 12760, 1063, 17609, 19140, 25520,
2, 17240, 1437, 23791, 25860, 34480,
3, 21720, 1810, 29974, 32580, 43440,
4, 26200, 2183, 36156, 39300, 52400,
5, 30680, 2557, 42338, 46020, 61360,
6, 35160, 2930, 48521, 52740, 70320,
7, 39640, 3303, 54703, 59460, 79280,
8, 44120, 3677, 60886, 66180, 88240
)
poverty_line %>%
mutate(across(-Size, dollar)) %>%
kbl(align = "c")
```
]
.pull-right-narrow[
.box-inv-2.smaller[**Medicaid**<br>138%*]
.box-inv-2.smaller[**ACA subsidies**<br>138β400%*]
.box-inv-2.smaller[**CHIP**<br>200%]
.box-inv-2.smaller[**SNAP/Free lunch**<br>130%]
.box-inv-2.smaller[**Reduced lunch**<br>130β185%]
]
---
# Hypothetical tutoring program
--
.box-inv-2.medium[Students take an entrance exam]
--
.box-inv-2.medium[Those who score 70 or lower<br>get a free tutor for the year]
--
.box-inv-2.medium[Students then take an exit exam<br>at the end of the year]
---
layout: false
```{r tutoring-running, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = tutoring_text, fill = tutoring_text)) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_point(size = 5, pch = 21, color = "white", alpha = 0.7,
position = position_jitter(width = 0, height = 0.2, seed = 1234)) +
labs(x = "Entrance exam score", y = NULL) +
guides(fill = FALSE) +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
class: title title-2
# Causal inference intuition
.box-inv-2.medium[The people right before and right after the threshold are essentially the same]
---
```{r tutoring-running-threshold, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = tutoring_text, fill = tutoring_text)) +
annotate(geom = "rect", fill = "grey50", alpha = 0.25, ymin = -Inf, ymax = Inf,
xmin = 70 - 5, xmax = 70 + 5) +
annotate(geom = "rect", fill = "grey50", alpha = 0.5, ymin = -Inf, ymax = Inf,
xmin = 70 - 2, xmax = 70 + 2) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_point(size = 5, pch = 21, color = "white", alpha = 0.7,
position = position_jitter(width = 0, height = 0.2, seed = 1234)) +
labs(x = "Entrance exam score", y = NULL) +
guides(fill = FALSE) +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
```{r tutoring-running-threshold-zoomed, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = tutoring_text, fill = tutoring_text)) +
annotate(geom = "rect", fill = "grey50", alpha = 0.25, ymin = -Inf, ymax = Inf,
xmin = 70 - 5, xmax = 70 + 5) +
annotate(geom = "rect", fill = "grey50", alpha = 0.5, ymin = -Inf, ymax = Inf,
xmin = 70 - 2, xmax = 70 + 2) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_point(size = 5, pch = 21, color = "white", alpha = 0.7,
position = position_jitter(width = 0, height = 0.2, seed = 1234)) +
labs(x = "Entrance exam score", y = NULL) +
guides(fill = FALSE) +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
coord_cartesian(xlim = c(70 - 6, 70 + 6)) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
class: title title-2
# Causal inference intuition
--
.box-inv-2.medium[The people right before and right after the threshold are essentially the same]
--
.box-2.medium[Pseudo treatment and control groups!]
--
.box-inv-2.medium[Compare outcomes for those<br>right before/after, calculate difference]
---
```{r tutoring-outcome, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = exit_exam, fill = tutoring_text)) +
geom_point(size = 4, pch = 21, color = "white", alpha = 1) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
labs(x = "Entrance exam score", y = "Exit exam score") +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
guides(fill = guide_legend(reverse = TRUE)) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
```{r tutoring-outcome-lines, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = exit_exam, fill = tutoring_text, color = tutoring_text)) +
geom_point(size = 4, pch = 21, color = "white", alpha = 0.25) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_smooth(data = filter(tutoring, entrance_exam <= 70),
method = "lm", size = 2, show.legend = FALSE) +
geom_smooth(data = filter(tutoring, entrance_exam > 70),
method = "lm", size = 2, show.legend = FALSE) +
labs(x = "Entrance exam score", y = "Exit exam score") +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
scale_color_manual(values = c("#85144b", "#0074D9")) +
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)), color = FALSE) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
```{r tutoring-outcome-delta, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
rdd_tutoring <- lm(exit_exam ~ entrance_centered + tutoring, data = tutoring) %>%
tidy()
effect_control <- filter(rdd_tutoring, term == "(Intercept)")$estimate
late <- filter(rdd_tutoring, term == "tutoringTRUE")$estimate
effect_treatment <- effect_control + late
ggplot(tutoring, aes(x = entrance_exam, y = exit_exam, fill = tutoring_text, color = tutoring_text)) +
geom_point(size = 4, pch = 21, color = "white", alpha = 0.25) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_smooth(data = filter(tutoring, entrance_exam <= 70),
method = "lm", size = 2, show.legend = FALSE) +
geom_smooth(data = filter(tutoring, entrance_exam > 70),
method = "lm", size = 2, show.legend = FALSE) +
annotate(geom = "segment", x = 70, xend = 70,
y = effect_control, yend = effect_treatment,
size = 4, color = "#FF4136") +
annotate(geom = "label", x = 82, y = effect_treatment - (late / 2),
label = "Ξ΄\n(causal effect)", family = "Fira Sans Condensed", fontface = "bold",
color = "white", fill = "#FF4136", size = 7, label.padding = unit(0.75, "lines")) +
labs(x = "Entrance exam score", y = "Exit exam score") +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
scale_color_manual(values = c("#85144b", "#0074D9")) +
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)), color = FALSE) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
```{r tutoring-outcome-delta-zoomed, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(tutoring, aes(x = entrance_exam, y = exit_exam, fill = tutoring_text, color = tutoring_text)) +
geom_point(size = 4, pch = 21, color = "white", alpha = 0.25) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_smooth(data = filter(tutoring, entrance_exam <= 70),
method = "lm", size = 2, show.legend = FALSE) +
geom_smooth(data = filter(tutoring, entrance_exam > 70),
method = "lm", size = 2, show.legend = FALSE) +
annotate(geom = "segment", x = 70, xend = 70,
y = effect_control, yend = effect_treatment,
size = 4, color = "#FF4136") +
labs(x = "Entrance exam score", y = "Exit exam score") +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
scale_color_manual(values = c("#85144b", "#0074D9")) +
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)), color = FALSE) +
coord_cartesian(xlim = c(70 - 6, 70 + 6)) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
---
layout: true
class: title title-2
---
# Geographic discontinuities
.center[
<figure>
<img src="img/10/timezones-1.png" alt="Holbein time zones" title="Holbein time zones" width="100%">
</figure>
]
---
# Geographic discontinuities
.pull-left-wide.center[
<figure>
<img src="img/10/timezones-2.png" alt="Holbein time zones" title="Holbein time zones" width="100%">
</figure>
]
.pull-right-narrow[
.box-inv-2[Lower turnout in counties on the eastern side of the boundary]
.box-inv-2[Election schedules cause fluctuations in turnout]
]
---
# Time discontinuities
.pull-left-wide.center[
<figure>
<img src="img/10/hospitals-1.png" alt="Hospital stays title" title="Hospital stays title" width="90%">
</figure>
]
.pull-right-narrow[
.box-inv-2[California requires that insurance cover two days of post-partum hospitalization]
.box-inv-2[Does extra time in the hospital improve health outcomes?]
]
---
# Time discontinuities
.center[
<figure>
<img src="img/10/hospitals-2.png" alt="Hospital stays duration" title="Hospital stays duration" width="100%">
</figure>
]
.box-inv-2[Delivering at 12:01 AM makes you stay longer in the hospitalβ¦]
---
# Time discontinuities
.pull-left-wide.center[
<figure>
<img src="img/10/hospitals-3.png" alt="Hospital stays outcomes" title="Hospital stays outcomes" width="65%">
</figure>
]
.pull-right-narrow[
.box-inv-2[β¦but delivering at 12:01 AM has no effect on readmission rates or mortality rates]
]
---
# Test score discontinuities
.pull-left-wide.center[
<figure>
<img src="img/10/flagship-1.png" alt="Flagship universities" title="Flagship universities" width="100%">
</figure>
]
.pull-right-narrow[
.box-inv-2[Does going to the main state university (e.g. UGA) make you earn more money?]
.box-inv-2[SAT scores are an arbitrary cutoff for accessing the university]
]
---
# Test score discontinuities
.pull-left.center[
<figure>
<img src="img/10/flagship-2.png" alt="Flagship cutoff" title="Flagship cutoff" width="100%">
</figure>
.box-inv-2[Cutoff seems rule-based]
]
--
.pull-right.center[
<figure>
<img src="img/10/flagship-3.png" alt="Flagship outcome" title="Flagship outcome" width="100%">
</figure>
.box-inv-2[Earnings are slightly higher]
]
---
# RDDs are all the rage
.box-inv-2.medium[People love these things!]
--
.box-2[They're intuitive, compelling, and highly graphical]
--
.pull-left.center[
<figure>
<img src="img/10/rdd-p-hacking.png" alt="RDD p-hacking" title="RDD p-hacking" width="80%">
</figure>
]
.pull-right[
.box-2[RDD less susceptible to p-hacking and selective publication than DID or IV]
]
---
layout: false
name: lines-gaps
class: center middle section-title section-title-4 animated fadeIn
# Drawing lines<br>and measuring gaps
---
class: title title-4
# Main goal of RD
--
.box-inv-4.medium[Measure the gap in outcome for<br>people on both sides of the cutpoint]
--
.box-inv-4.medium[Gap = **Ξ΄** =<br>local average treatment effect (LATE)]
---
![](`r knitr::fig_chunk("tutoring-outcome-delta", "png")`)
---
layout: true
class: title title-4
---
# Drawing lines
--
.box-inv-4.medium[The size of the gap depends on how<br>you draw the lines on each side of the cutoff]
--
.box-inv-4.medium.sp-after[The type of lines you choose can<br>change the estimate of Ξ΄βsometimes by a lot!]
--
.box-4.medium[There's no one right way to draw lines!]
---
# Line-drawing considerations
--
.box-inv-4.medium[Parametric vs. non-parametric lines]
--
.box-inv-4.medium[Measuring the gap]
--
.box-inv-4.medium[Bandwidths]
--
.box-inv-4.medium[Kernels]
---
# Parametric lines
.box-inv-4.medium[Formulas with *parameters*]
--
.medium[
$$y = mx + b$$
$$y = \beta_0 + \beta_1 x_1 + \beta_2 x_2$$
]
---
layout: false
```{r parameters-stuff, echo=FALSE}
line_colors_df <- tribble(
~name, ~color, ~formula,
"Linear", "#0074D9", "y = Ξ²_1 x",
"Squared", "#FF851B", "y = Ξ²_1 x + Ξ²_2 x^2",
"Cubed", "#FF4136", "y = Ξ²_1 x + Ξ²_2 x^2 + Ξ²_3 x^3",
"Trigonometric", "#2ECC40", "y = Ξ²_1 x + Ξ²_2 sin(x)",
"Loess", "#B10DC9", "Loess"
)
line_colors <- line_colors_df$color %>%
set_names(line_colors_df$name)
fun_linear <- function(x) 10 + 4 * x
fun_squared <- function(x) 120 - 3 * x + 0.07 * x^2
fun_cubic <- function(x) 300 - 25 * x + 0.65 * x^2 - 0.004 * x^3
fun_trig <- function(x) 10 + 4 * x + 50 * sin(0.25 * x)
fun_trig_effect <- function(x) ifelse(x >= 75, fun_trig(x) + 200, fun_trig(x))
fun_loess <- function(x) 100 + 30 * sin(5 * x) + 0.2 * x^2 - 0.002 * x^3
set.seed(1234)
df_params <- tibble(id = 1:400) %>%
mutate(x_uniform = runif(n(), min = 0, max = 100)) %>%
mutate(y_linear = fun_linear(x_uniform) + rnorm(n(), 0, 50)) %>%
mutate(y_squared = fun_squared(x_uniform) + rnorm(n(), 0, 50)) %>%
mutate(y_cubed = fun_cubic(x_uniform) + rnorm(n(), 0, 50)) %>%
mutate(y_trig = fun_trig(x_uniform) + rnorm(n(), 0, 50)) %>%
mutate(y_loess = fun_loess(x_uniform) + rnorm(n(), 0, 50)) %>%
mutate_at(vars(starts_with("y_")),
list(effect = ~ifelse(x_uniform >= 75, . + 200, .)))
```
.small[
$$y = 10 + 4x$$
]
```{r params-plot-linear, echo=FALSE, fig.width=12, fig.height=5.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_linear)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ x,
se = FALSE, size = 3, aes(color = "Linear")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
class: title title-4
# Parametric lines
.box-inv-4.medium[Not just for straight lines!<br>Make curvy with exponents or trigonometry]
--
.medium[
$$y = \beta_0 + \beta_1 x + \beta_2 x^2 + \beta_3 x^7$$
$$y = \beta_0 + \beta_1 x + \beta_2 \sin(x)$$
]
---
.small[
$$y = 120 - 3x + 0.07x^2$$
]
```{r params-plot-square, echo=FALSE, fig.width=12, fig.height=5.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_squared)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2),
se = FALSE, size = 3, aes(color = "Squared")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
.small[
$$y = 300 - 25x + 0.65x^2 - 0.004x^3$$
]
```{r params-plot-poly, echo=FALSE, fig.width=12, fig.height=5.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_cubed)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3),
se = FALSE, size = 3, aes(color = "Cubed")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
.small[
$$y = 10 + 4x + 50 \times \sin (\frac{x}{4})$$
]
```{r params-plot-trig, echo=FALSE, fig.width=12, fig.height=5.25, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_trig)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
stat_function(fun = fun_trig, size = 3, aes(color = "Trigonometric")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
class: title title-4
# Parametric lines
--
.box-inv-4.medium.sp-after[It's important to get the parameters right!]
--
.box-inv-4.medium[Line should fit the data pretty well]
---
```{r params-plot-linear-squared, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_squared)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ x,
se = FALSE, size = 3, aes(color = "Linear")) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2),
se = FALSE, size = 3, aes(color = "Squared")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
```{r params-plot-linear-poly, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_cubed)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "lm", formula = y ~ x,
se = FALSE, size = 3, aes(color = "Linear")) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2),
se = FALSE, size = 3, aes(color = "Squared")) +
geom_smooth(method = "lm", formula = y ~ poly(x, 3),
se = FALSE, size = 3, aes(color = "Cubed")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
class: title title-4
# Nonparametric lines
--
.box-inv-4.medium[Lines without parameters]
--
.box-4[Use the data to find the best line,<br>often with windows and moving averages]
--
.box-4[<span style="color: #F6D645;">Lo</span>cally <span style="color: #F6D645;">e</span>stimated/<span style="color: #F6D645;">we</span>ighted <span style="color: #F6D645;">s</span>catterplot <span style="color: #F6D645;">s</span>moothing (LOESS/LOWESS)<br>is a common method (but not the only one!)]
---
.small[
$$y = \text{who knows?}$$
]
```{r params-plot-loess, echo=FALSE, fig.width=12, fig.height=5.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_loess)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "loess", formula = y ~ x, aes(color = "Loess"),
se = FALSE, size = 3) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
.center[
<video controls>
<source src="img/10/loess_window.mp4" type="video/mp4">
</video>
]
---
```{r params-plot-loess-lines, echo=FALSE, fig.width=12, fig.height=6.5, out.width="100%"}
ggplot(df_params, aes(x = x_uniform, y = y_loess)) +
geom_point(size = 5, pch = 21, color = "white", fill = "black", alpha = 0.5) +
geom_smooth(method = "loess", formula = y ~ x, aes(color = "Loess"),
se = FALSE, size = 3) +
geom_smooth(method = "lm", formula = y ~ x,
se = FALSE, size = 3, aes(color = "Linear")) +
geom_smooth(method = "lm", formula = y ~ poly(x, 2),
se = FALSE, size = 3, aes(color = "Squared")) +
scale_color_manual(values = line_colors, breaks = names(line_colors),
labels = map(line_colors_df$formula, TeX), name = NULL) +
labs(x = NULL, y = NULL) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed") +
theme(legend.position = "bottom") +
theme(legend.box.spacing = unit(0.5, "lines"),
legend.margin = margin(t = 0, b = 0))
```
---
layout: true
class: title title-4
---
# Measuring gap with parametric lines
.center[
<figure>
<img src="`r knitr::fig_chunk("tutoring-outcome-lines", "png")`" alt="Parametric gap" title="Parametric gap" width="85%">
</figure>
]
---
# Measuring gap with parametric lines
.box-inv-4[Easiest way: center the running variable around the threshold]
.small[
```{r center-running-var, echo=FALSE}
tutoring %>%
select(id, exit_exam, entrance_exam, entrance_centered, tutoring) %>%
mutate(across(c(exit_exam, entrance_exam, entrance_centered), ~round(., 0))) %>%
head(5) %>%
kbl(align = "c")
```
]
.small[
$$y = \beta_0 + \beta_1 \text{Running variable (centered)} + \beta_2 \text{Indicator for treatment}$$
]
---
# Measuring gap with parametric lines
.center[
<figure>
<img src="`r knitr::fig_chunk("tutoring-outcome-lines", "png")`" alt="Parametric gap" title="Parametric gap" width="35%">
</figure>
]
.left-code[
```{r simple-model}
program_data <- tutoring %>%
mutate(entrance_centered =
entrance_exam - 70)
model1 <- lm(exit_exam ~
entrance_centered + tutoring,
data = program_data)
```
]
.right-code[
```{r show-simple-model-fake, eval=FALSE}
tidy(model1)
```
```{r show-simple-model-real, echo=FALSE}
tidy(model1) %>% select(term, estimate, std.error)
```
]
---
# Measuring gap with nonparametric lines
.center[
```{r tutoring-outcome-loess, echo=FALSE, fig.width=12, fig.height=5.5, out.width="80%"}
ggplot(tutoring, aes(x = entrance_exam, y = exit_exam, fill = tutoring_text, color = tutoring_text)) +
geom_point(size = 4, pch = 21, color = "white", alpha = 0.25) +
geom_vline(xintercept = 70, size = 2, color = "#FFDC00") +
geom_smooth(data = filter(tutoring, entrance_exam <= 70),
method = "loess", size = 2, show.legend = FALSE) +
geom_smooth(data = filter(tutoring, entrance_exam > 70),
method = "loess", size = 2, show.legend = FALSE) +
labs(x = "Entrance exam score", y = "Exit exam score") +
scale_fill_manual(values = c("#85144b", "#0074D9"), name = NULL) +
scale_color_manual(values = c("#85144b", "#0074D9")) +
guides(fill = guide_legend(reverse = TRUE, override.aes = list(alpha = 1)), color = FALSE) +
theme_bw(base_size = 28, base_family = "Fira Sans Condensed")
```
]
.box-inv-4[Can't use regression; use `rdrobust` R package]
---
# Measuring gap with nonparametric lines
.center[
<figure>
<img src="`r knitr::fig_chunk("tutoring-outcome-loess", "png")`" alt="Nonparametric gap" title="Nonparametric gap" width="40%">
</figure>
]
.small-code[
```{r rdrobust, eval=FALSE}
rdrobust(y = tutoring$exit_exam, x = tutoring$entrance_exam, c = 70)
```
]
.small-code[
```{r calculate-rdrobust, echo=FALSE, results="hide"}
rd_out <- rdrobust(y = tutoring$exit_exam,
x = tutoring$entrance_exam,
c = 70)
x <- capture.output(summary(rd_out))
```
```{r show-rdrobust, echo=FALSE}
cat(x[17:22], sep = "\n")
```
]