-------------
      name:  <unnamed>
       log:  /Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 7/HW 7/assign7_stephenchang.smcl
  log type:  smcl
 opened on:  20 May 2017, 14:38:58

.
. /* Assignment Week 7
> Specify a hypothesis regarding a particular exposure and outcome and a
> binary effect modifier including specific measures of association
> (specify the magnitudes of that association you anticipate: I suggest making
> everything cross-sectional).
>
> Using the software of your choice, generate a population with 1000 people under
> a causal structure consistent with this hypothesis. Draw a simple random sample
> 100 individuals from this population and estimate the population average
> exposure-outcome association and the association stratified by your modifier of
> interest within this subset.  Repeat this 10 times and write the parameter
> estimates and CI each time.
>
> Repeat the data set construction, setting the causal effect to the null.  Again
> repeat this 10 times and write the parameter estimate and CI each time (if you
> figure out how to automate it, run it 1000 times and post the histogram of the
> parameter estimates and p-values).
>
> Use your code above and also a canned software command to estimate statistical
> power to detect the difference in means under the settings below:
> *n=100, μ0=.02, μ1=.12, SD=1, α=.05
> *n=100,μ0=.02, μ1=.12, SD=2, α=.05
> *n=500, μ0=.3, μ1=.3, SD=1, α=.05
> For each of the 3 settings above, what is the power to detect whether the ratio
> of the means=1?
> */
.
. clear

.
. set obs 1000


number of observations (_N)  was 0,
now 1,000


.
. set seed 98770

.
. /* Model: BMD = beta0 + beta1A*CalciumIntake[Smoking] + beta1B*CalciumIntake[NoSmoking] + beta2*Smoking + ErrorTerm
>           BMD and CalcumIntake are continuous,
>                   Smoking is a dichotomous effect modifier
>
>                   if smoking calcium intake less effecive in increasing BMD
>                   => beta1A < beta1B
>
>                   pick:
>                   beta0 = 1.0 g/cm^2
>                   beta1A = 0.01 g/cm^2 per 1000 mg Calcium / day
>                   beta1B = 0.03 g/cm^2 per 1000 mg Calcium / day
>                   beta2 = -0.1 g/cm^2  (smokers w/o Calcium intake should have somewhat smaller
>                                         BMD values than non-smokers w/o Calcium intake)
>
>                   */
.
. * ---- 1. Data Generation
. gen Smoking = runiform()>=0.5  /* effect modifier; total sample has ~half people that are smoking */

.
. gen CalciumIntake=rnormal(1000,300) /* Calcium Intake, mean 1000 mg/day, std-dev 300 mg/day */

. replace CalciumIntake = 0 if CalciumIntake < 0 /* don't allow negative Intake */
(0 real changes made)

.
. gen CalciumIntakeSmoking = CalciumIntake if Smoking == 1
(507 missing values generated)

. replace CalciumIntakeSmoking = 0 if Smoking == 0
(507 real changes made)

.
. gen CalciumIntakeNotSmoking = CalciumIntake if Smoking == 0
(493 missing values generated)

. replace CalciumIntakeNotSmoking = 0 if Smoking == 1
(493 real changes made)

.
. gen ErrorTerm = rnormal(0, 0.1)  /* normally distributed error term */

.
. gen BMD = 1.0 + 0.01 * CalciumIntakeSmoking + 0.03 * CalciumIntakeNotSmoking -0.1 * Smoking + ErrorTerm

.
. * ---- 2. run regression on 10 random samples of 100 patients each
.
. * the population average exposure-outcome association, i.e., don't account for eff. modifier
. bootstrap, reps(10) size(100) noisily: regress BMD CalciumIntake
bootstrap: First call to regress with data as is:

. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =     1,000
-------------+----------------------------------   F(1, 998)       =    292.01
       Model |  31205.6872         1  31205.6872   Prob > F        =    0.0000
    Residual |  106650.317       998  106.864045   R-squared       =    0.2264
-------------+----------------------------------   Adj R-squared   =    0.2256
       Total |  137856.005       999  137.993999   Root MSE        =    10.338

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0190545    .0011151     17.09    0.000      .0168664     .0212426
        _cons |    2.004213    1.147482      1.75    0.081     -.2475413     4.255967
--------------+----------------------------------------------------------------

Bootstrap replications (10)
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     39.49
       Model |  4589.36692         1  4589.36692   Prob > F        =    0.0000
    Residual |  11388.0052        98  116.204134   R-squared       =    0.2872
-------------+----------------------------------   Adj R-squared   =    0.2800
       Total |  15977.3721        99  161.387597   Root MSE        =     10.78

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |     .019565    .0031133      6.28    0.000      .0133868     .0257431
        _cons |    3.000773    3.344565      0.90    0.372     -3.636407     9.637954
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     41.58
       Model |  4557.90374         1  4557.90374   Prob > F        =    0.0000
    Residual |  10741.4092        98  109.606216   R-squared       =    0.2979
-------------+----------------------------------   Adj R-squared   =    0.2908
       Total |  15299.3129        99  154.538514   Root MSE        =    10.469

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0269773    .0041834      6.45    0.000      .0186754     .0352793
        _cons |   -5.791539    4.380094     -1.32    0.189     -14.48369     2.900615
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     21.65
       Model |   2275.9501         1   2275.9501   Prob > F        =    0.0000
    Residual |  10302.9605        98   105.13225   R-squared       =    0.1809
-------------+----------------------------------   Adj R-squared   =    0.1726
       Total |  12578.9106        99  127.059703   Root MSE        =    10.253

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0166687    .0035825      4.65    0.000      .0095593     .0237781
        _cons |    4.586797    3.638756      1.26    0.210     -2.634196     11.80779
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     28.39
       Model |  2879.37388         1  2879.37388   Prob > F        =    0.0000
    Residual |   9940.3098        98  101.431733   R-squared       =    0.2246
-------------+----------------------------------   Adj R-squared   =    0.2167
       Total |  12819.6837        99  129.491754   Root MSE        =    10.071

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0194998    .0036599      5.33    0.000      .0122368     .0267627
        _cons |      1.9592    3.637379      0.54    0.591     -5.259059     9.177459
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     21.35
       Model |  2203.85388         1  2203.85388   Prob > F        =    0.0000
    Residual |  10115.1135        98  103.215444   R-squared       =    0.1789
-------------+----------------------------------   Adj R-squared   =    0.1705
       Total |  12318.9674        99  124.434014   Root MSE        =     10.16

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0197824    .0042811      4.62    0.000      .0112866     .0282782
        _cons |    .3580954    4.303101      0.08    0.934     -8.181268     8.897459
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     14.42
       Model |  1517.45188         1  1517.45188   Prob > F        =    0.0003
    Residual |  10311.8183        98  105.222636   R-squared       =    0.1283
-------------+----------------------------------   Adj R-squared   =    0.1194
       Total |  11829.2702        99  119.487577   Root MSE        =    10.258

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0125262    .0032985      3.80    0.000      .0059804     .0190719
        _cons |    7.457918    3.441096      2.17    0.033      .6291737     14.28666
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     34.62
       Model |  3683.66249         1  3683.66249   Prob > F        =    0.0000
    Residual |  10426.7055        98  106.394954   R-squared       =    0.2611
-------------+----------------------------------   Adj R-squared   =    0.2535
       Total |  14110.3679        99  142.528969   Root MSE        =    10.315

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0217282    .0036927      5.88    0.000      .0144002     .0290563
        _cons |    .2143975    3.778695      0.06    0.955     -7.284299     7.713094
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     27.40
       Model |  2761.18361         1  2761.18361   Prob > F        =    0.0000
    Residual |  9876.11352        98  100.776669   R-squared       =    0.2185
-------------+----------------------------------   Adj R-squared   =    0.2105
       Total |  12637.2971        99  127.649466   Root MSE        =    10.039

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0175534    .0033535      5.23    0.000      .0108986     .0242083
        _cons |    3.216072    3.330537      0.97    0.337      -3.39327     9.825415
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     16.46
       Model |  1702.16732         1  1702.16732   Prob > F        =    0.0001
    Residual |  10132.3137        98  103.390956   R-squared       =    0.1438
-------------+----------------------------------   Adj R-squared   =    0.1351
       Total |   11834.481        99  119.540212   Root MSE        =    10.168

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0143796    .0035439      4.06    0.000      .0073468     .0214125
        _cons |    6.755841    3.597928      1.88    0.063     -.3841304     13.89581
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        =     36.86
       Model |    4093.626         1    4093.626   Prob > F        =    0.0000
    Residual |  10884.7878        98  111.069263   R-squared       =    0.2733
-------------+----------------------------------   Adj R-squared   =    0.2659
       Total |  14978.4138        99  151.297109   Root MSE        =    10.539

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0212909     .003507      6.07    0.000      .0143314     .0282505
        _cons |    .4812509    3.661148      0.13    0.896     -6.784178      7.74668
--------------+----------------------------------------------------------------

Linear regression                               Number of obs     =      1,000
                                                Replications      =         10
                                                Wald chi2(1)      =      22.03
                                                Prob > chi2       =     0.0000
                                                R-squared         =     0.2264
                                                Adj R-squared     =     0.2256
                                                Root MSE          =    10.3375

--------------+----------------------------------------------------------------
              |   Observed    Bootstrap                        Norm al-based
           BMD |      Coef.    Std. Err.       z    P>|z|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0190545    .0040592      4.69    0.000      .0110986     .0270105
        _cons |    2.004213    3.800265      0.53    0.598      -5.44417     9.452595
--------------+----------------------------------------------------------------

.
. * the association stratified by your modifier of interest
. bootstrap, reps(10) size(100) noisily: regress BMD Smoking##c.CalciumIntake
bootstrap: First call to regress with data as is:

. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =     1,000
-------------+----------------------------------   F(3, 996)       >  99999.00
       Model |  137846.666         3  45948.8888   Prob > F        =    0.0000
    Residual |  9.33819578       996  .009375699   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  137856.005       999  137.993999   Root MSE        =    .09683

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1139296    .0215362     -5.29    0.000     -.1561911    -.0716681
          CalciumIntake |    .0299713    .0000153   1962.23    0.000      .0299413     .0300012
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199719    .0000209   -954.06    0.000      -.020013    -.0199308
                        |
                  _cons |    1.028911    .0156329     65.82    0.000       .998234     1.059588
------------------------+----------------------------------------------------------------

Bootstrap replications (10)
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  14500.6083         3   4833.5361   Prob > F        =    0.0000
    Residual |  .903353832        96  .009409936   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  14501.5116        99  146.479916   Root MSE        =      .097

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1691619    .0744032     -2.27    0.025      -.316851    -.0214727
          CalciumIntake |    .0299128    .0000582    513.74    0.000      .0297973     .0300284
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199481    .0000728   -274.05    0.000     -.0200926    -.0198036
                        |
                  _cons |     1.09429     .061001     17.94    0.000      .9732038     1.215376
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |   13323.151         3  4441.05033   Prob > F        =    0.0000
    Residual |  .910025887        96  .009479436   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |   13324.061        99  134.586475   Root MSE        =    .09736

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1180278    .0719342     -1.64    0.104     -.2608161     .0247605
          CalciumIntake |    .0299256    .0000458    653.16    0.000      .0298347     .0300165
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199604    .0000713   -280.03    0.000     -.0201018    -.0198189
                        |
                  _cons |    1.081556    .0460797     23.47    0.000      .9900883     1.173023
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  13497.7714         3  4499.25715   Prob > F        =    0.0000
    Residual |  .909826543        96   .00947736   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  13498.6813        99  136.350316   Root MSE        =    .09735

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.0554188    .0718007     -0.77    0.442      -.197942     .0871044
          CalciumIntake |    .0300026    .0000518    579.19    0.000      .0298998     .0301054
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0200615    .0000704   -285.02    0.000     -.0202012    -.0199218
                        |
                  _cons |    1.009756    .0529161     19.08    0.000      .9047188     1.114794
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  15827.1058         3  5275.70194   Prob > F        =    0.0000
    Residual |  .893723634        96  .009309621   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  15827.9995        99  159.878783   Root MSE        =    .09649

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.0652996    .0683239     -0.96    0.342     -.2009214     .0703223
          CalciumIntake |    .0299731    .0000468    640.17    0.000      .0298802      .030066
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199875    .0000647   -309.14    0.000     -.0201159    -.0198592
                        |
                  _cons |    1.017419    .0506524     20.09    0.000      .9168746     1.117963
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |   13952.977         3  4650.99232   Prob > F        =    0.0000
    Residual |  1.00469747        96  .010465599   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  13953.9817        99   140.94931   Root MSE        =     .1023

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1900684     .070432     -2.70    0.008     -.3298748     -.050262
          CalciumIntake |    .0298533    .0000488    611.76    0.000      .0297564     .0299502
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0198914    .0000677   -293.93    0.000     -.0200257     -.019757
                        |
                  _cons |    1.166516    .0501764     23.25    0.000      1.066917     1.266115
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  13014.0019         3  4338.00064   Prob > F        =    0.0000
    Residual |  .971984598        96   .01012484   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  13014.9739        99  131.464383   Root MSE        =    .10062

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.0520091     .071555     -0.73    0.469     -.1940447     .0900265
          CalciumIntake |    .0300435    .0000471    637.36    0.000        .02995     .0301371
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0200271    .0000702   -285.13    0.000     -.0201665    -.0198877
                        |
                  _cons |    .9502599    .0467586     20.32    0.000      .8574448     1.043075
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |   13475.518         3  4491.83932   Prob > F        =    0.0000
    Residual |  .961490736        96  .010015528   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  13476.4795        99  136.126055   Root MSE        =    .10008

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |    .0028424    .0688114      0.04    0.967     -.1337471     .1394319
          CalciumIntake |    .0300463    .0000547    548.85    0.000      .0299376      .030155
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0200842     .000067   -299.63    0.000     -.0202173    -.0199511
                        |
                  _cons |    .9475204    .0556733     17.02    0.000      .8370098     1.058031
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  14457.4907         3  4819.16357   Prob > F        =    0.0000
    Residual |  .832284253        96  .008669628   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |   14458.323        99  146.043667   Root MSE        =    .09311

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1347341    .0625966     -2.15    0.034     -.2589875    -.0104808
          CalciumIntake |    .0300511    .0000409    734.94    0.000      .0299699     .0301323
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199147     .000065   -306.59    0.000     -.0200436    -.0197858
                        |
                  _cons |     .939027    .0407665     23.03    0.000      .8581061     1.019948
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |  11917.4661         3  3972.48871   Prob > F        =    0.0000
    Residual |  .914833735        96  .009529518   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |   11918.381        99  120.387687   Root MSE        =    .09762

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |    -.128612    .0677671     -1.90    0.061     -.2631286     .0059046
          CalciumIntake |    .0299347    .0000551    543.65    0.000      .0298254      .030044
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199444    .0000674   -295.95    0.000     -.0200782    -.0198106
                        |
                  _cons |    1.046459    .0540746     19.35    0.000      .9391223     1.153797
------------------------+----------------------------------------------------------------
. regress BMD Smoking##c.CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(3, 96)        >  99999.00
       Model |   13123.217         3  4374.40567   Prob > F        =    0.0000
    Residual |   .78823604        96  .008210792   R-squared       =    0.9999
-------------+----------------------------------   Adj R-squared   =    0.9999
       Total |  13124.0052        99   132.56571   Root MSE        =    .09061

------------------------+----------------------------------------------------------------
                     BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1628457    .0606693     -2.68    0.009     -.2832733    -.0424181
          CalciumIntake |    .0300283    .0000446    673.40    0.000      .0299397     .0301168
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199284    .0000586   -339.99    0.000     -.0200447     -.019812
                        |
                  _cons |     .962684    .0447959     21.49    0.000      .8737648     1.051603
------------------------+----------------------------------------------------------------

Linear regression                               Number of obs     =      1,000
                                                Replications      =         10
                                                Wald chi2(3)      = 1341698.20
                                                Prob > chi2       =     0.0000
                                                R-squared         =     0.9999
                                                Adj R-squared     =     0.9999
                                                Root MSE          =     0.0968

------------------------+----------------------------------------------------------------
                        |   Observed    Bootstrap                        Norm al-based
                     BMD |      Coef.    Std. Err.       z    P>|z|      [95% Con f. Interval]
------------------------+----------------------------------------------------------------
              1.Smoking |   -.1139296    .0621189     -1.83    0.067     -.2356805     .0078213
          CalciumIntake |    .0299713    .0000682    439.47    0.000      .0298376     .0301049
                        |
Smoking#c.CalciumIntake |
                     1  |   -.0199719    .0000641   -311.67    0.000     -.0200975    -.0198463
                        |
                  _cons |    1.028911    .0756611     13.60    0.000      .8806181     1.177204
------------------------+----------------------------------------------------------------

.
. * ---- 3. Setting the causal effect to the null and repeat 1. & 2.
. drop BMD

. gen BMD = 1.0 + 0.02 * CalciumIntake + ErrorTerm

.
. * the population average exposure-outcome association, i.e., don't account for eff. modifier
. bootstrap, reps(10) size(100) noisily: regress BMD CalciumIntake
bootstrap: First call to regress with data as is:

. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =     1,000
-------------+----------------------------------   F(1, 998)       >  99999.00
       Model |  34332.6744         1  34332.6744   Prob > F        =    0.0000
    Residual |  9.40269665       998   .00942154   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  34342.0771       999  34.3764535   Root MSE        =    .09706

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199864    .0000105   1908.94    0.000      .0199659      .020007
        _cons |    1.020791    .0107743     94.74    0.000      .9996482     1.041934
--------------+----------------------------------------------------------------

Bootstrap replications (10)
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3310.23431         1  3310.23431   Prob > F        =    0.0000
    Residual |  .816624525        98  .008332903   R-squared       =    0.9998
-------------+----------------------------------   Adj R-squared   =    0.9998
       Total |  3311.05093        99  33.4449589   Root MSE        =    .09128

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199957    .0000317    630.28    0.000      .0199328     .0200587
        _cons |    1.001822    .0316862     31.62    0.000       .938942     1.064702
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3444.28342         1  3444.28342   Prob > F        =    0.0000
    Residual |  .996514138        98  .010168512   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3445.27993        99  34.8008074   Root MSE        =    .10084

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0200372    .0000344    582.00    0.000      .0199688     .0201055
        _cons |    .9650738    .0363245     26.57    0.000       .892989     1.037159
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  2984.52678         1  2984.52678   Prob > F        =    0.0000
    Residual |  .758354252        98  .007738309   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  2985.28513        99  30.1543953   Root MSE        =    .08797

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199486    .0000321    621.03    0.000      .0198849     .0200124
        _cons |    1.055382    .0332941     31.70    0.000      .9893109     1.121453
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3636.00343         1  3636.00343   Prob > F        =    0.0000
    Residual |   .96319935        98  .009828565   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3636.96663        99  36.7370366   Root MSE        =    .09914

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0200241    .0000329    608.23    0.000      .0199587     .0200894
        _cons |    .9635048    .0344384     27.98    0.000       .895163     1.031847
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3486.30668         1  3486.30668   Prob > F        =    0.0000
    Residual |  .793091246        98  .008092768   R-squared       =    0.9998
-------------+----------------------------------   Adj R-squared   =    0.9998
       Total |  3487.09977        99  35.2232301   Root MSE        =    .08996

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199699    .0000304    656.35    0.000      .0199095     .0200303
        _cons |    1.051733    .0332769     31.61    0.000      .9856965      1.11777
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3522.58435         1  3522.58435   Prob > F        =    0.0000
    Residual |  .899390783        98  .009177457   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3523.48374        99  35.5907449   Root MSE        =     .0958

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199314    .0000322    619.54    0.000      .0198675     .0199952
        _cons |     1.07559    .0316983     33.93    0.000      1.012686     1.138494
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |  3471.70149         1  3471.70149   Prob > F        =    0.0000
    Residual |  .871947403        98  .008897422   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3472.57344        99  35.0764994   Root MSE        =    .09433

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0200204    .0000321    624.65    0.000      .0199568      .020084
        _cons |    .9892422    .0344696     28.70    0.000      .9208384     1.057646
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |   3331.0541         1   3331.0541   Prob > F        =    0.0000
    Residual |  .926753539        98  .009456669   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3331.98085        99  33.6563722   Root MSE        =    .09725

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199847    .0000337    593.50    0.000      .0199179     .0200515
        _cons |    1.023243    .0350209     29.22    0.000      .9537455     1.092741
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |   2577.3417         1   2577.3417   Prob > F        =    0.0000
    Residual |  .696932195        98  .007111553   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  2578.03864        99  26.0407943   Root MSE        =    .08433

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199321    .0000331    602.01    0.000      .0198664     .0199978
        _cons |    1.064855    .0332615     32.01    0.000      .9988482     1.130861
--------------+----------------------------------------------------------------
. regress BMD CalciumIntake

      Source |       SS           df       MS      Number of obs   =       100
-------------+----------------------------------   F(1, 98)        >  99999.00
       Model |   3394.7368         1   3394.7368   Prob > F        =    0.0000
    Residual |  1.04642792        98  .010677836   R-squared       =    0.9997
-------------+----------------------------------   Adj R-squared   =    0.9997
       Total |  3395.78323        99  34.3008407   Root MSE        =    .10333

--------------+----------------------------------------------------------------
           BMD |      Coef.    Std. Err.       t    P>|t|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199713    .0000354    563.85    0.000       .019901     .0200416
        _cons |    1.037706    .0350405     29.61    0.000      .9681689     1.107243
--------------+----------------------------------------------------------------

Linear regression                               Number of obs     =      1,000
                                                Replications      =         10
                                                Wald chi2(1)      =  278522.51
                                                Prob > chi2       =     0.0000
                                                R-squared         =     0.9997
                                                Adj R-squared     =     0.9997
                                                Root MSE          =     0.0971

--------------+----------------------------------------------------------------
              |   Observed    Bootstrap                        Norm al-based
           BMD |      Coef.    Std. Err.       z    P>|z|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199864    .0000379    527.75    0.000      .0199122     .0200606
        _cons |    1.020791    .0409483     24.93    0.000       .940534     1.101048
--------------+----------------------------------------------------------------

.
. * run it 1000 times
. bootstrap, reps(1000) size(100): regress BMD CalciumIntake
(running regress on estimation sample)

Bootstrap replications (1000)
----+--- 1 ---+--- 2 ---+--- 3 ---+--- 4 ---+--- 5
..................................................    50
..................................................   100
..................................................   150
..................................................   200
..................................................   250
..................................................   300
..................................................   350
..................................................   400
..................................................   450
..................................................   500
..................................................   550
..................................................   600
..................................................   650
..................................................   700
..................................................   750
..................................................   800
..................................................   850
..................................................   900
..................................................   950
..................................................  1000

Linear regression                               Number of obs     =      1,000
                                                Replications      =      1,000
                                                Wald chi2(1)      =  344527.15
                                                Prob > chi2       =     0.0000
                                                R-squared         =     0.9997
                                                Adj R-squared     =     0.9997
                                                Root MSE          =     0.0971

--------------+----------------------------------------------------------------
              |   Observed    Bootstrap                        Norm al-based
           BMD |      Coef.    Std. Err.       z    P>|z|      [95% Con f. Interval]
--------------+----------------------------------------------------------------
CalciumIntake |    .0199864    .0000341    586.96    0.000      .0199197     .0200532
        _cons |    1.020791    .0354324     28.81    0.000      .9513451     1.090237
--------------+----------------------------------------------------------------

.
. * ---- 4. estimate statistical power to detect the difference in means
. power onemean 0.02 0.12, sd(1) n(100)


Estimated power for a one-sample mean test

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

Study parameters:

alpha =      0.0500
N =         100
delta =      0.1000
m0 =      0.0200
ma =      0.1200
sd =      1.0000


Estimated power:


power =      0.1677

. power onemean 0.02 0.12, sd(2) n(100)


Estimated power for a one-sample mean test

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

Study parameters:

alpha =      0.0500
N =         100
delta =      0.0500
m0 =      0.0200
ma =      0.1200
sd =      2.0000


Estimated power:


power =      0.0785

. power onemean 0.3 0.3, sd(1) n(500)


null and alternative means are equal; this is not allowed


r(198);

end of do-file

r(198);

. help runiform

. do "/var/folders/rm/pcbq8xt88v52870059bb8fbh0000gn/T//SD28463.000000"

. ***Epidemiology 265, Assignment 8
. **Stephen Chang
.
. clear

.
. cd "/Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 8/HW 8"
unable to change to /Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 8/HW 8
r(170);

end of do-file

r(170);

. do "/Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 8/HW8/Week 8.do"

. ***Epidemiology 265, Assignment 8
. **Stephen Chang
.
. clear

.
. cd "/Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 8/HW8"
/Users/stephenchang/Desktop/TICR Program/Spring 2017/EPI 265 Epidemiologic Methods III Research Methods in Chronic Disease Epidemiology/Lecture 8/HW8

.
. capture log close