Learn R Programming

exact2x2 (version 1.4.0)

exact2x2: Exact Conditional Tests for 2 by 2 Tables of Count Data

Description

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.

Usage

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, 
    plot=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)

Arguments

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
plot
logical. TRUE gives basic plot of point null odds ratios by p-values, for greater plot control use exact2x2Plot

Value

  • A list with class "htest" containing the following components:
  • p.valuethe p-value of the test
  • conf.inta confidence interval for the odds ratio
  • estimatean 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.valuethe odds ratio under the null, or.
  • alternativea character string describing the alternative hypothesis
  • methoda character string, changes depending on alternative and tsmethod
  • data.namea character string giving the names of the data

Details

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, 2010). 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. For details of McNemar-type tests see Fay (2010, R Journal).

References

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. (2010). Confidence intervals that Match Fisher's exact and Blaker's exact tests. Biostatistics, 11: 373-374 (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). Fay M.P. (2010). Two-sided Exact Tests and Matching Confidence Intervals for Discrete Data. R Journal 2(1):53-58. Fisher, R.A. (1935) The logic of inductive inference. Journal of the Royal Statistical Society Series A 98:39-54.

See Also

fisher.test or mcnemar.test

Examples

Run this code
## 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")

Run the code above in your browser using DataLab