10.08.2013 Views

Introduction to Stata 8

Introduction to Stata 8

Introduction to Stata 8

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

. tab1 rating type (table for type not shown)<br />

-> tabulation of rating<br />

quality |<br />

rating | Freq. Percent Cum.<br />

--------------+-----------------------------------<br />

1. poor | 6 17.14 17.14<br />

2. acceptable | 9 25.71 42.86<br />

3. good | 16 45.71 88.57<br />

4. excellent | 4 11.43 100.00<br />

--------------+-----------------------------------<br />

Total | 35 100.00<br />

tab2 [R] tabulate<br />

tab2 with two variables give a two-way table (crosstable). In the following you see the use<br />

of three options. column requests percentage distributions by column; chi2 a χ 2 test, and<br />

exact a Fisher's exact test:<br />

. tab2 rating type , column chi2 exact<br />

-><br />

tabulation of rating by type<br />

quality | type of wine<br />

rating | 1 red 2 white 3 rosé 4 undeter | Total<br />

--------------+--------------------------------------------+----------<br />

1. poor | 4 1 1 0 | 6<br />

| 25.00 9.09 20.00 0.00 | 17.14<br />

--------------+--------------------------------------------+----------<br />

(Part of output omitted)<br />

--------------+--------------------------------------------+----------<br />

4. excellent | 2 1 0 1 | 4<br />

| 12.50 9.09 0.00 33.33 | 11.43<br />

--------------+--------------------------------------------+----------<br />

Total | 16 11 5 3 | 35<br />

| 100.00 100.00 100.00 100.00 | 100.00<br />

Pearson chi2(9) = 6.9131 Pr = 0.646<br />

Fisher's exact = 0.653<br />

You can request a three-way table (a two-way table for each value of nation) with:<br />

bysort nation: tabulate rating type<br />

The command:<br />

tab2 rating type nation<br />

gives three two-way tables, one for each of the combinations of the variables. But beware:<br />

you can easily produce a huge number of tables.<br />

[P] foreach<br />

Imagine that you want 10 two-way tables: each of the variables q1-q10 by sex. With<br />

tabulate you must issue 10 commands <strong>to</strong> obtain the result desired. If you call tab2 with<br />

11 variables you get 55 two-way tables: all possible combinations of the 11 variables. The<br />

foreach command (see section 15.7) lets you circumvent the problem:<br />

foreach Q of varlist q1-q10 {<br />

tabulate `Q' sex<br />

}<br />

The local macro Q is a stand-in for q1 <strong>to</strong> q10, and the commands generate 10 commands:<br />

tabulate q1 sex<br />

tabulate q2 sex etc.<br />

28

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!