Sunday, June 16, 2013

How can you get 3-way, 4-way, 5-way or more cross tabulation in Stata?

The tabulate command is great for 2-way cross tabulations. But how do you do 3-way, 4-way, 5-way of more cross tabulations? The answer is to use the table command with the contents(freq) option. Here is the general form of the table command.
table row_variabble column_variable super_column_variable, by(super_row_var_list) contents(frq)
Before we begin let's load the dataset and create an additional categorical variable, read_level.
* create new categorical variable
gen read_level=read>=55
label define read_level 0 "low" 1 "high"
label values read_level read_level* create new categorical variable

gen read_level=read>=55

label define read_level 0 "low" 1 "high"

label values read_level read_level
We begin by looking at all of the 1-way tables for our categorical variables.
tab1

-> tabulation of prog  

    type of |
    program |      Freq.     Percent        Cum.
------------+-----------------------------------
    general |         45       22.50       22.50
   academic |        105       52.50       75.00
   vocation |         50       25.00      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of ses  

        ses |      Freq.     Percent        Cum.
------------+-----------------------------------
        low |         47       23.50       23.50
     middle |         95       47.50       71.00
       high |         58       29.00      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of female  

     female |      Freq.     Percent        Cum.
------------+-----------------------------------
       male |         91       45.50       45.50
     female |        109       54.50      100.00
------------+-----------------------------------
      Total |        200      100.00

-> tabulation of honors  

      honors |
     english |      Freq.     Percent        Cum.
-------------+-----------------------------------
not enrolled |        147       73.50       73.50
    enrolled |         53       26.50      100.00
-------------+-----------------------------------
       Total |        200      100.00

-> tabulation of read_level  

 read_level |      Freq.     Percent        Cum.
------------+-----------------------------------
        low |        117       58.50       58.50
       high |         83       41.50      100.00
------------+-----------------------------------
      Total |        200      100.00
Next the 2-way table.
tab prog ses

   type of |               ses
   program |       low     middle       high |     Total
-----------+---------------------------------+----------
   general |        16         20          9 |        45 
  academic |        19         44         42 |       105 
  vocation |        12         31          7 |        50 
-----------+---------------------------------+----------
     Total |        47         95         58 |       200 
Now the 3-way table.
table prog ses female, contents(freq)

------------------------------------------------------------
          |                  female and ses                 
type of   | -------- male --------    ------- female -------
program   |    low  middle    high       low  middle    high
----------+-------------------------------------------------
  general |      7      10       4         9      10       5
 academic |      4      22      21        15      22      21
 vocation |      4      15       4         8      16       3
------------------------------------------------------------
Followed by a 4-way table.
table prog ses female, by(honors) contents(freq)

---------------------------------------------------------------
honors       |
english and  |                  female and ses                 
type of      | -------- male --------    ------- female -------
program      |    low  middle    high       low  middle    high
-------------+-------------------------------------------------
not enrolled |
     general |      7      10       2         7       8       4
    academic |      3      17      12         9      16       8
    vocation |      4      14       4         6      14       2
-------------+-------------------------------------------------
enrolled     |
     general |                      2         2       2       1
    academic |      1       5       9         6       6      13
    vocation |              1                 2       2       1
---------------------------------------------------------------
Finally a 5-way table.
table prog ses female, by(honors read_level) contents(freq)

---------------------------------------------------------------
honors       |
english,     |
read_level   |                  female and ses                 
and type of  | -------- male --------    ------- female -------
program      |    low  middle    high       low  middle    high
-------------+-------------------------------------------------
not enrolled |
low          |
     general |      4       5       1         6       6       4
    academic |      2       8       7         8      12       3
    vocation |      4      11       3         6      12       2
-------------+-------------------------------------------------
not enrolled |
high         |
     general |      3       5       1         1       2        
    academic |      1       9       5         1       4       5
    vocation |              3       1                 2        
-------------+-------------------------------------------------
enrolled     |
low          |
     general |                                2               1
    academic |              2       2         1       1       1
    vocation |                                1       1       1
-------------+-------------------------------------------------
enrolled     |
high         |
     general |                      2                 2        
    academic |      1       3       7         5       5      12
    vocation |              1                 1       1        
--------------------------------------------------------------- 

No comments:

Post a Comment