Chi-squared test (goodness of fit & independence)
Test whether data fit a model, or whether two categorical variables are independent, with the exact key presses for the TI-84 Plus CE, Casio fx-CG50, Casio fx-CG100 and TI-Nspire CX, plus what to write in the exam.
For a test of independence, enter the observed counts as a matrix and run the two-way chi-squared test; the calculator works out the expected counts, , the p-value and the degrees of freedom. For goodness of fit, enter the observed and expected counts as two lists and give the degrees of freedom yourself. Then compare the p-value with the significance level.
Example
(a) A die is rolled 60 times and the scores 1 to 6 occur 4, 6, 17, 16, 8 and 9 times. Test, at the 5% significance level, whether the die is fair.
(b) Students in two year groups were asked how they travel to school.
| Walk | Bus | Car | |
|---|---|---|---|
| Year 10 | 22 | 31 | 17 |
| Year 12 | 38 | 19 | 13 |
(a) : the die is fair. The expected count for each score is , and there are degrees of freedom. The calculator gives and . Since , reject : there is evidence the die is not fair.
(b) : travel method is independent of year group. The degrees of freedom are . The calculator gives and . Since , reject : there is evidence that the way students travel depends on year group.
Choose your calculator
How to do a chi-squared test on a TI-84 Plus CE
Test of independence (two-way table) | |
Put the observed counts in a matrix. Press 2nd x⁻¹ (MATRIX), press ▶ to reach EDIT, choose 1:[A] and press enter. Type the size, 2 enter 3 enter for 2 rows and 3 columns, then type the counts row by row, pressing enter after each one. |
MATRIX[A] 2 ×3 22 31 17 38 19 13 |
Press 2nd mode (QUIT) to leave the editor. Press stat, press ▶ twice to reach TESTS, and scroll down to C:χ²-Test.... Press enter. |
|
Leave Observed as [A] and Expected as [B]: the calculator stores the expected counts in [B] for you. Move down to Calculate and press enter. , and . |
χ²-Test χ²=7.68 p=0.0214936 df=2 |
To see the expected counts, press 2nd x⁻¹ (MATRIX), choose 2:[B] from the NAMES list and press enter twice. Every expected count should be at least 5. |
[B] 30 25 15 30 25 15 |
Goodness of fit (observed and expected lists) | |
Press stat, choose 1:Edit and press enter. Type the observed counts 4,6,17,16,8,9 in L1, and the expected counts, 10 six times, in L2, pressing enter after each value. |
L1 L2 4 10 6 10 17 10 16 10 8 10 9 10 |
Press stat, press ▶ twice to reach TESTS, and scroll down to D:χ²GOF-Test.... Press enter. |
|
Set Observed to L1 (press 2nd 1) and Expected to L2 (press 2nd 2). Type the degrees of freedom, 5, because there are 6 categories. Move down to Calculate and press enter. , and . |
χ²GOF-Test χ²=14.2 p=0.01438768 df=5 |
The goodness-of-fit test does not work out df for you: type the number of categories minus 1, and minus 1 more for each parameter estimated from the data.
How to do a chi-squared test on a Casio fx-CG50
Test of independence (2WAY) | |
Press MENU and choose Run-Matrix, then press F3 (▶MAT/VCT) to open the Matrix Editor. Highlight Mat A, press F3 (DIM), type 2 EXE for the rows and 3 EXE for the columns, then press EXE. |
Mat A: 2 ×3 |
Type the counts row by row, pressing EXE after each one: the highlight moves to the next cell on its own. Then press EXIT until you are back at the main menu. |
22 31 17 38 19 13 |
Press MENU and choose Statistics. Press F3 (TEST), F3 (CHI), then F2 (2WAY). Set Observed to Mat A and Expected to Mat B: highlight the line, press F1 (Mat) and choose the letter. Move down to Execute and press F1 (CALC). , and . |
χ²Test χ²=7.6800 p=0.0214936 df=2 |
The expected counts are saved in Mat B. While the result is on the screen, press F6 (▶MAT) to open the Matrix Editor and look at them. The matrix must have at least 2 rows and 2 columns. |
Mat B: 2 ×3 30 25 15 30 25 15 |
Goodness of fit (GOF) | |
Press MENU and choose Statistics. In the List Editor type the observed counts 4,6,17,16,8,9 in List 1 and the expected counts, 10 six times, in List 2, pressing EXE after each value. |
List 1 List 2 4 10 6 10 17 10 16 10 8 10 9 10 |
Press F3 (TEST), F3 (CHI), then F1 (GOF). Set Observed to List 1 and Expected to List 2, and type the degrees of freedom, 5. Move down to Execute and press F1 (CALC). , and . |
χ²GOF Test χ²=14.2 p=0.0143877 df=5 |
On the Casio the expected counts for a two-way test are calculated for you and saved in the second matrix. For goodness of fit you type the expected counts in a list yourself.
How to do a chi-squared test on a Casio fx-CG100
Test of independence (Two-Way Test) | |
Press HOME and choose Calculate. Go to the Matrix tab with the previous-tab key, highlight Mat A, and choose TOOLS → Dimension. Type 2 for the rows and press OK, type 3 for the columns and press OK, then press OK again on Confirm. |
Mat A: 2 ×3 |
Type the counts row by row, pressing EXE after each one. Then press HOME and choose Statistics. |
22 31 17 38 19 13 |
Go to the Setup tab with the next-tab key. Choose Hypothesis Test on the first line, then on the second line choose χ² Test → χ² Two-Way Test. Set Observed to Mat A and Expected to Mat B. Choose Execute and press OK. , and . |
χ²=7.6800 p=0.0214936 df=2 |
Goodness of fit (Goodness-of-Fit Test) | |
Press HOME and choose Statistics. In the List Editor type the observed counts 4,6,17,16,8,9 in List 1 and the expected counts, 10 six times, in List 2. |
List 1 List 2 4 10 6 10 17 10 16 10 8 10 9 10 |
Go to the Setup tab. Choose Hypothesis Test, then χ² Test → χ² Goodness-of-Fit Test. Set Observed to List 1, Expected to List 2, and df to 5. Choose Execute and press OK. , and . |
χ²=14.2 p=0.0143877 df=5 |
Every observed count must be a whole number, and the matrix for a two-way test needs at least 2 rows and 2 columns.
How to do a chi-squared test on a TI-Nspire CX
Test of independence (χ² 2-way Test) | |
On a Calculator page press menu → Matrix & Vector → Create → Matrix. Enter 2 rows and 3 columns and choose OK. Type the counts row by row, using tab to move between cells. |
22 31 17 38 19 13 |
Press ctrl var (sto→), type the name obs, and press enter to store the matrix. |
[...]→obs |
Press menu → Statistics → Stat Tests → χ² 2-way Test. Type obs in the Observed Matrix field and press enter. , and . |
χ² 2-way Test χ²=7.68 PVal=0.0214936 df=2 |
The expected counts are kept in a variable. Type |
stat.expmat 30 25 15 30 25 15 |
Goodness of fit (χ² GOF) | |
Add a Lists & Spreadsheet page. In the top cell of column A type the name obs, and in column B type ex. Below them type the observed counts 4,6,17,16,8,9 and the expected counts, 10 six times, pressing enter after each value. |
obs ex 4 10 6 10 17 10 16 10 8 10 9 10 |
Press menu → Statistics → Stat Tests → χ² GOF. Type obs for the observed list, ex for the expected list, and 5 for the degrees of freedom. Choose OK. , and . |
χ² GOF χ²=14.2 PVal=0.0143877 df=5 |
Give the matrix or the lists a name first, then type that name in the test box. The names obs and ex used here are only examples.
Sources. Key presses follow the manufacturers' documentation: the TI-84 Plus CE eGuide, the TI-Nspire eGuide, the Casio fx-CG50 Software User's Guide (version 3.30) and the Casio fx-CG100 online manual.
What to write in the exam
- State the hypotheses first. For independence: : the two variables are independent, and : they are not independent. For goodness of fit: : the data fit the model. Say the significance level too.
- Write the degrees of freedom. For a table with rows and columns it is . For goodness of fit it is the number of categories minus 1, then minus 1 for each parameter estimated from the data.
- Write the statistic and the p-value. For example and . Do not write the key presses or the name of the calculator test as working.
- Compare p with the significance level and state the decision in words: reject if is less than the level, otherwise do not reject it. Then give the conclusion in the context of the question.
- Check the expected counts. Each expected count should be at least 5. If one is smaller, combine categories (add neighbouring rows, columns or groups) before the test, and use the new number of categories for the degrees of freedom.
Which test to use
- Independence: the data are counts in a table of two categorical variables, and the question asks whether one affects the other. Enter the table as a matrix.
- Goodness of fit: the data are counts in one row of categories, and the question asks whether they fit a model such as equally likely outcomes or a stated ratio. Enter observed and expected counts as two lists.
- Expected counts for goodness of fit come from the model, not from the data: total count multiplied by the probability of each category. Under a fair die with 60 rolls each expected count is 10.
For what the p-value means and the errors a test can make, see interpret p-values, significance levels and errors. For the matrix editor in more detail, see matrix operations.
Error messages and what to do
| Calculator | Message | Usual cause | What to try |
|---|---|---|---|
| TI-84 Plus CE | ERR:DIMENSION MISMATCH | The observed and expected lists in the goodness-of-fit test have different numbers of values. | Check L1 and L2 hold the same number of values, one for each category. |
| Casio fx-CG50 | Dimension ERROR | The observed and expected lists have different numbers of values. | Check both lists have one value for each category. |
| Casio fx-CG100 | Dimension ERROR | The observed and expected lists have different numbers of values. | Check both lists have one value for each category. |
| TI-Nspire CX | Dimension mismatch | The observed and expected lists have different numbers of values. | Check both lists have one value for each category. |
Common mistakes
- Entering totals in the matrix. Enter only the counts in the body of the table. The calculator works out the row and column totals itself.
- Mixing up the rows and columns. Enter the table the way it is printed. A transposed matrix gives the same statistic, but a matrix with the wrong dimensions does not match the question.
- The wrong degrees of freedom for goodness of fit. The calculator asks for df, or takes the value you type. It is the number of categories minus 1, not the number of observations.
- Typing percentages or proportions instead of counts. The test needs whole-number counts for the observed values.
- Forgetting the conclusion. After the p-value, compare it with the significance level, state whether you reject , and say what that means in the context.
Try it yourself
Real questions from the IB Maths Help question bank. Type your answer and press Mark it: the worked solution and mark scheme then appear. Parts (a) and (b) use the chi-squared test; use your calculator for the statistic.
Practise with your calculator
Choose your course to see the practice sets that use this skill. You can also try practice mode or browse every GDC guide.
Quick questions
How do I do a chi-squared test of independence on a calculator?
Enter the observed counts as a matrix, then run the two-way chi-squared test: STAT, TESTS, C on the TI-84 Plus CE; Statistics, TEST, CHI, 2WAY on the fx-CG50; Hypothesis Test, Two-Way Test on the fx-CG100; Stat Tests, 2-way Test on the TI-Nspire. The calculator gives the statistic, p-value and degrees of freedom.
How do I do a goodness-of-fit test?
Enter the observed counts in one list and the expected counts in another, then run the goodness-of-fit test (GOF) and type the degrees of freedom: the number of categories minus 1, and minus 1 more for each parameter estimated from the data.
How do I find the expected counts?
For a two-way test the calculator stores them: in matrix [B] on the TI-84 Plus CE, in the second matrix on the Casio calculators, and in stat.expmat on the TI-Nspire. For goodness of fit you work them out from the model.
What do I do if an expected count is less than 5?
Combine neighbouring categories so that every expected count is at least 5, enter the new counts, and use the new number of categories to work out the degrees of freedom.
Do I write the calculator test in my exam working?
No. Write the hypotheses, the degrees of freedom, the values of the statistic and the p-value, the comparison with the significance level, and the conclusion in context.