exact2x2 {exact2x2} | R Documentation |
Performs exact conditional tests for two by two tables. For independent binary responses, performs
either Fisher's exact test or Blaker's exact test for testing hypotheses about the odds ratio.
The commands follow the style of fisher.test
, the difference is that
for two-sided tests there are three methods for calculating the exact test, and for each of the three methods
its matching
confidence interval is returned (see details).
For paired binary data resulting in a two by two table, performs an exact McNemar's test.
exact2x2(x, y = NULL, or = 1, alternative = "two.sided", tsmethod = NULL, conf.int = TRUE, conf.level = 0.95, tol = 0.00001, conditional = TRUE, paired=FALSE) fisher.exact(x, y = NULL, or = 1, alternative = "two.sided", tsmethod = "minlike", conf.int = TRUE, conf.level = 0.95, tol = 0.00001) blaker.exact(x, y = NULL, or = 1, alternative = "two.sided", conf.int = TRUE, conf.level = 0.95, tol = 0.00001) mcnemar.exact(x,y=NULL, conf.level=.95)
x |
either a two-dimensional contingency table in matrix form, or a factor object. |
y |
a factor object; ignored if x is a matrix. |
or |
the hypothesized odds ratio. Must be a single numeric. |
alternative |
indicates the alternative hypothesis and must be
one of "two.sided" , "greater" or "less" .
if "two.sided" uses method defined by tsmethod. |
tsmethod |
one of "minlike","central", or "blaker". NULL defaults to "minlike" when paired=FALSE and "central" when paired=TRUE. Defines type of two-sided method (see details). Ignored if alternative="less" or "greater". |
conf.int |
logical indicating if a confidence interval should be computed. |
conf.level |
confidence level for the returned confidence
interval. Only used if
conf.int = TRUE . |
tol |
tolerance for confidence interval estimation. |
conditional |
TRUE. Unconditional exact tests not supported at this time. |
paired |
logical. TRUE gives exact McNemar's test, FALSE are all other tests |
The motivation for this package is to match the different two-sided conditional exact tests for 2x2 tables with the appropriate confidence intervals.
There are three ways to calculate the two-sided conditional exact tests,
motivated by three different ways to define the p-value.
The usual two-sided Fisher's exact test defines the p-value as the sum of probability
of tables with
smaller likelihood than the observed table (tsmethod
="minlike").
The central Fisher's exact test defines the p-value as twice the one-sided p-values
(but with a maximum p-value of 1). Blaker's (2000) exact test defines the p-value
as the sum of the tail probibility in the observed tail plus the largest tail probability
in the opposite tail that is not greater than the observed tail probability.
In fisher.test
the p-value uses the two-sample method
associated with tsmethod
="minlike", but the confidence interval method
associated with tsmethod
="central". The probability that the
lower central confidence limit is less than the true odds ratio is bounded by
1-(1-conf.level)/2
for the central intervals, but not for the other two two-sided
methods.
The confidence intervals in for exact2x2
match the test associated
with alternative. In other words, the confidence interval is the smallest interval that contains the confidence set that is
the inversion of the associated test (see Fay, 2009).
The functions fisher.exact
and blaker.exact
are just wrappers for certain
options in exact2x2
.
If x
is a matrix, it is taken as a two-dimensional contingency
table, and hence its entries should be nonnegative integers.
Otherwise, both x
and y
must be vectors of the same
length. Incomplete cases are removed, the vectors are coerced into
factor objects, and the contingency table is computed from these.
P-values are obtained directly using the (central or non-central) hypergeometric distribution.
The null of conditional
independence is equivalent to the hypothesis that the odds ratio
equals one. ‘Exact’ inference can be based on observing that in
general, given all marginal totals fixed, the first element of the
contingency table has a non-central hypergeometric distribution with
non-centrality parameter given by the odds ratio (Fisher, 1935). The
alternative for a one-sided test is based on the odds ratio, so
alternative = "greater"
is a test of the odds ratio being bigger
than or
.
When paired=TRUE, this denotes there is some pairing of the data. For example,
instead of Group A and Group B, we may have pretest and posttest binary responses.
The proper two-sided test for such a setup is McNemar's Test, which only uses the off-diagonal
elements of the 2x2 table, and tests that both are equal or not. The exact version
is based on the binomial distribution on one of the off-diagonal values conditioned on the total
of both off-diagonal values. We use binom.exact
from the exactci
package, and convert the
p estimates and confidence intervals (see note) to odds ratios (see Breslow and Day, 1980, p. 165). The function
mcnemar.exact
is just a wrapper to call exact2x2
with paired=TRUE, alternative="two.sided",tsmethod="central"
.
One-sided exact McNemar-type tests may be calculated using the exact2x2
function with paired=TRUE
.
A list with class "htest"
containing the following components:
p.value |
the p-value of the test |
conf.int |
a confidence interval for the odds ratio |
estimate |
an estimate of the odds ratio. Note that the conditional Maximum Likelihood Estimate (MLE) rather than the unconditional MLE (the sample odds ratio) is used. |
null.value |
the odds ratio under the null, or . |
alternative |
a character string describing the alternative hypothesis |
method |
a character string, changes depending on alternative and tsmethod |
data.name |
a character string giving the names of the data |
The default exact confidence intervals for the odds ratio when paired=TRUE (those matching the exact McNemar's test)
are transformations of the Clopper-Pearson exact confidence intervals for a single binomial parameter which are central intervals.
See note for binom.exact
for discussion of exact binomial confidence intervals.
Michael Fay
Blaker, H. (2000) Confidence curves and improved exact confidence intervals for discrete distributions. Canadian Journal of Statistics 28: 783-798.
Breslow, NE and Day NE (1980). Staistical Methods in Cancer Research: Vol 1-The analysis of Case-Control Studies. IARC Scientific Publications. IARC, Lyon.
Fay, M. P. (2009). Confidence intervals that Match Fisher's exact and Blaker's exact tests. Biostatistics, doi:10.1093/biostatistics/kxp050 (go to doc directory for earlier version or http://www3.niaid.nih.gov/about/organization/dcr/BRB/staff/michael.htm for link to official version).
Fisher, R.A. (1935) The logic of inductive inference. Journal of the Royal Statistical Society Series A 98:39-54.
## In example 1, notice how fisher.test rejects the null at the 5 percent level, ## but the 95 percent confidence interval on the odds ratio contains 1 ## The intervals do not match the p-value. ## In fisher.exact you get p-values and the matching confidence intervals example1<-matrix(c(6,12,12,5),2,2,dimnames=list(c("Group A","Group B"),c("Event","No Event"))) example1 fisher.test(example1) fisher.exact(example1,tsmethod="minlike") fisher.exact(example1,tsmethod="central") blaker.exact(example1) ## In example 2, this same thing happens, for tsmethod="minlike"... this cannot be avoided because of the ## holes in the confidence set. ## example2<-matrix(c(7,255,30,464),2,2,dimnames=list(c("Group A","Group B"),c("Event","No Event"))) example2 fisher.test(example2) exact2x2(example2,tsmethod="minlike") ## you can never get a test-CI inconsistency when tsmethod="central" exact2x2(example2,tsmethod="central")