Search code examples
stata

Power twomeans: Means must contain numbers


I am trying to conduct power analysis in Stata using the stored results from the ttest command.

What I did exactly is as follows:

. ttest r, by(predictor)
Two-sample t test with equal variances
------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       0 |       8         .49    .0368394    .1041976    .4028886    .5771114
       1 |       8        .595    .0547396    .1548271    .4655613    .7244387
---------+--------------------------------------------------------------------
combined |      16       .5425     .034635      .13854    .4686772    .6163228
---------+--------------------------------------------------------------------
    diff |               -.105    .0659816               -.2465165    .0365165
------------------------------------------------------------------------------
    diff = mean(0) - mean(1)                                      t =  -1.5914
Ho: diff = 0                                     degrees of freedom =       14

    Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
 Pr(T < t) = 0.0669         Pr(|T| > |t|) = 0.1339          Pr(T > t) = 0.9331

. display r(mu_1),r(mu_2),r(sd_1),r(sd_2),r(p_l)
.49 .595 .10419762 .1548271 .06692561

However, when I run the following command:

power twomeans r(mu_1) r(mu_2), sd1(r(sd_1)) sd2(r(sd_2))

I always receive the error below:

means must contain numbers

I thought the stored results r(mu_1) etc. are numbers?


Solution

  • Using the auto dataset as a toy example:

    sysuse auto, clear
    ttest mpg, by(foreign)
    
    Two-sample t test with equal variances
    ------------------------------------------------------------------------------
       Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
    Domestic |      52    19.82692     .657777    4.743297    18.50638    21.14747
     Foreign |      22    24.77273     1.40951    6.611187    21.84149    27.70396
    ---------+--------------------------------------------------------------------
    combined |      74     21.2973    .6725511    5.785503     19.9569    22.63769
    ---------+--------------------------------------------------------------------
        diff |           -4.945804    1.362162               -7.661225   -2.230384
    ------------------------------------------------------------------------------
        diff = mean(Domestic) - mean(Foreign)                         t =  -3.6308
    Ho: diff = 0                                     degrees of freedom =       72
    
        Ha: diff < 0                 Ha: diff != 0                 Ha: diff > 0
     Pr(T < t) = 0.0003         Pr(|T| > |t|) = 0.0005          Pr(T > t) = 0.9997
    

    You need to explicitly tell to power that the returned items from ttestare local macros:

    power twomeans `r(mu_1)' `r(mu_2)', sd1(`r(sd_1)') sd2(`r(sd_2)')
    
    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.8000
            delta =    4.9458
               m1 =   19.8269
               m2 =   24.7727
              sd1 =    4.7433
              sd2 =    6.6112
    
    Estimated sample sizes:
    
                N =        46
      N per group =        23