-------------
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 7.smcl
  log type:  smcl
 opened on:   7 Aug 2018, 16:46:40

.
. /* In this lab, we will show how to use various power commands in Stata to calculate sample size, power, and minimum detectable effects for use in study planning.  For a comprehensive introduction to this subject, read the intro section of the pdf help for PSS, the suite of Stata power and sample size commands.  In general these functions calculate sample size, power, or minimum detectable effects (MDEs).  By specifying two of these parameters, we get an estimate of the third.
>
> ********************************************************************************
> One-sample testing against a fixed null hypothesis.  In lab 5, we tested the null hypothesis that mean BMI among men with diabetes does not differ from the population mean among men without diabetes, assumed to be 24 kg/m^2. Now suppose the population mean (SD) among men with diabetes is 25 (2.7).  How large a sample would we need to provide 90% power to reject the null hypothesis that the mean is 24? To do this, we have to specify the MDE (via the two group means) and power. */
.
. power onemean 24 25, sd(2.7) power(.9)

Performing iteration ...


Estimated sample size for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m != m0

Study parameters:

alpha =      0.0500
power =      0.9000
delta =      0.3704
m0 =     24.0000
ma =     25.0000
sd =      2.7000


Estimated sample size:


N =          79

.
. /* Now suppose you have a limited budget and can only afford 50 particpants.  What would your power be? Here we specify the sample size rather than power. */
.
. power onemean 24 25, sd(2.7) n(50)


Estimated power for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m != m0

Study parameters:

alpha =      0.0500
N =          50
delta =      0.3704
m0 =     24.0000
ma =     25.0000
sd =      2.7000


Estimated power:


power =      0.7282

.
. /* So a little lower than the conventional level of 80%.  It might be reasonable to use a one-sided test in this context, although it can be controversial. */
.
. power onemean 24 25, sd(2.7) n(50) onesided


Estimated power for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m > m0

Study parameters:

alpha =      0.0500
N =          50
delta =      0.3704
m0 =     24.0000
ma =     25.0000
sd =      2.7000


Estimated power:


power =      0.8258

.
. /* That would fix the problem.
>
> Stata can also estimate the MDE, fixing the sample size at 50 and power at 80%. */
.
. power onemean 24, sd(2.7) n(50) power(.8)

Performing iteration ...


Estimated target mean for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m != m0  ; ma > m0

Study parameters:

alpha =      0.0500
power =      0.8000
N =          50
m0 =     24.0000
sd =      2.7000


Estimated effect size and  target mean:


delta =      0.4042
ma =     25.0913

.
. /* So this indicates the population mean BMI among men with diabetes would have to at least 25.1 for a sample of size 50 to provide 80% power to detect the difference.
>
> Stata can do any of this for a range of inputs, and output the results as both a table and a graph.  First do this to estimate sample size. Note the syntax to specify numeric lists: Stata automatically expands 25/27 as 25 26 27 28, and likewise interprets .8(.05).95 as .8 .85 .9 .95. */
.
. power onemean 24 (25/28), sd(2.7) power(.8(.05).95) ///
>         table graph(name(plot1, replace))

Performing iteration ...


Estimated sample size for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m != m0

  +---------------------------------------------------------+
  |alpha  power  N  delta  m0  ma  sd   |
  +---------------------------------------------------------+
  |.05  .8  60  .3704  24  25  2.7   |
  |.05  .85  68  .3704  24  25  2.7   |
  |.05  .9  79  .3704  24  25  2.7   |
  |.05  .95  97  .3704  24  25  2.7   |
  |.05  .8  17  .7407  24  26  2.7   |
  |.05  .85  19  .7407  24  26  2.7   |
  |.05  .9  22  .7407  24  26  2.7   |
  |.05  .95  26  .7407  24  26  2.7   |
  |.05  .8  9  1.111  24  27  2.7   |
  |.05  .85  10  1.111  24  27  2.7   |
  |.05  .9  11  1.111  24  27  2.7   |
  |.05  .95  13  1.111  24  27  2.7   |
  |.05  .8  6  1.481  24  28  2.7   |
  |.05  .85  7  1.481  24  28  2.7   |
  |.05  .9  7  1.481  24  28  2.7   |
  |.05  .95  9  1.481  24  28  2.7   |
  +---------------------------------------------------------+

.
. /* This plot shows that as the difference in mean increases, the sample size decreases and increments in power get cheaper.  The parameter delta is the between-group difference in means, standardized by the common SD.
>
> Now calculate MDEs, varying the sample size and SD. */
.
. power onemean 24, sd(2.5(0.25)3.25) n(50(10)100) power(.8) ///
>         table graph(name(plot2, replace))

Performing iteration ...


Estimated target mean for a one-sample mean test

t test
Ho: m = m0    versus  Ha: m != m0  ; ma > m0

  +---------------------------------------------------------+
  |alpha  power  N  delta  m0  ma  sd   |
  +---------------------------------------------------------+
  |.05  .8  50  .4042  24  25.01  2.5   |
  |.05  .8  50  .4042  24  25.11  2.75   |
  |.05  .8  50  .4042  24  25.21  3   |
  |.05  .8  50  .4042  24  25.31  3.25   |
  |.05  .8  60  .3677  24  24.92  2.5   |
  |.05  .8  60  .3677  24  25.01  2.75   |
  |.05  .8  60  .3677  24  25.1  3   |
  |.05  .8  60  .3677  24  25.2  3.25   |
  |.05  .8  70  .3396  24  24.85  2.5   |
  |.05  .8  70  .3396  24  24.93  2.75   |
  |.05  .8  70  .3396  24  25.02  3   |
  |.05  .8  70  .3396  24  25.1  3.25   |
  |.05  .8  80  .3171  24  24.79  2.5   |
  |.05  .8  80  .3171  24  24.87  2.75   |
  |.05  .8  80  .3171  24  24.95  3   |
  |.05  .8  80  .3171  24  25.03  3.25   |
  |.05  .8  90  .2985  24  24.75  2.5   |
  |.05  .8  90  .2985  24  24.82  2.75   |
  |.05  .8  90  .2985  24  24.9  3   |
  |.05  .8  90  .2985  24  24.97  3.25   |
  |.05  .8  100  .2829  24  24.71  2.5   |
  |.05  .8  100  .2829  24  24.78  2.75   |
  |.05  .8  100  .2829  24  24.85  3   |
  |.05  .8  100  .2829  24  24.92  3.25   |
  +---------------------------------------------------------+

.
. /* This plot shows that we can detect smaller between-group differences as the sample size increases and the SD decreases.
>
> Stata can do similar calculations for binary outcomes. Suppose the population prevalence of hypertension (HTN) is 40% and 25% among men with and without diabetes, respectively.  How large a sample of men with diabetes would we need to provide 80% power to reject a null prevalence of 25%? */
.
. power oneproportion .25 .4, power(.8)

Performing iteration ...


Estimated sample size for a one-sample proportion test

Score z test
Ho: p = p0    versus  Ha: p != p0

Study parameters:

alpha =      0.0500
power =      0.8000
delta =      0.1500
p0 =      0.2500
pa =      0.4000


Estimated sample size:


N =          71

.
. * Now calculate for a sample of 50 men with diabetes
.
. power oneproportion .25 .4, n(50)


Estimated power for a one-sample proportion test

Score z test
Ho: p = p0    versus  Ha: p != p0

Study parameters:

alpha =      0.0500
N =          50
delta =      0.1500
p0 =      0.2500
pa =      0.4000


Estimated power:


power =      0.6674

.
. * Last, get MDEs for a range of sample sizes and power
.
. power oneproportion .25, ///
>         n(50(10)100) power(.8(.05).95) table graph(name(plot3, replace))

Performing iteration ...


Estimated target proportion for a one-sample proportion test

Score z test
Ho: p = p0    versus  Ha: p != p0  ; pa > p0

  +-------------------------------------------------+
  |alpha  power  N  delta  p0  pa   |
  +-------------------------------------------------+
  |.05  .8  50  .1789  .25  .4289   |
  |.05  .8  60  .1631  .25  .4131   |
  |.05  .8  70  .1507  .25  .4007   |
  |.05  .8  80  .1408  .25  .3908   |
  |.05  .8  90  .1326  .25  .3826   |
  |.05  .8  100  .1256  .25  .3756   |
  |.05  .85  50  .1928  .25  .4428   |
  |.05  .85  60  .1757  .25  .4257   |
  |.05  .85  70  .1624  .25  .4124   |
  |.05  .85  80  .1517  .25  .4017   |
  |.05  .85  90  .1428  .25  .3928   |
  |.05  .85  100  .1353  .25  .3853   |
  |.05  .9  50  .2104  .25  .4604   |
  |.05  .9  60  .1917  .25  .4417   |
  |.05  .9  70  .1772  .25  .4272   |
  |.05  .9  80  .1655  .25  .4155   |
  |.05  .9  90  .1558  .25  .4058   |
  |.05  .9  100  .1476  .25  .3976   |
  |.05  .95  50  .2363  .25  .4863   |
  |.05  .95  60  .2155  .25  .4655   |
  |.05  .95  70  .1992  .25  .4492   |
  |.05  .95  80  .1861  .25  .4361   |
  |.05  .95  90  .1752  .25  .4252   |
  |.05  .95  100  .1659  .25  .4159   |
  +-------------------------------------------------+

.
. /* So MDEs decrease with sample size but increase with power.
>
> ********************************************************************************
> Two-sample testing.
>
> Now suppose we need to estimate mean BMI in men without diabetes,rather than assuming we know the population mean for this group. You'll see that the pricetag for estimating the control mean is considerable.
>
> By default, for the two-sample calculations the SD is assumed to be the same in both groups, and specified using a single sd() option.  But the command does allow us to specify different SDs. To see how this works, we'll assume that the SDs are 2.4 and 2.7 in men without and with diabetes respectively. */
.
. power twomeans 24 25, sd1(2.4) sd2(2.7) power(.9)

Performing iteration ...


Estimated sample sizes for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1

Study parameters:

alpha =      0.0500
power =      0.9000
delta =      1.0000
m1 =     24.0000
m2 =     25.0000
sd1 =      2.4000
sd2 =      2.7000


Estimated sample sizes:


N =         278
N per group =         139

.
. /* Now suppose it is twice as hard to recruit men with diabetes, so you can recruit at most 75 men with diabetes and 150 controls. What would your power be? */
.
. power twomeans 24 25, sd1(2.4) sd2(2.7) n1(150) n2(75)


Estimated power for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1

Study parameters:

alpha =      0.0500
N =         225
N1 =         150
N2 =          75
N2/N1 =      0.5000
delta =      1.0000
m1 =     24.0000
m2 =     25.0000
sd1 =      2.4000
sd2 =      2.7000


Estimated power:


power =      0.7692

.
. /* So a little lower than the conventional level of 80%.  It might be reasonable to use a one-sided test in this context. */
.
. power twomeans 24 25, sd1(2.4) sd2(2.7) n1(150) n2(75) onesided


Estimated power for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 > m1

Study parameters:

alpha =      0.0500
N =         225
N1 =         150
N2 =          75
N2/N1 =      0.5000
delta =      1.0000
m1 =     24.0000
m2 =     25.0000
sd1 =      2.4000
sd2 =      2.7000


Estimated power:


power =      0.8547

.
. /* We can estimate the MDE with a sample of 225, fixing power at 80%. */
.
. power twomeans 24, sd1(2.4) sd2(2.7) n1(150) n2(75) power(.8)

Performing iteration ...


Estimated experimental-group mean for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1  ; m2 > m1

Study parameters:

alpha =      0.0500
power =      0.8000
N =         225
N1 =         150
N2 =          75
N2/N1 =      0.5000
m1 =     24.0000
sd1 =      2.4000
sd2 =      2.7000


Estimated effect size and  experimental-group mean:


delta =      1.0391
m2 =     25.0391

.
. /* With a continuous outcome, the mean in the reference group doesn't affect the MDE. */
.
. power twomeans 0, sd1(2.4) sd2(2.7) n1(150) n2(75) power(.8)

Performing iteration ...


Estimated experimental-group mean for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1  ; m2 > m1

Study parameters:

alpha =      0.0500
power =      0.8000
N =         225
N1 =         150
N2 =          75
N2/N1 =      0.5000
m1 =      0.0000
sd1 =      2.4000
sd2 =      2.7000


Estimated effect size and  experimental-group mean:


delta =      1.0391
m2 =      1.0391

.
. /* So both calculations indicate that population mean BMI in the two groups would need to differ by about 1.04 kg/m^2 for a sample of size 225 to provide 80% power.
>
> Now calculate sample size for a range of means among men with diabetes and power from 80 to 95%.  */
.
. power twomeans 24 (25/28), sd1(2.4) sd2(2.7) power(.8(.05).95) ///
>         table graph(name(plot4, replace))

Performing iteration ...


Estimated sample sizes for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1

  +---------------------------------------------------------------------------------+
  |alpha  power  N  N1  N2  delta  m1  m2  sd1  sd2   |
  +---------------------------------------------------------------------------------+
  |.05  .8  208  104  104  1  24  25  2.4  2.7   |
  |.05  .85  238  119  119  1  24  25  2.4  2.7   |
  |.05  .9  278  139  139  1  24  25  2.4  2.7   |
  |.05  .95  342  171  171  1  24  25  2.4  2.7   |
  |.05  .8  54  27  27  2  24  26  2.4  2.7   |
  |.05  .85  62  31  31  2  24  26  2.4  2.7   |
  |.05  .9  72  36  36  2  24  26  2.4  2.7   |
  |.05  .95  88  44  44  2  24  26  2.4  2.7   |
  |.05  .8  26  13  13  3  24  27  2.4  2.7   |
  |.05  .85  30  15  15  3  24  27  2.4  2.7   |
  |.05  .9  34  17  17  3  24  27  2.4  2.7   |
  |.05  .95  40  20  20  3  24  27  2.4  2.7   |
  |.05  .8  16  8  8  4  24  28  2.4  2.7   |
  |.05  .85  18  9  9  4  24  28  2.4  2.7   |
  |.05  .9  20  10  10  4  24  28  2.4  2.7   |
  |.05  .95  24  12  12  4  24  28  2.4  2.7   |
  +---------------------------------------------------------------------------------+

.
. /* Now calculate MDEs, varying the sample size and SDs, fixing the ratio of the sample sizes at 2:1. */
.
. power twomeans 24, sd1(2.2 2.6) sd2(2.5 2.9) n(250(25)400) nratio(0.5) power(.8) ///
>         table graph(name(plot5, replace))

Performing iteration ...


Estimated experimental-group mean for a two-sample means test

Satterthwaite's t test assuming unequal variances
Ho: m2 = m1    versus  Ha: m2 != m1  ; m2 > m1

  +---------------------------------------------------------------------------------+
  |alpha  power  N  N1  N2  nratio  delta  m1  m2  sd1   |
  +---------------------------------------------------------------------------------+
  |.05  .8  250  166  83  .5  .9114  24  24.91  2.2   |
  |.05  .8  250  166  83  .5  1.02  24  25.02  2.2   |
  |.05  .8  250  166  83  .5  .9597  24  24.96  2.6   |
  |.05  .8  250  166  83  .5  1.063  24  25.06  2.6   |
  |.05  .8  275  183  91  .5  .8693  24  24.87  2.2   |
  |.05  .8  275  183  91  .5  .9724  24  24.97  2.2   |
  |.05  .8  275  183  91  .5  .9152  24  24.92  2.6   |
  |.05  .8  275  183  91  .5  1.014  24  25.01  2.6   |
  |.05  .8  300  200  100  .5  .8294  24  24.83  2.2   |
  |.05  .8  300  200  100  .5  .9276  24  24.93  2.2   |
  |.05  .8  300  200  100  .5  .8735  24  24.87  2.6   |
  |.05  .8  300  200  100  .5  .9671  24  24.97  2.6   |
  |.05  .8  325  216  108  .5  .7978  24  24.8  2.2   |
  |.05  .8  325  216  108  .5  .8922  24  24.89  2.2   |
  |.05  .8  325  216  108  .5  .8402  24  24.84  2.6   |
  |.05  .8  325  216  108  .5  .9303  24  24.93  2.6   |
  |.05  .8  350  233  116  .5  .7691  24  24.77  2.2   |
  |.05  .8  350  233  116  .5  .8601  24  24.86  2.2   |
  |.05  .8  350  233  116  .5  .8099  24  24.81  2.6   |
  |.05  .8  350  233  116  .5  .8967  24  24.9  2.6   |
  |.05  .8  375  250  125  .5  .741  24  24.74  2.2   |
  |.05  .8  375  250  125  .5  .8287  24  24.83  2.2   |
  |.05  .8  375  250  125  .5  .7805  24  24.78  2.6   |
  |.05  .8  375  250  125  .5  .8641  24  24.86  2.6   |
  |.05  .8  400  266  133  .5  .7182  24  24.72  2.2   |
  |.05  .8  400  266  133  .5  .8031  24  24.8  2.2   |
  |.05  .8  400  266  133  .5  .7565  24  24.76  2.6   |
  |.05  .8  400  266  133  .5  .8375  24  24.84  2.6   |
  +---------------------------------------------------------------------------------+
  +---------+
  |sd2   |
  +---------+
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  |2.5   |
  |2.9   |
  +---------+


Note: N1 and N2 have been rounded down.


.
. /* Pretty much the same story as in the one-sample case, except that now the overall sample size is almost 4 times larger!
>
> Now we will do this for binary outcomes, again assuming hypertension prevalence of 25% and 40% among men without and with diabetes. Again, suppose men with diabetes are twice as hard to recruit, so the ratio of the sample sizes is 0.5. */
.
. power twoproportions .25 .4, power(.8) nratio(0.5)

Performing iteration ...


Estimated sample sizes for a two-sample proportions test

Pearson's chi-squared test
Ho: p2 = p1    versus  Ha: p2 != p1

Study parameters:

alpha =      0.0500
power =      0.8000
delta =      0.1500  (difference)
p1 =      0.2500
p2 =      0.4000
N2/N1 =      0.5000


Estimated sample sizes:


N =         336
N1 =         224
N2 =         112

.
. /* Note that in contrast to continuous outcomes, for binary outcomes the reference proportion does matter, because it drives the variance. */
.
. power twoproportions .2 .35, power(.8) nratio(0.5)

Performing iteration ...


Estimated sample sizes for a two-sample proportions test

Pearson's chi-squared test
Ho: p2 = p1    versus  Ha: p2 != p1

Study parameters:

alpha =      0.0500
power =      0.8000
delta =      0.1500  (difference)
p1 =      0.2000
p2 =      0.3500
N2/N1 =      0.5000


Estimated sample sizes:


N =         303
N1 =         202
N2 =         101

. power twoproportions .3 .45, power(.8) nratio(0.5)

Performing iteration ...


Estimated sample sizes for a two-sample proportions test

Pearson's chi-squared test
Ho: p2 = p1    versus  Ha: p2 != p1

Study parameters:

alpha =      0.0500
power =      0.8000
delta =      0.1500  (difference)
p1 =      0.3000
p2 =      0.4500
N2/N1 =      0.5000


Estimated sample sizes:


N =         362
N1 =         241
N2 =         121
N2/N1 =      0.5021

.
. /* Now calculate power for sample of 75/150 men with/without diabetes. */
.
. power twoproportions .25 .4, n1(150) n2(75)


Estimated power for a two-sample proportions test

Pearson's chi-squared test
Ho: p2 = p1    versus  Ha: p2 != p1

Study parameters:

alpha =      0.0500
N =         225
N1 =         150
N2 =          75
N2/N1 =      0.5000
delta =      0.1500  (difference)
p1 =      0.2500
p2 =      0.4000


Estimated power:


power =      0.6348

.
. /* Last, get MDEs for a range of overall sample sizes, fixing the reference mean at 25%, the ratio of men with and without diabetes at 50%, and varying power from 80 to 95%. */
.
. power twoproportions .25, n(200(25)300) nratio(0.5) power(.8(.05).95) ///
>         table graph(name(plot6, replace))

Performing iteration ...


Estimated experimental-group proportion for a two-sample proportions test

Pearson's chi-squared test
Ho: p2 = p1    versus  Ha: p2 != p1  ; p2 > p1

  +-------------------------------------------------------------------------+
  |alpha  power  N  N1  N2  nratio  delta  p1  p2   |
  +-------------------------------------------------------------------------+
  |.05  .8  200  133  66  .5  .1976  .25  .4476   |
  |.05  .8  225  150  75  .5  .185  .25  .435   |
  |.05  .8  250  166  83  .5  .1754  .25  .4254   |
  |.05  .8  275  183  91  .5  .167  .25  .417   |
  |.05  .8  300  200  100  .5  .1591  .25  .4091   |
  |.05  .85  200  133  66  .5  .2123  .25  .4623   |
  |.05  .85  225  150  75  .5  .1988  .25  .4488   |
  |.05  .85  250  166  83  .5  .1885  .25  .4385   |
  |.05  .85  275  183  91  .5  .1794  .25  .4294   |
  |.05  .85  300  200  100  .5  .1709  .25  .4209   |
  |.05  .9  200  133  66  .5  .2307  .25  .4807   |
  |.05  .9  225  150  75  .5  .2161  .25  .4661   |
  |.05  .9  250  166  83  .5  .205  .25  .455   |
  |.05  .9  275  183  91  .5  .1951  .25  .4451   |
  |.05  .9  300  200  100  .5  .1859  .25  .4359   |
  |.05  .95  200  133  66  .5  .2579  .25  .5079   |
  |.05  .95  225  150  75  .5  .2417  .25  .4917   |
  |.05  .95  250  166  83  .5  .2293  .25  .4793   |
  |.05  .95  275  183  91  .5  .2184  .25  .4684   |
  |.05  .95  300  200  100  .5  .2081  .25  .4581   |
  +-------------------------------------------------------------------------+


Note: N1 and N2 have been rounded down.


.
. /* What does this plot indicate about the effects of sample size and power on the MDE? */
.
. log close
      name:  <unnamed>
       log:  /Users/eric/Work/MB/ATCR/200/Labs/lab 7.smcl
  log type:  smcl
 closed on:   7 Aug 2018, 16:46:47
-------------