-
Notifications
You must be signed in to change notification settings - Fork 6
/
Estimation_of_Causal_Effects.tex
765 lines (610 loc) · 30.1 KB
/
Estimation_of_Causal_Effects.tex
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
\RequirePackage[l2tabu, orthodox]{nag} % warn about outdated packages
\documentclass[11pt,leqno]{article}\usepackage[]{graphicx}\usepackage[]{color}
%% maxwidth is the original width if it is less than linewidth
%% otherwise use linewidth (to make sure the graphics do not exceed the margin)
\makeatletter
\def\maxwidth{ %
\ifdim\Gin@nat@width>\linewidth
\linewidth
\else
\Gin@nat@width
\fi
}
\makeatother
\definecolor{fgcolor}{rgb}{0.345, 0.345, 0.345}
\newcommand{\hlnum}[1]{\textcolor[rgb]{0.686,0.059,0.569}{#1}}%
\newcommand{\hlstr}[1]{\textcolor[rgb]{0.192,0.494,0.8}{#1}}%
\newcommand{\hlcom}[1]{\textcolor[rgb]{0.678,0.584,0.686}{\textit{#1}}}%
\newcommand{\hlopt}[1]{\textcolor[rgb]{0,0,0}{#1}}%
\newcommand{\hlstd}[1]{\textcolor[rgb]{0.345,0.345,0.345}{#1}}%
\newcommand{\hlkwa}[1]{\textcolor[rgb]{0.161,0.373,0.58}{\textbf{#1}}}%
\newcommand{\hlkwb}[1]{\textcolor[rgb]{0.69,0.353,0.396}{#1}}%
\newcommand{\hlkwc}[1]{\textcolor[rgb]{0.333,0.667,0.333}{#1}}%
\newcommand{\hlkwd}[1]{\textcolor[rgb]{0.737,0.353,0.396}{\textbf{#1}}}%
\usepackage{framed}
\makeatletter
\newenvironment{kframe}{%
\def\at@end@of@kframe{}%
\ifinner\ifhmode%
\def\at@end@of@kframe{\end{minipage}}%
\begin{minipage}{\columnwidth}%
\fi\fi%
\def\FrameCommand##1{\hskip\@totalleftmargin \hskip-\fboxsep
\colorbox{shadecolor}{##1}\hskip-\fboxsep
% There is no \\@totalrightmargin, so:
\hskip-\linewidth \hskip-\@totalleftmargin \hskip\columnwidth}%
\MakeFramed {\advance\hsize-\width
\@totalleftmargin\z@ \linewidth\hsize
\@setminipage}}%
{\par\unskip\endMakeFramed%
\at@end@of@kframe}
\makeatother
\definecolor{shadecolor}{rgb}{.97, .97, .97}
\definecolor{messagecolor}{rgb}{0, 0, 0}
\definecolor{warningcolor}{rgb}{1, 0, 1}
\definecolor{errorcolor}{rgb}{1, 0, 0}
\newenvironment{knitrout}{}{} % an empty environment to be redefined in TeX
\usepackage{alltt}
\usepackage{microtype} %
\usepackage{setspace}
\onehalfspacing
\usepackage{xcolor, color, ucs} % http://ctan.org/pkg/xcolor
\usepackage{natbib}
\usepackage{booktabs} % package for thick lines in tables
\usepackage{amsfonts,amsthm,amsmath,amssymb} % AMS Stuff
\usepackage[linewidth=1pt]{mdframed}
\usepackage{mdframed}
\usepackage{empheq} % To use left brace on {align} environment
\usepackage{graphicx} % Insert .pdf, .eps or .png
\usepackage{enumitem} % http://ctan.org/pkg/enumitem
\usepackage[mathscr]{euscript} % Font for right expectation sign
\usepackage{tabularx} % Get scale boxes for tables
\usepackage{float} % Force floats around
\usepackage{afterpage}% http://ctan.org/pkg/afterpage
\usepackage[T1]{fontenc}
\usepackage{rotating} % Rotate long tables horizontally
\usepackage{bbm} % for bold betas
\usepackage{csquotes} % \enquote{} and \textquote[][]{} environments
\usepackage{subfig}
\usepackage{titling} % modify maketitle in latex
% \usepackage{mathtools} % multlined environment with size option
\usepackage{verbatim}
\usepackage{geometry}
\usepackage{bigfoot}
\usepackage[format=hang,
font={small},
labelfont=bf,
textfont=rm]{caption}
\geometry{verbose,margin=2cm,nomarginpar}
\setcounter{secnumdepth}{2}
\setcounter{tocdepth}{2}
\usepackage{url}
\usepackage{relsize} % \mathlarger{} environment
\usepackage[unicode=true,
pdfusetitle,
bookmarks=true,
bookmarksnumbered=true,
bookmarksopen=true,
bookmarksopenlevel=2,
breaklinks=false,
pdfborder={0 0 1},
backref=page,
colorlinks=true,
hyperfootnotes=true,
hypertexnames=false,
pdfstartview={XYZ null null 1},
citecolor=blue!70!black,
linkcolor=red!70!black,
urlcolor=green!70!black]{hyperref}
\usepackage{hypernat}
\usepackage{multirow}
\usepackage[noabbrev]{cleveref} % Should be loaded after \usepackage{hyperref}
\parskip=12pt
\parindent=0pt
\delimitershortfall=-1pt
\interfootnotelinepenalty=100000
\makeatletter
\def\thm@space@setup{\thm@preskip=0pt
\thm@postskip=0pt}
\makeatother
\makeatletter
% align all math after the command
\newcommand{\mathleft}{\@fleqntrue\@mathmargin\parindent}
\newcommand{\mathcenter}{\@fleqnfalse}
% tilde with text over it
\newcommand{\distas}[1]{\mathbin{\overset{#1}{\kern\z@\sim}}}%
\newsavebox{\mybox}\newsavebox{\mysim}
\newcommand{\distras}[1]{%
\savebox{\mybox}{\hbox{\kern3pt$\scriptstyle#1$\kern3pt}}%
\savebox{\mysim}{\hbox{$\sim$}}%
\mathbin{\overset{#1}{\kern\z@\resizebox{\wd\mybox}{\ht\mysim}{$\sim$}}}%
}
\makeatother
\newtheoremstyle{newstyle}
{12pt} %Aboveskip
{12pt} %Below skip
{\itshape} %Body font e.g.\mdseries,\bfseries,\scshape,\itshape
{} %Indent
{\bfseries} %Head font e.g.\bfseries,\scshape,\itshape
{.} %Punctuation afer theorem header
{ } %Space after theorem header
{} %Heading
\theoremstyle{newstyle}
\newtheorem{thm}{Theorem}
\newtheorem{prop}[thm]{Proposition}
\newtheorem{lem}{Lemma}
\newtheorem{cor}{Corollary}
\newcommand*\diff{\mathop{}\!\mathrm{d}}
\newcommand*\Diff[1]{\mathop{}\!\mathrm{d^#1}}
\newcommand*{\QEDA}{\hfill\ensuremath{\blacksquare}}%
\newcommand*{\QEDB}{\hfill\ensuremath{\square}}%
\DeclareMathOperator{\E}{\mathbb{E}}
\DeclareMathOperator{\Var}{\rm{Var}}
\DeclareMathOperator{\Cov}{\rm{Cov}}
% \DeclareMathOperator{\Pr}{\rm{Pr}}
% COLORS FOR GRAPHICS (3-class Set1)
\definecolor{Blue}{RGB}{55,126,184}
\definecolor{Red}{RGB}{228,26,28}
\definecolor{Green}{RGB}{77,175,74}
% COLORS FOR EQUATIONS (3-class Dark2)
\definecolor{eqgreen}{RGB}{27,158,119}
\definecolor{eqblue}{RGB}{117,112,179}
\definecolor{eqred}{RGB}{217,95,2}
\title{Estimation of Causal Effects}
\author{Jake Bowers, Ben Hansen \& Tom Leavitt}
\date{\today}
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\begin{document}
\maketitle
\tableofcontents
\newpage
\section{Estimation of Mean Unit-Level Causal Effects}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Load data}
\hlstd{news_df} \hlkwb{<-} \hlkwd{read.csv}\hlstd{(}\hlstr{"http://jakebowers.org/PS531Data/news.df.csv"}\hlstd{)}
\hlcom{## Create potential outcomes}
\hlstd{news_df} \hlopt{%<>%} \hlkwd{rename}\hlstd{(}\hlkwc{y} \hlstd{= r)} \hlopt{%>%} \hlkwd{mutate}\hlstd{(}\hlkwc{yc} \hlstd{=} \hlkwd{ifelse}\hlstd{(}\hlkwc{test} \hlstd{= z} \hlopt{==} \hlnum{0}\hlstd{,} \hlkwc{yes} \hlstd{= y,} \hlkwc{no} \hlstd{=} \hlnum{NA}\hlstd{),} \hlkwc{yt} \hlstd{=} \hlkwd{ifelse}\hlstd{(}\hlkwc{test} \hlstd{= z} \hlopt{==}
\hlnum{1}\hlstd{,} \hlkwc{yes} \hlstd{= y,} \hlkwc{no} \hlstd{=} \hlnum{NA}\hlstd{))}
\hlkwd{kable}\hlstd{(news_df[,} \hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{3}\hlopt{:}\hlnum{4}\hlstd{,} \hlnum{8}\hlopt{:}\hlnum{9}\hlstd{,} \hlnum{7}\hlstd{)])}
\end{alltt}
\end{kframe}
\begin{tabular}{l|r|r|r|r|r}
\hline
city & y & z & yc & yt & s\\
\hline
Saginaw & 16 & 0 & 16 & NA & 1\\
\hline
Sioux City & 22 & 1 & NA & 22 & 1\\
\hline
Battle Creek & 14 & 0 & 14 & NA & 2\\
\hline
Midland & 7 & 1 & NA & 7 & 2\\
\hline
Oxford & 23 & 0 & 23 & NA & 3\\
\hline
Lowell & 27 & 1 & NA & 27 & 3\\
\hline
Yakima & 58 & 0 & 58 & NA & 4\\
\hline
Richland & 61 & 1 & NA & 61 & 4\\
\hline
\end{tabular}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Now let's imagine the \textit{true} unit-level treatment effect is some specific positive natural number that differs for almost every unit. What do we mean by ``\textit{true} unit-level treatment effect'' as opposed to a unit-level treatment effect that we hypothesize and subsequently assume to be true in order to assess evidence in favor of or against that hypothesis?
\end{itemize}
\end{mdframed}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{news_df} \hlopt{%<>%} \hlkwd{mutate}\hlstd{(}\hlkwc{true_tau} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{6}\hlstd{,} \hlnum{4}\hlstd{,} \hlnum{19}\hlstd{,} \hlnum{3}\hlstd{,} \hlnum{9}\hlstd{,} \hlnum{9}\hlstd{,} \hlnum{13}\hlstd{,} \hlnum{15}\hlstd{),} \hlkwc{yc} \hlstd{=} \hlkwd{ifelse}\hlstd{(}\hlkwc{test} \hlstd{=} \hlkwd{is.na}\hlstd{(yc),} \hlkwc{yes} \hlstd{= yt} \hlopt{-}
\hlstd{true_tau,} \hlkwc{no} \hlstd{= yc),} \hlkwc{yt} \hlstd{=} \hlkwd{ifelse}\hlstd{(}\hlkwc{test} \hlstd{=} \hlkwd{is.na}\hlstd{(yt),} \hlkwc{yes} \hlstd{= yc} \hlopt{+} \hlstd{true_tau,} \hlkwc{no} \hlstd{= yt))}
\hlcom{## Other ways to get the true ATE if you knew the true individual level effects.}
\hlcom{## true_ate<-with(news_df,mean(yt) - mean(yc)) true_ate<-news_df %$% \{mean(yt) - mean(yc)\}}
\hlstd{true_ate} \hlkwb{<-} \hlstd{news_df} \hlopt{%$%} \hlkwd{mean}\hlstd{(true_tau)}
\hlstd{true_ate}
\end{alltt}
\begin{verbatim}
[1] 9.75
\end{verbatim}
\begin{alltt}
\hlkwd{kable}\hlstd{(news_df[,} \hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{3}\hlopt{:}\hlnum{4}\hlstd{,} \hlnum{8}\hlopt{:}\hlnum{9}\hlstd{,} \hlnum{7}\hlstd{)])}
\end{alltt}
\end{kframe}
\begin{tabular}{l|r|r|r|r|r}
\hline
city & y & z & yc & yt & s\\
\hline
Saginaw & 16 & 0 & 16 & 22 & 1\\
\hline
Sioux City & 22 & 1 & 18 & 22 & 1\\
\hline
Battle Creek & 14 & 0 & 14 & 33 & 2\\
\hline
Midland & 7 & 1 & 4 & 7 & 2\\
\hline
Oxford & 23 & 0 & 23 & 32 & 3\\
\hline
Lowell & 27 & 1 & 18 & 27 & 3\\
\hline
Yakima & 58 & 0 & 58 & 71 & 4\\
\hline
Richland & 61 & 1 & 46 & 61 & 4\\
\hline
\end{tabular}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item I claim that the difference-in-means estimator and the stratified difference-in-means estimator are both good estimators. Name some criteria that people use to assess the quality of estimators . . .
\end{itemize}
\end{mdframed}
In the code chunk below, we create a function that calculates $\widehat{ATE}$ on the outcomes we observe under different treatment assignment permutations.
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{treatment_permutations} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{z}\hlstd{,} \hlkwc{yc}\hlstd{,} \hlkwc{yt}\hlstd{,} \hlkwc{s}\hlstd{) \{}
\hlkwa{if} \hlstd{(}\hlkwd{missing}\hlstd{(s)) \{}
\hlcom{## If this is not a block-randomized experiment then do the following}
\hlcom{## Permute treatment assignment Student Question: What is the difference between Z and z? Why is one}
\hlcom{## uppercase and the other lower?}
\hlstd{Z} \hlkwb{=} \hlkwd{sample}\hlstd{(z)}
\hlstd{Y} \hlkwb{=} \hlstd{Z} \hlopt{*} \hlstd{yt} \hlopt{+} \hlstd{(}\hlnum{1} \hlopt{-} \hlstd{Z)} \hlopt{*} \hlstd{yc}
\hlcom{## Calculate unstratified test statistic}
\hlstd{ate_hat_unstrat} \hlkwb{=} \hlkwd{coef}\hlstd{(}\hlkwd{lm}\hlstd{(Y} \hlopt{~} \hlstd{Z))[[}\hlstr{"Z"}\hlstd{]]}
\hlkwd{return}\hlstd{(ate_hat_unstrat)}
\hlstd{\}} \hlkwa{else} \hlstd{\{}
\hlcom{# If the experiment is block randomized}
\hlcom{## Permute treatment assignment WITHIN blocks}
\hlstd{Z} \hlkwb{=} \hlkwd{unsplit}\hlstd{(}\hlkwd{lapply}\hlstd{(}\hlkwd{split}\hlstd{(}\hlkwc{x} \hlstd{= z,} \hlkwc{f} \hlstd{= s), sample), s)}
\hlstd{Y} \hlkwb{=} \hlstd{Z} \hlopt{*} \hlstd{yt} \hlopt{+} \hlstd{(}\hlnum{1} \hlopt{-} \hlstd{Z)} \hlopt{*} \hlstd{yc}
\hlcom{## Calculate stratified test-statistic}
\hlstd{ate_hat_strat} \hlkwb{=} \hlkwd{coef}\hlstd{(}\hlkwd{lm}\hlstd{(Y} \hlopt{~} \hlstd{Z} \hlopt{+} \hlstd{s))[[}\hlstr{"Z"}\hlstd{]]}
\hlkwd{return}\hlstd{(ate_hat_strat)}
\hlstd{\}}
\hlstd{\}}
\end{alltt}
\end{kframe}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Describe in words what the function above is doing.
\end{itemize}
\end{mdframed}
Now we are repeatedly estimating the ATE under 1000 treatment assignment permutations in order to assess unbiasedness.
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Which two quantities would we like to compare in order to assess whether or not the difference-in-means estimator is unbiased?
\end{itemize}
\end{mdframed}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Set seed for simulations}
\hlkwd{set.seed}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{5}\hlstd{)}
\hlcom{## If we use blocks}
\hlstd{block_randomization_distribution} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{ate} \hlstd{=} \hlkwd{replicate}\hlstd{(}\hlnum{10}\hlopt{^}\hlnum{3}\hlstd{,} \hlkwd{treatment_permutations}\hlstd{(}\hlkwc{z} \hlstd{= news_df}\hlopt{$}\hlstd{z,}
\hlkwc{yc} \hlstd{= news_df}\hlopt{$}\hlstd{yc,} \hlkwc{yt} \hlstd{= news_df}\hlopt{$}\hlstd{yt,} \hlkwc{s} \hlstd{= news_df}\hlopt{$}\hlstd{s)))}
\hlkwd{colMeans}\hlstd{(block_randomization_distribution)}
\end{alltt}
\begin{verbatim}
ate
9.6
\end{verbatim}
\begin{alltt}
\hlcom{## If we don't use blocks}
\hlstd{randomization_distribution} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{ate} \hlstd{=} \hlkwd{replicate}\hlstd{(}\hlnum{10}\hlopt{^}\hlnum{3}\hlstd{,} \hlkwd{treatment_permutations}\hlstd{(}\hlkwc{z} \hlstd{= news_df}\hlopt{$}\hlstd{z,}
\hlkwc{yc} \hlstd{= news_df}\hlopt{$}\hlstd{yc,} \hlkwc{yt} \hlstd{= news_df}\hlopt{$}\hlstd{yt)))}
\hlkwd{colMeans}\hlstd{(randomization_distribution)}
\end{alltt}
\begin{verbatim}
ate
9.53
\end{verbatim}
\begin{alltt}
\hlstd{block_randomization_distribution}\hlopt{$}\hlstd{type} \hlkwb{<-} \hlstr{"Stratified"}
\hlstd{randomization_distribution}\hlopt{$}\hlstd{type} \hlkwb{<-} \hlstr{"Unstratified"}
\hlstd{randomization_dists} \hlkwb{<-} \hlkwd{as.data.frame}\hlstd{(}\hlkwd{rbind}\hlstd{(block_randomization_distribution, randomization_distribution))}
\hlkwd{ggplot}\hlstd{(randomization_dists,} \hlkwd{aes}\hlstd{(}\hlkwc{x} \hlstd{= ate,} \hlkwc{fill} \hlstd{= type))} \hlopt{+} \hlkwd{geom_histogram}\hlstd{(}\hlkwc{alpha} \hlstd{=} \hlnum{0.5}\hlstd{,} \hlkwd{aes}\hlstd{(),} \hlkwc{position} \hlstd{=} \hlstr{"identity"}\hlstd{)} \hlopt{+}
\hlkwd{xlab}\hlstd{(}\hlstr{"Estimated ATEs"}\hlstd{)} \hlopt{+} \hlkwd{ylab}\hlstd{(}\hlstr{"Count"}\hlstd{)} \hlopt{+} \hlkwd{ggtitle}\hlstd{(}\hlstr{"Randomization Distributions"}\hlstd{)} \hlopt{+} \hlkwd{scale_fill_discrete}\hlstd{(}\hlkwc{name} \hlstd{=} \hlstr{"Random \textbackslash{}nAssignment"}\hlstd{)}
\end{alltt}
\end{kframe}
\includegraphics[width=1\textwidth]{figs/figunnamed-chunk-4-1}
\begin{kframe}\begin{alltt}
\hlcom{## If you want to add the observed ate hat + geom_vline(xintercept = obs_ate_hat, colour = 'black',}
\hlcom{## linetype = 'longdash')}
\end{alltt}
\end{kframe}
\end{knitrout}
Notice that the randomization distributions look a little strange; that is, both randomization distributions are very different from the normal distribution to which we are accustomed. This is important. Nothing so far has invoked any claim about the shape of the randomization distribution, such as its normality if the finite population central limit theorem obtains \citep{hajek1960}. What matters (in terms of unbiasedness) is whether the mean of the randomization distribution---i.e., the mean of all the estimated
difference in means---is equal to the true average treatment effect.\footnote{Notice that we do not have simulated results that are \emph{exactly} equal to the truth. Why is that? What might we do to move them closer to the truth? If I ran this simulation with one seed, and you ran it with another seed, by how much would we expect our two averages to differ?}
\vspace{5mm}
\begin{mdframed}
\textbf{Questions for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Is the variance of the randomization distribution tighter when treatment is assigned within blocks or when treatment is \textit{not} assigned within blocks? And why?
\item Our \textit{single} $\widehat{ATE}$ from the experiment is our ``best guess'' about the true ATE. But what do we mean by ``best guess''? And can one's ``best guess'' be misleading?
\end{itemize}
\end{mdframed}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## Let's calculate out single observed test statistic that accounts for blocks}
\hlstd{obs_block_ate_hat} \hlkwb{<-} \hlkwd{coef}\hlstd{(}\hlkwd{lm}\hlstd{(y} \hlopt{~} \hlstd{z} \hlopt{+} \hlstd{s,} \hlkwc{data} \hlstd{= news_df))[[}\hlstr{"z"}\hlstd{]]}
\hlcom{## This is our SINGLE estimated ATE that we actually observe}
\hlstd{obs_block_ate_hat}
\end{alltt}
\begin{verbatim}
[1] 1.5
\end{verbatim}
\begin{alltt}
\hlcom{## This is the expected value of the estimated ATE}
\hlkwd{mean}\hlstd{(block_randomization_distribution}\hlopt{$}\hlstd{ate)}
\end{alltt}
\begin{verbatim}
[1] 9.6
\end{verbatim}
\begin{alltt}
\hlcom{## This is the true ATE (not estimated) that we are trying to estimate}
\hlstd{true_ate}
\end{alltt}
\begin{verbatim}
[1] 9.75
\end{verbatim}
\end{kframe}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item How would you relate this \texttt{[R]} exercise to the analytic proof we did in class?
\end{itemize}
\end{mdframed}
\section{Binary Outcomes}
Now let's pretend that we have an experiment with a binary outcome:
\citet{freedman2008b} famously argues that ``randomization does not justify logistic regression." Let's recall that the coefficient of a logistic regression model is the difference in potential log odds.
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{experiment} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwc{unit} \hlstd{=} \hlkwd{seq}\hlstd{(}\hlkwc{from} \hlstd{=} \hlnum{1}\hlstd{,} \hlkwc{to} \hlstd{=} \hlnum{8}\hlstd{,} \hlkwc{by} \hlstd{=} \hlnum{1}\hlstd{))}
\hlstd{experiment} \hlopt{%<>%} \hlkwd{mutate}\hlstd{(}\hlkwc{Z} \hlstd{=} \hlkwd{complete_ra}\hlstd{(}\hlkwc{N} \hlstd{=} \hlkwd{length}\hlstd{(unit)))} \hlopt{%>%} \hlkwd{arrange}\hlstd{(}\hlkwd{desc}\hlstd{(Z))}
\hlstd{experiment} \hlopt{%<>%} \hlkwd{mutate}\hlstd{(}\hlkwc{y1} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{1}\hlstd{),} \hlkwc{y0} \hlstd{=} \hlkwd{c}\hlstd{(}\hlnum{1}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{0}\hlstd{,} \hlnum{1}\hlstd{,} \hlnum{0}\hlstd{),} \hlkwc{Y} \hlstd{= Z} \hlopt{*} \hlstd{y1} \hlopt{+} \hlstd{(}\hlnum{1} \hlopt{-}
\hlstd{Z)} \hlopt{*} \hlstd{y0)}
\hlstd{N} \hlkwb{<-} \hlkwd{length}\hlstd{(experiment}\hlopt{$}\hlstd{Z)}
\hlstd{Z} \hlkwb{<-} \hlstd{experiment}\hlopt{$}\hlstd{Z}
\hlstd{m} \hlkwb{<-} \hlkwd{sum}\hlstd{(experiment}\hlopt{$}\hlstd{Z} \hlopt{==} \hlnum{1}\hlstd{)}
\hlstd{y1} \hlkwb{<-} \hlstd{experiment}\hlopt{$}\hlstd{y1}
\hlstd{y0} \hlkwb{<-} \hlstd{experiment}\hlopt{$}\hlstd{y0}
\hlstd{Y} \hlkwb{<-} \hlstd{experiment}\hlopt{$}\hlstd{Y}
\hlstd{true_ate_binary} \hlkwb{<-} \hlkwd{mean}\hlstd{(y1)} \hlopt{-} \hlkwd{mean}\hlstd{(y0)}
\hlstd{true_ate_binary}
\end{alltt}
\begin{verbatim}
[1] 0.125
\end{verbatim}
\begin{alltt}
\hlcom{## Log odds}
\hlstd{true_logit} \hlkwb{<-} \hlkwd{log}\hlstd{(}\hlkwd{mean}\hlstd{(y1)}\hlopt{/}\hlstd{(}\hlnum{1} \hlopt{-} \hlkwd{mean}\hlstd{(y1)))} \hlopt{-} \hlkwd{log}\hlstd{(}\hlkwd{mean}\hlstd{(y0)}\hlopt{/}\hlstd{(}\hlnum{1} \hlopt{-} \hlkwd{mean}\hlstd{(y0)))}
\hlstd{true_logit}
\end{alltt}
\begin{verbatim}
[1] 0.511
\end{verbatim}
\begin{alltt}
\hlstd{est_ate} \hlkwb{<-} \hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{1}\hlstd{])} \hlopt{-} \hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{0}\hlstd{])}
\hlstd{est_logit} \hlkwb{<-} \hlkwd{log}\hlstd{(}\hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{1}\hlstd{])}\hlopt{/}\hlstd{(}\hlnum{1} \hlopt{-} \hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{1}\hlstd{])))} \hlopt{-} \hlkwd{log}\hlstd{(}\hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{0}\hlstd{])}\hlopt{/}\hlstd{(}\hlnum{1} \hlopt{-} \hlkwd{mean}\hlstd{(Y[Z} \hlopt{==} \hlnum{0}\hlstd{])))}
\hlkwd{all.equal}\hlstd{(est_logit,} \hlkwd{coef}\hlstd{(}\hlkwd{glm}\hlstd{(Y} \hlopt{~} \hlstd{Z,} \hlkwc{family} \hlstd{=} \hlkwd{binomial}\hlstd{(}\hlkwc{link} \hlstd{=} \hlstr{"logit"}\hlstd{)))[[}\hlstr{"Z"}\hlstd{]])}
\end{alltt}
\begin{verbatim}
[1] TRUE
\end{verbatim}
\begin{alltt}
\hlstd{new_experiment} \hlkwb{<-} \hlkwa{function}\hlstd{(}\hlkwc{z}\hlstd{,} \hlkwc{y0}\hlstd{,} \hlkwc{y1}\hlstd{) \{}
\hlstd{Z} \hlkwb{=} \hlkwd{sample}\hlstd{(z)}
\hlstd{Y} \hlkwb{=} \hlstd{Z} \hlopt{*} \hlstd{y1} \hlopt{+} \hlstd{(}\hlnum{1} \hlopt{-} \hlstd{Z)} \hlopt{*} \hlstd{y0}
\hlstd{est_ate} \hlkwb{=} \hlkwd{coef}\hlstd{(}\hlkwd{lm}\hlstd{(Y} \hlopt{~} \hlstd{Z))[[}\hlstr{"Z"}\hlstd{]]}
\hlstd{est_logit} \hlkwb{=} \hlkwd{coef}\hlstd{(}\hlkwd{glm}\hlstd{(Y} \hlopt{~} \hlstd{Z,} \hlkwc{family} \hlstd{=} \hlkwd{binomial}\hlstd{(}\hlkwc{link} \hlstd{=} \hlstr{"logit"}\hlstd{)))[[}\hlstr{"Z"}\hlstd{]]}
\hlkwd{return}\hlstd{(}\hlkwd{c}\hlstd{(est_ate, est_logit))}
\hlstd{\}}
\hlkwd{set.seed}\hlstd{(}\hlnum{1}\hlopt{:}\hlnum{5}\hlstd{)}
\hlstd{randomization_dists} \hlkwb{<-} \hlkwd{data.frame}\hlstd{(}\hlkwd{t}\hlstd{(}\hlkwd{replicate}\hlstd{(}\hlnum{10}\hlopt{^}\hlnum{3}\hlstd{,} \hlkwd{new_experiment}\hlstd{(}\hlkwc{z} \hlstd{= Z,} \hlkwc{y0} \hlstd{= y0,} \hlkwc{y1} \hlstd{= y1))))} \hlopt{%>%} \hlkwd{rename}\hlstd{(}\hlkwc{est_ates} \hlstd{= X1,}
\hlkwc{est_logits} \hlstd{= X2)}
\hlkwd{colMeans}\hlstd{(randomization_dists)}
\end{alltt}
\begin{verbatim}
est_ates est_logits
0.134 1.943
\end{verbatim}
\end{kframe}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Questions for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Is the difference-in-means estimator still unbiased (give or take some simulation error) when potential outcomes are binary? Is the coefficient from a logistic regression model unbiased?
\item To which quantities (defined as \texttt{[R]} objects above) are we comparing the means of the two respective randomization distributions in order to assess unbiasedness?
\item How does the randomization distribution above differ from the \textit{null} randomization distribution?
\end{itemize}
\end{mdframed}
\vspace{5mm}
\begin{mdframed}
\textbf{Exercise for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item In a code chunk below, plot both randomization distributions (of the estimated ATEs and estimated logit coefficients) in \texttt{[R]}:
\end{itemize}
\end{mdframed}
\section{Application of Unbiased Inference to Matched Designs}
\subsection{Make Your Own Matched Design}
\vspace{5mm}
\begin{mdframed}
\textbf{Exercise for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Please create a matched design with a matched design with the data from the
\citet{cerdaetal2012} paper. We made one of our own, that we call, `fm1,' but
you should make your own.
\end{itemize}
\end{mdframed}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## This our data with the matched sets that you DON'T have access to}
\hlkwd{load}\hlstd{(}\hlstr{"matched_data.Rdata"}\hlstd{)}
\end{alltt}
\end{kframe}
\end{knitrout}
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlcom{## To load the meddat data and then make your own matched design}
\hlkwd{load}\hlstd{(}\hlkwd{url}\hlstd{(}\hlstr{"http://jakebowers.org/Matching/meddat.rda"}\hlstd{))}
\end{alltt}
\end{kframe}
\end{knitrout}
\subsection{Perform Outcome Analysis After Matching}
Now let's think about how we can draw upon the principles above in order to actually do outcome analysis once we have a matched design.
We want to perform outcome analysis (in this case to estimate average treatment effects) as if we have a \textit{block} randomized experiment in which the matched sets are the experiment's blocks. We therefore want to estimate an average treatment effect \textit{within} each block. But then we need some scheme to weight each block-specific $\widehat{ATE}$ to yield one overall $\widehat{ATE}$. There are three different weighting possibilities below.
\subsubsection{Harmonic Mean Weighting}
Let's first estimate the average treatment effect \textit{within} each block:
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{blocks} \hlkwb{<-} \hlkwd{unique}\hlstd{(meddat}\hlopt{$}\hlstd{fm1)}
\hlstd{ATE_hat_by_block} \hlkwb{<-} \hlkwd{sapply}\hlstd{(blocks,} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{) \{}
\hlkwd{coef}\hlstd{(}\hlkwd{lm}\hlstd{(HomRate08} \hlopt{~} \hlstd{nhTrt,} \hlkwc{data} \hlstd{= meddat,} \hlkwc{subset} \hlstd{= fm1} \hlopt{==} \hlstd{x))[[}\hlstr{"nhTrt"}\hlstd{]]}
\hlstd{\})}
\hlstd{ATE_hat_by_block}
\end{alltt}
\begin{verbatim}
[1] 0.5269 -0.2250 -0.1704 -1.8248 -0.1292 0.0016 0.3156 0.1431 -0.7949 -0.3351 -1.6912 -0.4539
[13] -0.6041 -0.1776 0.0572 -0.8062 0.2695 1.6010 -0.5905 -0.0246
\end{verbatim}
\end{kframe}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Question for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Describe what the output above is giving us . . .
\end{itemize}
\end{mdframed}
Now let's calculate an overall $\widehat{ATE}$ that uses harmonic mean weighting. Harmonic mean weighting weights each block by $\frac{ \frac{2}{n_{st}^{-1} + n_{sc}^{-1}} }{\sum \limits_{s = 1}^{S} \frac{2}{n_{st}^{-1} + n_{sc}^{-1}} }$.
\vspace{5mm}
\begin{mdframed}
\textbf{Question and Exercise for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Why is there a $2$ in the numerator?
\item Pick a block with more than $2$ units and calculate its ``effective sample size'' by hand using the appropriate formula---i.e., $\frac{2}{n_{ts}^{-1} + n_{cs}^{-1}}$.
\end{itemize}
\end{mdframed}
Now let's estimate an overall ATE using harmonic mean weights:
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{num_treated} \hlkwb{<-} \hlstd{meddat} \hlopt{%$%} \hlstd{\{}
\hlkwd{sapply}\hlstd{(}\hlkwd{split}\hlstd{(nhTrt, fm1), sum)}
\hlstd{\}}
\hlstd{num_control} \hlkwb{<-} \hlstd{meddat} \hlopt{%$%} \hlstd{\{}
\hlkwd{sapply}\hlstd{(}\hlkwd{split}\hlstd{(}\hlnum{1} \hlopt{-} \hlstd{nhTrt, fm1), sum)}
\hlstd{\}}
\hlstd{harm_mean} \hlkwb{<-} \hlnum{2}\hlopt{/}\hlstd{(}\hlnum{1}\hlopt{/}\hlstd{num_treated} \hlopt{+} \hlnum{1}\hlopt{/}\hlstd{num_control)}
\hlstd{obs_overall_ate_hat_hm} \hlkwb{<-} \hlkwd{sum}\hlstd{(ATE_hat_by_block} \hlopt{*} \hlstd{(harm_mean}\hlopt{/}\hlkwd{sum}\hlstd{(harm_mean)))}
\end{alltt}
\end{kframe}
\end{knitrout}
What are some other weighting schemes one could use? Let's try two others.
\subsubsection{Block Size Weighting}
\vspace{5mm}
\begin{mdframed}
\textbf{Exercise for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Write a mathematical expression for the weights attached to each block-specific $\widehat{ATE}$ in which each block-specific $\widehat{ATE}$ is weighted by the total number of units in each block.
\end{itemize}
\end{mdframed}
Now let's implement this estimation strategy in \texttt{[R]}:
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{num_units_per_block} \hlkwb{<-} \hlkwd{sapply}\hlstd{(blocks,} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{) \{}
\hlkwd{length}\hlstd{(meddat}\hlopt{$}\hlstd{nhTrt[meddat}\hlopt{$}\hlstd{fm1} \hlopt{==} \hlstd{x])}
\hlstd{\})}
\hlstd{obs_overall_ate_hat_bs} \hlkwb{<-} \hlkwd{sum}\hlstd{(ATE_hat_by_block} \hlopt{*} \hlstd{(num_units_per_block}\hlopt{/}\hlkwd{nrow}\hlstd{(meddat)))}
\end{alltt}
\end{kframe}
\end{knitrout}
\subsubsection{ETT Weighting}
Let's try one final weighting scheme (although there are surely many other possibilities.) Let's weight each block-specific $\widehat{ATE}$ by the number of \textit{treated} units within that block.
That is, the weights associated with each block-specific $\widehat{ATE}$ are: $W_{ETT} = \frac{n_{st}}{\sum \limits_{s = 1}^S n_{st}}$.
\begin{knitrout}\footnotesize
\definecolor{shadecolor}{rgb}{0.969, 0.969, 0.969}\color{fgcolor}\begin{kframe}
\begin{alltt}
\hlstd{num_treated_units_per_block} \hlkwb{<-} \hlkwd{sapply}\hlstd{(blocks,} \hlkwa{function}\hlstd{(}\hlkwc{x}\hlstd{) \{}
\hlkwd{sum}\hlstd{(meddat}\hlopt{$}\hlstd{nhTrt[meddat}\hlopt{$}\hlstd{fm1} \hlopt{==} \hlstd{x])}
\hlstd{\})}
\hlstd{obs_overall_ate_hat_ett} \hlkwb{<-} \hlkwd{sum}\hlstd{(ATE_hat_by_block} \hlopt{*} \hlstd{(num_treated_units_per_block}\hlopt{/}\hlkwd{sum}\hlstd{(meddat}\hlopt{$}\hlstd{nhTrt)))}
\end{alltt}
\end{kframe}
\end{knitrout}
\vspace{5mm}
\begin{mdframed}
\textbf{Questions for Students:}
\vspace{-5mm}
\begin{itemize}\itemsep1pt
\item Now, compare all the overall estimates of the ATE from the $3$ different weighting schemes. Are they all similar?
\item Notice that thus far we have not talked about standard errors, confidence intervals, or hypothesis testing. We have focused on only \textit{estimating} causal effects. How would you describe difference between \textit{estimation} and \textit{testing}?
\end{itemize}
\end{mdframed}
\section{Agenda for Tomorrow}
Tomorrow we will discuss standard errors and \textit{testing} hypotheses about causal effects.
\section{Recommended Readings}
\subsection{Readings on Weighting Schemes}
\subsubsection{Harmonic Mean Weighting}
\begin{itemize}
\item \citet[Section 9.3.3]{hansen2011}
\item \citet[Section 2.1]{hansenbowers2008}
\end{itemize}
\subsubsection{Block-Size Weights}
\begin{itemize}
\item \citet[Section 3.6.1]{gerbergreen2012}
\end{itemize}
\subsubsection{ATT Weighting}
\begin{itemize}
\item
\url{http://egap.org/methods-guides/10-types-treatment-effect-you-should-know-about}
\end{itemize}
\subsection{Readings on Standard Errors for Tomorrow's Class}
\begin{itemize}
\item \citet[Chapter 3]{gerbergreen2012}
\item \citet[Chapter 6]{dunning2012}
\item \citet[Chapter 33, footnote 11]{fpp2007}
\item \citet{lin2013}
\item \citet{aronowetal2014}
\end{itemize}
\newpage
\bibliographystyle{chicago}
\begin{singlespace}
\bibliography{refs} % name your BibTeX data base
\end{singlespace}
\newpage
\end{document}