Skip to content
All apps

χ² test of independence

About this app

A contingency table of two categorical variables as a mosaic: cells colored by standardized residual, observed vs. expected, χ² statistic, degrees of freedom and p-value live. Are two variables independent — e.g. department × promotion?

The statistics

The χ² test of independence checks whether two categorical variables are independent. Under independence each cell of the contingency table is expected to hold E = row total · column total / N. The statistic χ² = Σ (O − E)²/E adds up the squared deviations of the observed counts O from the expected ones. The degrees of freedom are df = (r − 1)(c − 1). From χ² and df follows the p-value, the probability of a deviation at least this large if the variables were independent.

What you see

The contingency table appears as a mosaic plot: column widths are proportional to column totals, and row heights within each column to the conditional proportions. Under independence the dividing lines of all columns sit at the same height; offset lines reveal an association. Cells are coloured by whether more or fewer cases were observed than expected. Three examples are ready: independent, associated, and promotion by department. A values panel lists N, df, χ² and p with a short interpretation.

Things to try

Load the independent example: the dividing lines line up, χ² is 0 and p = 1. Switch to the association and watch the lines jump while p becomes tiny. In the promotion example you test whether the promotion rate depends on department (sales, engineering, admin). Change individual cell counts with the plus and minus buttons and follow how χ² and p respond. Toggle between observed and expected counts.

Frequently asked questions

When do you use a chi-square test?
The χ² test of independence is used when you have two categorical variables, such as department and promotion, and want to know whether they are related. It works on a contingency table of counts. As a rule of thumb the expected counts should not be too small; E ≥ 5 is often quoted.
How do you calculate expected frequencies?
Assuming independence, the expected count for a cell is E = row total · column total / grand total N. It is the number of cases you would see if the variables had nothing to do with each other. The χ² statistic compares these values with the observed counts.
How many degrees of freedom does a chi-square test have?
For a contingency table with r rows and c columns, df = (r − 1)(c − 1). A 2 × 2 table therefore has one degree of freedom and a 3 × 2 table two. The degrees of freedom determine which χ² distribution is used for the p-value.
What does the chi-square app show?
It shows a contingency table as a mosaic plot with observed and expected counts and colour-coded deviations. You pick one of three examples, change cell counts with buttons and see N, degrees of freedom, χ² and p-value instantly.

Subject: Statistics

More from Statistics

A VisuApp by heyprof: interactive, free in your browser, no sign-up. What is a VisuApp? · All apps