{
  "id": "contingency-table",
  "title": "Contingency Table",
  "lang": "en",
  "source_lang": "en",
  "summary": "A contingency table organizes counts by combinations of categorical variables, providing a basis for describing distributions, measuring association, and testing independence.",
  "category": "mathematics",
  "subcategory": "probability-statistics",
  "type": "concept",
  "aliases": [
    "Cross-tabulation",
    "Contingency matrix",
    "Two-way frequency table"
  ],
  "generated_at": "2026-11-05",
  "model": "pro/gpt-6.1-sol",
  "url": "https://aiwiki.page/en/contingency-table/",
  "translations": {
    "zh-Hans": {
      "url": "https://aiwiki.page/zh/contingency-table/",
      "json": "https://aiwiki.page/zh/contingency-table.json"
    }
  },
  "sections": [
    {
      "heading": "",
      "text": "A contingency table is a table of frequencies that classifies observations according to two or more categorical variables. Each cell records the number of observations belonging to a particular combination of categories. In statistics, contingency tables are used to summarize data, compare conditional distributions, and investigate whether variables are associated. A two-variable table is usually displayed as rows and columns; tables involving additional variables can be represented as collections of two-way tables or as multidimensional arrays."
    },
    {
      "heading": "Structure and notation",
      "text": "For two variables with \\(r\\) row categories and \\(c\\) column categories, an \\(r\\times c\\) contingency table has observed cell counts \\(n_{ij}\\), where \\(i=1,\\ldots,r\\) and \\(j=1,\\ldots,c\\).\n\nIts row totals, column totals, and overall total are\n\n\\[n_{i+}=\\sum_{j=1}^{c}n_{ij}, \\qquad n_{+j}=\\sum_{i=1}^{r}n_{ij}, \\qquad N=\\sum_{i=1}^{r}\\sum_{j=1}^{c}n_{ij}.\\]\n\nThe plus sign indicates summation over the corresponding index. Row and column totals are called marginal totals, while \\(N\\) is the grand total.\n\nFor example, a survey might classify 100 respondents by their work arrangement and preferred meeting format:\n\nWork arrangement | Online preferred | In-person preferred | Total\nRemote | 30 | 20 | 50\nOn-site | 20 | 30 | 50\nTotal | 50 | 50 | 100\n\nThe cell count 30 represents respondents who both work remotely and prefer online meetings. The marginal totals describe each variable separately, without retaining information about how the two variables are related.\n\nIn an ordinary count table, categories for each variable are mutually exclusive, and each observation contributes to exactly one cell. Multiple-response questions, in which respondents may select several categories, require a different interpretation: the entries may count responses rather than distinct individuals."
    },
    {
      "heading": "Counts, proportions, and conditional distributions",
      "text": "Dividing each count by the grand total produces relative frequencies,\n\n\\[\\widehat p_{ij}=\\frac{n_{ij}}{N},\\]\n\nwhich form an empirical joint probability distribution for the two variables.\n\nThree percentage displays answer different questions:\n\n- Overall percentages: \\(100n_{ij}/N\\), the percentage of all observations in a cell.\n- Row percentages: \\(100n_{ij}/n_{i+}\\), the distribution of column categories within a row.\n- Column percentages: \\(100n_{ij}/n_{+j}\\), the distribution of row categories within a column.\n\nRow and column percentages estimate different conditional probabilities. Their denominators must therefore be stated explicitly.\n\nIn the example, 60% of remote workers prefer online meetings, compared with 40% of on-site workers. Conversely, 60% of those who prefer online meetings work remotely. These two percentages happen to be equal because of the table’s particular margins; they need not be equal in general.\n\nWhen group sizes differ, raw counts alone can be misleading. A larger group may contain more observations in every outcome category even when its within-group proportion for a particular outcome is smaller."
    },
    {
      "heading": "Independence and expected frequencies",
      "text": "Two variables \\(X\\) and \\(Y\\) exhibit statistical independence when\n\n\\[P(X=i,Y=j)=P(X=i)P(Y=j)\\]\n\nfor every category pair. Equivalently, the conditional distribution of either variable is unchanged across categories of the other, wherever the conditioning probabilities are positive.\n\nUnder the fitted independence model, the expected cell counts are\n\n\\[E_{ij} =N\\left(\\frac{n_{i+}}{N}\\right) \\left(\\frac{n_{+j}}{N}\\right) =\\frac{n_{i+}n_{+j}}{N}.\\]\n\nThese are fitted expectations, not additional observations. They need not be integers.\n\nIn the example, every expected count is \\(50\\times50/100=25\\). The observed counts depart from this pattern: the remote–online and on-site–in-person cells are more frequent than the independence model predicts.\n\nSample proportions rarely satisfy independence exactly, even when the population variables are independent. Statistical inference assesses whether the observed departures are compatible with sampling variation."
    },
    {
      "heading": "Statistical tests",
      "text": "Pearson’s chi-squared test\n\nThe standard Pearson chi-squared test of independence uses the test statistic\n\n\\[X^2=\\sum_{i=1}^{r}\\sum_{j=1}^{c} \\frac{(n_{ij}-E_{ij})^2}{E_{ij}}.\\]\n\nUnder the null hypothesis of independence and suitable sampling conditions, its large-sample distribution is approximately a chi-squared distribution with\n\n\\[(r-1)(c-1)\\]\n\ndegrees of freedom. This formula assumes an ordinary table without structural restrictions and with positive population marginal probabilities.\n\nFor the example,\n\n\\[X^2=4, \\qquad \\mathrm{df}=1.\\]\n\nThe resulting p-value describes how unusual a statistic at least this large would be under the null model. It is not the probability that independence is true, and it does not measure the strength or practical importance of an association.\n\nThe same calculation can test homogeneity: whether several independently sampled populations share the same categorical distribution. The numerical procedure is the same, but the sampling design and interpretation differ from those of a test of independence in a single sampled population.\n\nThe approximation depends on expected counts and table structure, not simply on total sample size. Very small expected counts can make it inaccurate. Familiar thresholds, such as an expected count of five, are rules of thumb rather than universal validity conditions.\n\nExact and simulation-based tests\n\nFisher’s exact test is widely used for \\(2\\times2\\) tables, particularly with small or sparse counts. Under independence, conditioning on both margins gives a hypergeometric distribution for one cell count, which determines the remaining cells.\n\nExact procedures also exist for larger tables, although computation can become demanding. Conditional Monte Carlo methods can approximate test probabilities when enumerating all possible tables is impractical. For discrete distributions, different definitions of a two-sided exact test can produce different p-values.\n\nLikelihood-ratio test\n\nAn alternative statistic is the likelihood-ratio deviance,\n\n\\[G^2=2\\sum_{i,j}n_{ij} \\log\\left(\\frac{n_{ij}}{E_{ij}}\\right),\\]\n\nwhere a term with \\(n_{ij}=0\\) is defined to contribute zero. Under standard regularity conditions, \\(G^2\\) has the same asymptotic chi-squared reference distribution as Pearson’s statistic.\n\nThis statistic also has an information-theoretic interpretation:\n\n\\[G^2=2N\\widehat I(X;Y),\\]\n\nwhere \\(\\widehat I(X;Y)\\) is the empirical mutual information calculated with natural logarithms."
    },
    {
      "heading": "Measuring association",
      "text": "A significance test addresses compatibility with a null model; an effect size describes the magnitude or pattern of association.\n\nTwo-by-two tables\n\nFor a table written as\n\n | Outcome present | Outcome absent\nGroup 1 | \\(a\\) | \\(b\\)\nGroup 2 | \\(c\\) | \\(d\\)\n\nthe odds ratio is\n\n\\[\\mathrm{OR}=\\frac{a/b}{c/d}=\\frac{ad}{bc},\\]\n\nwhen the relevant denominators are nonzero. It compares the odds of the outcome in Group 1 with those in Group 2.\n\nTwo other comparisons are the difference in outcome proportions,\n\n\\[\\frac{a}{a+b}-\\frac{c}{c+d},\\]\n\nand their ratio,\n\n\\[\\frac{a/(a+b)}{c/(c+d)}.\\]\n\nWhich quantities have a meaningful population interpretation depends on how observations were sampled. For example, when sampling fixes the numbers of outcome-positive and outcome-negative observations, observed outcome proportions generally do not estimate population outcome probabilities directly.\n\nFor the illustrative table, taking “online preferred” as the outcome and remote workers as Group 1 gives an odds ratio of \\(2.25\\), a proportion difference of \\(0.20\\), and a proportion ratio of \\(1.5\\). These describe different aspects of the same association.\n\nLarger tables\n\nA common standardized association measure is Cramér’s V:\n\n\\[V=\\sqrt{\\frac{X^2}{N\\min(r-1,c-1)}}.\\]\n\nIt ranges from zero to one for a nondegenerate table. In a \\(2\\times2\\) table it equals the absolute value of the phi coefficient. It does not indicate a direction of association, and conventional labels such as “small” or “large” depend on context.\n\nFor ordered categories, methods that use the ordering may be more informative than nominal-category measures. Treating categories as unordered discards information about their sequence."
    },
    {
      "heading": "Sampling models and assumptions",
      "text": "The same observed table can arise under different sampling schemes:\n\n- Multinomial sampling: the grand total \\(N\\) is fixed, and observations are classified into cells.\n- Product-multinomial sampling: one set of marginal totals is fixed, as when predetermined numbers are sampled from several groups.\n- Poisson sampling: cell counts are modeled as independent Poisson variables, with the grand total random.\n- Fixed-margin sampling or conditional inference: both margins are fixed by design or conditioned upon in the analysis.\n\nThese distinctions affect likelihoods, parameter interpretation, and exact inference, even though several large-sample procedures coincide.\n\nOrdinary chi-squared procedures assume independent observational units. Repeated measurements on the same person, matched pairs, household clusters, and complex survey designs generally require methods that account for dependence or sampling design. A cross-tabulation remains a valid descriptive display, but its appearance alone does not establish that the usual test is appropriate."
    },
    {
      "heading": "Multiway tables and statistical models",
      "text": "A three-way table adds a third classification, often displayed as a series of two-way tables, one for each category of the third variable. Such tables can distinguish marginal association from conditional independence.\n\nAssociations may change after stratification. In Simpson’s paradox, an association in an aggregated table can reverse when the data are separated into groups. Differences in group composition can create such patterns. Whether a variable should be conditioned on is a question of study design and causal structure, not something the table alone determines.\n\nLog-linear models express expected cell counts through main effects and interaction terms. For a two-way independence model,\n\n\\[\\log \\mu_{ij} =\\lambda+\\lambda_i^{X}+\\lambda_j^{Y},\\]\n\nwith constraints imposed for identifiability. Additional interaction terms represent departures from independence. Multiway versions can describe more complex patterns of conditional association.\n\nWhen one categorical variable is designated as the response, logistic regression and related models provide another way to analyze the data, particularly when explanatory variables include continuous measurements."
    },
    {
      "heading": "Applications and limitations",
      "text": "Contingency tables appear in surveys, quality control, population studies, and many other settings. A classification model’s confusion matrix is a specialized contingency table comparing predicted categories with observed categories.\n\nImportant limitations include:\n\n- Association is not causation. An association can arise through confounding, selection processes, or other features of the data-generating process.\n- Zero counts have different meanings. A sampling zero is an unobserved but possible combination; a structural zero is an impossible combination. Structural zeros alter the model and may change degrees of freedom.\n- Sparse tables can be difficult to analyze. Adding categories or variables rapidly increases the number of cells, often leaving little information in individual cells.\n- Grouping loses information. Converting continuous measurements into categories makes a table possible but discards within-category variation and can make results sensitive to cut points.\n- Missing observations affect interpretation. Excluding incomplete records or treating missingness as a category changes what the table describes and may introduce bias.\n- Overall tests do not identify specific departures. A significant result establishes evidence against the overall independence model, not that every row or cell differs. Examining many cells or contrasts raises multiple-testing issues.\n- Precision matters. Effect estimates can be unstable in small or sparse tables; suitable confidence intervals convey uncertainty that counts and p-values alone do not."
    }
  ],
  "links": [
    "statistics",
    "joint-probability-distribution",
    "conditional-probability",
    "statistical-independence",
    "test-statistic",
    "null-hypothesis",
    "chi-squared-distribution",
    "degrees-of-freedom",
    "p-value",
    "monte-carlo-method",
    "information-theory",
    "mutual-information",
    "effect-size",
    "odds-ratio",
    "poisson-distribution",
    "conditional-independence",
    "logistic-regression",
    "confounding",
    "multiple-testing",
    "confidence-interval"
  ],
  "sources": [],
  "license": "https://creativecommons.org/licenses/by/4.0/"
}