Skip to Main Content

An Introduction to Categorical Analysis by Alan Agresti Chapter 3: Three-Way Contingency Tables | Stata Textbook Examples

Table 3.1, page 54. The command tab3way can be downloaded from the internet
by typing search tab3way in the command line (see How can I use the search command
to search for programs and get additional help?
for more information about
using search).

use https://stats.idre.ucla.edu/stat/stata/examples/icda/deathp, clear

tab3way victim deathp defendant [fw=count], coltot rowp

Frequency weights are based on the expression: count
Table entries are cell frequencies and row percentages
Missing categories ignored

--------------------------------------------
          |       defendant and deathp      
          | ------ 1 -----    ------ 2 -----
   victim |      0       1         0       1
----------+---------------------------------
        1 |    414      53        37      11
          |  88.65   11.35     77.08   22.92
          | 
        2 |     16               139       4
          | 100.00             97.20    2.80
          | 
    TOTAL |    430      53       176      15
          |  89.03   10.97     92.15    7.85
--------------------------------------------

Table 3.2 on page 58 and calculation on page 59.

use https://stats.idre.ucla.edu/stat/stata/examples/icda/clinic, clear

tab3way treat  clinic response [fw=count] , rowtot

Frequency weights are based on the expression: count
Table entries are cell frequencies
Missing categories ignored
------------------------------------------------------
          |            response and clinic            
          | -------- 0 --------    -------- 1 --------
    treat |     0      1  TOTAL        0      1  TOTAL
----------+-------------------------------------------
        0 |    18      2     20       12      8     20
        1 |    12      8     20        8     32     40
------------------------------------------------------

cc response treat [fw=count], by(clinic)

          clinic |       OR      [95% Conf. Interval]    M-H Weight
-----------------+-------------------------------------------------
               0 |          1      .266931    3.66141          2.88 (exact)
               1 |          1     .0872055   6.599538          1.28 (exact)
-----------------+-------------------------------------------------
           Crude |          2     .8134474   4.921018               (exact)
    M-H combined |          1     .3825288   2.614182               
-------------------------------------------------------------------
Test of homogeneity (M-H)      chi2(1) =     0.00  Pr>chi2 = 1.0000
                   Test that combined OR = 1:
                                Mantel-Haenszel chi2(1) =      0.00
                                                Pr>chi2 =    1.0000
                                                
cc treat clinic [fw=count], by(response)

        response |       OR      [95% Conf. Interval]    M-H Weight
-----------------+-------------------------------------------------
               0 |          6     .9222592   64.61686            .6 (exact)
               1 |          6     1.587285   23.15472      1.066667 (exact)
-----------------+-------------------------------------------------
           Crude |          6     2.264456   16.37396               (exact)
    M-H combined |          6     2.259288   15.93423               
-------------------------------------------------------------------
Test of homogeneity (M-H)      chi2(1) =     0.00  Pr>chi2 = 1.0000
                   Test that combined OR = 1:
                                Mantel-Haenszel chi2(1) =     14.07
                                                Pr>chi2 =    0.0002
                                                
use https://stats.idre.ucla.edu/stat/stata/examples/icda/deathp, clear

cc defendant deathp [fw=count], by(victim)

          victim |       OR      [95% Conf. Interval]    M-H Weight
-----------------+-------------------------------------------------
                 |   .4306105     .2003285   .9955573      8.842718 (exact)
           white |          0            0   8.894517      .4025157 (exact)
-----------------+-------------------------------------------------
           Crude |   1.446202     .7776757   2.836998               (exact)
    M-H combined |   .4118627     .1991173   .8519141               
-------------------------------------------------------------------
Test of homogeneity (B-D)      chi2(1) =     0.20  Pr>chi2 = 0.6517
                   Test that combined OR = 1:
                                Mantel-Haenszel chi2(1) =      5.80
                                                Pr>chi2 =    0.0161

Section 3.2, page 60. Cochran-Mantel-Haenszel Methods using Chinese smoking and lung
cancer data set.

use https://stats.idre.ucla.edu/stat/stata/examples/icda/cancer, clear

cc cancer smoke [fw=count], by(center) 

          center |       OR      [95% Conf. Interval]    M-H Weight
-----------------+-------------------------------------------------
               1 |      2.196     1.307112   3.710289      10.86957 (exact)
               2 |   2.142962     1.840433   2.495332       117.909 (exact)
               3 |   2.175265     1.838162   2.574387      96.75867 (exact)
               4 |   2.850341     1.939615   4.204899      17.02389 (exact)
               5 |   2.319148     1.759581   3.060508      35.62906 (exact)
               6 |   1.587963     1.076653   2.344985      22.11024 (exact)
               7 |   2.369146     1.092397   5.476443      5.112676 (exact)
               8 |    2.00321      1.04818   3.883171         7.476 (exact)
-----------------+-------------------------------------------------
           Crude |   2.135552     1.948418   2.340687               (exact)
    M-H combined |   2.174482     1.984002   2.383249               
-------------------------------------------------------------------
Test of homogeneity (M-H)      chi2(7) =     5.19  Pr>chi2 = 0.6372
                   Test that combined OR = 1:
                                Mantel-Haenszel chi2(1) =    280.14
                                                Pr>chi2 =    0.0000