row_total = 40
column_total = 30
grand_total = 100
expected = row_total * column_total / grand_total
expected12.0
Chi-square tests are used with categorical count data. They compare what we observed with what a null model says we should expect.
After this lesson you should be able to:
(observed - expected)^2 / expected;Chi-square methods use counts, not raw measurements. Examples:
Percentages are useful for interpretation, but the test is based on counts.
The chi-square statistic adds a standardized discrepancy for each category:
chi-square = sum((observed - expected)^2 / expected)
Large discrepancies create larger chi-square statistics.
A goodness-of-fit test uses one categorical variable. It asks whether the distribution of counts matches a claimed distribution.
Example: Are colors of candies produced in the claimed proportions?
A chi-square test of independence uses two categorical variables in a two-way table. It asks whether the variables are associated.
Example: Is smoking status associated with disease status?
For a test of independence:
expected count = (row total * column total) / grand total
This formula represents what we would expect if the row and column variables were independent.
A fair die should have equal counts for six faces. It is rolled 60 times, so the expected count for each face is:
60 / 6 = 10
Suppose face 1 appears 16 times. Its chi-square contribution is:
(16 - 10)^2 / 10 = 36/10 = 3.6
You would compute this contribution for each face and add them.
A table has row total 40, column total 30, and grand total 100. Find the expected count for that cell under independence.
Step 1: Write the formula.
expected = row total * column total / grand total
Step 2: Substitute.
expected = 40 * 30 / 100 = 12
Interpretation: If the variables are independent, we would expect about 12 observations in that cell.
A chi-square test finds a small p-value for association between exercise category and stress category.
What can we say?
The variables are statistically associated in the data.
What can we not automatically say?
We cannot automatically say exercise causes lower stress. The data may be observational, and variables such as health, work schedule, or income may confound the relationship.
Chi-square tests generally require:
50, column total 20, grand total 200.14 and expected 10.50*20/200=5.(14-10)^2/10 = 16/10 = 1.6.Guess first. One categorical variable or two?
Guided exercise.
One categorical variable usually suggests goodness-of-fit. Two categorical variables in a table usually suggest independence or association.
Checkpoint. Favorite color in one sample versus claimed distribution: which test type?
Guess first. If row total and column total are large, should the expected count also tend to be larger?
Guided exercise.
Yes. The formula multiplies row total and column total, then divides by the grand total.
Checkpoint. Compute expected count for row total 30, column total 50, grand total 150.
Guess first. If observed equals expected, what is the contribution?
Guided exercise.
(observed - expected)^2 / expected = 0^2 / expected = 0
Checkpoint. Compute contribution for observed 8, expected 5.
Guess first. Does a large chi-square statistic mean observed counts are close to expected counts?
Guided exercise.
No. Large chi-square statistics come from large discrepancies between observed and expected counts.
Checkpoint. What does a small p-value suggest in a chi-square test?
30*50/150=10.(8-5)^2/5 = 9/5 = 1.8.Compute expected counts with Python.
12.0
Use SymPy to represent the chi-square contribution formula.