Rev 68948 | Rev 81832 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed
% File src/library/stats/man/chisq.test.Rd% Part of the R package, https://www.R-project.org% Copyright 1995-2018 R Core Team% Distributed under GPL 2 or later\name{chisq.test}\alias{chisq.test}\concept{goodness-of-fit}\title{Pearson's Chi-squared Test for Count Data}\description{\code{chisq.test} performs chi-squared contingency table testsand goodness-of-fit tests.}\usage{chisq.test(x, y = NULL, correct = TRUE,p = rep(1/length(x), length(x)), rescale.p = FALSE,simulate.p.value = FALSE, B = 2000)}\arguments{\item{x}{a numeric vector or matrix. \code{x} and \code{y} can alsoboth be factors.}\item{y}{a numeric vector; ignored if \code{x} is a matrix. If\code{x} is a factor, \code{y} should be a factor of the same length.}\item{correct}{a logical indicating whether to apply continuitycorrection when computing the test statistic for 2 by 2 tables: onehalf is subtracted from all \eqn{|O - E|} differences; however, thecorrection will not be bigger than the differences themselves. No correctionis done if \code{simulate.p.value = TRUE}.}\item{p}{a vector of probabilities of the same length of \code{x}.An error is given if any entry of \code{p} is negative.}\item{rescale.p}{a logical scalar; if TRUE then \code{p} is rescaled(if necessary) to sum to 1. If \code{rescale.p} is FALSE, and\code{p} does not sum to 1, an error is given.}\item{simulate.p.value}{a logical indicating whether to computep-values by Monte Carlo simulation.}\item{B}{an integer specifying the number of replicates used in theMonte Carlo test.}}\details{If \code{x} is a matrix with one row or column, or if \code{x} is avector and \code{y} is not given, then a \emph{goodness-of-fit test}is performed (\code{x} is treated as a one-dimensionalcontingency table). The entries of \code{x} must be non-negativeintegers. In this case, the hypothesis tested is whether thepopulation probabilities equal those in \code{p}, or are all equal if\code{p} is not given.If \code{x} is a matrix with at least two rows and columns, it istaken as a two-dimensional contingency table: the entries of \code{x}must be non-negative integers. Otherwise, \code{x} and \code{y} mustbe vectors or factors of the same length; cases with missing valuesare removed, the objects are coerced to factors, and the contingencytable is computed from these. Then Pearson's chi-squared test isperformed of the null hypothesis that the joint distribution of thecell counts in a 2-dimensional contingency table is the product of therow and column marginals.If \code{simulate.p.value} is \code{FALSE}, the p-value is computedfrom the asymptotic chi-squared distribution of the test statistic;continuity correction is only used in the 2-by-2 case (if \code{correct}is \code{TRUE}, the default). Otherwise the p-value is computed for aMonte Carlo test (Hope, 1968) with \code{B} replicates.In the contingency table case simulation is done by random samplingfrom the set of all contingency tables with given marginals, and worksonly if the marginals are strictly positive. Continuity correction isnever used, and the statistic is quoted without it. Note that this isnot the usual sampling situation assumed for the chi-squared test butrather that for Fisher's exact test.In the goodness-of-fit case simulation is done by random sampling fromthe discrete distribution specified by \code{p}, each sample beingof size \code{n = sum(x)}. This simulation is done in \R and may beslow.}\value{A list with class \code{"htest"} containing the followingcomponents:\item{statistic}{the value the chi-squared test statistic.}\item{parameter}{the degrees of freedom of the approximatechi-squared distribution of the test statistic, \code{NA} if thep-value is computed by Monte Carlo simulation.}\item{p.value}{the p-value for the test.}\item{method}{a character string indicating the type of testperformed, and whether Monte Carlo simulation or continuitycorrection was used.}\item{data.name}{a character string giving the name(s) of the data.}\item{observed}{the observed counts.}\item{expected}{the expected counts under the null hypothesis.}\item{residuals}{the Pearson residuals,\code{(observed - expected) / sqrt(expected)}.}\item{stdres}{standardized residuals,\code{(observed - expected) / sqrt(V)}, where \code{V} is the residual cell variance (Agresti, 2007,section 2.4.5 for the case where \code{x} is a matrix, \code{n * p * (1 - p)} otherwise).}}\seealso{For goodness-of-fit testing, notably of continuous distributions,\code{\link{ks.test}}.}\source{The code for Monte Carlo simulation is a C translation of the Fortranalgorithm of Patefield (1981).}\references{Hope, A. C. A. (1968).A simplified Monte Carlo significance test procedure.\emph{Journal of the Royal Statistical Society Series B}, \bold{30},582--598.\url{http://www.jstor.org/stable/2984263}.Patefield, W. M. (1981).Algorithm AS 159: An efficient method of generating r x c tableswith given row and column totals.\emph{Applied Statistics}, \bold{30}, 91--97.\doi{10.2307/2346669}.Agresti, A. (2007).\emph{An Introduction to Categorical Data Analysis}, 2nd ed.New York: John Wiley & Sons.Page 38.}\examples{## From Agresti(2007) p.39M <- as.table(rbind(c(762, 327, 468), c(484, 239, 477)))dimnames(M) <- list(gender = c("F", "M"),party = c("Democrat","Independent", "Republican"))(Xsq <- chisq.test(M)) # Prints test summaryXsq$observed # observed counts (same as M)Xsq$expected # expected counts under the nullXsq$residuals # Pearson residualsXsq$stdres # standardized residuals## Effect of simulating p-valuesx <- matrix(c(12, 5, 7, 7), ncol = 2)chisq.test(x)$p.value # 0.4233chisq.test(x, simulate.p.value = TRUE, B = 10000)$p.value# around 0.29!## Testing for population probabilities## Case A. Tabulated datax <- c(A = 20, B = 15, C = 25)chisq.test(x)chisq.test(as.table(x)) # the samex <- c(89,37,30,28,2)p <- c(40,20,20,15,5)try(chisq.test(x, p = p) # gives an error)chisq.test(x, p = p, rescale.p = TRUE)# worksp <- c(0.40,0.20,0.20,0.19,0.01)# Expected count in category 5# is 1.86 < 5 ==> chi square approx.chisq.test(x, p = p) # maybe doubtful, but is ok!chisq.test(x, p = p, simulate.p.value = TRUE)## Case B. Raw datax <- trunc(5 * runif(100))chisq.test(table(x)) # NOT 'chisq.test(x)'!}\keyword{htest}\keyword{distribution}