chisq.test {stats} | R Documentation |
chisq.test performs chi-squared contingency table tests and goodness-of-fit tests.
chisq.test(x,
y = NULL,
correct = TRUE,
p = "~rep(1/length(x), length(x))",
rescale.p = FALSE,
simulate.p.value = FALSE,
B = 2000);
If x is a matrix with one row or column, or if x is a vector and y is not given, then a goodness-of-fit test is performed (x is treated as a one-dimensional contingency table). The entries of x must be non-negative integers. In this case, the hypothesis tested is whether the population probabilities equal those in p, or are all equal if p is not given.
If x is a matrix with at least two rows and columns, it is taken as a two-dimensional contingency table: the entries of x must be non-negative integers. Otherwise, x and y must be vectors or factors of the same length; cases with missing values are removed, the objects are coerced to factors, and the contingency table is computed from these. Then Pearson's chi-squared test is performed of the null hypothesis that the joint distribution of the cell counts in a 2-dimensional contingency table is the product of the row and column marginals.
If simulate.p.value is FALSE, the p-value is computed from the asymptotic chi-squared distribution of the test statistic; continuity correction is only used in the 2-by-2 case (if correct is TRUE, the default). Otherwise the p-value is computed for a Monte Carlo test (Hope, 1968) with B replicates. The default B = 2000 implies a minimum p-value of about 0.0005 (1 / (B + 1)).
In the contingency table case, simulation is done by random sampling from the set of all contingency tables with given marginals, and works only if the marginals are strictly positive. Continuity correction is never used, and the statistic is quoted without it. Note that this is not the usual sampling situation assumed for the chi-squared test but rather that for Fisher's exact test.
In the goodness-of-fit case simulation is done by random sampling from the discrete distribution specified
by p, each sample being of size n = sum(x)
. This simulation is done in R and may be slow.
table <- matrix(c(30, 50, 20, 10), nrow = 2)
rownames(table) <- c("Group1", "Group2")
colnames(table) <- c("Yes", "No")
result <- chisq.test(table)
print(result)