Stata Textbook Examples
Introduction to the Practice of Statistics by Moore and McCabe
Chapter 7: Inference for Distributions

NOTE: This page has been delinked.  It is no longer being maintained, and information on this page may be out of date.

Example 7.1, page 507 shows a confidence interval for the mean. We first input the data as shown below. This is illustrated in this short Quicktime Movie.
clear
input vitc
26
31
23
22
11
22
14
31
end
We use the ci command to get the confidence interval for vitc.
ci vitc

Variable |     Obs         Mean    Std. Err.       [95% Conf. Interval]
---------+-------------------------------------------------------------
    vitc |       8         22.5    2.542496        16.48795    28.51205
Example 7.2, page 508. The ttest command can test whether the mean is significantly different from 40. Since this is a two-tailed test, we would look at the results under Ha: mean ~= 40 (the ~= means not equal to).
ttest vitc = 40

One-sample t test

------------------------------------------------------------------------------
Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
    vitc |       8        22.5    2.542496    7.191265    16.48795    28.51205
------------------------------------------------------------------------------
Degrees of freedom: 7

                             Ho: mean(vitc) = 40

     Ha: mean < 40             Ha: mean ~= 40              Ha: mean > 40
       t =  -6.8830                t =  -6.8830              t =  -6.8830
   P < t =   0.0001          P > |t| =   0.0002          P > t =   0.9999
For example 7.3, we would look at the same results as above, except inspect the results under Ha: mean < 40
Example 7.4, page 510. Looking at the data in Table 1.1, you can see that the two outliers are both less than 0, so we can eliminate them with the drop if command.
use http://www.ats.ucla.edu/stat/stata/examples/mm/webdata/ta01_001, clear

drop if time < 0
(2 observations deleted)
We can get the mean and standard deviation with the summarize command. Note that this is based on just the 64 remaining observations.
summarize time

    Variable |       Obs        Mean    Std. Dev.       Min        Max
-------------+--------------------------------------------------------
        time |        64       27.75    5.083431         16         40
We can get the 99% confidence interval with the ci command.
ci time , level(99)

    Variable |        Obs        Mean    Std. Err.       [99% Conf. Interval]
-------------+---------------------------------------------------------------
        time |         64       27.75    .6354289        26.06221    29.43779
Example 7.5, page 511 can be answered with the ttest command.
ttest time=33.02

One-sample t test

------------------------------------------------------------------------------
Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
    time |      64       27.75    .6354289    5.083431     26.4802     29.0198
------------------------------------------------------------------------------
Degrees of freedom: 63

                           Ho: mean(time) = 33.02

   Ha: mean < 33.02           Ha: mean != 33.02          Ha: mean > 33.02
       t =  -8.2936                t =  -8.2936              t =  -8.2936
   P < t =   0.0000          P > |t| =   0.0000          P > t =   1.0000
Example 7.7, page 513 shows how to do a paired t-test using the file ta07_001. This is illustrated by making a gain score (which is already present in ta07_001 and is called gain) and testing if that gain score differs from 0, illustrated below.
use http://www.ats.ucla.edu/stat/stata/examples/mm/webdata/ta07_001, clear

ttest gain=0

One-sample t test

------------------------------------------------------------------------------
Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
    gain |      20         2.5    .6468547    2.892822    1.146117    3.853883
------------------------------------------------------------------------------
Degrees of freedom: 19

                             Ho: mean(gain) = 0

     Ha: mean < 0               Ha: mean != 0              Ha: mean > 0
       t =   3.8649                t =   3.8649              t =   3.8649
   P < t =   0.9995          P > |t| =   0.0010          P > t =   0.0005
Stata also allows you to simply test whether the pretest is different from the posttest, and yields the same results as above.
ttest pretest=posttest
Paired t test

------------------------------------------------------------------------------
Variable |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
 pretest |      20        25.8    1.409741    6.304551    22.84938    28.75062
posttest |      20        28.3    1.330018     5.94802    25.51624    31.08376
---------+--------------------------------------------------------------------
    diff |      20        -2.5    .6468547    2.892822   -3.853883   -1.146117
------------------------------------------------------------------------------

                Ho: mean(pretest - posttest) = mean(diff) = 0

  Ha: mean(diff) < 0         Ha: mean(diff) != 0        Ha: mean(diff) > 0
       t =  -3.8649                t =  -3.8649              t =  -3.8649
   P < t =   0.0005          P > |t| =   0.0010          P > t =   0.9995
We can use the ci command with the variable gain to get the confidence interval shown in Example 7.8, page 514.
ci gain

    Variable |        Obs        Mean    Std. Err.       [95% Conf. Interval]
-------------+---------------------------------------------------------------
        gain |         20         2.5    .6468547        1.146117    3.853883
We have skipped the rest of the examples in section 7.1.
We have skipped example 7.13
Example 7.14, page 542 shows a t-test comparing a treatment group with a control group, and is illustrated below. Given the alternative hypothesis, the results under Ha: diff > 0 are of most interest.
use http://www.ats.ucla.edu/stat/stata/examples/mm/webdata/ta07_002, clear

ttest drp_scor, by(indicati)

Two-sample t test with equal variances

------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       0 |      21    51.47619    2.402002    11.00736     46.4657    56.48668
       1 |      23    41.52174    3.575758    17.14873    34.10607    48.93741
---------+--------------------------------------------------------------------
combined |      44    46.27273    2.296786    15.23515    41.64082    50.90464
---------+--------------------------------------------------------------------
    diff |            9.954451    4.391893                1.091253    18.81765
------------------------------------------------------------------------------
Degrees of freedom: 42

                      Ho: mean(0) - mean(1) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   2.2666                t =   2.2666              t =   2.2666
   P < t =   0.9857          P > |t| =   0.0286          P > t =   0.0143
Example 7.15, page 544 shows a confidence interval for the difference in the means, which is shown in the output from the example above in the row labeled combined. Stata uses a slightly different rule for choosing the df for t (using n1 + n2 - 2) so the confidence interval is slightly different from that shown in the book.
Example 7.16, page 546 is solved using the ttesti command below. The values supplied are the N, mean and sd for group 1, and then the N, mean and sd for group 2. Stata gives a t value of .6489, just slightly different from .654 from the book. The two sided p value is 0.5169, but our version of the book reports 0.051 (which we think is a misprint, and should have been 0.51).
ttesti 133 25.34 5.05 162 24.94 5.44

Two-sample t test with equal variances

------------------------------------------------------------------------------
         |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       x |     133       25.34    .4378905        5.05    24.47381    26.20619
       y |     162       24.94    .4274068        5.44    24.09595    25.78405
---------+--------------------------------------------------------------------
combined |     295    25.12034    .3064055    5.262687    24.51731    25.72337
---------+--------------------------------------------------------------------
    diff |                  .4    .6164008               -.8131343    1.613134
------------------------------------------------------------------------------
Degrees of freedom: 293

                      Ho: mean(x) - mean(y) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   0.6489                t =   0.6489              t =   0.6489
   P < t =   0.7416          P > |t| =   0.5169          P > t =   0.2584
For example 7.17, page 547 we can input the data like this.
clear
input control spike
0 12.207
0 16.869
0 25.050
0 22.429
0 8.456
0 20.589
1 11.074
1 9.686
1 12.064
1 9.351
1 8.182
1 6.642
end
We can use the ttest command to test that the mean spike is the same for the control and poison group (when control=0 that is the poison group, and when control=1 that is the control group).
ttest spike, by(control)

Two-sample t test with equal variances

------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       0 |       6        17.6    2.588355    6.340148    10.94642    24.25358
       1 |       6    9.499833    .7961084    1.950059    7.453372     11.5463
---------+--------------------------------------------------------------------
combined |      12    13.54992     1.77704    6.155848    9.638677    17.46116
---------+--------------------------------------------------------------------
    diff |            8.100167    2.708019                2.066324    14.13401
------------------------------------------------------------------------------
Degrees of freedom: 10

                      Ho: mean(0) - mean(1) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   2.9912                t =   2.9912              t =   2.9912
   P < t =   0.9932          P > |t| =   0.0135          P > t =   0.0068
We repeat the ttest command and include the unequal option and we get the results shown for the unequal variances.
ttest spike, by(control) unequal

Two-sample t test with unequal variances

------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       0 |       6        17.6    2.588355    6.340148    10.94642    24.25358
       1 |       6    9.499833    .7961084    1.950059    7.453372     11.5463
---------+--------------------------------------------------------------------
combined |      12    13.54992     1.77704    6.155848    9.638677    17.46116
---------+--------------------------------------------------------------------
    diff |            8.100167    2.708019                1.456971    14.74336
------------------------------------------------------------------------------
Satterthwaite's degrees of freedom:  5.93762

                      Ho: mean(0) - mean(1) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   2.9912                t =   2.9912              t =   2.9912
   P < t =   0.9877          P > |t| =   0.0246          P > t =   0.0123
Example 7.18, page 549. We repeat example 7.14 but include the unequal option to get the results shown in example 7.18. Indeed, Stata uses 37.86 degrees of freedom as shown in the book
use http://www.ats.ucla.edu/stat/stata/examples/mm/webdata/ta07_002, clear

ttest  drp_scor, by(indicati) unequal

Two-sample t test with unequal variances

------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [95% Conf. Interval]
---------+--------------------------------------------------------------------
       0 |      21    51.47619    2.402002    11.00736     46.4657    56.48668
       1 |      23    41.52174    3.575758    17.14873    34.10607    48.93741
---------+--------------------------------------------------------------------
combined |      44    46.27273    2.296786    15.23515    41.64082    50.90464
---------+--------------------------------------------------------------------
    diff |            9.954451    4.307628                 1.23302    18.67588
------------------------------------------------------------------------------
Satterthwaite's degrees of freedom:  37.8554

                      Ho: mean(0) - mean(1) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   2.3109                t =   2.3109              t =   2.3109
   P < t =   0.9868          P > |t| =   0.0264          P > t =   0.0132
Example 7.20 and 7.21, page 553-554 is illustrated below. We use the level(90) option to request a 90% confidence interval as shown in 7.21. The results from Stata correspond to those in the book.
use http://www.ats.ucla.edu/stat/stata/examples/mm/webdata/ta07_003, clear

ttest  decrease, by(group) level(90)

Two-sample t test with equal variances

------------------------------------------------------------------------------
   Group |     Obs        Mean    Std. Err.   Std. Dev.   [90% Conf. Interval]
---------+--------------------------------------------------------------------
 Calcium |      10           5    2.764859    8.743251   -.0682985     10.0683
 Placebo |      11   -.2727273    1.779126    5.900693   -3.497324    2.951869
---------+--------------------------------------------------------------------
combined |      21    2.238095    1.677448    7.687033   -.6550301    5.131221
---------+--------------------------------------------------------------------
    diff |            5.272727     3.22667               -.3066129    10.85207
------------------------------------------------------------------------------
Degrees of freedom: 19

                Ho: mean(Calcium) - mean(Placebo) = diff = 0

     Ha: diff < 0               Ha: diff != 0              Ha: diff > 0
       t =   1.6341                t =   1.6341              t =   1.6341
   P < t =   0.9407          P > |t| =   0.1187          P > t =   0.0593
We have skipped section 7.3 for now.

How to cite this page

Report an error on this page or leave a comment

The content of this web site should not be construed as an endorsement of any particular web site, book, or software product by the University of California.