Stat 151 Module 7
Chi-Square Tests
By: Rosana Fok
1
Stat 151 Module 7
Section 1: Goodness-of-Fit Test
By: Rosana Fok
2
Comparing Counts
u This module will cover 3 types of chi-square tests:
u Tests of hypotheses with one sample and one categorical variable with 2
or more categories, called goodness-of-fit tests.
u Tests of a claim about the distribution of a categorical variable among 2
or more independent groups, called chi-square test of homogeneity.
u Tests of a claim about the relationship between two categorical
variables for a group, called chi-square test of independence.
3
Example
u Suppose a report claims that 30% of teenagers between the ages of 12 and
15 years in a particular city like to eat fruit as an evening snack. Rosana
wants to test this claim by taking a random sample of 1000 teenagers
between the ages of 12 and 15 years in the city of interest. She asks each
teenager in the sample whether or not they like to eat fruit as an evening
snack.
u Rosana summarizes the counts in a table.
Like Fruit Count
Yes 280
No 720
Total 1000
u Carry out an appropriate hypothesis test to determine whether the report’s
claim is true or not at the level of significance of 0.05.
4
Example
1. Hypothesis: H0 : p = 0.3 vs Ha : p ≠ 0.3
2. Assumptions and Condition:
Ø Independent observations
Ø Success/failure conditions: np ≥ 10 and n(1-p) ≥10
! !
"#"
3. z0 =
"! #$"!
%
4. P-value =
5. P-value
6. There is not enough evidence to conclude the proportion of teenagers
who like to eat fruit as an evening snack is different from 30% at the
level of significance of 0.05.
5
Goodness-of-Fit Test or Univariate 𝝌𝟐 Test
u a test of whether the frequency distribution of a categorical variable
with more than 2 categories from a sample matches the probability
distribution predicted by a model.
6
The Goodness-of-Fit Test (Hypothesis & Assumption)
u Given a distribution p01, … , p0k
u Hypotheses:
H0 : p1 = p01, …, pk = p0k vs Ha : H0 is not true.
u Assumptions and Conditions:
u Counted Data Condition: Check that the data are counts for the
categories of a categorical variable.
u Independence Assumption: The counts in the cells should be
independent of each other.
u Randomization Condition: The individuals who have been counted
and whose counts are available for analysis should be a random
sample from some population.
u Sample Size Assumption: We must have enough data for the methods to
work.
u Expected Cell Frequency Condition: We should expect to see at least
5 individuals in each cell.
7
The Goodness-of-Fit Test (Test Statistic)
Test statistics:
u The test statistic, called the chi-square (or chi-squared) statistic, is
found by adding up the sum of the squares of the deviations between
the observed and expected counts divided by the expected counts:
c =
2 (Observed - Expected )2
0 å
all categories Expected
u where the expected value is the product of the total number of
observations times this proportion.
8
Idea behind the Test Statistic
u The chi-square statistic is used only for testing hypotheses, not for
constructing confidence intervals.
u If the observed counts don`t match the expected, the statistics will be
large.
u this statistic measures how far apart the observed and expected counts are
u When (Observed – Expected) is small, the claim is right.
(because Observed is closed to Expected value)
u When it is large, bad evidence.
9
𝝌𝟐 𝐝𝐢𝐬tribution
u The chi-square value follows a 𝜒 $ distribution, which identified by a value called
degrees of freedom.
u The number of degrees of freedom for a goodness-of-fit test is c – 1, where c is
the number of categories.
u As degrees of freedom increases, the 𝜒 $ distribution becomes less right skewed.
10
The Goodness-of-Fit Test (P-value)
P-value = P(𝜒 ! > 𝜒"! ) is the upper-tail area for a distribution with degree
of freedom of c – 1.
u The mechanics may work like a one-sided test, but the
interpretation of a chi-square test is in some ways many-sided.
u There are many ways the null hypothesis could be wrong.
u There is no direction to the rejection of the null model – all we know
is that it doesn’t fit.
11
𝝌𝟐 𝐓𝐚𝐛𝐥𝐞
𝐨𝐫 𝐓𝐚𝐛𝐥𝐞 𝝌
Example:
Find 𝑃 𝜒 $ > 5 with
𝑑𝑓 = 2.
12
Decision and Conclusion
5) Decision:
o If p-value ≤ α è Reject H0
o If p-value > α è Do not reject H0
6) Conclusion: conclude within context.
13
Example (Fruit as Evening Snack)
u Suppose a report claims that 30% of teenagers between the ages of 12 and
15 years in a particular city like to eat fruit as an evening snack. Rosana
wants to test this claim by taking a random sample of 1000 teenagers
between the ages of 12 and 15 years in the city of interest. She asks each
teenager in the sample whether or not they like to eat fruit as an evening
snack.
u Rosana summarizes the counts in a table.
Like Fruit Count
Yes 280
No 720
Total 1000
u Carry out an appropriate chi-square test to determine if the report’s claim is
true or not at the level of significance of 0.05.
14
Example: Fruit as Evening Snack
1. Hypothesis:
H0 : pyes = 0.3, pno = 0.7 vs
Ha : at least one of the proportions is not as claimed
2. Assumptions and Conditions:
• Observed Counts Like Fruit Count 𝑬 = 𝒏𝒑𝒐
• Independent observations
Yes 280
• E ≥ 5 for each cell
No 720
Total 1000
&#' &
3. 𝜒%$ = ∑
'
15
Example: Fruit as Evening Snack
4. P-value
5. Decision:
6. There is not enough evidence to conclude at least one of the proportions is
not as claimed at the level of significance of 0.05. 16
Relationship between 1-proportion z-
Test and Goodness-of-Fit Test
u 𝜒%$ = 𝑧% $
u p-values of the two tests are the same
17
Example (Favorite Fruit)
u A report claims that the favorite fruit for teenagers between age 12 and 15 in
a particular city follows the following distribution:
Favorite Fruit Proportion
Apple 0.25
Banana 0.15
Cherries 0.05
Durian 0.05
Other 0.50
u Rosana wants to check if the distribution of favorite fruit follows the
distribution stated in the report. She goes around the city, randomly selects
teenagers between age 12 and 15 and asks each of them what is their
favorite fruit. She gathers a total of 1000 observations and gets the following
summarized data:
Favorite Fruits Counts
Apple 233
Banana 160
Cherries 63
Durian 64
Other 480 18
Example (Grass Seed) Continued
u Carry out an appropriate hypothesis test and see whether the report’s claim is
true, ie. the report is giving a true distribution of the favorite fruit for teenagers
between 12 and 15 years old in this particular city at a level of significance of
0.05.
u Hypothesis:
H0: papple = 0.25; pbanana = 0.15; pcherries = 0.05; pdurian = 0.05; pother = 0.50
Ha: the proportion of at least one fruit is not in the claiming proportions
Favorite Fruit Proportion Counts (O) E = np
Apple 0.25 233
Banana 0.15 160
Cherries 0.05 63
Durian 0.05 64
Other 0.50 480
Total 1 1000
u Assumptions and Conditions:
u Counted Data Condition
u Independence Assumption
19
u Sample Size Assumption
Example (Grass Seed) Continued
u Test Statistic: Favorite Fruit Counts (O) E = np
Apple 233 250
Banana 160 150
Cherries 63 50
Durian 64 50
Others 480 500
Total 1000 1000
u P-value
20
Example (Grass Seed) Continued
u Decision:
u Conclusion:
21
Stat 151 Module 7
Section 2: Chi-square Test of Homogeneity
By: Rosana Fok
22
Review of Two-Proportion z-test
u Recall: The two-proportion z-test is a hypothesis test to compare the
proportions of two groups.
u Example: A random sample of students are collected from the Faculty of
Science and another random sample of students are collected from the
Faculty of Arts, and the students from both samples are asked if they
exercise daily, and the data are summarized in the table below:
Exercise Daily Science Arts
Yes 50 40
No 220 200
u Suppose we are interested in comparing the proportion of students from
the Faculty of Science who exercise daily with the proportion of students
from the Faculty of Arts who exercise daily. What is the appropriate
hypothesis test to carry out?
23
Example:
Ø Carry out a 2-proportion z- test to determine if there’s any difference
between the proportions of the two faculties at the level of significance
of 0.05.
1) 𝐻" : 𝑝# − 𝑝$ = 0 vs. 𝐻% : 𝑝# − 𝑝$ ≠ 0
2) Assumptions and Conditions:
- 2 independent populations
- Independent observations
- Normal distribution:
u 𝑛& 𝑝̂& ≥ 10 and 𝑛& (1 − 𝑝̂& ) ≥ 10 and
u 𝑛! 𝑝̂! ≥ 10 and 𝑛! (1 − 𝑝̂! ) ≥ 10
Exercise Daily Science Arts
Yes 50 40
No 220 200 24
Example:
('! )('"
3) Test statistic: 𝑧" = ! !
=0.5475615337 ~𝑁(0,1)
̇
('# (&)('# )($ ,$ )
! "
4) P-value = 2 x P(z < -0.55) = 2 x 0.2912 = 0.5824
(NOTE: Using computer software, the exact p-value = 0.583993)
5) Since p-value > 0.05, we do not reject H0.
6) There is not enough evidence to conclude the proportion of students
from the Faculty of Science who exercise daily is different from the
proportion of students from the Faculty of Arts who exercise daily at the
level of significance of 0.05.
25
A Test of Homogeneity
u A test comparing the distribution of counts for two or more groups on
the same categorical variable is called a chi-square test of
homogeneity.
u A test of homogeneity is a generalization of the two-proportion z-test.
u The method of calculation for the test statistic that we calculate for
this test is identical to the chi-square statistic for goodness-of-fit.
u The expected counts are found directly from the data.
u The homogeneity test has different degrees of freedom from the
goodness-of-fit test due to data structure.
26
Steps for the Homogeneity Test:
1) State the hypotheses:
u H0 : there is no difference in the distribution of the categorical
variable between the groups vs.
u Ha : there are some differences in the distribution of the
categorical variable between the groups
2) Assumptions and Conditions:
u Counted Data Condition: The data must be counts.
u Independent Group Assumption
u Independent Observations Assumptions
u Sample Size Assumption: We must have enough data for the
methods to work.
u Expected Cell Frequency Condition: The expected count in
each cell must be at least 5.
27
Steps for the Homogeneity Test:
3) We calculated the chi-square statistic as we did in the goodness-of-fit test:
0)1 " !
𝜒"! = ∑%-- ./--# ~ 𝜒23 , where
1
o E is the expected value,
o O is the observed count from the sample data, and
o df is the degrees of freedom calculated using (r – 1)(c – 1), where r
represents the number of categories for the categorical variable, and c
represents the number of groups that is being compared.
4) P-value = P(𝜒 ! > 𝜒"! )
5) Decision:
o If p-value ≤ α è Reject H0
o If p-value > α è Do not reject H0
28
6) Conclusion: conclude within context.
Example: (Exercise)
A random sample of students are collected from the Faculty of Science and another
random sample of students are collected from the Faculty of Arts, and the students
from both samples are asked if they exercise daily, and the data are summarized in
the table below:
Exercise Daily Science (S) Arts (A) Total
Yes 50 40 90
No 220 200 420
Total 270 240 510
Carry out a homogeneity test to determine whether there are some differences in the
distribution of exercise habit between the students in the two faculties at the level of
significance of 0.05.
1) State the hypotheses:
Ø H0 : there is no difference in the distribution of exercise habit between the
students in Faculty of Science and the Faculty of Arts vs.
Ø Ha : there are some differences in the distribution of exercise habit between
the students in the Faculty of Science and the Faculty of Arts
29
Example: (Exercise)
2) Assumptions and Conditions:
Ø Counted Data Condition
Ø 2 independent populations
Ø Independent observations
Ø Sample Size Assumption
Exercise Daily Science (S) Arts (A) Total
Yes 50 40 90
No 220 200 420
Total 270 240 510
3) Test statistic:
30
Example: (Exercise)
4) P-value
5) Decision:
6) Conclusion: There is not enough evidence to conclude there are some differences in
the distribution of daily exercise between the Faculty of Science and the Faculty of
Arts at the level of significance of 0.05.
31
Relationship between 2-proportion z-
Test and 𝜒 " −test of homogeneity
u The two tests give the same conclusion, in particular, we also note:
u 𝜒%$ = 𝑧% $
u p-values of the two tests are the same
32
Example: (Revised Exercise Example)
u What if we are interested in comparing the proportion of students exercise
daily among three groups instead of two groups? For example, we also
randomly select a sample of students from Kinesiology, Sports, &
Recreation.
Exercise Science Arts Kinesiology, Sports, & Total
Daily Recreation
Yes 50 40 25 115
No 220 200 40 460
Total 270 240 65 575
u Carry out an appropriate hypothesis test to determine whether there are some
differences in the distribution of exercise habit between the three faculties at
the level of significance of 0.05.
33
Example: (Revised Exercise Example)
1) State the hypotheses:
u H0 : there is no difference in the distribution of exercise habit between the
three faculties vs.
u Ha : there are some differences in the distribution of exercise habit
between the three faculties
2) Assumptions and Conditions:
- Counted Data Condition
- Independent Group Assumption
- Independent Observations Assumption
- Sample Size Assumption is satisfied as E are all at least 5.
Exercise Science Arts Kinesiology, Sports, Total
Daily & Recreation
Yes 50 40 25 115
34
No 220 200 40 460
Total 270 240 65 575
Example: (Revised Exercise Example)
Exercise Science Arts Kinesiology, Sports, Total
Daily & Recreation
Yes 50 54 40 48 25 13 115
No 220 216 200 192 40 52 460
Total 270 240 65 575
&#' &
3) Test statistics: 𝜒($ = ∑)** +,**-
'
35
Example: (Revised Exercise Example)
4)P-value
5) Decision:
6) Conclusion: There is enough evidence to conclude there are some
differences in the distribution of exercise habit among the 3 faculties at the
level of significance of 0.05.
36
Stat 151 Module 7
Section 3: Chi-square Test of Independence
By: Rosana Fok
37
The 𝜒 ! Test for Independence
u Contingency tables categorize counts on two variables so that we can
see whether the distribution of counts on one variable is contingent on
the other.
u Tests of independence examine counts from a single group for evidence
of an association between two categorical variables.
u A chi-square test of independence uses the same calculation as a test
of homogeneity; the main difference is what you think and how the
data are being collected.
38
Steps for the Independence Test:
1) State the hypotheses:
u H0 : the two categorical variables are independent vs.
u Ha : the two categorical variables are not independent
2) The assumptions and conditions are the same as for the chi-square goodness-
of-fit test:
u Counted Data Condition: The data must be counts.
u Independent Observations Assumptions
u Sample Size Assumption: We must have enough data for the methods to
work.
u Expected Cell Frequency Condition: The expected count in each cell must be
at least 5.
39
Steps for the Independent Test:
3) We calculated the chi-square statistic as we did in the goodness-of-fit test:
&#' & $
𝜒($ = ∑)** +,**-
'
~ 𝜒./ , where
o E is the expected value,
o O is the observed count from the sample data, and
o df is the degrees of freedom calculated using (r – 1)(c – 1), where r represents the
number of categories for the categorical variable, and c represents the number of
categories for the column categorical variable.
4) P-value = P(𝜒 $ > 𝜒($ )
5) Decision:
o If p-value ≤ α è Reject H0
o If p-value > α è Do not reject H0
6) Conclusion: conclude within context.
40
Example:
u For example, if Rosana is interested in whether there is a relationship between
two categorical variables, daily exercise and faculty, she can carry out a chi-
square test of independence. How would she collect the data?
u She would randomly select one group of students from the Faculty of Science
and the Faculty of Arts. After which, these students in the sample would be
asked whether they exercise daily and which faculty are they in? The results
can be put into a contingency table:
Exercise Daily Science Arts
Yes 50 40
No 220 200
41
Example (Tutoring)
Company ABC has developed a tutoring program for students enrolled in a first-year
statistics course. The company wants to learn whether there is a relationship between
participating in the tutoring program and receiving a passing course grade. They
randomly selected a group of 480 students and asked them whether they participated in
the tutoring program and whether they passed the first-year statistics course. The
collected data is presented in the 2-way table below:
Participated in tutoring program
Yes No Total
Failed 4 80 84
Passed 36 360 396
Total 40 440 480
Does the data provide evidence that there is a relationship between participating in the
tutoring program and receiving a passing course grade at the level of significance of 0.05?
u Ho: Tutoring program participation and passing the course are independent.
u Ha: Tutoring program participation and passing the course are not independent.
u Assumptions and Conditions:
u 1) Counted data
u 2) Independent observations 42
u 3) Each cell has expected count more than 5
Example (Tutoring) Continued
Participated in tutoring program
Yes No Total
Failed 4 80 84
Passed 36 360 396
Total 40 440 480
u Test statistic:
43
Example (Tutoring) Continued
u P-value:
u Decision:
u Conclusion:
44
Thank you for watching
this video!