The mosaic prop.test
provides wrapper functions around the function of the same name in stats.
These wrappers provide an extended interface (including formulas).
prop.test
performs an approximate test of a simple null hypothesis about the
probability of success in a Bernoulli or multinomial experiment
from summarized data or from raw data.
prop.test(
x,
n,
p = NULL,
alternative = c("two.sided", "less", "greater"),
conf.level = 0.95,
data = NULL,
success = NULL,
...
)
count of successes, length 2 vector of success and failure counts, a formula, or a character, numeric, or factor vector containing raw data.
sample size (successes + failures) or a data frame (for the formula interface)
a vector of probabilities of success. The length of p must be the same as the number of groups specified by x, and its elements must be greater than 0 and less than 1.
character string specifying the alternative hypothesis, must be one of
"two.sided"
(default), "greater"
or "less"
. You can specify just the initial letter.
Only used for testing the null that a single proportion equals a given value, or that two proportions
are equal; ignored otherwise.
confidence level of the returned confidence interval. Must be a single number between 0 and 1. Only used when testing the null that a single proportion equals a given value, or that two proportions are equal; ignored otherwise.
a data frame (if missing, n
may be a data frame)
level of variable to be considered success. All other levels are considered failure.
additional arguments (often ignored).
When x
is a formula, groups
can be used to compare groups:
x = ~ var, groups=g
is equivalent to x = var ~ g
. na.rm
can be a logical
or an integer vector of length 1 or 2 to indicate dimension along which NA's are
removed before coputing the test.
See the examples.
an htest
object
This is a wrapper around prop.test()
to simplify its use
when the raw data are available, in which case
an extended syntax for prop.test
is provided.
When x
is a 0-1 vector, 0 is treated as failure and 1 as success. Similarly,
for a logical vector TRUE
is treated as success and FALSE
as failure.
# Several ways to get a confidence interval for the proportion of Old Faithful
# eruptions lasting more than 3 minutes.
prop.test( faithful$eruptions > 3 )
#>
#> 1-sample proportions test with continuity correction
#>
#> data: > [with success = TRUE]faithful$eruptions [with success = TRUE]3 [with success = TRUE]
#> X-squared = 21.798, df = 1, p-value = 3.029e-06
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.5829473 0.6996958
#> sample estimates:
#> p
#> 0.6433824
#>
prop.test(97,272)
#>
#> 1-sample proportions test with continuity correction
#>
#> data: 97 out of 272
#> X-squared = 21.798, df = 1, p-value = 3.029e-06
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.3003042 0.4170527
#> sample estimates:
#> p
#> 0.3566176
#>
faithful$long <- faithful$eruptions > 3
prop.test( faithful$long )
#>
#> 1-sample proportions test with continuity correction
#>
#> data: $ [with success = TRUE]faithful [with success = TRUE]long [with success = TRUE]
#> X-squared = 21.798, df = 1, p-value = 3.029e-06
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.5829473 0.6996958
#> sample estimates:
#> p
#> 0.6433824
#>
prop.test( ~long , data = faithful )
#>
#> 1-sample proportions test with continuity correction
#>
#> data: faithful$long [with success = TRUE]
#> X-squared = 21.798, df = 1, p-value = 3.029e-06
#> alternative hypothesis: true p is not equal to 0.5
#> 95 percent confidence interval:
#> 0.5829473 0.6996958
#> sample estimates:
#> p
#> 0.6433824
#>
prop.test( homeless ~ sex, data = HELPrct )
#>
#> 2-sample test for equality of proportions with continuity correction
#>
#> data: tally(homeless ~ sex)
#> X-squared = 3.8708, df = 1, p-value = 0.04913
#> alternative hypothesis: two.sided
#> 95 percent confidence interval:
#> -0.226451636 -0.002763425
#> sample estimates:
#> prop 1 prop 2
#> 0.3738318 0.4884393
#>
prop.test( ~ homeless | sex, data = HELPrct )
#>
#> 2-sample test for equality of proportions with continuity correction
#>
#> data: tally(homeless ~ sex)
#> X-squared = 3.8708, df = 1, p-value = 0.04913
#> alternative hypothesis: two.sided
#> 95 percent confidence interval:
#> -0.226451636 -0.002763425
#> sample estimates:
#> prop 1 prop 2
#> 0.3738318 0.4884393
#>
prop.test( ~ homeless, groups = sex, data = HELPrct )
#>
#> 2-sample test for equality of proportions with continuity correction
#>
#> data: tally(homeless ~ sex)
#> X-squared = 3.8708, df = 1, p-value = 0.04913
#> alternative hypothesis: two.sided
#> 95 percent confidence interval:
#> -0.226451636 -0.002763425
#> sample estimates:
#> prop 1 prop 2
#> 0.3738318 0.4884393
#>
prop.test(anysub ~ link, data = HELPrct, na.rm = TRUE)
#>
#> 2-sample test for equality of proportions with continuity correction
#>
#> data: tally(anysub ~ link)
#> X-squared = 9.2749, df = 1, p-value = 0.002323
#> alternative hypothesis: two.sided
#> 95 percent confidence interval:
#> -0.29428286 -0.05895097
#> sample estimates:
#> prop 1 prop 2
#> 0.1567164 0.3333333
#>
prop.test(link ~ anysub, data = HELPrct, na.rm = 1)
#> Warning: NA is being treated as a category for anysub
#>
#> 3-sample test for equality of proportions without continuity correction
#>
#> data: tally(link ~ anysub)
#> X-squared = 19.25, df = 2, p-value = 6.607e-05
#> alternative hypothesis: two.sided
#> sample estimates:
#> prop 1 prop 2 prop 3
#> 0.3750000 0.6174863 0.6979167
#>
prop.test(link ~ anysub, data = HELPrct, na.rm = TRUE)
#>
#> 2-sample test for equality of proportions with continuity correction
#>
#> data: tally(link ~ anysub)
#> X-squared = 9.2749, df = 1, p-value = 0.002323
#> alternative hypothesis: two.sided
#> 95 percent confidence interval:
#> -0.3991840 -0.0857887
#> sample estimates:
#> prop 1 prop 2
#> 0.3750000 0.6174863
#>