A Chi-Square Test of Independence is used to determine whether or not there is a significant association between two categorical variables.
This tutorial explains how to perform a Chi-Square Test of Independence in Stata.
Example: Chi-Square Test of Independence in Stata
For this example we will use a dataset called auto, which contains information about 74 different automobiles from 1978.
Use the following steps to perform a Chi-Square Test of Independence to determine if there is a significant association between the following two variables:
- rep78: the number of times the car received a repair in 1978 (ranges from 1 to 5)
- foreign: whether or not the car type is foreign (0 = no, 1 = yes)
Step 1: Load and view the raw data.
First, we will load the data by typing in the following command:
sysuse auto
We can view the raw data by typing in the following command:
br

Each line displays information for an individual car including price, mpg, weight, length, and a variety of other variables. The only two variables that we care about are rep78 and foreign.
Step 3: Perform the Chi-Square Test of Independence.
We will use the following syntax to perform the test:
tab first_variable second_variable, chi2
Here is the exact syntax we’ll use in our case:
tab rep78 foreign, chi2

Here is how to interpret the output:
Summary table: This table shows the total counts for each combination of rep78 and foreign. For example
- There were 2 cars that were domestic and received 1 repair in 1978.
- There were 8 cars that were domestic and received 2 repairs in 1978.
- There were 27 cars that were domestic and received 3 repairs in 1978.
And so on.
Pearson chisq(4): This is the Chi-Square test statistic for the test. It turns out to be 27.2640.
Pr: This is the p-value associated with the Chi-Square test statistic. It turns out to be 0.000. Since this is less than 0.05, we fail to reject the null hypothesis that the two variables are independent. We have sufficient evidence to conclude that there is a statistically significant association between whether or not a car was foreign and the total number of repairs it received.
Do you mean “we reject the null hypothesis” rather than “we fail to reject the null hypothesis”?
Well elaborated
Very clear and understandable
hi, kindly help me out for the interpretation. In previous sessions, you said when p value is less than 0.05 we reject the Ho, now here chi square test for the p value is 0.00 but you are telling us we will fail to reject the null hypothesis. why
Hi Clarosky…This is a great question, and honestly it’s exactly the kind of thing that trips people up when they’re first working with hypothesis tests.
You’re absolutely right about the general rule: if the p-value is less than 0.05, we reject the null hypothesis. So if you’re seeing a p-value reported as 0.00, your first instinct should be “that’s definitely less than 0.05, so we should reject Ho.”
The key issue here is interpretation and context.
First, a p-value of 0.00 doesn’t literally mean zero. It usually just means “so small that the software rounded it down,” like < 0.001. So statistically, that would strongly support rejecting the null hypothesis. If someone is saying “fail to reject Ho” in that situation, a few things could be going on: * They may have flipped the hypotheses (for example, testing in a different direction than expected). * They might be interpreting the test incorrectly (this happens more often than you’d think). * There could be a mismatch between what the test is actually evaluating and what they think it’s evaluating. * Or it could simply be a mistake in explanation or a typo. For a chi-square test specifically, the rule is still the same: small p-value → evidence against the null → reject Ho. There isn’t a special exception just because it’s chi-square. So in short: if the p-value is truly 0.00 (i.e., extremely small), you should reject the null hypothesis. If you’re being told otherwise, it’s worth double-checking the setup of the hypotheses or asking for clarification, because something isn’t lining up.