You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

213 lines
8.6KB

  1. % Generated by roxygen2: do not edit by hand
  2. % Please edit documentation in R/convenience_functions.R
  3. \name{fisher.test}
  4. \alias{fisher.test}
  5. \alias{fisher.test.default}
  6. \alias{fisher.test.formula}
  7. \title{Fisher's Exact Test for Count Data}
  8. \usage{
  9. fisher.test(x, y, workspace, hybrid, control, or, alternative, conf.int,
  10. conf.level, simulate.p.value, B)
  11. \method{fisher.test}{default}(x, ...)
  12. \method{fisher.test}{formula}(x, y = NULL, workspace = 200000,
  13. hybrid = F, control = list(), or = 1, alternative = "two.sided",
  14. conf.int = T, conf.level = 0.95, simulate.p.value = F, B = 2000)
  15. }
  16. \arguments{
  17. \item{x}{either a two-dimensional contingency table in matrix form, a factor object, or a formula of the form \code{lhs ~ rhs} where \code{lhs} and \code{rhs} are factors.}
  18. \item{y}{a factor object; ignored if \code{x} is a matrix or a formula.}
  19. \item{workspace}{an integer specifying the size of the workspace
  20. used in the network algorithm. In units of 4 bytes. Only used for
  21. non-simulated p-values larger than \eqn{2 \times 2}{2 by 2} tables.
  22. Since \R version 3.5.0, this also increases the internal stack size
  23. which allows larger problems to be solved, however sometimes needing
  24. hours. In such cases, \code{simulate.p.values=TRUE} may be more
  25. reasonable.}
  26. \item{hybrid}{a logical. Only used for larger than \eqn{2 \times 2}{2 by 2}
  27. tables, in which cases it indicates whether the exact probabilities
  28. (default) or a hybrid approximation thereof should be computed.}
  29. \item{control}{a list with named components for low level algorithm
  30. control. At present the only one used is \code{"mult"}, a positive
  31. integer \eqn{\ge 2} with default 30 used only for larger than
  32. \eqn{2 \times 2}{2 by 2} tables. This says how many times as much
  33. space should be allocated to paths as to keys: see file
  34. \file{fexact.c} in the sources of this package.}
  35. \item{or}{the hypothesized odds ratio. Only used in the
  36. \eqn{2 \times 2}{2 by 2} case.}
  37. \item{alternative}{indicates the alternative hypothesis and must be
  38. one of \code{"two.sided"}, \code{"greater"} or \code{"less"}.
  39. You can specify just the initial letter. Only used in the
  40. \eqn{2 \times 2}{2 by 2} case.}
  41. \item{conf.int}{logical indicating if a confidence interval for the
  42. odds ratio in a \eqn{2 \times 2}{2 by 2} table should be
  43. computed (and returned).}
  44. \item{conf.level}{confidence level for the returned confidence
  45. interval. Only used in the \eqn{2 \times 2}{2 by 2} case and if
  46. \code{conf.int = TRUE}.}
  47. \item{simulate.p.value}{a logical indicating whether to compute
  48. p-values by Monte Carlo simulation, in larger than \eqn{2 \times
  49. 2}{2 by 2} tables.}
  50. \item{B}{an integer specifying the number of replicates used in the
  51. Monte Carlo test.}
  52. \item{...}{additional params to feed to original fisher.test}
  53. }
  54. \value{
  55. A list with class \code{"htest"} containing the following components:
  56. p.value: the p-value of the test.
  57. conf.int: a confidence interval for the odds ratio. Only present in
  58. the 2 by 2 case and if argument \code{conf.int = TRUE}.
  59. estimate: an estimate of the odds ratio. Note that the _conditional_
  60. Maximum Likelihood Estimate (MLE) rather than the
  61. unconditional MLE (the sample odds ratio) is used. Only
  62. present in the 2 by 2 case.
  63. null.value: the odds ratio under the null, \code{or}. Only present in the 2
  64. by 2 case.
  65. alternative: a character string describing the alternative hypothesis.
  66. method: the character string \code{"Fisher's Exact Test for Count Data"}.
  67. data.name: a character string giving the names of the data.
  68. }
  69. \description{
  70. Performs Fisher's exact test for testing the null of independence
  71. of rows and columns in a contingency table with fixed marginals, or with a formula expression.
  72. }
  73. \details{
  74. If \code{x} is a matrix, it is taken as a two-dimensional contingency
  75. table, and hence its entries should be nonnegative integers.
  76. Otherwise, both \code{x} and \code{y} must be vectors of the same length.
  77. Incomplete cases are removed, the vectors are coerced into factor
  78. objects, and the contingency table is computed from these.
  79. For 2 by 2 cases, p-values are obtained directly using the
  80. (central or non-central) hypergeometric distribution. Otherwise,
  81. computations are based on a C version of the FORTRAN subroutine
  82. FEXACT which implements the network developed by Mehta and Patel
  83. (1986) and improved by Clarkson, Fan and Joe (1993). The FORTRAN
  84. code can be obtained from \url{http://www.netlib.org/toms/643}.
  85. Note this fails (with an error message) when the entries of the
  86. table are too large. (It transposes the table if necessary so it
  87. has no more rows than columns. One constraint is that the product
  88. of the row marginals be less than 2^31 - 1.)
  89. For 2 by 2 tables, the null of conditional independence is
  90. equivalent to the hypothesis that the odds ratio equals one.
  91. \code{Exact} inference can be based on observing that in general, given
  92. all marginal totals fixed, the first element of the contingency
  93. table has a non-central hypergeometric distribution with
  94. non-centrality parameter given by the odds ratio (Fisher, 1935).
  95. The alternative for a one-sided test is based on the odds ratio,
  96. so \code{alternative = "greater"} is a test of the odds ratio being
  97. bigger than \code{or}.
  98. Two-sided tests are based on the probabilities of the tables, and
  99. take as \code{more extreme} all tables with probabilities less than or
  100. equal to that of the observed table, the p-value being the sum of
  101. such probabilities.
  102. For larger than 2 by 2 tables and \code{hybrid = TRUE}, asymptotic
  103. chi-squared probabilities are only used if the ‘Cochran
  104. conditions’ are satisfied, that is if no cell has count zero, and
  105. more than 80% of the cells have counts at least 5: otherwise the
  106. exact calculation is used.
  107. Simulation is done conditional on the row and column marginals,
  108. and works only if the marginals are strictly positive. (A C
  109. translation of the algorithm of Patefield (1981) is used.)
  110. }
  111. \examples{
  112. \dontrun{
  113. ## Agresti (1990, p. 61f; 2002, p. 91) Fisher's Tea Drinker
  114. ## A British woman claimed to be able to distinguish whether milk or
  115. ## tea was added to the cup first. To test, she was given 8 cups of
  116. ## tea, in four of which milk was added first. The null hypothesis
  117. ## is that there is no association between the true order of pouring
  118. ## and the woman's guess, the alternative that there is a positive
  119. ## association (that the odds ratio is greater than 1).
  120. TeaTasting <-
  121. matrix(c(3, 1, 1, 3),
  122. nrow = 2,
  123. dimnames = list(Guess = c("Milk", "Tea"),
  124. Truth = c("Milk", "Tea")))
  125. fisher.test(TeaTasting, alternative = "greater")
  126. ## => p = 0.2429, association could not be established
  127. ## Fisher (1962, 1970), Criminal convictions of like-sex twins
  128. Convictions <-
  129. matrix(c(2, 10, 15, 3),
  130. nrow = 2,
  131. dimnames =
  132. list(c("Dizygotic", "Monozygotic"),
  133. c("Convicted", "Not convicted")))
  134. Convictions
  135. fisher.test(Convictions, alternative = "less")
  136. fisher.test(Convictions, conf.int = FALSE)
  137. fisher.test(Convictions, conf.level = 0.95)$conf.int
  138. fisher.test(Convictions, conf.level = 0.99)$conf.int
  139. ## A r x c table Agresti (2002, p. 57) Job Satisfaction
  140. Job <- matrix(c(1,2,1,0, 3,3,6,1, 10,10,14,9, 6,7,12,11), 4, 4,
  141. dimnames = list(income = c("< 15k", "15-25k", "25-40k", "> 40k"),
  142. satisfaction = c("VeryD", "LittleD", "ModerateS", "VeryS")))
  143. fisher.test(Job)
  144. fisher.test(Job, simulate.p.value = TRUE, B = 1e5)
  145. ###
  146. }
  147. }
  148. \references{
  149. Agresti, A. (1990) _Categorical data analysis_. New York: Wiley.
  150. Pages 59-66.
  151. Agresti, A. (2002) _Categorical data analysis_. Second edition.
  152. New York: Wiley. Pages 91-101.
  153. Fisher, R. A. (1935) The logic of inductive inference. _Journal
  154. of the Royal Statistical Society Series A_ *98*, 39-54.
  155. Fisher, R. A. (1962) Confidence limits for a cross-product ratio.
  156. _Australian Journal of Statistics_ *4*, 41.
  157. Fisher, R. A. (1970) _Statistical Methods for Research Workers._
  158. Oliver & Boyd.
  159. Mehta, C. R. and Patel, N. R. (1986) Algorithm 643. FEXACT: A
  160. Fortran subroutine for Fisher's exact test on unordered r*c
  161. contingency tables. _ACM Transactions on Mathematical Software_,
  162. *12*, 154-161.
  163. Clarkson, D. B., Fan, Y. and Joe, H. (1993) A Remark on Algorithm
  164. 643: FEXACT: An Algorithm for Performing Fisher's Exact Test in r
  165. x c Contingency Tables. _ACM Transactions on Mathematical
  166. Software_, *19*, 484-488.
  167. Patefield, W. M. (1981) Algorithm AS159. An efficient method of
  168. generating r x c tables with given row and column totals.
  169. _Applied Statistics_ *30*, 91-97.
  170. }
  171. \seealso{
  172. \code{\link{chisq.test}}
  173. \code{fisher.exact} in package \pkg{kexact2x2} for alternative
  174. interpretations of two-sided tests and confidence intervals for 2
  175. by 2 tables.
  176. }