**[R] tabulate twoway** -- Two-way table of frequencies

__Syntax__

Two-way table

__ta__**bulate** *varname1* *varname2* [*if*] [*in*] [*weight*] [**,** *options*]

Two-way table for all possible combinations - a convenience tool

**tab2** *varlist* [*if*] [*in*] [*weight*] [**,** *options*]

Immediate form of two-way tabulations

**tabi** *#11* *#12* [*...*] **\** *#21* *#22* [*...*] [**\** *...*] [**,** *options*]

*options* Description
-------------------------------------------------------------------------
Main
__ch__**i2** report Pearson's chi-squared
__e__**xact**[**(***#***)**] report Fisher's exact test
__g__**amma** report Goodman and Kruskal's gamma
__lr__**chi2** report likelihood-ratio chi-squared
__t__**aub** report Kendall's tau-b
**V** report Cramér's V
__cchi__**2** report Pearson's chi-squared in each cell
__co__**lumn** report relative frequency within its column of each
cell
__r__**ow** report relative frequency within its row of each
cell
__clr__**chi2** report likelihood-ratio chi-squared in each cell
__ce__**ll** report the relative frequency of each cell
__exp__**ected** report expected frequency in each cell
__nof__**req** do not display frequencies
**rowsort** list rows in order of observed frequency
**colsort** list columns in order of observed frequency
__m__**issing** treat missing values like other values
__w__**rap** do not wrap wide tables
[**no**]**key** report/suppress cell contents key
__nol__**abel** display numeric codes rather than value labels
__nolo__**g** do not display enumeration log for Fisher's exact
test
* __first__**only** show only tables that include the first variable in
*varlist*

Advanced
**matcell(***matname***)** save frequencies in *matname*; programmer's option
**matrow(***matname***)** save unique values of *varname1* in *matname*;
programmers option
**matcol(***matname***)** save unique values of *varname2* in *matname*;
programmers option
# **replace** replace current data with given cell frequencies

__a__**ll** equivalent to specifying **chi2 lrchi2 V gamma taub**
-------------------------------------------------------------------------
* **firstonly** is available only for **tab2**.
# **replace** is available only for **tabi**.
**by** is allowed with **tabulate** and **tab2**; see **[D] by**.
**fweight**s, **aweight**s, and **iweight**s are allowed by **tabulate**. **fweight**s are
allowed by **tab2**. See weight.
**all** does not appear in the dialog box.

__Menu__

__tabulate__

**Statistics > Summaries, tables, and tests > Frequency tables >**
**Two-way table with measures of association**

__tab2__

**Statistics > Summaries, tables, and tests > Frequency tables >** **All**
**possible two-way tables**

__tabi__

**Statistics > Summaries, tables, and tests > Frequency tables > Table**
**calculator**

__Description__

**tabulate** produces a two-way table of frequency counts, along with various
measures of association, including the common Pearson's chi-squared, the
likelihood-ratio chi-squared, Cramér's V, Fisher's exact test, Goodman
and Kruskal's gamma, and Kendall's tau-b.

Line size is respected. That is, if you resize the Results window before
running **tabulate**, the resulting two-way tabulation will take advantage of
the available horizontal space. Stata for Unix(console) users can
instead use the **set linesize** command to take advantage of this feature.

**tab2** produces all possible two-way tabulations of the variables specified
in *varlist*.

**tabi** displays the r x c table, using the values specified; rows are
separated by '**\**'. If no options are specified, it is as if **exact** were
specified for a 2 x 2 table and **chi2** were specified otherwise. See immed
for a general description of immediate commands. See *Tables with*
*immediate data* in **[R] tabulate twoway** for examples using **tabi**.

See **[R] tabulate oneway** if you want a one-way table of frequencies. See
**[R] table** and **[R] tabstat** if you want one-, two-, or n-way table of
frequencies and a wide variety of summary statistics. See **[R] tabulate,**
**summarize()** for a description of **tabulate** with the **summarize()** option; it
produces a table (breakdowns) of means and standard deviations. **table** is
better than **tabulate, summarize()**, but **tabulate, summarize()** is faster.
See **[R] epitab** for a 2 x 2 table with statistics of interest to
epidemiologists.

__Options__

+------+
----+ Main +-------------------------------------------------------------

**chi2** calculates and displays Pearson's chi-squared for the hypothesis
that the rows and columns in a two-way table are independent. **chi2**
may not be specified if **aweight**s or **iweight**s are specified.

**exact**[**(***#***)**] displays the significance calculated by Fisher's exact test
and may be applied to r x c as well as to 2 x 2 tables. For 2 x 2
tables, both one- and two-sided probabilities are displayed. For r x
c tables, two-sided probabilities are displayed. The optional
positive integer *#* is a multiplier on the amount of memory that the
command is permitted to consume. The default is 1. This option
should not be necessary for reasonable r x c tables. If the command
terminates with error 910, try **exact(2)**. The maximum row or column
dimension allowed when computing Fisher's exact test is the maximum
row or column dimension for** tabulate** (see limits).

**gamma** displays Goodman and Kruskal's gamma along with its asymptotic
standard error. **gamma** is appropriate only when both variables are
ordinal. **gamma** may not be specified if **aweight**s or **iweight**s are
specified.

**lrchi2** displays the likelihood-ratio chi-squared statistic. **lrchi2** may
not be specified if **aweight**s or **iweight**s are specified.

**taub** displays Kendall's tau-b along with its asymptotic standard error.
**taub** is appropriate only when both variables are ordinal. **taub** may
not be specified if **aweight**s or **iweight**s are specified.

**V** (note capitalization) displays Cramér's V. **V** may not be specified if
**aweight**s or **iweight**s are specified.

**cchi2** displays each cell's contribution to Pearson's chi-squared in a
two-way table.

**column** displays the relative frequency of each cell within its column in
a two-way table.

**row** displays the relative frequency of each cell within its row in a
two-way table.

**clrchi2** displays each cell's contribution to the likelihood-ratio
chi-squared in a two-way table.

**cell** displays the relative frequency of each cell in a two-way table.

**expected** displays the expected frequency of each cell in a two-way table.

**nofreq** suppresses the printing of the frequencies.

**rowsort** and **colsort** specify that the rows and columns, respectively, be
presented in order of observed frequency.

By default, rows and columns are presented in ascending order of the
row and column variable. For instance, if you type **tabulate a b** and
**a** takes on the values 2, 3, and 5, then the first row of the table
will correspond to **a** = 2; the second row will correspond to **a** = 3;
and the third row will correspond to **a** = 5.

**rowsort** specifies that the rows instead be presented in descending
order of observed frequency of the values. If you type **twoway a b,**
**rowsort**, the most frequently observed value of **a** will be listed in
the first row, the second most frequently observed value of **a** in the
second row, and so on. If there are rows with equal frequencies,
they will be presented in ascending order of the values of **a**. If **a** =
5 occurs with frequency 1,000 and values **a** = 2 and **a** = 3 each occur
with frequency 500, the rows will be presented in the order **a** = 5, **a**
= 2, and **a** = 3.

**colsort** does the same as **rowsort**, except with the columns and the
column variable.

**rowsort** and **colsort** may be specified together.

**missing** requests that missing values be treated like other values in
calculations of counts, percentages, and other statistics.

**wrap** requests that Stata take no action on wide, two-way tables to make
them readable. Unless **wrap** is specified, wide tables are broken into
pieces to enhance readability.

[**no**]**key** suppresses or forces the display of a key above two-way tables.
The default is to display the key if more than one cell statistic is
requested, and otherwise to omit it. **key** forces the display of the
key. **nokey** suppresses its display.

**nolabel** causes the numeric codes to be displayed rather than the value
labels.

**nolog** suppresses the display of the log for Fisher's exact test. Using
Fisher's exact test requires counting all tables that have a
probability exceeding that of the observed table given the observed
row and column totals. The log counts down each stage of the network
computations, starting from the number of columns and counting down
to 1, displaying the number of nodes in the network at each stage. A
log is not displayed for 2 x 2 tables.

**firstonly**, available only with **tab2**, restricts the output to only those
tables that include the first variable in *varlist*. Use this option
to interact one variable with a set of others.

+----------+
----+ Advanced +---------------------------------------------------------

**matcell(***matname***)** saves the reported frequencies in *matname*. This option
is for use by programmers.

**matrow(***matname***)** saves the numeric values of the r x 1 row stub in
*matname*. This option is for use by programmers. **matrow()** may not be
specified if the row variable is a string.

**matcol(***matname***)** saves the numeric values of the 1 x c column stub in
*matname*. This option is for use by programmers. **matcol()** may not be
specified if the column variable is a string.

**replace** indicates that the immediate data specified as arguments to the
**tabi** command be left as the current data in place of whatever data
were there.

The following option is available with **tabulate** but is not shown in the
dialog box:

**all** is equivalent to specifying **chi2 lrchi2 V gamma taub**. Note the
omission of **exact**. When **all** is specified, **no** may be placed in front
of the other options. **all noV** requests all association measures
except Cramér's V (and Fisher's exact). **all exact** requests all
association measures, including Fisher's exact test. **all** may not be
specified if **aweight**s or **iweight**s are specified.

__Limits__

Two-way tables may have a maximum of 1,200 rows and 80 columns (Stata/MP
and Stata/SE) or 300 rows and 20 columns (Stata/IC). If larger tables
are needed, see **[R] table**.

__Examples__

---------------------------------------------------------------------------
Setup
**. webuse citytemp2**

Two-way table of frequencies
**. tabulate region agecat**

Include row percentages
**. tabulate region agecat, row**

Include column percentages
**. tabulate region agecat, column**

Include cell percentages
**. tabulate region agecat, cell**

Include row percentages, suppress frequency counts
**. tabulate region agecat, row nofreq**

Include chi-squared test for independence of rows and columns
**. tabulate region agecat, chi2**

---------------------------------------------------------------------------
Setup
**. webuse dose**

Include all measures of association, except Fisher's exact test
**. tabulate dose function, all**

Include all measures of association, including Fisher's exact test
**. tabulate dose function, all exact**

---------------------------------------------------------------------------
Immediate form
**. tabi 30 18 \ 18 14**

Immediate form, 2 x 3 table
**. tabi 30 18 38 \ 13 7 22**

Add Fisher's exact test
**. tabi 30 18 38 \ 13 7 22, chi2 exact**

3 by 2 table, all measures of association
**. tabi 30 13 \ 18 7 \ 38 22, all exact**
---------------------------------------------------------------------------

__Video examples__

Pearson's chi2 and Fisher's exact test in Stata

Tables and cross-tabulations in Stata

Cross-tabulations and chi-squared tests calculator

__Stored results__

**tabulate**, **tab2**, and **tabi** store the following in **r()**:

Scalars
**r(N)** number of observations
**r(r)** number of rows
**r(c)** number of columns
**r(chi2)** Pearson's chi-squared test
**r(p)** p-value for Pearson's chi-squared test
**r(gamma)** gamma
**r(p1_exact)** one-sided Fisher's exact p
**r(p_exact)** Fisher's exact p
**r(chi2_lr)** likelihood-ratio chi-squared
**r(p_lr)** p-value for likelihood-ratio test
**r(CramersV)** Cramér's V
**r(ase_gam)** ASE of gamma
**r(ase_taub)** ASE of tau_b
**r(taub)** tau_b

**r(p1_exact)** is defined only for 2 x 2 tables. Also, the **matrow()**,
**matcol()**, and **matcell()** options allow you to obtain the row values,
column values, and frequencies, respectively.