Unit 5
Unit 5
Unit 5
Carrie Madden
µp̂ = p
s
p (1 − p)
and σp̂ =
n
X − µX
Z= ∼ N(0, 1)
σX
Result
So when the sample size n is high,
p̂ − p
⇒Z = s ∼ N(0, 1)
p(1 − p)
n
np ≥ 10 and n (1 − p) ≥ 10
and that the population size is very large compared to the sample size.
Example
Example
Suppose we randomly select 200 U of M students and ask them whether
they are left- or right-handed. Assuming 10% of all people are left-handed,
what is the probability that at least 25 (12.5%) of the students in our
sample are left-handed?
Solution
Let p̂ be the proportion of students in the sample who are left-handed.
Then for a sample of size 200, the mean and standard deviation of p̂ are
µp̂ = p = 0.10
s
0.10 (0.90)
and σp̂ = = 0.02121
200
Example
We calculate
Since the population is large, we can use the normal distribution. The
probability that at least 12.5% of the sampled students are left-handed is:
p̂ − p 0.125 − 0.10
P(p̂ ≥ 0.125) = P Z ≥
s = P Z ≥
s
p(1 − p) 0.1(0.9)
n 200
= P(Z ≥ 1.18) = 1 − P(Z < 1.18) = 1 − 0.8810 = 0.1190
Example – R code
prob<- 1-pnorm(1.18)
prob
## [1] 0.1190001
Example – Airlines
Example
A large airplane making a flight from Winnipeg to Toronto has 150 seats.
The airline knows from past records that 8% of customers do not show up
for their flight, and so they regularly overbook their flights. They have sold
160 tickets for this flight. What is the probability that all passengers who
arrive will get a seat?
Solution
Let p̂ be the proportion of passengers who do not show up for their flight.
The mean and standard deviation of p̂ are
µp̂ = p = 0.08
s s
p (1 − p) 0.08 (0.92)
and σp̂ = = = 0.0214
n 160
\end{frame}
Carrie Madden STAT 2000 – Unit 5 10 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Example
Solution
We can calculate np = 12.8 > 10 and n (1 − p) = 147.2 > 10, so we can
safely use the normal approximation. For everyone to get a seat, at least
10
10 passengers, or a proportion of 160 = 0.0625 must not show up.
Therefore, the probability
that everyonewho shows
up gets a seat is
p̂ − p 0.0625 − 0.08
P(p̂ ≥ 0.0625) = P Z ≥
s = P Z ≥
s
p(1 − p) 0.08(0.92)
n 160
= P(Z ≥ −0.82) = 1 − P(Z < −0.82) = 1 − 0.2061 = 0.7939
Example – R code
prob<- 1-pnorm(-0.82)
prob
## [1] 0.7938919
Practice Question
It is known that 20% of a certain type of lottery ticket are winners. If you
buy 100 lottery tickets, what is the approximate probability that at least
25 of them are winners?
A 0.1056
B 0.1539
C 0.2061
D 0.2578
E 0.3085
We now examine inference methods for the case where the parameter of
interest is some population proportion p.
We estimate p by the sample proportion p̂, which has mean and standard
deviation s
p (1 − p)
µp̂ = p and σp̂ =
n
Recall that, in order to use the normal distribution when doing probability
calculations for p̂, we required that np ≥ 10 and n (1 − p) ≥ 10, and that
the sample size was small compared to the population size.
Although we won’t formally verify this for each example in this unit, these
assumptions will hold for all of them, and so the use of the normal
distribution in doing probability calculations is justified.
Example – Obama
Example
In a survey of 1000 randomly selected Americans, 58% said they approve
of the job being done by President Barack Obama. The following is a 90%
confidence interval for the true proportion of all Americans who approve of
Obama:
s s
∗ p̂ (1 − p̂) 0.58 (0.42)
p̂ ± z = 0.58 ± 1.645
n 1000
= 0.58 ± 0.026
= (0.554, 0.606)
Practice Question
s
p̂ (1 − p̂)
m = z∗
n
∗ 2
z
⇒ n= p̂ (1 − p̂)
m
But we have a problem – we are at the stage where we have not yet
selected the sample, and so we don’t know the value of p̂. We will estimate
the value of p by some value p ∗ . We can either use an educated guess for
p ∗ , or we can use a conservative estimate p ∗ = 0.5, which will result in a
margin of error no greater than m, regardless of the sample proportion p̂.
Example
If we believe the value of p is relatively close to 0.5 (say, between 0.3 and
0.7), we should use p ∗ = 0.5. Otherwise, we use some educated guess.
Example
Example
Suppose we would like to take a sample large enough to estimate the true
proportion of all consumers who prefer Pepsi over Coke to within 3% with
95% confidence. We require a sample of size
∗ 2
z
n= p ∗ (1 − p ∗ )
m
1.96 2
= (0.5) (0.5)
0.03
= 1067.11 ≈ 1068
Example
Using the conservative estimate p ∗ = 0.5 does not result in a much higher
sample size than if we had used p ∗ = 0.3 or p ∗ = 0.7, for which the
required sample size would be n = 897.
However, if we suspect the sample proportion will be quite far from 0.5,
we may want to use an educated guess for p ∗ .
Example
Example
Suppose we would like to estimate the true proportion of all Canadians
who are left-handed to within 0.02 with 90% confidence. If we use
p ∗ = 0.5, we require a sample of size
∗ 2
z
n= p ∗ (1 − p ∗ )
m
1.645 2
= (0.5) (0.5)
0.02
= 1691.3 ≈ 1692
Example
Example
But we know the proportion of people who are left-handed is much lower
than 0.5. Suppose we believe the true proportion is somewhere close to
0.10. Using p ∗ = 0.10, we require a sample size
∗ 2
z
n= p ∗ (1 − p ∗ )
m
1.645 2
= (0.1) (0.9)
0.02
= 608.9 ≈ 609
Example
Practice Question
We would like to estimate the true proportion of people who use cell
phones while driving to within 0.05 with 95% confidence. What sample
size is required?
A 49
B 385
C 91
D 196
E 246
Practice Question
Example
The subject of the long gun registry has been very controversial in recent
years. We would like to conduct a hypothesis test, at the 5% level of
significance, to determine whether a majority of Canadians support the
registry. In a simple random sample of 850 Canadians, 459 (54%)
indicated their support for the registry.
Example
Solution
1 Level of significance:
Let α = 0.05.
2 Hypothesis:
H0 : Canadians are evenly split in their support for the long gun registry.
Ha : A majority of Canadians support the long gun registry.
Equivalently,
H0 : p = 0.5 vs. Ha : p > 0.5
Example
Solution
3 Decision rule:
0.54 − 0.50
Z=s = 2.33
0.50(0.50)
850
Example
Solution
5 The p-value is
Example
Suppose we had instead use the critical value method to conduct the test.
Our decision rule would be to reject H0 if Z ≥ z ∗ = 1.645.
##
## 1-sample proportions test without continuity correction
##
## data: 459 out of 850, null probability 0.5
## X-squared = 5.44, df = 1, p-value = 0.01968
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.5063896 0.5732504
## sample estimates:
## p
## 0.54
Practice Question
Example
Example
In the 2011 Canadian general election, the NDP received 30.6% of all votes
cast. The party would like to determine whether their popular support has
changed since the election, using α = 0.05. They take a simple random
sample of 500 voters, 141 of whom say they support the NDP.
Also, calculate a 95% confidence interval for the true proportion of all
voters who support the NDP.
Example
Solution
141
We calculate p̂ = 500 = 0.282. So, our 95% confidence interval is
s s
p̂ (1 − p̂) 0.282 (0.718)
p̂ ± z ∗ = 0.282 ± 1.96
n 500
= 0.282 ± 0.039 = (0.243, 0.321)
Example
Solution
1 Let α = 0.05.
H0 : The popular support for the NDP is the same as last election.
Ha : The popular support the for NDP has changed since the
last election.
Equivalently,
H0 : p = 0.306
Ha : p ̸= 0.306
Example
Solution
4 The test statistic is:
0.282 − 0.306
Z=s = −1.16
0.306(0.694)
500
5 The pvalue is
Example
Suppose we had instead used the critical value method to conduct the
test. Our decision rule would be to reject H0 if |Z | ≥ z ∗ = 1.96 (i.e., if
Z ≤ −1.96 or Z ≥ 1.96), where z ∗ = 1.96 is the upper 0.025 critical value
for the standard normal distribution.
Practice Question
A drug company manufactures antacid that is known to be successful in
providing relief for 70% of people with heartburn.The company tests a new
formula to determine whether it is better than the current one.A total of
150 people with heartburn try the new antacid and 120 report feeling
some relief. The appropriate test statistic for testing whether the new
formula is more effective than the old formula is:
0.8 − 0.7
A Z=s
0.8(0.2)
150
0.7 − 0.8
B Z=s
0.7(0.3)
150
0.8 − 0.7
C Z=s
0.7(0.3)
150
0.8 − 0.7
Carrie Madden STAT 2000 – Unit 5 41 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Power Calculations
We can also calculate the power of hypothesis tests for proportions. For
example, we conducted a test of
for the proportion of Canadians who support the long gun registry at the
5% level of significance. What would be the power of the test if the true
proportion of Canadians who supported the registry was 0.55?
Power = P(reject H0 | p = 0.55)
Example
Step 1:
Find the rejection rule in terms of p̂, assuming H0 is true:
Reject H0 if Z ≥ 1.645
p̂ − p
Z=s
p(1 − p)
n
s
0.5(0.5)
⇒ p̂ ≥ 0.50 + 1.645
850
⇒ p̂ ≥ 0.5282
Example
Step 2:
Find the probability of rejecting H0 assuming Ha is true.
p̂ − p
Z ≥ s
Power = P
p(1 − p)
n
0.5282 − 0.55
⇒P
Z ≥ s
0.55(0.45)
850
⇒ P(Z ≥ −1.28) = 1 − P(Z < −1.28) = 1 − 0.1003 = 0.8997
Example
A health organization claims that less than one quarter of all adults smoke
cigarettes. We would like to conduct a hypothesis test, at the 10% level of
significance, to verify this claim. We will select a simple random sample of
250 adults and ask them whether they smoke. What is the power of the
test if the true proportion of adults who smoke is 0.20?
We want the power of the test if H0 : p = 0.25 vs. Ha : p < 0.25 when
p = 0.20.
Example
Step 1:
Find the rejection rule in terms of p̂, assuming H0 is true:
Reject H0 if Z ≤ −1.282
p̂ − p
Z=s
p(1 − p)
n
s
0.25(0.75)
⇒ p̂ ≤ 0.25 − 1.282
250
⇒ p̂ ≤ 0.2149
Example
Step 2:
Find the probability of rejecting H0 assuming Ha is true.
p̂ − p
Z ≤ s
Power = P
p(1 − p)
n
0.2149 − 0.20
⇒P
Z ≤ s
0.20(0.80)
250
⇒ P(Z ≤ 0.59) = 0.7224
We will now turn our attention to the case where we wish to compare two
population proportions.
Let p1 be the true proportion of all individuals in Population 1 who possess
some attribute. Let p2 be the true proportion of all individuals in
Population 2 who possess the same attribute.
Confidence Intervals
(1−C )
where z ∗ is the upper 2 critical value from the standard normal
distribution.
The use of the normal distribution as an approximation is only appropriate
if n1 pˆ1 , n1 (1 − pˆ1 ), n2 pˆ2 and n2 (1 − pˆ2 ) are all greater than or equal to
ten (i.e., if there are at least ten successes and ten failures in each of the
two samples.)
Example – Legalization
Example
Do older adults and young adults have different views on legalizing
marijuana in Canada? A sample of 150 young adults (aged 18 – 30) and a
sample of 120 older adults (aged 40+) were selected. Respondents were
asked if they supported the legalization of marijuana. Of the young adults,
87 indicated their support, while 54 of the older adults supported
legalization of the drug.
Example – Legalization
Let p1 be the true population proportion of all young adults who support
the legalization of marijuana and let p2 be the true population proportion
of all older adults who support it. We calculate the sample proportions:
87 54
pˆ1 = 150 = 0.58 pˆ2 = 120 = 0.45
We check
Example – Legalization
Example – Legalization
(−0.2490, −0.0110)
Practice Question
Practice Question
A university administrator would like to estimate the true proportion of
students at the university with student loans. She takes a random sample
of 137 students at the university and calculates a 95% confidence interval
to be (0.46, 0.59). What is the correct interpretation of this confidence
interval?
A 95% of samples of 137 students will give proportions between 0.46
and 0.59.
B 95% of similarly constructed intervals will contain the sample
proportion of students at the university with loans.
C 95% of similarly constructed intervals will contain the true proportion
of students at the university with loans.
D The probability that the true proportion is between 0.46 and 0.59 is
95%.
E Between 46% and 59% of students have loans.
Carrie Madden STAT 2000 – Unit 5 57 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Hypothesis Testing
Recall;
For large sample sizes,
(pˆ1 − pˆ2 ) − (p1 − p2 )
Z=s
pˆ1 (1 − pˆ1 ) pˆ2 (1 − pˆ2 )
+
n1 n2
However, if the null hypothesis is true, p1 = p2 , and so p1 − p2 = 0, and
so under H0 ,
(pˆ1 − pˆ2 ) − 0
Z=s
pˆ1 (1 − pˆ1 ) pˆ2 (1 − pˆ2 )
+
n1 n2
Hypothesis Testing
Hypothesis Testing
(pˆ1 − pˆ2 )
Z=s
1 1
pˆc (1 − pˆc ) +
n1 n2
H0 : p1 = p2 vs. Ha : p1 > p2
(0.58 − 0.45)
=s = 2.12
1 1
0.5222(1 − 0.5222) +
150 120
5 The p-value is
Suppose we had instead used the critical value method to conduct the
test. Our decision would be to reject H0 if Z ≥ z ∗ = 1.645.
##
## 2-sample test for equality of proportions without continui
## correction
##
## data: c(87, 54) out of c(150, 120)
## X-squared = 4.5156, df = 1, p-value = 0.01679
## alternative hypothesis: greater
## 95 percent confidence interval:
## 0.03013015 1.00000000
## sample estimates:
## prop 1 prop 2
## 0.58 0.45
Example – Smoking
We can also use the two-sample test for proportions to compare two
treatments in an experiment.
Example
We would like to compare the effectiveness of two popular treatments that
are designed to help smokers quit smoking. A sample of 125 smokers who
have expressed a desire to quit smoking volunteer to participate in an
experiment. The 63 subjects in Group 1 are assigned to chew nicotine gum
and the 62 subjects in Group 2 are assigned to wear a nicotine patch. At
the end of six months, 22 of the subjects in Group 1 and 17 of the
subjects in Group 2 have quit smoking.
22 17
pˆ1 = = 0.3492 pˆ2 = = 0.2742
63 62
We check
x1 +x2 22+17 39
p̂ = n1 +n2 = 63+62 = 125 = 0.312
(0.3492 − 0.2742)
=s = 0.90
1 1
0.312(1 − 0.312) +
63 62
5 The p-value is
2PZ ≥ 0.90 = 2 (1 − P(Z < 0.90) = 2 (0.1841) = 0.3682
Since the pvalue = 0.3682>α = 0.05, we fail to reject H0 .
6 There is insufficient evidence that there is a difference in effectiveness
between nicotine gum and the nicotine patch.
Suppose we had instead used the critical value method to conduct the
test. Our decision rule would be to reject H0 if |Z | ≥ z ∗ = 1.96 (i.e., if
z ≤ −1.96 or z ≥ 1.96), where z ∗ = 1.96 is the upper 0.025 critical value
from the standard normal distribution.
We would fail to reject H0 , since −1.96 < z = 0.090 < z ∗ = 1.96.
##
## 2-sample test for equality of proportions without continui
## correction
##
## data: c(22, 17) out of c(63, 62)
## X-squared = 0.81912, df = 1, p-value = 0.3654
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## -0.08681405 0.23683966
## sample estimates:
## prop 1 prop 2
## 0.3492063 0.2741935
Practice Question
An SRS of 200 of a certain model of 2010 car found that 50 had minor
brake defects. An SRS of 100 of the same model of 2011 car found that 10
had minor brake defects. Let p1 and p2 be the true proportions of all 2010
and 2011 cars with brake defects. We wish to conduct a hypothesis test of
H0 : p1 = p2 vs. Ha : p1 ̸= p2
Practice Questions
An SRS of 200 of a certain model of 2010 car found that 50 had minor
brake defects. An SRS of 100 of the same model of 2011 car found that 10
had minor brake defects. Let p1 and p2 be the true proportions of all 2010
and 2011 cars with brake defects. We wish to conduct a hypothesis test of
H0 : p1 = p2 vs. Ha : p1 ̸= p2
Practice Question
Contingency Tables
Contingency Tables in R
That is the contingency table followed by the row totals and column totals
Contingency Tables in R
That is the contingency table followed by the row totals and column totals
colour<-matrix(c(11,8,39,14,6,15,7,16,3,10,9,4,12,2,5),
nrow = 3, ncol = 5, byrow = TRUE )
dimnames(colour) = list(c("Brown","Blue","Green"),
c("Blonde","Red", "Brown","Black","Gre
colour2<-data.frame(colour)
colour2
rowSums(colour2)
colSums(colour2)
Contingency Table
The entries in the table are referred to as observed cell frequencies and are
denoted by O.
The previous two-way table is called a 3 × 5 table, since there are three
rows and five columns (i.e., three possible eye colours and five possible
hair colours). In general, a two-way table with r rows and c columns is
called an r × c table.
Contingency tables are very useful in helping us conduct several different
tests of significance. The first use we will make of these tables is to
conduct tests of significance for the homogeneity of several populations
with respect to some variable of interest.
The results for a sample of students in each of the four classes are shown
in the two-way table below:
Section
Rating A01 A02 A03 A04
Positive 22 16 25 10
Neutral 14 21 13 14
Negative 4 10 7 19
the same proportion should rate their professor as positive for each
section,
the same proportion should rate their professor as neutral for each
section, and
the same proportion should rate their professor as negative for each
section.
How do we estimate this common proportion for each of the three rating
categories?
To help us answer this question, we examine the table again, this time
with row, column and table totals included:
Section
Rating A01 A02 A03 A04 Row Total
Positive 22 16 25 10 73
Neutral 14 21 13 14 62
Negative 4 10 7 19 40
Column Total 40 47 45 43 175
The column totals represent the sample sizes from each of the four classes
and the row totals represent the total number of positive, neutral and
negative ratings given by all students in the sample. Note that we can
obtain the table total (which in this case is 175) by adding the row totals
or the column totals.
Section
Rating A01 A02 A03 A04 Row Total
Positive 22 16 25 10 73
Neutral 14 21 13 14 62
Negative 4 10 7 19 40
Column Total 40 47 45 43 175
Let us examine the row for Positive ratings. The estimated proportion p̂
for all students who rate their professor as positive is
As such, if opinions are homogenous for all four classes, we would expect
to see 41.71% of students in each class give a positive rating.
By a similar argument, the expected count for the cell at the intersection
of the r th row and the c th column is
(row r total) (column c total)
E=
table total
The expected counts for all cells are calculated similarly and are displayed
in the following table below the observed counts (and it parentheses):
Section
Rating A01 A02 A03 A04 Row Total
22 16 25 10 73
Positive (16.69) (19.61) (18.77) (17.94)
14 21 13 14 62
Neutral (14.17) (16.65) (15.94) (15.23)
4 10 7 19 40
Negative (9.14) (10.74) (10.29) (9.83)
Column Total 40 47 45 43 175
The test statistic we will use to test for homogeneity for these four
populations measures how far off our observed counts are from our
expected counted. The test statistic is
X (O − E )2
χ2 =
all cells
E
Chi-Square Distributions
Chi-Square Distributions
Chi−Square at Various Degrees of Freedom
0.15
0.10
df
Density
df_05
df_15
df_30
0.05
0.00
0 20 40 60
Chi−square
Test Statistic
If the null hypothesis is true, then we will likely have observed cell counts
which are quite close to their expected cell counts and the value of the
test statistic
X (O − E )2
χ2 =
all cells
E
should be quite low. On the other hand, if the populations are not
homogenous, observed cell counts will differ substantially from expected
cell counts and the value of the test statistic will be high.
Example
Example
## [1] 0.1502827
Like the z procedures for comparing two proportions, the chi-square test
for homogeneity is an approximate method. The approximation becomes
more accurate as the observed counts in the cells become larger.
In practice, we can safely use the chi-square distribution in our tests for
homogeneity if:
no more than 20% of expected cell counts are less than five, and
there are no expected cell counts less than one.
In our example, none of the expected cell counts are less than five, and so
the chi-square approximation is justified. We will now conduct the formal
hypothesis test from the beginning.
1 Let α = 0.05.
2 We are testing the hypotheses
In order to compute the test statistic, we must first calculate each of the
cell chi-square values separately.
For instance, we previously calculated that the expected count for the first
row and second column (positive ratings from A02 students) is 19.61. The
observed cell count is 16, and so the cell chi-square value is
(O − E )2 (16 − 19.61)2
= = 0.66
E 19.61
Other cell chi-square values are calculated similarly and are shown in the
following table below both the observed and expected cell counts (and are
in brackets).
Section
Rating A01 A02 A03 A04 Row Total
22 16 25 10 73
Positive (16.69) (19.61) (18.77) (17.94)
[1.09] [0.66] [2.07] [3.51]
14 21 13 14 62
Neutral (14.17) (16.65) (15.94) (15.23)
[0.00] [1.14] [0.54] [0.10]
4 10 7 19 40
Negative (9.14) (10.74) (10.29) (9.83)
[2.89] [0.05] [1.05] [8.55]
Column Total 40 47 45 43 175
Solution
4 We can now add all the cell chi square values to obtain the value of
Solution
5 The p-value is
χ2 ≥ χ2∗ = 12.59
where χ2∗ = 12.59 is the upper 0.05 critical value from the chi-square
distribution with 6 degrees of freedom.
We would still reject the null hypothesis, since χ2 = 22.25 > χ2∗ = 12.59.
We can examine which cells contribute the most to the value of the test
statistic in an effort to understand why opinions were found not to be
homogeneous.
The highest cell chi-square values are the negative rating for A04, which is
higher than expected, the positive rating for A04, which is lower than
expected, and the negative rating for A01, which is lower than expected.
This tells us that students in A01 liked their instructor more than average
and students in A04 like their instructor less than average.
opinion<-matrix(c(22,16,25,10,14,21,13,14,4,10,7,19), nrow = 3
dimnames(opinion) = list(c("Positive","Neutral","Negative"), c
opinion
chisq.test(opinion)
##
## Pearson’s Chi-squared test
##
## data: opinion
## X-squared = 22.268, df = 6, p-value = 0.001083
p-value
## [1] 0.001090816
Critical Value
## [1] 12.59159
Example – Archer
Five archers shoot several arrows at a target. The table below displays the
number of times each archer hit and missed the bull’s-eye on the target:
Archer
Result Archer 1 Archer 2 Archer 3 Archer 4 Archer 5 Row Total
Hit 25 30 30 50 25 160
Missed 10 25 10 20 25 90
Column
Total 35 55 40 70 50 250
Example – Archer
Solution
1 Let α = 0.05.
Note that, since there are only two values of the explanatory variable
(hit or miss), we are actually testing the equality of five population
proportions.
H0 : p1 = p2 = p3 = p4 = p5
Ha : At least one of the population proportions differs from the others.
Example – Archer
We first calculate the expected cell counts. For example, the expected
number of hits for Archer 5 is
(row 1 total) (column 5 total) (160)(50)
E= = = 32.0
table total 250
Example – Archer
The rest of the expected cell counts are calculated similarly and are shown
in the table:
Archer
Result Archer 1 Archer 2 Archer 3 Archer 4 Archer 5 Row Total
25 30 30 50 25 160
Hit
(22.4) (35.2) (25.6) (44.8) (32.0)
10 25 10 20 25 90
Missed
(12.6) (19.8) (14.4) (25.2) (18.0)
Column
Total 35 55 40 70 50 250
Note:
None of the expected cell counts are less than five, and so the chi-square
approximation is justified.
Example – Archer
We now calculate the cell chi-square values. For example, the cell
chi-square value for the number of misses for Archer 2 is
(O − E )2 (25 − 19.8)2
= = 1.37
E 19.8
Example – Archer
Other cell chi-square values are calculated similarly and are shown below
with the expected cell counts:
Archer
Result Archer 1 Archer 2 Archer 3 Archer 4 Archer 5 Row Total
25 30 30 50 25 160
Hit (22.4) (35.2) (25.6) (44.8) (32.0)
[0.30] [0.77] [0.76] [0.60] [1.53]
10 25 10 20 25 90
Missed (12.6) (19.8) (14.4) (25.2) (18.0)
[0.56] [1.37] [1.34] [1.07] [2.72]
Column
Total 35 55 40 70 50 250
None of the expected cell counts are less than five, and so the chi-square
approximation is justified.
Carrie Madden STAT 2000 – Unit 5 119 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Example – Archer
Solution
4 The test statistic is:
X (O − E )2
χ2 =
all cells
E
= 0.30 + 0.77 + . . . + 2.72 = 11.02
Example – Archer
Solution
5 We see from Table 5 that
Since 9.49 < χ2 = 11.02 < 11.14, our p-value is between 0.025 and
0.05.
Example – Archer
Solution
Since the p-value < α = 0.05, we reject the null hypothesis.
6 We have sufficient evidence to conclude that the five archers are not
homogeneous with respect to their accuracy.
Suppose we had instead conducted the test using the critical value
approach. The decision rule would be to reject H0 if
χ2 ≥ χ2∗ = 9.49
where χ2∗ = 9.49 is the upper 0.05 critical value from the chi-square
distribution with 4 degrees of freedom.
We would still reject the null hypothesis, since χ2 = 11.02 > χ2∗ = 9.49.
Practice Question
A survey is conducted in each of four regions of Canada. Respondents are
asked whether they approve of the job being done by the prime minister.
Results are shown in the table below:
Region
Rating West Prairies Central Atlantic Row Total
Approve 94 65 61 38 258
Disapprove 28 30 89 32 179
Neutral 18 15 20 10 63
Column Total 140 110 170 80 500
What are the degrees of freedom for the appropriate test statistic?
A 5
B 6
C 8
D 9
E 11
Carrie Madden STAT 2000 – Unit 5 123 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Practice Question
A survey is conducted in each of four regions of Canada. Respondents are
asked whether they approve of the job being done by the prime minister.
Results are shown in the table below:
Rating West Prairies Central Atlantic Row Total
Approve 94 65 61 38 258
Disapprove 28 30 89 32 179
Neutral 18 15 20 10 63
Column Total 140 110 170 80 500
We say that a chi-square test can be used to test the equality of several
population proportions. In the case where we are conducting a two-sided
test comparing just two proportions, the chi-square test is in fact
equivalent to the two-sample z test. It can be shown that z 2 = χ2 and the
p-value’s of the two tests are identical.
Example – Smoking
Example – Smoking
s<-2*(1-pnorm(0.9))
s
## [1] 0.3681203
Example – Smoking
Treatment
result Gum Patch Row Total
22 17 39
Quit (19.656) (19.344)
[0.2795] [0.2840]
41 45 86
Didn’t Quit (43.344) (42.656)
[0.1268] [0.1288]
Column Total 63 62 125
Example – Smoking
χ2 = 0.2795 + . . . + 0.1288
= 0.8191
which is the same (after a rounding error) as we found using the z test
(z 2 = (0.9)2 = 0.81).
Under the null hypothesis, the test statistics follows a chi-square
distribution with (r − 1)(c − 1) = (1)(1) = 1 degree of freedom.
Example – Smoking
## [1] 0.3681203
Example – Smoking
Practice Question
According to a famous saying “money can’t buy you happiness”, but are
wealth and happiness really independent?
A psychological study was conducted, in which subjects were analyzed and
categorized as either very happy, somewhat happy or unhappy. The income
levels of subjects were also examined, and each subject was categorized as
either low income, middle class, or wealthy. Let α = 0.10.
Income Levels
Happiness Low Income Middle Class Wealthy Row Total
Very Happy 7 16 13 36
Somewhat Happy 10 20 11 41
Unhappy 5 7 5 17
Column Total 22 43 29 94
For example, if wealth and happiness were independent, what would be the
expected number of individuals in the sample who are unhappy and
wealthy?
Recall:
If two events are independent then:
P(AandB) = P(A)P(B)
P(UandW ) = P(U)P(W )
By a similar line of reasoning, the expected frequency for the cell at the
intersection of the r th row and the c th column is
T
he formula for the expected cell count is the same as it was for the test
of homogeneity, but for different reasons! The two test are in fact
identical.
Other expected cell counts are calculated similarly and are shown in the
table below:
Income Levels
Happiness Low Income Middle Class Wealthy Row Total
7 16 13 36
Very Happy
(8.43) (16.47) (11.11)
10 20 11 41
Somewhat Happy
(9.60) (18.76) (12.65)
5 7 5 17
Unhappy
(3.98) (7.78) (5.24)
Column Total 22 43 29 94
One of the nine cells (11% < 20%) has an expected count less than five,
and all expected cell counts are greater than one, so the use of the
chi-square approximation is justified.
We will now calculate the cell chi-square values. For example, the cell
chi-square value for somewhat happy and wealthy individuals is
(O − E )2 (11 − 12.65)2
= = 0.22
E 12.65
Other cell chi-square values are calculated similarly and are shown in the
table below:
Income Levels
Happiness Low Income Middle Class Wealthy Row Total
7 16 13 36
Very Happy (8.43) (16.47) (11.11)
[0.24] [0.01] [0.32]
10 20 11 41
Somewhat Happy (9.60) (18.76) (12.65)
[0.02] [0.08] [0.22]
5 7 5 17
Unhappy (3.98) (7.78) (5.24)
[0.26] [0.08] [0.01]
Column Total 22 43 29 94
Test Statistic
X (O − E )2
χ2 =
all cells
E
= 0.24 + 0.01 + . . . + 0.01 = 1.24.
P-Value
Example
Since the p-value>α = 0.10, we fail to reject the null hypothesis at the
10% level of significance. We have insufficient evidence to conclude that
wealth and happiness are dependent (i.e., independence appears to be a
reasonable assumption).
Suppose we had instead conducted the test using the critical value
approach. The decision rule would be to reject H0 if
χ2 ≥ χ2∗ = 7.78
where χ2∗ = 7.78 is the upper 0.10 critical value from the chi-square
distribution with 4 degrees of freedom. We would still fail to reject the
null hypothesis, since χ2 = 1.24 < χ2∗ = 7.78.
Example
The individuals in the previous example were also asked which political
party they support. We would like to conduct a hypothesis test, at the 1%
level of significance, to determine whether wealth and political preference
are independent. The data are displayed in the table below:
Income Levels
Political Party Low Income Middle Class Wealthy Row Total
Conservative 2 14 20 36
Liberal 6 10 6 22
NDP 11 12 2 25
Green 3 7 1 11
Column Total 22 43 29 94
\end{frame}
Solution
1 Let α = 0.01.
Solution
We will first calculate the expected cell counts. For example, the expected
number of middle class individuals in the sample who support the NDP is
Other expected cell counts are calculated similarly and are shown in the
table on the following page.
Solution
Income Levels
Political Party Low Income Middle Class Wealthy Row Total
2 14 20 36
Conservative
(8.43) (16.47) (11.11)
6 10 6 22
Liberal
(5.15) (10.06) (6.79)
11 12 2 25
NDP
(5.85) (11.44) (7.71)
3 7 1 11
Green
(2.57) (5.03) (3.39)
Column Total 22 43 29 94
Note:
Two of the 12 cells (17% < 20%) have expected counts less than five, and
all expected cell counts are greater than one, so the use of the chi-square
approximation is justified.
Solution
We will now calculate the cell chi-square values. For example, the cell
chi-square value for wealthy individuals who support the Liberal Party is
(O − E )2 (6 − 6.79)2
χ2 = = = 0.09
E 6.79
Other cell chi-square values are calculated similarly and are shown in the
table on the following page.
Solution
4 The test statistic is:
X (O − E )2
χ2 =
all cells
E
= 4.90 + 0.37 + . . . + 1.68 = 23.92
Solution
Since the p-value < α = 0.01, we reject the null hypothesis.
6 We have sufficient evidence to conclude that wealth and political
preference are dependent.
Suppose we had instead conducted the test using the critical value
approach. The decision rule would be to reject H0 if
χ2 ≥ χ2∗ = 16.81
where χ2∗ = 16.81 is the upper 0.01 critical value from the chi-square
distribution with 6 degrees of freedom. We would still reject the null
hypothesis, since χ2 = 23.92 > χ2∗ = 16.81
We see that the four cells that contribute the most to the test statistics
are for low income and wealthy individuals who support the Conservative
Party and the NDP.
It appears that poorer individuals tend to support the NDP while wealthier
voters are more likely to support the Conservative Party.
Practice Question
In a survey, 400 people were classified with respect to stress level and the
occurrence of migraine headaches. The data are shown in the table below:
Stress Level
Migraine Low High
Never 110 43 153
Occasional 114 80 194
Often 26 27 53
250 150 400
We would like to test whether the two variables are independent at the 5%
level of significance. What is the critical value of the test?
A 4.61
B 5.99
C 7.81
D 9.49
E 11.07
Carrie Madden STAT 2000 – Unit 5 158 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Practice Question
In a survey, 400 people were classified with respect to stress level and the
occurrence of migraine headaches. The data are shown in the table below:
Stress Level
Migraine Low High
Never 110 43 153
Occasional 114 80 194
Often 26 27 53
250 150 400
Practice Question
In a survey, 400 people were classified with respect to stress level and the
occurrence of migraine headaches. The data are shown in the table below:
Stress Level
Migraine Low High
110 43 153
Never
95.625 57.375
114 80 194
Occasional
121.25 72.75
26 27 53
Often
33.125 19.875
250 150 400
What is the cell chi-square value for low stress individuals who never
experience migraines?
A 1.97 (B) 2.04 (C) 2.16 (D) 1.78 (E) 1.89
Practice Questions
Goodness-of-fit
The parameters of any discrete uniform distribution are the minimum and
maximum values of X (in our case, 0 and 9, respectively), which enable us
to calculate probabilities of occurrence for any value of X .
We will now conduct the chi-square goodness of fit test to investigate
whether the player’s claim has any merit.
Let α = 0.05.
We are testing the hypotheses
Data were tabulated for all 1000 Pick 3 draws from November 1, 2008 to
July 30, 2011. The number of times each digit was drawn is shown in the
contingency table below.
Digit 0 1 2 3 4 5 6 7 8 9
Freq. 314 285 302 307 294 277 284 321 318 298
Note that in 1000 draws, a total of 3(1000) = 3000 digits were drawn.
300
250
200
150
100
50
0
0 2 4 6 8 10
The expected frequency of each of these digits under the null hypothesis is
Note:
The use of the chi-square distribution for goodness of fit tests is
approximate, and can safely be used when all cell counts are at least
five.
We now calculate the cell chi-square values for each of the digits. For
example, the cell chi-square value for the digit 8 is
(O − E )2 (318 − 300)2
= = 1.08
E 300
Other cell chi-square values are calculated similarly and are displayed under
the observed and expected counts in the table on the next page.
Digit 0 1 2 3 4 5 6 7 8 9
Freq. 314 285 302 307 294 277 284 321 318 298
300 300 300 300 300 300 300 300 300 300
0.65 0.75 0.02 0.16 0.12 1.76 0.85 1.47 1.08 0.01
(# of cells − 1) = 10 − 1 = 9
The degrees of freedom for goodness of fit tests are only (# of cells − 1) if
we know the values of all necessary parameters. In the above example, we
knew the maximum and minimum values, so all parameter values are
known. When we don’t know the value of a parameter, we must estimate
it and deduct one additional degree of freedom.
χ2 ≥ χ2∗ = 16.92
where χ2∗ = 16.92 is the upper 0.01 critical value from the chi-square
distribution with 9 degrees of freedom.
We would still fail to reject the null hypothesis since
Example – Optometrist
Example – Optometrist
1 Let α = 0.05
2 H0 The Optometrists proposed distribution is correct vs. Ha : The
distribution is something other than the proposition.
3 Decision Rule: Reject H0 if p-value is ≤ 0.05
Example – Optometrist
4 Test Statistic:
Eye Colour Brown Blue Green Other
Observed 66 23 35 26
Expected (150*0.5=75) 15 30 30
Cell Chi-Square 1.08 4.27 0.833 0.533
Therefore χ2 = 6.717
Example – Optometrist
5 p-value:
p-value: Follow along #cells − 1 = 3df. We see our test statistic falls
between 6.25 and 7.82 which corresponds to a p-value between 0.05
and 0.10.
## [1] 0.08152236
Example – Optometrist
6 Conclusion :
Since our p-value is > 0.05, we fail to reject H0 , there is insufficent
evidence to support the alternative, hence the Optometrist’s
proposition is plausible.
Example – Optometrist
Supposed we used the critical value method, the decision rule would be:
Reject H0 if χ2 ≤ χ2∗
3,0.05 = 7.82. Since our test statistic is less than 7.82
we have the same conclusion.
qchisq(0.05,3,lower.tail = FALSE)
## [1] 7.814728
Practice Question
A website claims that the distribution of blood types for a certain group of
people is as follows:
Blood Type A B AB O
Probability 0.4 0.2 0.1 0.3
In a random sample of 200 people from this group, 70 had blood type A,
20 had blood type B, 30 had blood type AB and 80 had blood type O.
We would like to conduct a chi-square goodness of fit test at the 1% level
of significance to verify the website’s claim. What is the critical value of
the test?
A 7.82
B 9.49
C 13.28
D 11.35
E 6.63
Carrie Madden STAT 2000 – Unit 5 184 / 185
Unit 5 – Test for Proportions and Analysis of Categorical Data
and Goodness-of-fit Tests
Practice Question
Under the null hypothesis (that the distribution of blood types is the one
claimed by the website), the expected number of people in the sample
with blood type A is 200(0.4) = 80. Other expected counts are calculated
similarly and are shown below:
Blood Type A B AB O
Count 70 20 30 80
Expected 80 40 20 60