One-Proportion Z-Test in R Programming

22 Jul, 2020
The One proportion Z-test is used to compare an observed proportion to a theoretical one when there are only two categories. For example, we have a population of mice containing half male and half females (p = 0.5 = 50%). Some of these mice (n = 160) have developed spontaneous cancer, including 95 males and 65 females. We want to know, whether cancer affects more males than females? So in this problem:
The number of successes (male with cancer) is 95
The observed proportion (po) of the male is 95/160
The observed proportion (q) of the female is 1 – po
The expected proportion (pe) of the male is 0.5 (50%)
The number of observations (n) is 160
The test statistic (also known as z-test) can be calculated as follow:
where,
po: the observed proportionq: 1 – pope: the expected proportionn: the sample size
In R Language, the function used for performing a z-test is binom.test() and prop.test().
Syntax:binom.test(x, n, p = 0.5, alternative = “two.sided”)prop.test(x, n, p = NULL, alternative = “two.sided”, correct = TRUE)
Parameters:x = number of successes and failures in data set.n = size of data set.p = probabilities of success. It must be in the range of 0 to 1.alternative = a character string specifying the alternative hypothesis.correct = a logical indicating whether Yates’ continuity correction should be applied where possible.
Example 1:Let’s assume that 30 out of 70 people recommend Street Food to their friends. To test this claim, a random sample of 150 people obtained. Of these 150 people, 80 indicate that they recommend Street Food to their friend. Is this claim accurate? Use alpha = 0.05.
Solution:Now given x=.80, P=.30, n=150. We want to know, whether the People recommend street food to their friend than healthy food? Let’s use the function prop.test() in R.
# Using prop.test()prop.test(x = 80, n = 150, p = 0.3,                    correct = FALSE)
Output:
        1-sample proportions test without continuity correction
data:  80 out of 150, null probability 0.3
X-squared = 38.889, df = 1, p-value = 4.489e-10
alternative hypothesis: true p is not equal to 0.3
95 percent confidence interval:
0.4536625 0.6113395
sample estimates:
       p  
0.5333333

It returns p-value which is 4.186269
alternative hypothesis.
a 95% confidence intervals.
probability of success is 0.53
The p-value of the test is 4.486269, which is less than the significance level alpha = 0.05. The claim that 30 out of 70 People recommend Street Food to their friends is not accurate.
Example 2:Suppose that current vitamin pills cure 80% of all cases. A new vitamin pill has been discovered or made. In a sample of 150 patients with the lack of vitamins that were treated with the new vitamins, 95 were cured. Do the results of this study support the claim that the new vitamins have a higher cure rate than the existing vitamins?
Solution:Now given x=.95, P=.80, n=160. Let’s use the function prop.test() in R.
# Using prop.test() prop.test(x = 95, n = 160, p = 0.8,                   correct = FALSE)
Output:
 
        1-sample proportions test without continuity correction
data:  95 out of 160, null probability 0.8
X-squared = 42.539, df = 1, p-value = 6 . 928e-11
alternative hypothesis: true p is not equal to 0.8
95 percent confidence interval:
0.5163169 0.6667870
sample estimates:
     p  
0.59375

It returns p-value which is 6.928462
alternative hypothesis
a 95% confidence intervals.
probability of success is 0.59
The p-value of the test is 6.928462, which is greater than the significance level alpha = 0.05. The claim that 95 out of 160 people cured with new vitamins are accurate.
Example 3:Suppose that in India 15% of people like Work from home. In a sample of 100 people, the people who like Work from home are 25. Is this claim accurate?
Solution:Now given x=.25, P=.15, n=100. Let’s use the function binom.test() in R.
# Using binom.test()binom.test(x =25, n = 100, p = 0.15)
Output:
Exact binomial test

data:  25 and 100
number of successes = 25, number of trials = 100, p-value = 0.007633
alternative hypothesis: true probability of success is not equal to 0.15
95 percent confidence interval:
 0.1687797 0.3465525
sample estimates:
probability of success 
                  0.25 

It returns p-value which is 0.007633
alternative hypothesis.
a 95% confidence intervals.
probability of success is 0.25
The p-value of the test is 0.007633 which is less than the significance level alpha = 0.05. The claim that 25 out of 100 people like work form home is not accurate.
data-science
Picked
R Machine-Learning
R Language
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.