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 have female (p = 0.5 = 50%). Some of these mice (n = 160) have developed a spontaneous cancer, including 95 male and 65 female.
We want to know, whether the cancer affects more male than female?
The R functions binom.test() and prop.test() can be used to perform one-proportion test:
binom.test(): compute exact binomial test. Recommended when sample size is small
prop.test(): can be used when sample size is large ( N > 30). It uses a normal approximation to binomial
The syntax of the two functions are exactly the same. The simplified format is as follow:
binom.test(x, n, p = 0.5, alternative = “two.sided”) prop.test(x, n, p = NULL, alternative = “two.sided”, correct = TRUE)
x: the number of of successes
n: the total number of trials
p: the probability to test against.
correct: a logical indicating whether Yates’ continuity correction should be applied where possible
Note that, by default, the function prop.test() used the Yates continuity correction, which is really important if either the expected successes or failures is < 5. If you don’t want the correction, use the additional argument correct = FALSE in prop.test() function. The default value is TRUE. (This option must be set to FALSE to make the test mathematically equivalent to the uncorrected z-test of a proportion.)
We want to know, whether the cancer affects more male than female?
We’ll use the function prop.test()
res <- prop.test(x = 95, n = 160, p = 0.5,
correct = FALSE)
# Printing the results
res
##
## 1-sample proportions test without continuity correction
##
## data: 95 out of 160, null probability 0.5
## X-squared = 5.625, df = 1, p-value = 0.01771
## alternative hypothesis: true p is not equal to 0.5
## 95 percent confidence interval:
## 0.5163169 0.6667870
## sample estimates:
## p
## 0.59375
The function returns:
the value of Pearson’s chi-squared test statistic.
a p-value
a 95% confidence intervals
an estimated probability of success (the proportion of male with cancer)
Note that:
if you want to test whether the proportion of male with cancer is less than 0.5 (one-tailed test), type this:
prop.test(x = 95, n = 160, p = 0.5, correct = FALSE, alternative = “less”)
Or, if you want to test whether the proportion of male with cancer is greater than 0.5 (one-tailed test), type this:
prop.test(x = 95, n = 160, p = 0.5, correct = FALSE, alternative = “greater”)
The p-value of the test is 0.01771, which is less than the significance level alpha = 0.05. We can conclude that the proportion of male with cancer is significantly different from 0.5 with a p-value = 0.01771.
The result of prop.test() function is a list containing the following components:
statistic: the number of successes
parameter: the number of trials
p.value: the p-value of the test
conf.int: a confidence interval for the probability of success. estimate: the estimated probability of success.
# printing the p-value
res$p.value
## [1] 0.01770607
# printing the mean
res$estimate
## p
## 0.59375
# printing the confidence interval
res$conf.int
## [1] 0.5163169 0.6667870
## attr(,"conf.level")
## [1] 0.95