chisq.test {stats} R Documentation

Pearson's Chi-squared Test for Count Data

Description


chisq.test performs chi-squared contingency table tests and 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

x

a numeric vector Or matrix. x And y can also both be factors.

y

a numeric vector; ignored if x is a matrix. If x is a factor, y should be a factor of the same length.

correct

a logical indicating whether to apply continuity correction when computing the test statistic for 2 by 2 tables: one half is subtracted from all ||O-E|| differences; however, the correction will Not be bigger than the differences themselves. No correction Is done if simulate.p.value = TRUE. [as boolean]

p

a vector of probabilities of the same length as x. An error is given if any entry of p is negative.

rescale.p

a logical scalar; if TRUE then p is rescaled (if necessary) to sum to 1. If rescale.p is FALSE, and p does not sum to 1, an error is given. [as boolean]

simulate.p.value

a logical indicating whether to compute p-values by Monte Carlo simulation. [as boolean]

B

an integer specifying the number of replicates used in the Monte Carlo test. [as integer]

Details

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.

Authors

sciBASIC.NET

Value

this function returns data object of type any kind.

clr value class

Examples

 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)

[Package stats version 5.0.1.2389 Index]