bundles / scipy latest / scipy / stats / contingency / chi2_contingency
function
scipy.stats.contingency:chi2_contingency
source: /scipy/stats/contingency.py :148
Signature
def chi2_contingency ( observed , correction = True , lambda_ = None , * , method = None ) Summary
Chi-square test of independence of variables in a contingency table.
Extended Summary
This function computes the chi-square statistic and p-value for the hypothesis test of independence of the observed frequencies in the contingency table [1] observed. The expected frequencies are computed based on the marginal sums under the assumption of independence; see scipy.stats.contingency.expected_freq. The number of degrees of freedom is (expressed using numpy functions and attributes)
dof = observed.size - sum(observed.shape) + observed.ndim - 1Parameters
observed: array_likeThe contingency table. The table contains the observed frequencies (i.e. number of occurrences) in each category. In the two-dimensional case, the table is often described as an "R x C table".
correction: bool, optionalIf True, and the degrees of freedom is 1, apply Yates' correction for continuity. The effect of the correction is to adjust each observed value by 0.5 towards the corresponding expected value.
lambda_: float or str, optionalBy default, the statistic computed in this test is Pearson's chi-squared statistic [2].
lambda_allows a statistic from the Cressie-Read power divergence family [3] to be used instead. See scipy.stats.power_divergence for details.method: ResamplingMethod, optionalDefines the method used to compute the p-value. Compatible only with
correction=False, defaultlambda_, and two-way tables. Ifmethodis an instance of PermutationMethod/MonteCarloMethod, the p-value is computed using scipy.stats.permutation_test/scipy.stats.monte_carlo_test with the provided configuration options and other appropriate settings. Otherwise, the p-value is computed as documented in the notes. Note that ifmethodis an instance of MonteCarloMethod, thervsattribute must be left unspecified; Monte Carlo samples are always drawn using thervsmethod ofscipy.stats.random_table.
Returns
res: Chi2ContingencyResultAn object containing attributes:
statistic
statistic
pvalue
pvalue
dof
dof
expected_freq
expected_freq
Notes
An often quoted guideline for the validity of this calculation is that the test should be used only if the observed and expected frequencies in each cell are at least 5.
This is a test for the independence of different categories of a population. The test is only meaningful when the dimension of observed is two or more. Applying the test to a one-dimensional table will always result in expected equal to observed and a chi-square statistic equal to 0.
This function does not handle masked arrays, because the calculation does not make sense with missing values.
Like scipy.stats.chisquare, this function computes a chi-square statistic; the convenience this function provides is to figure out the expected frequencies and degrees of freedom from the given contingency table. If these were already known, and if the Yates' correction was not required, one could use scipy.stats.chisquare. That is, if one calls
res = chi2_contingency(obs, correction=False)then the following is true
(res.statistic, res.pvalue) == stats.chisquare(obs.ravel(), f_exp=ex.ravel(), ddof=obs.size - 1 - dof)
The lambda_ argument was added in version 0.13.0 of scipy.
Array API Standard Support
chi2_contingency has experimental support for Python Array API Standard compatible backends in addition to NumPy. Please consider testing these features by setting an environment variable SCIPY_ARRAY_API=1 and providing CuPy, PyTorch, JAX, or Dask arrays as array arguments. The following combinations of backend and device (or other capability) are supported.
==================== ==================== ==================== Library CPU GPU ==================== ==================== ==================== NumPy ✅ n/a CuPy n/a ⛔ PyTorch ⛔ ⛔ JAX ⛔ ⛔ Dask ⛔ n/a ==================== ==================== ====================
See
dev-arrayapifor more information.
Examples
A two-way example (2 x 3):import numpy as np from scipy.stats import chi2_contingency obs = np.array([[10, 10, 20], [20, 20, 20]]) res = chi2_contingency(obs)✓
res.statistic res.pvalue✗
res.dof
✓res.expected_freq
✗res = chi2_contingency(obs, lambda_="log-likelihood")
✓res.statistic res.pvalue✗
obs = np.array( [[[[12, 17], [11, 16]], [[11, 12], [15, 16]]], [[[23, 15], [30, 22]], [[14, 17], [15, 16]]]]) res = chi2_contingency(obs)✓
res.statistic res.pvalue✗
from scipy.stats import PermutationMethod obs = np.asarray([[12, 3], [17, 16]]) res = chi2_contingency(obs, correction=False) ref = chi2_contingency(obs, correction=False, method=PermutationMethod())✓
res.pvalue, ref.pvalue
✗See also
- hypothesis_chi2_contingency
Extended example
- scipy.stats.barnard_exact
- scipy.stats.boschloo_exact
- scipy.stats.chisquare
- scipy.stats.contingency.expected_freq
- scipy.stats.fisher_exact
- scipy.stats.power_divergence
Aliases
-
scipy.stats.chi2_contingency