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.

184 lines
6.8KB

  1. % Generated by roxygen2: do not edit by hand
  2. % Please edit documentation in R/convenience_functions.R
  3. \name{chisq.test}
  4. \alias{chisq.test}
  5. \alias{chisq.test.default}
  6. \alias{chisq.test.formula}
  7. \title{Pearson's Chi-squared Test for Count Data}
  8. \source{
  9. The code for Monte Carlo simulation is a C translation of the Fortran algorithm of Patefield (1981).
  10. }
  11. \usage{
  12. chisq.test(x, y, correct, p, rescale.p, simulate.p.value, B)
  13. \method{chisq.test}{default}(
  14. x,
  15. y = NULL,
  16. correct = TRUE,
  17. p = rep(1/length(x), length(x)),
  18. rescale.p = FALSE,
  19. simulate.p.value = FALSE,
  20. B = 2000
  21. )
  22. \method{chisq.test}{formula}(
  23. x,
  24. y = NULL,
  25. correct = T,
  26. p = rep(1/length(x), length(x)),
  27. rescale.p = F,
  28. simulate.p.value = F,
  29. B = 2000
  30. )
  31. }
  32. \arguments{
  33. \item{x}{a numeric vector, or matrix, or formula of the form \code{lhs ~ rhs} where \code{lhs} and \code{rhs} are factors. \code{x} and \code{y} can also both be factors.}
  34. \item{y}{a numeric vector; ignored if \code{x} is a matrix or a formula. If \code{x} is a factor, \code{y} should be a factor of the same length.}
  35. \item{correct}{a logical indicating whether to apply continuity
  36. correction when computing the test statistic for 2 by 2 tables: one
  37. half is subtracted from all \eqn{|O - E|} differences; however, the
  38. correction will not be bigger than the differences themselves. No correction
  39. is done if \code{simulate.p.value = TRUE}.}
  40. \item{p}{a vector of probabilities of the same length of \code{x}.
  41. An error is given if any entry of \code{p} is negative.}
  42. \item{rescale.p}{a logical scalar; if TRUE then \code{p} is rescaled
  43. (if necessary) to sum to 1. If \code{rescale.p} is FALSE, and
  44. \code{p} does not sum to 1, an error is given.}
  45. \item{simulate.p.value}{a logical indicating whether to compute
  46. p-values by Monte Carlo simulation.}
  47. \item{B}{an integer specifying the number of replicates used in the
  48. Monte Carlo test.}
  49. }
  50. \value{
  51. A list with class \code{"htest"} containing the following components:
  52. statistic: the value the chi-squared test statistic.
  53. parameter: the degrees of freedom of the approximate chi-squared
  54. distribution of the test statistic, \code{NA} if the p-value is
  55. computed by Monte Carlo simulation.
  56. p.value: the p-value for the test.
  57. method: a character string indicating the type of test performed, and
  58. whether Monte Carlo simulation or continuity correction was
  59. used.
  60. data.name: a character string giving the name(s) of the data.
  61. observed: the observed counts.
  62. expected: the expected counts under the null hypothesis.
  63. residuals: the Pearson residuals, ‘(observed - expected) /
  64. sqrt(expected)’.
  65. stdres: standardized residuals, \code{(observed - expected) / sqrt(V)},
  66. where \code{V} is the residual cell variance (Agresti, 2007,
  67. section 2.4.5 for the case where \code{x} is a matrix, ‘n * p * (1
  68. - p)’ otherwise).
  69. }
  70. \description{
  71. \code{chisq.test} performs chi-squared contingency table tests and goodness-of-fit tests, with an added method for formulas.
  72. }
  73. \details{
  74. If \code{x} is a matrix with one row or column, or if \code{x} is a vector
  75. and \code{y} is not given, then a _goodness-of-fit test_ is performed
  76. (\code{x} is treated as a one-dimensional contingency table). The
  77. entries of \code{x} must be non-negative integers. In this case, the
  78. hypothesis tested is whether the population probabilities equal
  79. those in \code{p}, or are all equal if \code{p} is not given.
  80. If \code{x} is a matrix with at least two rows and columns, it is taken
  81. as a two-dimensional contingency table: the entries of \code{x} must be
  82. non-negative integers. Otherwise, \code{x} and \code{y} must be vectors or
  83. factors of the same length; cases with missing values are removed,
  84. the objects are coerced to factors, and the contingency table is
  85. computed from these. Then Pearson's chi-squared test is performed
  86. of the null hypothesis that the joint distribution of the cell
  87. counts in a 2-dimensional contingency table is the product of the
  88. row and column marginals.
  89. If \code{simulate.p.value} is \code{FALSE}, the p-value is computed from the
  90. asymptotic chi-squared distribution of the test statistic;
  91. continuity correction is only used in the 2-by-2 case (if
  92. \code{correct} is \code{TRUE}, the default). Otherwise the p-value is
  93. computed for a Monte Carlo test (Hope, 1968) with \code{B} replicates.
  94. In the contingency table case simulation is done by random
  95. sampling from the set of all contingency tables with given
  96. marginals, and works only if the marginals are strictly positive.
  97. Continuity correction is never used, and the statistic is quoted
  98. without it. Note that this is not the usual sampling situation
  99. assumed for the chi-squared test but rather that for Fisher's
  100. exact test.
  101. In the goodness-of-fit case simulation is done by random sampling
  102. from the discrete distribution specified by \code{p}, each sample being
  103. of size \code{n = sum(x)}. This simulation is done in R and may be
  104. slow.
  105. }
  106. \examples{
  107. \dontrun{
  108. ## From Agresti(2007) p.39
  109. M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))
  110. dimnames(M) <- list(gender = c("F", "M"),
  111. party = c("Democrat","Independent", "Republican"))
  112. (Xsq <- chisq.test(M)) # Prints test summary
  113. Xsq$observed # observed counts (same as M)
  114. Xsq$expected # expected counts under the null
  115. Xsq$residuals # Pearson residuals
  116. Xsq$stdres # standardized residuals
  117. ## Effect of simulating p-values
  118. x <- matrix(c(12, 5, 7, 7), ncol = 2)
  119. chisq.test(x)$p.value # 0.4233
  120. chisq.test(x, simulate.p.value = TRUE, B = 10000)$p.value
  121. # around 0.29!
  122. ## Testing for population probabilities
  123. ## Case A. Tabulated data
  124. x <- c(A = 20, B = 15, C = 25)
  125. chisq.test(x)
  126. chisq.test(as.table(x)) # the same
  127. x <- c(89,37,30,28,2)
  128. p <- c(40,20,20,15,5)
  129. try(
  130. chisq.test(x, p = p) # gives an error
  131. )
  132. chisq.test(x, p = p, rescale.p = TRUE)
  133. # works
  134. p <- c(0.40,0.20,0.20,0.19,0.01)
  135. # Expected count in category 5
  136. # is 1.86 < 5 ==> chi square approx.
  137. chisq.test(x, p = p) # maybe doubtful, but is ok!
  138. chisq.test(x, p = p, simulate.p.value = TRUE)
  139. ## Case B. Raw data
  140. x <- trunc(5 * runif(100))
  141. chisq.test(table(x)) # NOT 'chisq.test(x)'!
  142. ###
  143. }
  144. }
  145. \references{
  146. Hope, A. C. A. (1968) A simplified Monte Carlo significance test
  147. procedure. _J. Roy, Statist. Soc. B_ *30*, 582-598.
  148. Patefield, W. M. (1981) Algorithm AS159. An efficient method of
  149. generating r x c tables with given row and column totals.
  150. _Applied Statistics_ *30*, 91-97.
  151. Agresti, A. (2007) _An Introduction to Categorical Data Analysis,
  152. 2nd ed._, New York: John Wiley & Sons. Page 38.
  153. }
  154. \seealso{
  155. For goodness-of-fit testing, notably of continuous distributions, \code{\link{ks.test}}.
  156. }