##One-proportion z-Test: #Compute one proportion z-test in R
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
# 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
##Two-proportions z-Test #Compute two-proportions z-test in R:
res <- prop.test(x = c(490, 400), n = c(500, 500))
# Printing the results
res
##
## 2-sample test for equality of proportions with continuity correction
##
## data: c(490, 400) out of c(500, 500)
## X-squared = 80.909, df = 1, p-value < 2.2e-16
## alternative hypothesis: two.sided
## 95 percent confidence interval:
## 0.1408536 0.2191464
## sample estimates:
## prop 1 prop 2
## 0.98 0.80
# printing the p-value
res$p.value
## [1] 2.363439e-19
# printing the mean
res$estimate
## prop 1 prop 2
## 0.98 0.80
# printing the confidence interval
res$conf.int
## [1] 0.1408536 0.2191464
## attr(,"conf.level")
## [1] 0.95
##Chi-square goodness of fit test in R:
tulip <- c(81, 50, 27)
res <- chisq.test(tulip, p = c(1/3, 1/3, 1/3))
res
##
## Chi-squared test for given probabilities
##
## data: tulip
## X-squared = 27.886, df = 2, p-value = 8.803e-07
# Access to the expected values
res$expected
## [1] 52.66667 52.66667 52.66667
#comparing observed to expected proportions
tulip <- c(81, 50, 27)
res <- chisq.test(tulip, p = c(1/2, 1/3, 1/6))
res
##
## Chi-squared test for given probabilities
##
## data: tulip
## X-squared = 0.20253, df = 2, p-value = 0.9037
# printing the p-value
res$p.value
## [1] 0.9036928
# printing the mean
res$estimate
## NULL