Chi-squared test for the relationship between two categorical variables - overview
This page offers structured overviews of one or more selected methods. Add additional methods for comparisons (max. of 3) by clicking on the dropdown button in the right-hand column. To practice with a specific method click the button at the bottom row of the table
Chi-squared test for the relationship between two categorical variables
Mann-Whitney-Wilcoxon test
McNemar's test
You cannot compare more than 3 methods
Independent /column variable
Independent/grouping variable
Independent variable
One categorical with $I$ independent groups ($I \geqslant 2$)
One categorical with 2 independent groups
2 paired groups
Dependent /row variable
Dependent variable
Dependent variable
One categorical with $J$ independent groups ($J \geqslant 2$)
One of ordinal level
One categorical with 2 independent groups
Null hypothesis
Null hypothesis
Null hypothesis
H0: there is no association between the row and column variable
More precisely, if there are $I$ independent random samples of size $n_i$ from each of $I$ populations, defined by the independent variable:
H0: the distribution of the dependent variable is the same in each of the $I$ populations
If there is one random sample of size $N$ from the total population:
H0: the row and column variables are independent
If the dependent variable is measured on a continuous scale and the shape of the distribution of the dependent variable is the same in both populations:
H0: the population median for group 1 is equal to the population median for group 2
Else:
Formulation 1:
H0: the population scores in group 1 are not systematically higher or lower than the population scores in group 2
Formulation 2:
H0:
P(an observation from population 1 exceeds an observation from population 2) = P(an observation from population 2 exceeds observation from population 1)
Several different formulations of the null hypothesis can be found in the literature, and we do not agree with all of them. Make sure you (also) learn the one that is given in your text book or by your teacher.
Let's say that the scores on the dependent variable are scored 0 and 1. Then for each pair of scores, the data allow four options:
First score of pair is 0, second score of pair is 0
First score of pair is 0, second score of pair is 1 (switched)
First score of pair is 1, second score of pair is 0 (switched)
First score of pair is 1, second score of pair is 1
The null hypothesis H0 is that for each pair of scores, P(first score of pair is 0 while second score of pair is 1) = P(first score of pair is 1 while second score of pair is 0). That is, the probability that a pair of scores switches from 0 to 1 is the same as the probability that a pair of scores switches from 1 to 0.
Other formulations of the null hypothesis are:
H0: $\pi_1 = \pi_2$, where $\pi_1$ is the population proportion of ones for the first paired group and $\pi_2$ is the population proportion of ones for the second paired group
H0: for each pair of scores, P(first score of pair is 1) = P(second score of pair is 1)
Alternative hypothesis
Alternative hypothesis
Alternative hypothesis
H1: there is an association between the row and column variable
More precisely, if there are $I$ independent random samples of size $n_i$ from each of $I$ populations, defined by the independent variable:
H1: the distribution of the dependent variable is not the same in all of the $I$ populations
If there is one random sample of size $N$ from the total population:
H1: the row and column variables are dependent
If the dependent variable is measured on a continuous scale and the shape of the distribution of the dependent variable is the same in both populations:
H1 two sided: the population median for group 1 is not equal to the population median for group 2
H1 right sided: the population median for group 1 is larger than the population median for group 2
H1 left sided: the population median for group 1 is smaller than the population median for group 2
Else:
Formulation 1:
H1 two sided: the population scores in group 1 are systematically higher or lower than the population scores in group 2
H1 right sided: the population scores in group 1 are systematically higher than the population scores in group 2
H1 left sided: the population scores in group 1 are systematically lower than the population scores in group 2
Formulation 2:
H1 two sided: P(an observation from population 1 exceeds an observation from population 2) $\neq$ P(an observation from population 2 exceeds an observation from population 1)
H1 right sided: P(an observation from population 1 exceeds an observation from population 2) > P(an observation from population 2 exceeds an observation from population 1)
H1 left sided: P(an observation from population 1 exceeds an observation from population 2) < P(an observation from population 2 exceeds an observation from population 1)
The alternative hypothesis H1 is that for each pair of scores, P(first score of pair is 0 while second score of pair is 1) $\neq$ P(first score of pair is 1 while second score of pair is 0). That is, the probability that a pair of scores switches from 0 to 1 is not the same as the probability that a pair of scores switches from 1 to 0.
Other formulations of the alternative hypothesis are:
H1: $\pi_1 \neq \pi_2$
H1: for each pair of scores, P(first score of pair is 1) $\neq$ P(second score of pair is 1)
Assumptions
Assumptions
Assumptions
Sample size is large enough for $X^2$ to be approximately chi-squared distributed under the null hypothesis. Rule of thumb:
2 $\times$ 2 table: all four expected cell counts are 5 or more
Larger than 2 $\times$ 2 tables: average of the expected cell counts is 5 or more, smallest expected cell count is 1 or more
There are $I$ independent simple random samples from each of $I$ populations defined by the independent variable, or there is one simple random sample from the total population
Group 1 sample is a simple random sample (SRS) from population 1, group 2 sample is an independent SRS from population 2. That is, within and between groups, observations are independent of one another
Sample of pairs is a simple random sample from the population of pairs. That is, pairs are independent of one another
Test statistic
Test statistic
Test statistic
$X^2 = \sum{\frac{(\mbox{observed cell count} - \mbox{expected cell count})^2}{\mbox{expected cell count}}}$
Here for each cell, the expected cell count = $\dfrac{\mbox{row total} \times \mbox{column total}}{\mbox{total sample size}}$, the observed cell count is the observed sample count in that same cell, and the sum is over all $I \times J$ cells.
Two different types of test statistics can be used; both will result in the same test outcome. The first is the Wilcoxon rank sum statistic $W$:
The second type of test statistic is the Mann-Whitney $U$ statistic:
$U = W - \dfrac{n_1(n_1 + 1)}{2}$
where $n_1$ is the sample size of group 1.
Note: we could just as well base W and U on group 2. This would only 'flip' the right and left sided alternative hypotheses. Also, tables with critical values for $U$ are often based on the smaller of $U$ for group 1 and for group 2.
$X^2 = \dfrac{(b - c)^2}{b + c}$
Here $b$ is the number of pairs in the sample for which the first score is 0 while the second score is 1, and $c$ is the number of pairs in the sample for which the first score is 1 while the second score is 0.
Sampling distribution of $W$ and of $U$ if H0 were true
Sampling distribution of $X^2$ if H0 were true
Approximately the chi-squared distribution with $(I - 1) \times (J - 1)$ degrees of freedom
Sampling distribution of $W$:
For large samples, $W$ is approximately normally distributed with mean $\mu_W$ and standard deviation $\sigma_W$ if the null hypothesis were true. Here
$$
\begin{aligned}
\mu_W &= \dfrac{n_1(n_1 + n_2 + 1)}{2}\\
\sigma_W &= \sqrt{\dfrac{n_1 n_2(n_1 + n_2 + 1)}{12}}
\end{aligned}
$$
Hence, for large samples, the standardized test statistic
$$
z_W = \dfrac{W - \mu_W}{\sigma_W}\\
$$
follows approximately the standard normal distribution if the null hypothesis were true. Note that if your $W$ value is based on group 2, $\mu_W$ becomes $\frac{n_2(n_1 + n_2 + 1)}{2}$.
Sampling distribution of $U$:
For large samples, $U$ is approximately normally distributed with mean $\mu_U$ and standard deviation $\sigma_U$ if the null hypothesis were true. Here
$$
\begin{aligned}
\mu_U &= \dfrac{n_1 n_2}{2}\\
\sigma_U &= \sqrt{\dfrac{n_1 n_2(n_1 + n_2 + 1)}{12}}
\end{aligned}
$$
Hence, for large samples, the standardized test statistic
$$
z_U = \dfrac{U - \mu_U}{\sigma_U}\\
$$
follows approximately the standard normal distribution if the null hypothesis were true.
For small samples, the exact distribution of $W$ or $U$ should be used.
Note: if ties are present in the data, the formula for the standard deviations $\sigma_W$ and $\sigma_U$ is more complicated.
If $b + c$ is large enough (say, > 20), approximately the chi-squared distribution with 1 degree of freedom.
If $b + c$ is small, the Binomial($n$, $P$) distribution should be used, with $n = b + c$ and $P = 0.5$. In that case the test statistic becomes equal to $b$.
Find $p$ value corresponding to observed $X^2$ and check if it is equal to or smaller than $\alpha$
If $b + c$ is small, the table for the binomial distribution should be used, with as test statistic $b$:
Check if $b$ observed in sample is in the rejection region or
Find two sided $p$ value corresponding to observed $b$ and check if it is equal to or smaller than $\alpha$
n.a.
Equivalent to
Equivalent to
-
If there are no ties in the data, the two sided Mann-Whitney-Wilcoxon test is equivalent to the Kruskal-Wallis test with an independent variable with 2 levels ($I$ = 2).
Stuart-Maxwell test, with a categorical dependent variable consisting of two independent groups
Put your dependent variable in the box below Test Variable List and your independent (grouping) variable in the box below Grouping Variable
Click on the Define Groups... button. If you can't click on it, first click on the grouping variable so its background turns yellow
Fill in the value you have used to indicate your first group in the box next to Group 1, and the value you have used to indicate your second group in the box next to Group 2