-
Notifications
You must be signed in to change notification settings - Fork 20
/
06_Power.Rmd
2011 lines (1633 loc) · 121 KB
/
06_Power.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
# (PART) Additional Topics {-}
# Power Analysis {#Power}
In this chapter, we are going to study how to choose the size of our sample and how to gauge the size of sampling noise before conducting a study.
This is important because we might decide not to conduct a study if it is not going to bring us precise enough information on the impact in view of its anticipated size.
In practice, there are two ways to run a power analysis:
1. Using test statistics (power study per se)
2. Gauging sampling noise or choosing sample size to reach a given sampling noise
Let me first start by describing the usual approach before moving to my more personal proposal.
## Basics of traditional power analysis using test statistics
Traditional power analysis is based on test-statistics and p-values.
Intuitively, the approach to power analysis based on test statistics computes the sample size required so that a test of a given size $\alpha$ (in general $\alpha=0.05$) can reject the null of a true effect $\beta_A$ in a pre-specified proportion of samples $\kappa$ (in general $\kappa=0.8$).
$\kappa$ is called the **power** of the test and $\beta_A$ the **Minimum Detectable Effect** (MDE).
Let's define these quantities more formally:
Here, we first define power, then minimum detectable effect then the minimum required sample size.
### Power
```{definition,power,name='Power'}
Power $\kappa$ is the probability of rejecting the null hypothesis of a negative or null (for a one-sided test) or null (for a two-sided test) average treatment effect when the true effect is of at least $\beta_A$ applying a test of size $\alpha$ to an estimator $\hat{E}$ with a sample of size $N$. $\beta_A$ is called the Minimum Detectable Effect (MDE).
- for a One-Sided Test: $H_0$: $E\leq0$ \textit{vs} $H_A$: $E=\beta_A>0$
- For a Two-Sided Test: $H_0$: $E=0$ \textit{vs} $H_A$: $E=\beta_A\neq0$
```
Now, if we can assume that the distribution of our estimator $\hat{E}$ can be well approximated by a normal distribution (which is the case of most estimators we have seen so far) and that moreover they are $\sqrt{N}$-consistent (that is that their variance is of the same magnitude as $\sqrt{N}$), we can derive useful formulae for the power parameter, the MDE and the minimum sample size.
Let's first state our assumption:
```{hypothesis,AsymNE,name='Asymptotically Normal Estimator'}
We assume that the estimator $\hat{E}$ is such that there exists a constant (independent of $N$) $C(\hat{E})$ such that:
\begin{align*}
\lim_{N\rightarrow\infty}\Pr\left(\frac{\hat{E}-E}{\sqrt{\var{\hat{E}}}}\leq u\right) & = \Phi\left(u\right),
\end{align*}
with $\var{\hat{E}}=\frac{C(\hat{E})}{N}$.
```
Equipped with Assumption \@ref(hyp:AsymNE), we can now derive a closed-form formula for power $\kappa$:
```{theorem,PowerE,name='Power with an Asymptotically Normal Estimator'}
With $\hat{E}$ complying with Assumption \@ref(hyp:AsymNE), and with $\beta_A>0$, we have:
```
- For a One-Sided Test: $H_0$: $E\leq0$ \textit{vs} $H_A$: $E=\beta_A>0$
\begin{align*}
\kappa_{\text{oneside}} & = \Phi\left(\frac{\beta_A}{\sqrt{\var{\hat{E}}}}-\Phi^{-1}\left(1-\alpha\right)\right),
\end{align*}
- For a Two-Sided Test: $H_0$: $E=0$ \textit{vs} $H_A$: $E=\beta_A\neq0$
\begin{align*}
\kappa_{\text{twoside}} & \approx \Phi\left(\frac{\beta_A}{\sqrt{\var{\hat{E}}}}-\Phi^{-1}\left(1-\frac{\alpha}{2}\right)\right).
\end{align*}
```{proof}
Let us start with a one-sided test first.
We want to build a test statistic $t$ such that, under $H_0$, $\Pr(\hat{E}\geq t)=\alpha$.
Under $H_0$ and using Assumption \@ref(hyp:AsymNE), we have:
\begin{align*}
\Pr(\hat{E}\geq t) & = \Pr\left(\frac{\hat{E}-0}{\sqrt{\var{\hat{E}}}}\geq\frac{t-0}{\sqrt{\var{\hat{E}}}}\right)
\approx 1-\Phi\left(\frac{t}{\sqrt{\var{\hat{E}}}}\right)
\end{align*}
As a consequence of $\Pr(\hat{E}\geq t)=\alpha$, we have:
\begin{align*}
t & \approx\Phi^{-1}\left(1-\alpha\right)\sqrt{\var{\hat{E}}}.
\end{align*}
Power is $\Pr(\hat{E}\geq t)$ under $H_A$.
Using Assumption \@ref(hyp:AsymNE) again:
\begin{align*}
\Pr(\hat{E}\geq t) & = \Pr\left(\frac{\hat{E}-\beta_A}{\sqrt{\var{\hat{E}}}}\geq\frac{t-\beta_A}{\sqrt{\var{\hat{E}}}}\right)
\approx 1-\Phi\left(\frac{t-\beta_A}{\sqrt{\var{\hat{E}}}}\right)
= \Phi\left(\frac{\beta_A-t}{\sqrt{\var{\hat{E}}}}\right),
\end{align*}
which proves the first part of the result.
With a two-sided test, we want a test statistic $t$ such that, under $H_0$, $\Pr(\hat{E}\leq -t\lor\hat{E}\geq t)=\alpha$.
Because the events are disjoint, under $H_0$ and using Assumption \@ref(hyp:AsymNE), we have:
\begin{align*}
\Pr(\hat{E}\leq -t\lor\hat{E}\geq t) & = \Pr(\hat{E}\leq -t) + \Pr(\hat{E}\geq t) \\
& = \Pr\left(\frac{\hat{E}-0}{\sqrt{\var{\hat{E}}}}\leq\frac{-t-0}{\sqrt{\var{\hat{E}}}}\right)+ \Pr\left(\frac{\hat{E}-0}{\sqrt{\var{\hat{E}}}}\geq\frac{t-0}{\sqrt{\var{\hat{E}}}}\right) \\
& \approx \Phi\left(-\frac{t}{\sqrt{\var{\hat{E}}}}\right) + 1-\Phi\left(\frac{t}{\sqrt{\var{\hat{E}}}}\right)\\
& = 2\left(1-\Phi\left(\frac{t}{\sqrt{\var{\hat{E}}}}\right)\right),
\end{align*}
where the last equality uses the symmetry of the normal distribution.
As a consequence of $\Pr(\hat{E}\leq -t\lor\hat{E}\geq t)=\alpha$, we have:
\begin{align*}
t & \approx\Phi^{-1}\left(1-\frac{\alpha}{2}\right)\sqrt{\var{\hat{E}}}.
\end{align*}
Power is $\Pr(\hat{E}\leq -t\lor\hat{E}\geq t)$ under $H_A$.
Using Assumption \@ref(hyp:AsymNE) and the fact that the two events are disjoint again:
\begin{align*}
\Pr(\hat{E}\leq -t\lor\hat{E}\geq t) & = \Pr(\hat{E}\leq -t) + \Pr(\hat{E}\geq t) \\
& = \Pr\left(\frac{\hat{E}-\beta_A}{\sqrt{\var{\hat{E}}}}\leq\frac{-t-\beta_A}{\sqrt{\var{\hat{E}}}}\right)
+ \Pr\left(\frac{\hat{E}-\beta_A}{\sqrt{\var{\hat{E}}}}\geq\frac{t-\beta_A}{\sqrt{\var{\hat{E}}}}\right) \\
& \approx \Phi\left(\frac{-t-\beta_A}{\sqrt{\var{\hat{E}}}}\right)
+ \Phi\left(\frac{t-\beta_A}{\sqrt{\var{\hat{E}}}}\right).
\end{align*}
When $\beta_A$ is positive, the first part of the power formula is negligible with respect to the second part.
Hence the result.
```
```{example}
Let us see how these notions work in our example.
```
Let us first write functions to compute the power according to Theorem \@ref(thm:PowerE):
```{r PowerFun,eval=TRUE,echo=TRUE,results='hide'}
power <- function(betaA,alpha,varE){
return(pnorm(betaA/sqrt(varE)-qnorm(1-alpha)))
}
power.twoside <- function(betaA,alpha,varE){
return(pnorm(-betaA/sqrt(varE)-qnorm(1-alpha/2))+pnorm(betaA/sqrt(varE)-qnorm(1-alpha/2)))
}
```
Let us now choose values for the parameters.
For the variance, we are going to choose the Monte Carlo estimate of the variance of the WW estimator in a Brute Force design that we have studied in Chapter \@ref(RCT) and $\beta_A=0.2$.
```{r ParamPower,eval=TRUE,echo=TRUE,results='hide'}
param <- c(8,.5,.28,1500,0.9,0.01,0.05,0.05,0.05,0.1)
names(param) <- c("barmu","sigma2mu","sigma2U","barY","rho","theta","sigma2epsilon","sigma2eta","delta","baralpha")
alpha <- 0.05
betaA <- 0.2
varE <- var(simuls.brute.force.ww[['1000']][,'WW'])
```
Let us first plot the distributions under the null and under the alternative hypothesis:
```{r PowerPlot,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Power with $\\beta_A=0.2$',fig.align='center',out.width='65%',fig.pos='htbp'}
hist(simuls.brute.force.ww[['1000']][,'WW']-delta.y.ate(param)+betaA,breaks=30,main='N=1000',xlab=expression(hat(Delta^yWW)),xlim=c(-0.15,0.55),probability=T)
curve(dnorm(x, mean=betaA, sd=sqrt(varE)),col="darkblue", lwd=2, add=TRUE, yaxt="n")
curve(dnorm(x, mean=0, sd=sqrt(varE)),col="green", lwd=2, add=TRUE, yaxt="n")
abline(v=betaA,col="red")
abline(v=qnorm(1-alpha)*sqrt(varE),col="green")
abline(v=qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=-qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=0,col="green")
text(x=c(qnorm(1-alpha)*sqrt(varE),qnorm(1-alpha/2)*sqrt(varE)),y=c(adj+3,adj+4),labels=c('t_oneside','t_twoside'),pos=c(2,2),col=c('green','green'),lty=c(lty.obs,lty.unobs))
```
Figure \@ref(fig:PowerPlot) shows the distribution of $\hat{E}$ under the null in green and the distribution under the alternativein blue.
In black is the distribution stemming from the Monte Carlo simulations recentered at $\beta_A=0.2$.
All of these distributions have the same shape (they are normal with variance `r round(varE,3)`) and differ only by their mean.
Under the null of no effect, $\hat{E}$ would be distributed as the green curve, centered at $0$.
The green continuous vertical line materializes the threshold $t_{\text{oneside}}=\Phi^{-1}\left(1-\alpha\right)\sqrt{\var{\hat{E}}}$ of the one-sided test (as defined in the proof of Theorem \@ref(fig:PowerPlot)).
The green discontinuous vertical lines materialize the thresholds $t_{\text{twoside}}=\pm\Phi^{-1}\left(1-\frac{\alpha}{2}\right)\sqrt{\var{\hat{E}}}$ of the two-sided test (as defined in the proof of Theorem \@ref(fig:PowerPlot)).
We are more accustomed to seeing these thresholds standardized by $\sqrt{\var{\hat{E}}}$.
I find it simpler to visualize their nonstandardized versions.
It enables to position the test statistics on the actual distribution of $\hat{E}$ and to compare their values with the values of the parameter estimates.
Note that you can easily go back between the classical standardized thresholds for the test statistics and the nonstandardized ones by dividing and multiplying by $\sqrt{\var{\hat{E}}}$.
As a result, we find that the standardized threshold for the one sided test, $\frac{t_{\text{oneside}}}{\sqrt{\var{\hat{E}}}}=\Phi^{-1}\left(1-\alpha\right)$, is equal to `r round(qnorm(1-alpha),2)`, for $\alpha=$ `r alpha`.
For the two-sided test, the absolute value of the standardized threshold is equal to $\left|\frac{t_{\text{twoside}}}{\sqrt{\var{\hat{E}}}}\right|=\Phi^{-1}\left(1-\frac{\alpha}{2}\right)$, which is equal to `r round(qnorm(1-alpha/2),2)`, for $\alpha=$ `r alpha`.
You probably already know these threshold values, especially the second one, as the classical threshold for 5\% statistical significance in t-tests for the null of an estimated parameter being zero.
What we are doing here is express the thresholds of the test statistics as multiples of the standard error of the estimated parameter.
For a one-sided test, we consider as statistically significant an estimated effect that is `r round(qnorm(1-alpha),2)` times larger than its standard error, and for a two-sided test, an estimated effect whose larger in absolute value than `r round(qnorm(1-alpha/2),2)` its standard error.
Because the distribution of $\hat{E}$ is normal, under Assumption \@ref(hyp:AsymNE), the probability that the value of $\hat{E}$ falls above these thresholds under the null is equal to $\alpha=$ `r alpha`.
More precisely, for the one sided test, the probability that $\hat{E}$ falls above $t_{\text{oneside}}$ under the null is 5\%.
For the two-sided test, it is the probability that $\hat{E}$ falls above $t_{\text{twoside}}$ or below $-t_{\text{twoside}}$ under the null that is equal to 5\%.
Or, stated otherwise, it is the probability that $|\hat{E}|>t_{\text{twoside}}$ which is equal to 5\% under the null for the two-sided test.
In practice, with our current choice of parameter values (especially the variance of $\hat{E}$), we have $t_{\text{oneside}}=$ `r round(qnorm(1-alpha)*sqrt(varE),2)` and $t_{\text{twoside}}=$ `r round(qnorm(1-alpha/2)*sqrt(varE),2)`.
If the estimated treatment effect falls above these threshold values, a one-sided and a two-sided test respectively will find that these effects are statistically significantly different from zero at 5\%.
Power computation does not stop there.
It asks the following question: if the true value of $E$ is actually $E=\beta_A$, what is the probability that our estimator $\hat{E}$ will fall above $t_{\text{oneside}}$ or $t_{\text{twoside}}$?
Before going further, note that if $\beta_A>0$ and $\var{\hat{E}}$ is not too large, the probability that $\hat{E}$ falls below $-t_{\text{twoside}}$ under the alternative hypothesis is negligible.
It is materialized on Figure \@ref(fig:PowerPlot) as the area under the the portion of the blue curve below the lower discontinuous vertical green line positioned at $-t_{\text{twoside}}=$ `r round(-qnorm(1-alpha/2)*sqrt(varE),2)`.
It is obviously very small.
We know from Assumption \@ref(hyp:AsymNE) that this probability is equal to $\Pr(\hat{E}\leq -t_{\text{twoside}})=\Phi\left(\frac{-t_{\text{twoside}}-\beta_A}{\sqrt{\var{\hat{E}}}}\right)=$ `r pnorm((-qnorm(1-alpha/2)*sqrt(varE)-betaA)/(sqrt(varE)))`.
So now, what is the probability that $\hat{E}$ falls above $t_{\text{oneside}}$ or $t_{\text{twoside}}$ under the assumption that $E=\beta_A>0$?
Intuitively, it is the area under the portion of the blue curve which is above the continuous green vertical line positioned at $t_{\text{oneside}}=$ `r round(qnorm(1-alpha)*sqrt(varE),2)` or above the discontinuous green vertical line positioned at $t_{\text{twoside}}=$ `r round(qnorm(1-alpha/2)*sqrt(varE),2)`.
Because we know that $\hat{E}$ follows a normal with mean $\beta_A$ and variance $\var{\hat{E}}$ under the alternative, we can compute these quantities as equal to $\kappa_{t}=1-\Phi\left(\frac{t-\beta_A}{\sqrt{\var{\hat{E}}}}\right)=\Phi\left(\frac{\beta_A-t}{\sqrt{\var{\hat{E}}}}\right)$, for $t\in\left\{\text{oneside},\text{twoside}\right\}$.
Note that using the formulae for $t_{\text{oneside}}$ and $t_{\text{twoside}}$ yields the results in Theorem \@ref(thm:PowerE).
In practice, we have $\kappa_{\text{oneside}}=$ `r round(power(betaA,alpha,varE),2)` and $\kappa_{\text{twoside}}=$ `r round(power.twoside(betaA,alpha,varE),2)`.
What would happen to power if $\beta_A$ was lower?
For example, what if $\beta_A=0.1$?
```{r PowerPlot01,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Power with $\\beta_A=0.1$',fig.align='center',out.width='65%',fig.pos='htbp'}
betaA <- 0.1
hist(simuls.brute.force.ww[['1000']][,'WW']-delta.y.ate(param)+betaA,breaks=30,main='N=1000',xlab=expression(hat(Delta^yWW)),xlim=c(-0.15,0.55),probability=T)
curve(dnorm(x, mean=betaA, sd=sqrt(varE)),col="darkblue", lwd=2, add=TRUE, yaxt="n")
curve(dnorm(x, mean=0, sd=sqrt(varE)),col="green", lwd=2, add=TRUE, yaxt="n")
abline(v=betaA,col="red")
abline(v=qnorm(1-alpha)*sqrt(varE),col="green")
abline(v=qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=-qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=0,col="green")
text(x=c(qnorm(1-alpha)*sqrt(varE),qnorm(1-alpha/2)*sqrt(varE)),y=c(adj+3,adj+4),labels=c('t_oneside','t_twoside'),pos=c(2,2),col=c('green','green'),lty=c(lty.obs,lty.unobs))
```
Figure \@ref(fig:PowerPlot01) shows what would happen with $\beta_A=0.1$.
It becomes much harder to tell the green curve from the blue curve.
As a consequence, power decreases, since the portion of the blue curve located below the thresholds increases.
It is a consequence less likely that a t-test will reject the assumption that the treatment effect is zero, even when it is not zero but equal to $0.1$.
How less likely?
Well, power is now equal to $\kappa_{\text{oneside}}=$ `r round(power(betaA,alpha,varE),2)` and $\kappa_{\text{twoside}}=$ `r round(power.twoside(betaA,alpha,varE),2)`.
What would happen now if our estimator was way more precise?
For example, what would happen if we could reach the precision of the With/Without estimator with a sample size of $N=10000$ instead of $N=1000$ as we have assumed up to now?
```{r PowerPlot10000,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Power with $N=10000$',fig.align='center',out.width='65%',fig.pos='htbp'}
betaA <- 0.2
varE.10000 <- var(simuls.brute.force.ww[['10000']][,'WW'])
hist(simuls.brute.force.ww[['10000']][,'WW']-delta.y.ate(param)+betaA,breaks=30,main='N=10000',xlab=expression(hat(Delta^yWW)),xlim=c(-0.15,0.55),probability=T)
curve(dnorm(x, mean=betaA, sd=sqrt(varE)),col="darkblue", lwd=2, add=TRUE, yaxt="n")
curve(dnorm(x, mean=betaA, sd=sqrt(varE.10000)),col="darkblue", lwd=2, add=TRUE, yaxt="n")
curve(dnorm(x, mean=0, sd=sqrt(varE)),col="green", lwd=2, add=TRUE, yaxt="n")
curve(dnorm(x, mean=0, sd=sqrt(varE.10000)),col="green", lwd=2, add=TRUE, yaxt="n")
abline(v=betaA,col="red")
abline(v=qnorm(1-alpha)*sqrt(varE),col="green")
abline(v=qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=-qnorm(1-alpha/2)*sqrt(varE),col="green",lty=lty.unobs)
abline(v=qnorm(1-alpha)*sqrt(varE.10000),col="green")
abline(v=qnorm(1-alpha/2)*sqrt(varE.10000),col="green",lty=lty.unobs)
abline(v=-qnorm(1-alpha/2)*sqrt(varE.10000),col="green",lty=lty.unobs)
abline(v=0,col="green")
text(x=c(qnorm(1-alpha)*sqrt(varE),qnorm(1-alpha/2)*sqrt(varE)),y=c(adj+3,adj+4),labels=c('t_oneside','t_twoside'),pos=c(2,2),col=c('green','green'),lty=c(lty.obs,lty.unobs))
```
Figure \@ref(fig:PowerPlot10000) shows what would happen with $\beta_A=0.2$ and $N=10000$.
There are two effects of increased precision on power.
First, the blue curve is thinner, which means that less of its area lies before the thresholds of the test statisics.
Second, the green curve is also thinner, which means that the threshold are smaller.
With $N=10000$, $t_{\text{oneside}}=$ `r round(qnorm(1-alpha)*sqrt(varE.10000),3)` and $t_{\text{twoside}}=$ `r round(qnorm(1-alpha/2)*sqrt(varE.10000),3)`.
As a consequence, power increases sharply with $N=10000$.
For $\beta_A=0.2$, power is now $\kappa_{\text{oneside}}=$ `r round(power(betaA=0.2,alpha,varE.10000),2)` and $\kappa_{\text{twoside}}=$ `r round(power.twoside(betaA=0.2,alpha,varE.10000),2)`.
For $\beta_A=0.1$, power is now $\kappa_{\text{oneside}}=$ `r round(power(betaA=0.1,alpha,varE.10000),2)` and $\kappa_{\text{twoside}}=$ `r round(power.twoside(betaA=0.1,alpha,varE.10000),2)`.
Finally, let us see how power changes with $\var{\hat{E}}$ (through sample size), $\beta_A$ and $\alpha$.
Let us first run some simulations:
```{r PowerSimuls,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide'}
varE.N <- c(var(simuls.brute.force.ww[['100']][,'WW']),var(simuls.brute.force.ww[['1000']][,'WW']),var(simuls.brute.force.ww[['10000']][,'WW']),var(simuls.brute.force.ww[[4]][,'WW']))
alpha <- 0.05
betaA <- 0.2
power.N.05.02 <- sapply(varE.N,power,alpha=alpha,betaA=betaA)
power.N.twoside.05.02 <- sapply(varE.N,power.twoside,alpha=alpha,betaA=betaA)
betaA <- 0.1
power.N.05.01 <- sapply(varE.N,power,alpha=alpha,betaA=betaA)
power.N.twoside.05.01 <- sapply(varE.N,power.twoside,alpha=alpha,betaA=betaA)
alpha <- 0.01
power.N.01.01 <- sapply(varE.N,power,alpha=alpha,betaA=betaA)
power.N.twoside.01.01 <- sapply(varE.N,power.twoside,alpha=alpha,betaA=betaA)
betaA <- 0.2
power.N.01.02 <- sapply(varE.N,power,alpha=alpha,betaA=betaA)
power.N.twoside.01.02 <- sapply(varE.N,power.twoside,alpha=alpha,betaA=betaA)
N.sample <- c(100,1000,10000,100000)
power.sample <- data.frame("N"=rep(N.sample,8),"Power"=c(power.N.05.02,power.N.twoside.05.02,power.N.05.01,power.N.twoside.05.01,power.N.01.01,power.N.twoside.01.01,power.N.01.02,power.N.twoside.01.02))
colnames(power.sample) <- c('N','Power')
power.sample$Test <- rep(c(rep('One-sided',length(N.sample)),rep('Two-sided',length(N.sample))),4)
power.sample$betaA <- c(rep(0.2,2*length(N.sample)),rep(0.1,4*length(N.sample)),rep(0.2,2*length(N.sample)))
power.sample$alpha <- c(rep(0.05,4*length(N.sample)),rep(0.01,4*length(N.sample)))
```
Let us now plot the resulting estimates:
```{r PowerPlotFull,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Power and Sample Size as a function of $\\alpha$ (left vs right) and $\\beta_A$ (top vs bottom)',fig.align='center',out.width='65%',fig.pos='htbp'}
ggplot(power.sample, aes(x=as.factor(N), y=Power, fill=Test)) +
geom_bar(position=position_dodge(), stat="identity") +
xlab("Sample Size") +
theme_bw()+
facet_grid(betaA~alpha)
```
Figure \@ref(fig:PowerPlotFull) shows that sample size is a key determinant of power, but that $\alpha$ and $\beta_A$ are as well.
For example, with $\beta_A=0.1$ and $\alpha=0.01$, $N=1000$ has low power (around $0.25$).
Moving to sample size $N=10000$ would ensure very effective power, but let us keep $N=1000$ for now.
We can see that simply increasing $\alpha$ to $0.05$ increases power to around $0.5$.
But it is increasing $\beta_A$ to $0.2$ that has the strongest effect: power is now around $0.9$, whatever $\alpha$.
As a consequence, choosing $\beta_A$ correctly is key to ensure a correct power analysis.
### Minimum Detectable Effect
In general power analysis does not stop after computing power.
We also might want to compute the Minimum Detectable Effect (or MDE) that we can detect with a sample of size $N$ and a (one- or two-sided) test of size $\alpha$ with power $\kappa$.
The following theorem enables us to do just that:
```{theorem,MDE,name='Minimum Detectable Effect with an Asymptotically Normal Estimator'}
With $\hat{E}$ complying with Assumption \@ref(hyp:AsymNE), the Minimum Detectable Effect of a one- or two-sided test is:
```
- For a One-Sided Test: $H_0$: $E\leq0$ \textit{vs} $H_A$: $E=\beta_A>0$
\begin{align*}
\beta_A^{\text{oneside}} & = \left(\Phi^{-1}\left(\kappa\right)+\Phi^{-1}\left(1-\alpha\right)\right)\sqrt{\var{\hat{E}}},
\end{align*}
- For a Two-Sided Test: $H_0$: $E=0$ \textit{vs} $H_A$: $E=\beta_A\neq0$
\begin{align*}
\beta_A^{\text{twoside}} & \approx \left(\Phi^{-1}\left(\kappa\right)+\Phi^{-1}\left(1-\frac{\alpha}{2}\right)\right)\sqrt{\var{\hat{E}}}.
\end{align*}
```{proof}
Using Theorem \@ref(thm:PowerE), inverting the power formula yields the result.
```
With Theorem \@ref(thm:MDE), we have a way to compute the MDE for a wide range of applications, as long as we know $\var{\hat{E}}$ and that we have choosen properly $\alpha$ and $\kappa$.
In most applications, researchers choose $\alpha=0.05$ and $\kappa=0.8$, so that they compute the effect that they have 80\% chance to detect with a test of size 5\%.
```{example}
In our example, we can try to see what the MDE looks for various sample sizes.
Let us first write functions to compute the MDE:
```
```{r MDE.sample,eval=TRUE,echo=TRUE,results='hide'}
MDE.var <- function(alpha,kappa,varE,oneside){
if (oneside==TRUE){
MDE <- (qnorm(kappa)+qnorm(1-alpha))*sqrt(varE)
}
if (oneside==FALSE){
MDE <- (qnorm(kappa)+qnorm(1-alpha/2))*sqrt(varE)
}
return(MDE)
}
MDE <- function(N,alpha,kappa,CE,oneside){
if (oneside==TRUE){
MDE <- (qnorm(kappa)+qnorm(1-alpha))*sqrt(CE/N)
}
if (oneside==FALSE){
MDE <- (qnorm(kappa)+qnorm(1-alpha/2))*sqrt(CE/N)
}
return(MDE)
}
alpha <- 0.05
kappa <- 0.8
MDE.N.oneside <- sapply(varE.N,MDE.var,alpha=alpha,kappa=kappa,oneside=TRUE)
MDE.N.twoside <- sapply(varE.N,MDE.var,alpha=alpha,kappa=kappa,oneside=FALSE)
power.sample$MDE <- c(MDE.N.oneside,MDE.N.twoside)
```
Let us now plot the results:
```{r PlotMDE,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='MDE and Sample Size',fig.align='center',out.width='65%',fig.pos='htbp'}
ggplot(power.sample, aes(x=as.factor(N), y=MDE,fill=Test)) +
geom_bar(position=position_dodge(), stat="identity") +
xlab("Sample Size") +
theme_bw()
```
Figure \@ref(fig:PlotMDE) shows that, with $\alpha=$ `r alpha` and $\kappa=$ `r kappa` and a two-sided test, we can detect a minimum effect of `r round(filter(power.sample,Test=="Two-sided",N==100)[1,6],2)` with $N=$ `r round(filter(power.sample,Test=="Two-sided",N==100)[1,1],2)`, whereas the MDE decreases to `r round(filter(power.sample,Test=="Two-sided",N==1000)[1,6],2)` with $N=$ `r round(filter(power.sample,Test=="Two-sided",N==1000)[1,1],2)` and even further to `r round(filter(power.sample,Test=="Two-sided",N==10000)[1,6],2)` with $N=$ `r round(filter(power.sample,Test=="Two-sided",N==10000)[1,1],2)` and `r round(filter(power.sample,Test=="Two-sided",N==100000)[1,6],2)` with $N=$ `r round(filter(power.sample,Test=="Two-sided",N==100000)[1,1],2)`.
### Minimum Required Sample Size
Finally, we can also estimate the minimum sample size required to estimate an effect with given size and power.
The following theorem shows how:
```{theorem,MDEN,name='Minimum Required Sample Size with an Asymptotically Normal Estimator'}
With $\hat{E}$ complying with Assumption \@ref(hyp:AsymNE), the Minimum Required Sample Size to estimate an effect $\beta_A$ with a one- or two-sided test is:
```
- For a One-Sided Test: $H_0$: $E\leq0$ \textit{vs} $H_A$: $E=\beta_A>0$
\begin{align*}
N_{\text{oneside}} & = \left(\Phi^{-1}\left(\kappa\right)+\Phi^{-1}\left(1-\alpha\right)\right)^2\frac{C(\hat{E})}{\beta_A^2},
\end{align*}
- For a Two-Sided Test: $H_0$: $E=0$ \textit{vs} $H_A$: $E=\beta_A\neq0$
\begin{align*}
N_{\text{twoside}} & = \left(\Phi^{-1}\left(\kappa\right)+\Phi^{-1}\left(1-\frac{\alpha}{2}\right)\right)^2\frac{C(\hat{E})}{\beta_A^2}.
\end{align*}
```{proof}
Using Theorem \@ref(thm:MDE), and inverting the formula for MDE yields the result.
```
```{example}
Let us see how this works in our example.
Let us first write a function to compute the required formulae and then set up $C(\hat{E})$ and finally the minimum reauired sample size for a given treatment effect:
```
```{r MDE.sample.size,eval=TRUE,echo=TRUE,results='hide'}
# formula
sample.size <- function(betaA,alpha,kappa,CE,oneside){
if (oneside==TRUE){
N <- (qnorm(kappa)+qnorm(1-alpha))^2*(CE/(betaA^2))
}
if (oneside==FALSE){
N <- (qnorm(kappa)+qnorm(1-alpha/2))^2*(CE/(betaA^2))
}
return(round(N))
}
# C(E)
C.E.N <- varE.N*N.sample
# choose set of MDEs
MDE.set <- c(1,0.5,0.2,0.1,0.02)
# compute set of MDEs for a given C(E) (let's choose the one for 1000)
N.mini.oneside <- sapply(MDE.set,sample.size,alpha=alpha,kappa=kappa,CE=C.E.N[[2]],oneside=TRUE)
N.mini.twoside <- sapply(MDE.set,sample.size,alpha=alpha,kappa=kappa,CE=C.E.N[[2]],oneside=FALSE)
# Data frame
MDE.N.sample <- data.frame("betaA"=rep(MDE.set,2),"N"=c(N.mini.oneside,N.mini.twoside),"Test"=rep(c(rep('One-sided',length(MDE.set)),rep('Two-sided',length(MDE.set)))))
```
Let us now plot the results:
```{r PlotMiniN,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Minimum required sample size',fig.align='center',out.width='65%',fig.pos='htbp'}
ggplot(MDE.N.sample, aes(x=as.factor(betaA), y=N,fill=Test)) +
geom_bar(position=position_dodge(), stat="identity") +
xlab("betaA") +
ylab("Sample size (log scale)")+
yscale("log10",.format=TRUE)+
theme_bw()
```
Figure \@ref(fig:PlotMiniN) shows that Minimum Required Sample Size increases very fast as $\beta_A$, the minimum effect to detect, decreases.
For $\beta_A=$ `r MDE.set[[1]]`, the Minimum Required Sample Size is equal to `r N.mini.twoside[[1]]`.
For $\beta_A=$ `r MDE.set[[2]]`, the Minimum Required Sample Size is equal to `r N.mini.twoside[[2]]`.
For $\beta_A=$ `r MDE.set[[3]]`, the Minimum Required Sample Size is equal to `r N.mini.twoside[[3]]`.
For $\beta_A=$ `r MDE.set[[4]]`, the Minimum Required Sample Size is equal to `r N.mini.twoside[[4]]`.
For $\beta_A=$ `r MDE.set[[5]]`, the Minimum Required Sample Size is equal to `r N.mini.twoside[[5]]`.
## Traditional power analysis in practice
In the previous section, we have covered the basics of power analysis.
In order to implement it in practice, we still need an estimate of $\var{\hat{E}}$ or of $C(\hat{E})$.
When we want to compute power after we have estimated an effect, both of these quantities can easily be recovered from most estimators we have covered in this book.
One problem, though, is when we want to conduct a power analysis before running a study (for example, before collecting the data).
There, we need a way to find a reasonable estimate of $\var{\hat{E}}$ and $C(\hat{E})$.
We are going to see several ways of doing that, but basically, we need prior information on the properties of our outcomes and covariates.
They can come from baseline data or from data take in a similar population as our target population.
Sometimes, we have to make strong assumptions to move the results from one population to our target population.
Let's examine in practice how to do that, in the context of all the econometric methods we have studied so far.
### Power Analysis for Randomized Controlled Trials
We are going to decompose what needs to be done for each of the four RCT designs we have studied in Section \@ref(RCT).
#### Power Analysis for Brute Force Designs
For Brute Force designs, the mosts straightforward way to do a power analysis is to rely on the CLT-based approximation for the precision of our estimator.
Using Theorem \@ref(thm:asympnoiseWW) and especially Lemma \@ref(lem:asymWW), we have, in a Brute Force design:
\begin{align*}
\var{\hat{\Delta}^Y_{WW^{BF}}} & \approx \frac{1}{N}\left(\frac{\var{Y_i^1|R_i=1}}{\Pr(R_i=1)}+\frac{\var{Y_i^0|R_i=0}}{1-\Pr(R_i=1)}\right).
\end{align*}
We can see that Assumption \@ref(hyp:AsymNE) is valid for this estimator.
In order to compute $\var{\hat{\Delta}^Y_{WW}}$ of $C(\hat{\Delta}^Y_{WW})$, we need to come up with reasonable estimates of $\var{Y_i^1|R_i=1}$ and $\var{Y_i^0|R_i=0}$.
The problem is that these quantities will only be revealed after the treatment has been given.
It is fine for ex-post power analysis but is not feasible for ex-ante power analysis.
One way around this issue is to use an estimate of the variance of $Y_i$ in a related or similar sample to benchmark our formula.
In our case, we can use the variance of $Y_i^B$, outcomes observed in the period before the treatment is taken, as a source of estimates.
Since we cannot know who will get the treatment and who will not, we are going to use the same benchmark for both ($\var{Y_i^B}$).
This is not perfect but this is what we have.
As a result, our estimate of $C(\hat{\Delta}^Y_{WW})$ in the brute force design is:
\begin{align*}
\hat{C}(\hat{\Delta}^Y_{WW^{BF}}) & = \frac{\var{Y_i^B}}{p(1-p)},
\end{align*}
where $p$ is the proportion of individuals in our sample who will be allocated to the treatment.
```{example}
Let us see how this formula works out in our example.
First, we need to generate a sample:
```
```{r SimulBFPower,eval=TRUE,echo=TRUE,results='hide'}
set.seed(1234)
N <-1000
mu <- rnorm(N,param["barmu"],sqrt(param["sigma2mu"]))
UB <- rnorm(N,0,sqrt(param["sigma2U"]))
yB <- mu + UB
YB <- exp(yB)
Ds <- rep(0,N)
Ds[YB<=param["barY"]] <- 1
epsilon <- rnorm(N,0,sqrt(param["sigma2epsilon"]))
eta<- rnorm(N,0,sqrt(param["sigma2eta"]))
U0 <- param["rho"]*UB + epsilon
y0 <- mu + U0 + param["delta"]
alpha.i <- param["baralpha"]+ param["theta"]*mu + eta
y1 <- y0+alpha.i
Y0 <- exp(y0)
Y1 <- exp(y1)
# randomized allocation of 50% of individuals
Rs <- runif(N)
R <- ifelse(Rs<=.5,1,0)
y <- y1*R+y0*(1-R)
Y <- Y1*R+Y0*(1-R)
```
Let us now compute the minimum detectable effect for various sample sizes and proportions of treated individuals, using $\hatvar{y^B_i}=$ `r round(var(yB),2)` as an estimate of $\hatvar{y_i^1|R_i=1}=$ `r round(var(y[R==1]),2)` and $\hatvar{y_i^0|R_i=0}=$ `r round(var(y[R==0]),2)`.
```{r CEBFFun,eval=TRUE,echo=TRUE,results='hide'}
CE.BF.fun <- function(p,varYb){
return(varYb/(p*(1-p)))
}
MDE.BF.fun <- function(p,varYb,...){
return(MDE(CE=CE.BF.fun(p=p,varYb=varYb),...))
}
```
Let us finally check what Minimum Detectable Effect looks like as a function of $p$ and of sample size.
```{r MDEBFPlot,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Minimum detectable effect for the Brute Force design',fig.align='center',out.width='65%',fig.pos='htbp'}
ggplot() +
xlim(0,1) +
ylim(0,1) +
geom_function(aes(color="100"),fun=MDE.BF.fun,args=list(N=100,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.BF.fun,args=list(N=1000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.BF.fun,args=list(N=10000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.BF.fun,args=list(N=100000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name="N") +
ylab("MDE") +
xlab("p") +
theme_bw()
```
As figure \@ref(fig:MDEBFPlot) shows, the Minimum Detectable Effect is minimized, for a given sample size, at $p=0.5$.
This makes sense since it is where we get the most precision out of our treated and control samples.
This results depends crucially on the fact that we have assumed no heteroskedasticity (*i.e.* that we have assumed that the variance of outcomes does not change with treatment status).
We also can see that MDE decreases with sample size, but slower than proportionally.
Again, it makes sense, since power and MDE depend on the square root of sample size.
With a sample size of $N=100$, we now reach a MDE of `r round(MDE.BF.fun(p=0.5,N=100,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a sample size of $N=1000$, we now reach a MDE of `r round(MDE.BF.fun(p=0.5,N=1000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a sample size of $N=10000$, we now reach a MDE of `r round(MDE.BF.fun(p=0.5,N=10000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a sample size of $N=100000$, we now reach a MDE of `r round(MDE.BF.fun(p=0.5,N=100000,varYb=var(yB),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
#### Power Analysis for Self-Selection Designs
For self-selection designs, the approach is similar to that of brute force designs, except that, since randomization occurs after self-selection, you have to make a guess on the proportion of people who will take the treatment.
We have:
\begin{align*}
\var{\hat{\Delta}^Y_{WW^{SS}}} & \approx \frac{1}{N}\frac{1}{\Pr(D_i=1)}\left(\frac{\var{Y_i^1|D_i=1,R_i=1}}{\Pr(R_i=1|D_i=1)}+\frac{\var{Y_i^0|D_i=1,R_i=0}}{1-\Pr(R_i=1|D_i=1)}\right),
\end{align*}
with $N$ the total number of individuals in the sample, including the inegilible individuals and the individuals who do not self-select into the program.
Estimates of the MDE and of the Minimum Required Sample Size can be expressed in terms of $N$ or of $N^D=N\Pr(D_i=1)$, the number of individuals who self-select into the program and among which randomization is run.
The estimates we need to compute these quantities are more complex than with brute force designs: we need $\Pr(D_i=1)$, but also variances conditional on $D_i=1$.
If the program was operating before randomization, we can have some pretty good ideas of these numbers.
Otherwise, we have to use either surveys on intentions to participate, or evidence from similar programs, or at least try to enforce the eligibility criteria as much as we can.
```{example}
Let's see how we can make this work in our example.
```
Let us first update our parameters for modelling self-selection, as we did in Chapter \@ref(RCT):
```{r paramsSS,eval=TRUE,echo=TRUE,results='hide'}
param <- c(param,-6.25,0.9,0.5)
names(param) <- c("barmu","sigma2mu","sigma2U","barY","rho","theta","sigma2epsilon","sigma2eta","delta","baralpha","barc","gamma","sigma2V")
```
and let's generate a new dataset:
```{r SimulPowerSS,eval=TRUE,echo=TRUE,results='hide'}
set.seed(1234)
N <-1000
mu <- rnorm(N,param["barmu"],sqrt(param["sigma2mu"]))
UB <- rnorm(N,0,sqrt(param["sigma2U"]))
yB <- mu + UB
YB <- exp(yB)
E <- ifelse(YB<=param["barY"],1,0)
V <- rnorm(N,0,param["sigma2V"])
Dstar <- param["baralpha"]+param["theta"]*param["barmu"]-param["barc"]-param["gamma"]*mu-V
Ds <- ifelse(Dstar>=0 & E==1,1,0)
epsilon <- rnorm(N,0,sqrt(param["sigma2epsilon"]))
eta<- rnorm(N,0,sqrt(param["sigma2eta"]))
U0 <- param["rho"]*UB + epsilon
y0 <- mu + U0 + param["delta"]
alpha <- param["baralpha"]+ param["theta"]*mu + eta
y1 <- y0+alpha
Y0 <- exp(y0)
Y1 <- exp(y1)
#random allocation among self-selected
Rs <- runif(N)
R <- ifelse(Rs<=.5 & Ds==1,1,0)
y <- y1*R+y0*(1-R)
Y <- Y1*R+Y0*(1-R)
# computing application rate
pDhat <- mean(Ds)
```
We are going to assume that all conditional variances are equal to the pre-treatment variance: we use $\hatvar{y^B_i}=$ `r round(var(yB),2)` as an estimate of $\hatvar{y_i^1|D_i=1,R_i=1}=$ `r round(var(y1[Ds==1]),2)` and $\hatvar{y_i^0|D_i=1,R_i=0}=$ `r round(var(y0[Ds==1]),2)`.
This is obviously not a great choice since people with $D_i=1$ have lower variance in outcomes than the overall population.
What happens if we choose instead $\hatvar{y^B_i|y^B_i\leq\bar{y}}$.
Well, $\hatvar{y^B_i|y^B_i\leq\bar{y}}=$ `r round(var(yB[E==1]),2)`, which is a much better guess.
So trying to approximate the selection process (at least enforcing the eligibility criteria) is a good idea when doing a power analysis for self-selection designs.
We are going to use:
\begin{align*}
\hat{C}(\hat{\Delta}^Y_{WW^{SS}}) & = \frac{1}{\Pr(D_i=1)}\frac{\hatvar{y^B_i|y^B_i\leq\bar{y}}}{p(1-p)},
\end{align*}
with $p$ the proportion of applicants randomized into the program.
Let's write a function to compute the MDE in self-selection designs:
```{r CESSFun,eval=TRUE,echo=TRUE,results='hide'}
CE.SS.fun <- function(p,varYb,pD){
return(var(yB)/(pD*p*(1-p)))
}
MDE.SS.fun <- function(p,varYb,pD,...){
return(MDE(CE=CE.SS.fun(p=p,varYb=varYb,pD=pD),...))
}
alpha <- 0.05
kappa <- 0.8
```
Let us finally check what Minimum Detectable Effect looks like as a function of $p$ and of sample size.
```{r MDESSPlot,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Minimum detectable effect for the self-selection design',fig.subcap=c('Total sample','Sample of applicants'),fig.align='center',out.width='50%',fig.pos='htbp'}
# total sample size (including ineligibles and non applicants)
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.SS.fun,args=list(N=100,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.SS.fun,args=list(N=1000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.SS.fun,args=list(N=10000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.SS.fun,args=list(N=100000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name="N") +
ylab("MDE") +
xlab("p") +
theme_bw()
# Applicants sample size
pDhat <- 1
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.SS.fun,args=list(N=100,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.SS.fun,args=list(N=1000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.SS.fun,args=list(N=10000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.SS.fun,args=list(N=100000,pD=pDhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name=expression(N^D)) +
ylab("MDE") +
xlab("p") +
theme_bw()
# computing application rate
pDhat <- mean(Ds)
```
With a total sample size of $N=100$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=100,varYb=var(yB[Ds==1]),pD=pDhat,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=1000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=1000,varYb=var(yB[Ds==1]),pD=pDhat,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=10000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=10000,varYb=var(yB[Ds==1]),pD=pDhat,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=100000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=100000,varYb=var(yB[Ds==1]),pD=pDhat,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
Remember that these sample sizes include ineligible units and units that do not apply for the program.
Figure \@ref(fig:MDESSPlot) also shows what happens when sample size corresponds to only applicants to the program (plot on the right).
MDEs in that case look much more like the ones in the brute force design presented in Figure \@ref(fig:MDEBFPlot).
With a total sample size of $N^D=100$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=100,varYb=var(yB[Ds==1]),pD=1,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^D=1000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=1000,varYb=var(yB[Ds==1]),pD=1,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^D=10000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=10000,varYb=var(yB[Ds==1]),pD=1,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^D=100000$, we now reach a MDE of `r round(MDE.SS.fun(p=0.5,N=100000,varYb=var(yB[Ds==1]),pD=1,alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
#### Power Analysis for Eligibility Designs
In eligibility designs, we can make use Theorem \@ref(thm:asymBloom) to show that:
\begin{align*}
\var{\hat{\Delta}^Y_{Bloom}} & \approx \frac{1}{N}\frac{1}{p^{E}}\frac{1}{(p^{D}_1)^2}\left[\left(\frac{p^D}{p^R}\right)^2\frac{\var{Y_i|R_i=0,E_i=1}}{1-p^R}+\left(\frac{1-p^D}{1-p^R}\right)^2\frac{\var{Y_i|R_i=1,E_i=1}}{p^R}\right],
\end{align*}
with $p^E=\Pr(E_i=1)$, $p^D=\Pr(D_i=1|E_i=1)$, $p^R=\Pr(R_i=1|E_i=1)$ and $p^{D}_1=\Pr(D_i=1|R_i=1,E_i=1)$.
Note that $N$ corresponds to the size of the sample including the ineligible individuals which do not enter in the estimation of the treatment effect of the program.
MDEs and minimum required sample size can also be expressed in terms of $N^E=Np^E$, the size of the sample of eligible units.
There is a large number of parameters to find in order to compute this variance estimator.
We need to postulate a value for $p^{E}$ (unless we look for information on the sample size of the eligible population participating in the experiment, in which case we set $p^{E}=1$ and $N=N^E$ in the above formula), a value for $p^D$, for $p^R$ and for $p^D_1$.
For estimating $p^E$, we are going to choose the proportion of individuals eligible to the program ($\hat{p}^E=\Pr(y_i^B\leq\bar{y})$).
For $p^D$, we know that: $p^D=\Pr(D_i=1|E_i=1)=\Pr(D_i=1|R_i=1,E_i=1)\Pr(R_i=1|E_i=1)=p^{D}_1p^R$.
Since we can vary $p^R$, we only need to settle on a value for $p^{D}_1$.
We are going to choose the actual value of $\Pr(D_i=1|R_i=1,E_i=1)$, or $\hat{p}^{D}_1=$ `r round(mean(Ds[E==1 & R==1]),2)`.
Finally, we are going to assume that all conditional variances are equal to the pre-treatment variance among eligibles: we use $\hatvar{y^B_i|E_i=1}=$ `r round(var(yB[E==1]),2)` as an estimate of $\hatvar{y_i|R_i=1,E_i=1}=$ `r round(var(y[R==1 & E==1]),2)` and $\hatvar{y_i|R_i=0,E_i=1}=$ `r round(var(y0[R==0 & E==1]),2)`.
We can now write $C(E)$ for the total sample size $N$ (set $p^{E}=1$ for $N=N^E$):
\begin{align*}
\hat{C}(\hat{\Delta}^Y_{Bloom}) & = \frac{1}{\hat{p}^{E}}\frac{1}{(\hat{p}^{D}_1)^2}\left[\left(\frac{\hat{p}^D}{p^R}\right)^2\frac{\hatvar{y^B_i|E_i=1}}{1-p^R}+\left(\frac{1-\hat{p}^D}{1-p^R}\right)^2\frac{\hatvar{y^B_i|E_i=1}}{p^R}\right].
\end{align*}
Let's write a function to compute the MDE in eligibility designs:
```{r CEEligFun,eval=TRUE,echo=TRUE,results='hide'}
CE.Elig.fun <- function(pR,varYb,pE,p1D){
return((1/pE)*(1/p1D)^2*((p1D)^2*varYb/(1-pR)+(((1-p1D*pR)/(1-pR))^2*var(yB)/pR)))
}
MDE.Elig.fun <- function(pR,varYb,pE,p1D,...){
return(MDE(CE=CE.Elig.fun(pR=pR,varYb=varYb,pE=pE,p1D=p1D),...))
}
# computing candidate participation rate, using the observed proportion below baryB
pEhat <- mean(E)
p1Dhat <- mean(Ds[E==1 & R==1])
alpha <- 0.05
kappa <- 0.8
```
Let us finally check what Minimum Detectable Effect looks like as a function of $p^R$ and of sample size.
```{r MDEEligPlot,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Minimum detectable effect for the eligibility design',fig.subcap=c('Total sample','Sample of eligibles'),fig.align='center',out.width='50%',fig.pos='htbp'}
# total sample size (including ineligibles)
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.Elig.fun,args=list(N=100,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.Elig.fun,args=list(N=1000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.Elig.fun,args=list(N=10000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.Elig.fun,args=list(N=100000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name="N") +
ylab("MDE") +
xlab(expression(p^R)) +
theme_bw()
# Applicants sample size
pEhat <- 1
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.Elig.fun,args=list(N=100,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.Elig.fun,args=list(N=1000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.Elig.fun,args=list(N=10000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.Elig.fun,args=list(N=100000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name=expression(N^E)) +
ylab("MDE") +
xlab("p") +
theme_bw()
# computing application rate
pEhat <- mean(E)
```
Note that the minimum detectable effect is no longer minimized at $p^R=0.5$.
We are still going to give the examples at this proportion anyway, since the difference with the optimal proportion of treated is small in our application.
With a total sample size of $N=100$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=100,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=1000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=1000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=10000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=10000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=100000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=100000,pE=pEhat,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
Remember that these sample sizes include ineligible units.
Figure \@ref(fig:MDEEligPlot) also shows what happens when sample size corresponds to only units eligibles to the program (plot on the right).
With a total sample size of $N^E=100$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=100,pE=1,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=1000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=1000,pE=1,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=10000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=10000,pE=1,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=100000$, we now reach a MDE of `r round(MDE.Elig.fun(pR=0.5,N=100000,pE=1,p1D=p1Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
#### Power Analysis for Encouragement Designs
In encouragement designs, we can make use Theorem \@ref(thm:asymWald) to show that:
\begin{align*}
\var{\hat{\Delta}^Y_{Wald}} & \approx \frac{1}{N}\frac{1}{p^{E}}\frac{1}{(p^D_1-p^D_0)^2}\left[\left(\frac{p^D}{p^R}\right)^2\frac{\var{Y_i|E_i=1,R_i=0}}{1-p^R}+\left(\frac{1-p^D}{1-p^R}\right)^2\frac{\var{Y_i|E_i=1,R_i=1}}{p^R}\right],
\end{align*}
with $p^E=\Pr(E_i=1)$, $p^D=\Pr(D_i=1|E_i=1)$, $p^R=\Pr(R_i=1|E_i=1)$, $p^{D}_0=\Pr(D_i=1|R_i=0,E_i=1)$ and $p^{D}_1=\Pr(D_i=1|R_i=1,E_i=1)$.
Note that $N$ corresponds to the size of the sample including the ineligible individuals which do not enter in the estimation of the treatment effect of the program.
MDEs and minimum required sample size can also be expressed in terms of $N^E=Np^E$, the size of the sample in terms of eligible units.
As with eligibility designs, there is a large number of parameters to find in order to compute this variance estimator.
We need to postulate a value for $p^{E}$ (unless we look for information on the sample size of the eligible population participating in the experiment, in which case we set $p^{E}=1$ and $N=N^E$ in the above formula), a value for $p^D$, for $p^R$ and for $p^D_1$.
For estimating $p^E$, we are going to choose the proportion of individuals eligible to the program ($\hat{p}^E=\Pr(y_i^B\leq\bar{y})$).
For $p^D$, we know that: $p^D=\Pr(D_i=1|E_i=1)=\Pr(D_i=1|R_i=1,E_i=1)\Pr(R_i=1|E_i=1)+\Pr(D_i=1|R_i=0,E_i=1)\Pr(R_i=0|E_i=1)=p^{D}_1p^R+p^{D}_0(1-p^R)$.
Since we can vary $p^R$, we only need to settle on a value for $p^{D}_1$ and $p^{D}_0$.
We are going to choose their actual values in the sample, $\Pr(D_i=1|R_i=1,E_i=1)$ and $\Pr(D_i=1|R_i=0,E_i=1)$, or $\hat{p}^{D}_1=$ `r round(mean(Ds[E==1 & R==1]),2)` and $\hat{p}^{D}_0=$ `r round(mean(Ds[E==1 & R==0]),2)`.
In real life applications, this choice is much more difficult.
It can for example be based on pilot studies where the response rate to the encouragement is tested.
Finally, we are going to assume that all conditional variances are equal to the pre-treatment variance among eligibles: we use $\hatvar{y^B_i|E_i=1}=$ `r round(var(yB[E==1]),2)` as an estimate of $\hatvar{y_i|R_i=1,E_i=1}=$ `r round(var(y[R==1 & E==1]),2)` and $\hatvar{y_i|R_i=0,E_i=1}=$ `r round(var(y0[R==0 & E==1]),2)`.
We can now write $C(E)$ for the total sample size $N$ (set $p^{E}=1$ for $N=N^E$):
\begin{align*}
\hat{C}(\hat{\Delta}^Y_{Wald}) & = \frac{1}{\hat{p}^{E}}\frac{1}{(\hat{p}^{D}_1-\hat{p}^{D}_0)^2}\left[\left(\frac{p^{D}_1p^R+p^{D}_0(1-p^R)}{p^R}\right)^2\frac{\hatvar{y^B_i|E_i=1}}{1-p^R}\right.\\
& \phantom{= \frac{1}{\hat{p}^{E}}\frac{1}{(\hat{p}^{D}_1-\hat{p}^{D}_0)^2}\left[\right.}\left.+\left(\frac{1-(p^{D}_1p^R+p^{D}_0(1-p^R))}{1-p^R}\right)^2\frac{\hatvar{y^B_i|E_i=1}}{p^R}\right].
\end{align*}
Let's write a function to compute the MDE in encouragement designs:
```{r CEEncourageFun,eval=TRUE,echo=TRUE,results='hide'}
CE.Encourage.fun <- function(pR,varYb,pE,p1D,p0D){
return((1/pE)*(1/(p1D-p0D))^2*(((p1D*pR+p0D*(1-pR))/pR)^2*varYb/(1-pR)+((1-p1D*pR-p0D*(1-pR))/(1-pR))^2*var(yB)/pR))
}
MDE.Encourage.fun <- function(pR,varYb,pE,p1D,p0D,...){
return(MDE(CE=CE.Encourage.fun(pR=pR,varYb=varYb,pE=pE,p1D=p1D,p0D=p0D),...))
}
# computing candidate participation rate, using the observed proportion below baryB
pEhat <- mean(E)
p1Dhat <- mean(Ds[E==1 & R==1])
p0Dhat <- mean(Ds[E==1 & R==0])
alpha <- 0.05
kappa <- 0.8
```
Let us finally check what Minimum Detectable Effect looks like as a function of $p^R$ and of sample size.
```{r MDEEncouragePlot,eval=TRUE,echo=TRUE,warning=FALSE,error=FALSE,message=FALSE,results='hide',fig.cap='Minimum detectable effect for the encouragement design',fig.subcap=c('Total sample','Sample of eligibles'),fig.align='center',out.width='50%',fig.pos='htbp'}
# total sample size (including ineligibles)
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.Encourage.fun,args=list(N=100,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.Encourage.fun,args=list(N=1000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.Encourage.fun,args=list(N=10000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.Encourage.fun,args=list(N=100000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name="N") +
ylab("MDE") +
xlab(expression(p^R)) +
theme_bw()
# Applicants sample size
pEhat <- 1
ggplot() +
xlim(0,1) +
ylim(0,2) +
geom_function(aes(color="100"),fun=MDE.Encourage.fun,args=list(N=100,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="1000"),fun=MDE.Encourage.fun,args=list(N=1000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="10000"),fun=MDE.Encourage.fun,args=list(N=10000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
geom_function(aes(color="100000"),fun=MDE.Encourage.fun,args=list(N=100000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE)) +
scale_color_discrete(name=expression(N^E)) +
ylab("MDE") +
xlab("p") +
theme_bw()
# computing application rate
pEhat <- mean(E)
```
Note that the minimum detectable effect is no longer minimized at $p^R=0.5$.
We are still going to give the examples at this proportion anyway, since the difference with the optimal proportion of treated is small in our application.
With a total sample size of $N=100$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=100,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=1000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=1000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=10000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=10000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N=100000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=100000,pE=pEhat,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
Remember that these sample sizes include ineligible units.
Figure \@ref(fig:MDEEncouragePlot) also shows what happens when sample size corresponds to only units eligible to the program (plot on the right).
With a total sample size of $N^E=100$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=100,pE=1,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=1000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=1000,pE=1,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=10000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=10000,pE=1,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
With a total sample size of $N^E=100000$, we now reach a MDE of `r round(MDE.Encourage.fun(pR=0.5,N=100000,pE=1,p1D=p1Dhat,p0D=p0Dhat,varYb=var(yB[E==1]),alpha=alpha,kappa=kappa,oneside=FALSE),2)`.
### Power Analysis for Natural Experiments
Power analysis can be useful for natural experiments as well, especially to assess the level of precision we are likely to achieve with a given sample size.
It is still possible to use Theorem \@ref(thm:PowerE) to conduct a power analysis for natural experiments, since they comply with Assumption \@ref(hyp:AsymNE).
In this section, we are going to focus in Difference in Differences and Regression Discontinuity Designs, since the case Instrumental Variables is similar to the case of encouragement designs seen just above.
#### Power Analysis for RDD
Let us start with power analysis for Regression Discontinuity designs.
They are very similar to the case of instrumental variables, and thus to the case of encouragement designs.
The only thing that differs is the size of the bandwidth around the discontinuity, which is going to be a major influence on the effective size of the sample.
Let us study the case of sharp RDD first, and then move on to fuzzy RDD.
##### Power analysis for sharp RDD
One way to conduct the power analysis for RDD in sharp designs would be to use the formula for the asymptotic variance of the RDD estimator derived in Theorem \@ref(thm:CLTSharpRDDLLR).
This route requires to specify the density of the running variable at the threshold and the bandwidth.
Another route simply uses the fact that the RDD estimator is equivalent to a with-without estimator on both sides of the threshold.
Let's first explore the first route.
Following Theorem \@ref(thm:CLTSharpRDDLLR), we have that the variance of the simplified sharp RDD estimator can be approximated by:
\begin{align*}
\var{\hat{\Delta}_{LLRRDD}} & \approx \frac{1}{Nh}\frac{4}{f_{Z}(\bar{z})}\left(\lim_{e\rightarrow 0^{\text{+}}}\var{Y_i|Z_i=\bar{z}-e}+\lim_{e\rightarrow 0^{\text{+}}}\var{Y_i|Z_i=\bar{z}+e}\right),
\end{align*}
To implement this formula, we need to derive the variance of the outcome at the threshold, the optimal bandwidth and the density of the the running variable at the threshold.
All these quantities can be derived or at least approximated using the available pre-treatment data.
```{example}
Let us try to implement this in the example.
```
In order to be able to implement the power computation on data observed before the treatment takes place, we need to have at least three treatment periods: two before and one after.
Selection will take place in period $2$.
We will estimate power on the period before that ($1$).
Treatment effects will be observed in period $3$.
We are going to use a setting similar to the one we used for staggered DID.
Let us first choose some parameter values:
```{r param.RDD.3,eval=TRUE,echo=TRUE,results='hide'}
param <- c(8,.5,.28,1500,0.9,
0.01,0.01,0.01,
0.05,0.05,
0,0.1,0.2,
0.05,0.1,0.15,
0.25,0.1,0.05,
1.5,1.25,1,
0.5,0,-0.5,
0.1,0.28,0)
names(param) <- c("barmu","sigma2mu","sigma2U","barY","rho",
"theta1","theta2","theta3",
"sigma2epsilon","sigma2eta",
"delta1","delta2","delta3",
"baralpha1","baralpha2","baralpha3",
"barchi1","barchi2","barchi3",
"kappa1","kappa2","kappa3",
"xi1","xi2","xi3",
"gamma","sigma2omega","rhoetaomega")
```
Let us now generate the corresponding data (in long format):
```{r SimulRDDStaggered,eval=TRUE,warning=FALSE,error=FALSE,message=FALSE,echo=TRUE,results='hide'}
set.seed(1234)
N <- 1000
T <- 3
cov.eta.omega <- matrix(c(param["sigma2eta"],param["rhoetaomega"]*sqrt(param["sigma2eta"]*param["sigma2omega"]),param["rhoetaomega"]*sqrt(param["sigma2eta"]*param["sigma2omega"]),param["sigma2omega"]),ncol=2,nrow=2)
data <- as.data.frame(mvrnorm(N*T,c(0,0),cov.eta.omega))
colnames(data) <- c('eta','omega')
# time and individual identifiers
data$time <- c(rep(1,N),rep(2,N),rep(3,N))
data$id <- rep((1:N),T)
# unit fixed effects
data$mu <- rep(rnorm(N,param["barmu"],sqrt(param["sigma2mu"])),T)
# time fixed effects
data$delta <- c(rep(param["delta1"],N),rep(param["delta2"],N),rep(param["delta3"],N))
data$baralphat <- c(rep(param["baralpha1"],N),rep(param["baralpha2"],N),rep(param["baralpha3"],N))
# building autocorrelated error terms
data$epsilon <- rnorm(N*T,0,sqrt(param["sigma2epsilon"]))
data$U[1:N] <- rnorm(N,0,sqrt(param["sigma2U"]))
data$U[(N+1):(2*N)] <- param["rho"]*data$U[1:N] + data$epsilon[(N+1):(2*N)]
data$U[(2*N+1):(3*N)] <- param["rho"]*data$U[(N+1):(2*N)] + data$epsilon[(2*N+1):(3*N)]
# potential outcomes in the absence of the treatment
data$y0 <- data$mu + data$delta + data$U
data$Y0 <- exp(data$y0)
# treatment status
Ds <- if_else(data$y0[(N+1):(2*N)]<=log(param["barY"]),1,0)
data$Ds <- rep(Ds,T)
```
With pre-treatment data (period $2$), we can compute a density of the outcomes at the threshold, and then we can use period $1$ to compute the optimal bandwidth estimator and the variance of outcomes on each side of the threshold.
Let's start with the density estimation and bandwidth choice first.
```{r densPowerSharpRDD,eval=TRUE,warning=FALSE,error=FALSE,message=FALSE,echo=TRUE,results='hide'}
# density function estimated at one point
densy2.ybar <- density(data$y0[1:N],n=1,from=log(param["barY"]),to=log(param["barY"]),kernel="biweight",bw="nrd")[[2]]
# optimal bandwidth by cross validation
kernel <- 'gaussian'
#bw <- 0.1
MSE.grid <- seq(0.1,1,by=.1)
MSE.llr.0 <- sapply(MSE.grid,MSE.llr,y=data$y0[1:N],D=Ds,x=data$y0[(N+1):(2*N)],kernel=kernel,d=0)
MSE.llr.1 <- sapply(MSE.grid,MSE.llr,y=data$y0[1:N],D=Ds,x=data$y0[(N+1):(2*N)],kernel=kernel,d=1)
bw0 <- MSE.grid[MSE.llr.0==min(MSE.llr.0)]
bw1 <- MSE.grid[MSE.llr.1==min(MSE.llr.1)]
# final bandwidth choice: mean of the two
bw <- (bw1+bw0)/2
```
Let us now compute the conditional variance on both sides.
We are going to assume that they are the same, as they are by construction on pre-treatment data.
We could increase the variance of the outcome in the absence of the treatment by the variance of the treatment effect if we had any idea of the magnitude of this parameter.
To compute the conditional variance, we need to estimate the regression function, then compute the residuals, then estimate the regression function ofthe squared residuals.
Let's go.