Chi-Square Test in Stata
Exact Option, Expected Frequencies, Cramér's V
A chi-square test in Stata runs from tabulate with the chi2 option, and Stata applies no continuity correction, unlike R's default for 2x2 tables. When expected frequencies are small, the exact option gives Fisher's test and is the line to report.
IN SHORT
What tabulate Tests and the Options That Complete It
A chi-square test of association asks whether two categorical variables are related, by comparing the observed frequencies in a cross-tabulation with those expected if they were unrelated. To run a chi-square test in Stata, type tabulate followed by the row and column variables and the chi2 option. Stata prints the Pearson statistic with its degrees of freedom and p-value, and the expected, exact and V options add the expected frequencies, Fisher’s test and a measure of strength. The tabi command runs the same tests on counts typed in directly. OnlineSPSS, a specialist statistical consultancy for academic, clinical and professional research, carries out and reviews this analysis through an in-house PhD-level statistician.
In This Guide
ANALYSIS SUPPORT
Analysis Support for This Method
A test of association in a table is sensitive to sparse cells. When expected frequencies are small, an exact test answers the question that the approximation cannot.
Chi-Square Test
Association between two categorical variables tested and measured, from the layout of the table to the effect size and the sentence that reports them.
Fisher's Exact Test
Exact p-values for tables with small frequencies, where the chi-square approximation is unreliable, with an odds ratio to describe 2x2 tables.
BEFORE YOU RUN IT
What tabulate Expects of the Table
Counts, labels and the treatment of missing codes change which table gets tested, so these four points are settled before chi2 is added.
Cases or Frequency Weights Raw data hold one row per participant. A summary table of counts can be loaded as a frequency variable and used with fweight, or typed straight into tabi without any data file.
Independent Observations Each participant contributes to one cell. Before-and-after answers from the same people, or matched pairs, break that assumption and call for a test built for matched data.
Missing Codes Declared A code such as 9 or 99 for a missing answer counts as a category unless it is declared missing, which changes the table and its degrees of freedom. The missing option shows such codes.
Categories With Enough Cases Sparse categories make the Pearson approximation unreliable. Merging categories needs a substantive reason chosen before p-values are seen, and the exact option is the alternative.
IN STATA
How to Run a Chi-Square Test in Stata
Running tabulate chi2 in Stata needs no package, because tabulate and tabi come with Stata itself, and each run below adds a single piece of the report.
Tabulate With Missing Shown
Cross-tabulate the two variables with the missing option to see every code in use, including values that were meant to be missing.
Check: A category with a handful of cases, or a stray code, appears here before it is tested. Recode it or declare it missing in the do-file, not by editing the data.
Add Row Percentages to the Table
Add the row option so each cell also shows the percentage of its row, and compare the rows.
Tip: Put the variable treated as the explanatory one in the rows. Row percentages then compare its groups directly, which is the comparison most readers will look for.
Run chi2 With Expected Frequencies
Add chi2 and expected, and read the expected frequency printed beneath each observed count.
Watch: Expected frequencies below 5 are the usual warning sign. The figure is a guideline and not a threshold, and the exact option is the usual alternative to recoding categories.
Add exact for Small Frequencies
Add the exact option to get Fisher’s exact test in Stata, and decide in advance which line will be reported.
Tip: In tables larger than 2×2 the exact calculation can take a long time on large samples. Say in the methods which line was reported, and why it was chosen over the other.
Request Cramér's V for Strength
Add the V option for Cramér’s V, the strength of association, and read it beside the percentages.
Tip: The test says whether an association is detectable and V how strong it is. Quote V with the table rather than leaving the p-value to carry the size.
Rerun the Counts With tabi
Use tabi when only published counts exist, entering the rows separated by a backslash, and compare its output with the original.
Check: tabi on the original cell counts should reproduce the Pearson statistic and p-value from the raw data. A mismatch points to a recode or a missing code behaving differently.
tabulate row col, missing
tabulate row col, row chi2 expected
tabulate row col, chi2 exact V
tabi 30 10 20 40, chi2 exact
READING THE OUTPUT
Reading the Cross-Tabulation and Its Test Lines
The key above the table says what each number in a cell is, and the test lines beneath it carry the statistic, the exact p-value and the strength of association.
| Output element | What it tells you | What to check |
|---|---|---|
| Key: frequency, expected frequency, row percentage | What each number in a cell stands for, depending on the options given | That the number quoted is the one intended |
| Row and column totals | The marginal counts of both variables | That the grand total equals the analyzed n after missing values |
| Pearson chi2(df) | The statistic with df equal to (rows minus 1) times (columns minus 1) | That the df match the dimensions of the table |
| Pr (after Pearson chi2) | The p-value from the chi-square approximation, with no continuity correction | That it is quoted as printed and the lack of correction is noted when comparing programs |
| Fisher’s exact | The exact two-sided p-value; a 2×2 table also prints a one-sided value | That the line matches the one named in the plan |
| Cramér’s V | Strength of association, scaled by the sample size and the table’s dimensions | That it is reported beside the test and not instead of it |
| Expected frequency in a cell | What the cell would hold if the variables were unrelated | That small values are examined before the Pearson line is relied on |
REPORTING
Reporting the Test Line, the Pattern and the Strength
Three details decide whether the table can be reproduced from the sentence: which test line, which percentages and how many cases were dropped.
Give the Pearson statistic with its degrees of freedom and sample size as χ²(df, N = ___) = ___, p = ___, taken from the line that chi2 prints, which carries no continuity correction, and say so when results are compared with another program. Report the row percentages so that the pattern is visible, with Cramér’s V = ___ as the effect size. Where the expected frequencies were small, report Fisher’s exact p = ___ instead and give the reason: the expected frequencies, and not the p-value, justified the switch. Describe the association in words without a causal verb, and name the number of cases dropped for missing values.
COMMON MISTAKES
Table Lines That Reach the Text Misquoted
Because a cross-tabulation is short, a careful read of its lines exposes all six.
Continuity Correction Assumed Stata's chi2 gives the uncorrected Pearson statistic. A 2x2 result compared with R's default output, or with the corrected row in SPSS, differs unless the same version is named.
Exact Line Chosen After the Fact Switching to Fisher's line because the Pearson p-value missed the threshold is selective reporting. The choice follows from the expected frequencies and is set before either p-value is seen.
Percentages in the Wrong Direction Row percentages compare rows and column percentages compare columns. A sentence about the other direction describes a comparison that the printed table does not show.
Missing Codes Tabulated as Categories A code such as 99 left in the data becomes an extra row or column. It adds degrees of freedom, changes the statistic and spreads the real cases over more cells.
Matched Data in an Independent Test Before-and-after answers from the same people give counts that are not independent, so the Pearson p-value does not apply. A test for matched pairs addresses that design.
A p-Value Without Strength A statistic and a p-value alone say little about how strongly the variables are related, especially in a large sample. Cramér's V and the row percentages carry the size.
Pearson or Fisher’s line, and no rule written down? Independent Statistical Review checks the test selected, the expected frequencies and the effect size against your data.
FREQUENTLY ASKED QUESTIONS
Questions About tabulate, chi2 and exact
Stata and R disagree on 2x2 tables, tabulate offers two competing test lines, and its odds ratio comes from another command.
Why do Stata and R give different p-values for the same 2x2 table?
R’s chisq.test applies Yates’s continuity correction to 2×2 tables by default, whereas the Pearson line from tabulate with chi2 does not. Setting correct = FALSE in R reproduces the Stata statistic. Either version can be reported, provided the one used is named and the choice is not made after comparing p-values.
Which line do I report, Pearson or Fisher's exact?
Report Pearson when the expected frequencies are adequate and Fisher’s exact line when they are small, with the rule set before either p-value is seen. The usual warning sign is an expected frequency below 5 in a sizable share of cells, treated as a guideline. A 2×2 table with small counts is the clearest case for the exact line.
Does tabulate give an odds ratio for a 2x2 table?
No: tabulate reports the test and the measures of association, while the odds ratio with its confidence interval comes from the cc command, or cci for typed counts. Report that interval beside the test, since it shows how large the association is and in which direction.
Where does OnlineSPSS look when a chi-square result seems to contradict its table?
An in-house PhD-level statistician at OnlineSPSS compares the raw tabulation, the printed test lines and your sentence, looking for recodes, missing codes and a mismatch between the Pearson and exact lines. If the paragraph and table also need preparing, Results Reporting & Interpretation takes that stage, while the substantive reading of the pattern remains your call.
How do you protect my research data and confidentiality?
Your data and project details are handled confidentially, and project information is not published. Researcher names and institutions are not used publicly without permission. An NDA is available where required and can be arranged before sensitive materials are shared. Read more about confidentiality and data security.
What happens when I request an assessment, and how long does it take?
You share your research question, design and relevant materials, such as the dataset or the output you have, and a statistically trained Research Coordinator reviews the request and responds the same day. The analysis, deliverables, timeline and one fixed project fee are then confirmed before any work begins, so the timeline follows from the agreed scope.
Still have a question? Find more answers in our full FAQ.
NEXT STEP
Which Line of the Table
Will Your Text Quote?
A published table can be rerun from its counts with tabi, while a do-file built on the raw variables also records how recodes and missing codes shaped the cells.
No Commitment Required
Strictly Confidential
Fixed Project Fee
In-House PhD-Level Statisticians
Same-Day Response
Serving Researchers Since 2014