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.

172 lines
6.7KB

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