Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

R Lab7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 15

Lab 8

Two sample Z-test


Aim:
To test the hypothesis for large
samples by using Two sample Z-test
Test for difference in population
proportion
Problem 1: A popular cold-remedy was tested
for it's efficacy. In a sample of 150 people who
took the remedy upon getting a cold, 117
(78%) had no symptoms one week later. In a
sample of 120 people who took the placebo
upon getting a cold, 90 (75%) had no
symptoms one week later. The table
summarizes this information.
Problem 1: A popular cold-remedy was tested
for it's efficacy. In a sample of 150 people who
took the remedy upon getting a cold, 117
(78%) had no symptoms one week later. In a
sample of 120 people who took the placebo
upon getting a cold, 90 (75%) had no
symptoms one week later. The table
summarizes this information.
Group # who are symptom Total # in group (n) Proportions (x/n)
free after one week
(x)

Remedy 117 150 0.78


Placebo 90 120 0.75
Test the claim that the proportion of all
remedy users who are symptom-free after one
week is greater than the proportion for
placebo users. Test this claim at the 0.05
significance level.
Solution
Null hypothesis: P1=P2;
Alternative Hypothesis: P1>P2
R code
• p1=0.78
• p2=0.75
• n1=150
• n2=120
• alpha=0.05
• P=(n1*p1+n2*p2)/(n1+n2)
• Q=1-P
• SE=sqrt((P*Q/n1)+(P*Q/n2))
• zcal=(p1-p2)/SE
• zcal
[1] 0.5791405
• ztab=qnorm(1-alpha)
• ztab
[1] 1.644854
• if(zcal < ztab) {print("Accept H0")} else {print("Reject H0")}
[1] "Accept H0"
Interpretation
• We accept the null hypothesis because the
zcal value is lesser than the ztab value.
Therefore, we can’t support the claim that the
proportion of all remedy users who are
symptom-free after one week is greater than
the proportion for placebo users.
Problem 2
A survey is taken two times over the course of
two weeks. The pollsters wish to see if there is
a difference in the results as there has been a
new advertising campaign run. Test at 1% level
of significance. Also find 99% confidence limit
for the difference of proportion. Here is the
data
Week 1 Week 2
Favorable 45 56
Unfavorable 35 47
Solution
• H0: P1 = P2 against the alternative (two-sided) H1: P1 ≠ P2.
R code
n1=45+35
n2=56+47
x1=45
x2=56
p1=x1/n1
p2=x2/n2
P=(n1*p1+n2*p2)/(n1+n2)
Q=1-P
R code
SE=sqrt((P*Q/n1)+(P*Q/n2))
zcal=abs(p1-p2)/SE
zcal
[1] 0.2538201
alpha=0.01
ztab=qnorm(1-(alpha/2))
ztab
[1] 2.575829
if(zcal < ztab) {print("Accept H0")} else {print("Reject H0")}
[1] "Accept H0“
lowerlimit=(p1-p2)-ztab*SE
upperlimit=(p1-p2)+ztab*SE
CI_for_99percent=c(lowerlimit,upperlimit)
CI_for_99percent
[1] -0.1720848 0.2097061
Interpretation

We accept the null hypothesis and conclude that


there is no difference in the results as there
has been a new advertising campaign run.
Test for difference in means
The following data shows the heights of
individuals of two different countries with the
population variance of 5 and 8.5 respectively.
Is there any significant difference between the
average heights of two groups.
A 175 168 168 190 156 181 182 175 174 179

B 185 169 173 173 188 186 175 174 179 180
R code
• z.test2sam = function(a, b, var.a, var.b)
• {
• n.a = length(a)
• n.b = length(b)
• zeta = (mean(a) - mean(b)) / (sqrt(var.a/n.a + var.b/n.b))
• return(zeta)
• }
• a = c(175, 168, 168, 190, 156, 181, 182, 175, 174, 179)
• b = c(185, 169, 173, 173, 188, 186, 175, 174, 179, 180)
• zcal=abs(z.test2sam(a, b, 5, 8.5))
• alpha=0.05
• ztab=qnorm(1-alpha/2)
• if(zcal < ztab) {print("Accept H0")} else {print("Reject H0")}
Inference
The value of z is greater than the critical value
of z tabulated for alpha equal to 0.05 (z-
tabulated = 1.96 for a two-tailed test): we
reject the null hypothesis and conclude that
the two means are significantly different.
Assignment
• The Trial Urban District Assessment (TUDA) is a study
sponsored by the government of student achievement in
large urban school district. In 2009, 1311 of a random
sample of 1900 eighth-graders from Houston performed at
or above the basic level in mathematics . In 2011, 1440 of a
random sample of 2000 eighth-graders from Houston
performed at or above the basic level . (The study reports
the proportions).
(A ) Is there an increase in the proportion of eighth-graders
who performed at or above the basic level in mathematics
from 2009 to 2011 at the 5% significance level?
(B) Compute the 95% confidence interval for the difference in
proportion of eighth- graders who performed at or above
the basic level in mathematics from 2009 to 2011.

You might also like