Liz's Stata Guide

ttest

Performs a statistical test as to whether a variable has a specified mean, whether two variables are equal, or whether the mean of one variable is equal across values of another variable.

Typical Use:
ttest varx = 2   - or -
ttest varx == vary, unpaired   - or -
ttest varx, by(groupvar)

Examples

  1. Instructions given to surveyors were supposed to ensure that the sample was half male and half female, however, the final sample is actually 51% male. We test the hypothesis that respondent selection was actually carried out correctly such that male and female respondents had an equal chance of being interviewed (we cannot reject this hypothesis).
    . ttest male == 0.5
    
    One-sample t test
    
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
        male |    1100    .5136364    .0150768    .5000414    .4840538     .543219
    ------------------------------------------------------------------------------
    Degrees of freedom: 1099
    
                                Ho: mean(male) = 0.5
    
        Ha: mean < 0.5             Ha: mean != 0.5            Ha: mean > 0.5
           t =   0.9045                t =   0.9045              t =   0.9045
       P < t =   0.8170          P > |t| =   0.3660          P > t =   0.1830
    
  2. Next, let's test whether the same fraction of the population own a bicycle as own a TV. Again, we cannot reject this hypothesis.
    . ttest own_TV == own_bicycle, unpaired
    
    Two-sample t test with equal variances
    
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
      own_TV |    1098    .0801457    .0081978    .2716424    .0640606    .0962308
    own_bi~e |    1098     .079235    .0081551    .2702282    .0632336    .0952363
    ---------+--------------------------------------------------------------------
    combined |    2196    .0796903    .0057803    .2708749    .0683549    .0910258
    ---------+--------------------------------------------------------------------
        diff |            .0009107    .0115633               -.0217654    .0235869
    ------------------------------------------------------------------------------
    Degrees of freedom: 2194
    
                   Ho: mean(own_TV) - mean(own_bicycle) = diff = 0
    
         Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
           t =   0.0788                t =   0.0788              t =   0.0788
       P < t =   0.5314          P > |t| =   0.9372          P > t =   0.4686
    
  3. Finally, let's test whether the average age of male respondents is equal to average age of female respdondents. We find that we can reject this hypothesis -- male respdondents are significantly older on average.
    . ttest age, by(gender)
    
    Two-sample t test with equal variances
    
    ------------------------------------------------------------------------------
    Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
    ---------+--------------------------------------------------------------------
        male |     563    43.56838    .6611666    15.68791    42.26972    44.86704
      female |     530    38.91698     .642816    14.79873     37.6542    40.17977
    ---------+--------------------------------------------------------------------
    combined |    1093     41.3129     .466795     15.4325    40.39698    42.22882
    ---------+--------------------------------------------------------------------
        diff |            4.651403    .9237718                2.838832    6.463973
    ------------------------------------------------------------------------------
    Degrees of freedom: 1091
    
                      Ho: mean(male) - mean(female) = diff = 0
    
         Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
           t =   5.0352                t =   5.0352              t =   5.0352
       P < t =   1.0000          P > |t| =   0.0000          P > t =   0.0000
    

Options

Notes

contact: djiboliz@gmail.com
last modified: 30 April 2007