AI Agents & Econometric Coding

Categorical data · Other

Pearson's Chi-Square Test of Independence

The Pearson statistic compares observed table counts with counts implied by independence.

Independence creates expected counts

For row $r$ and column $c$, independence implies

$$ E_{rc}=\frac{n_{r\cdot}n_{\cdot c}}{n}. $$

Pearson's statistic adds standardized squared deviations (Pearson, 1900).

$$ \chi^2=\sum_r\sum_c\frac{(O_{rc}-E_{rc})^2}{E_{rc}}. $$

A two-by-two table

Outcome AOutcome BRow total
Group 1302050
Group 2203050
Column total5050100

Every expected count is 25. Each cell contributes $(5^2)/25=1$, so the statistic equals 4. The degrees of freedom are $(2-1)(2-1)=1$.

Assumptions and implementation

Observations must be independent. Expected counts should support the large-sample approximation. Sparse tables can require exact or simulation methods.

Continuity correction, likelihood-ratio statistics, and missing-category handling can change software output. A signed-residual heatmap locates cells with large values of $(O-E)/\sqrt E$.

From a global statistic to cell patterns

The Pearson statistic summarizes all departures from independence. Each term $(O_{rc}-E_{rc})^2/E_{rc}$ is nonnegative, so cells with large standardized gaps drive the total. Report the contingency table with row or column percentages. Then show adjusted residuals when readers need to locate the association. Cramér's $V$ gives a scale-free summary of association strength, while the chi-square statistic also grows with sample size.

Suppose one cell contains 30 observations and its independence expectation is 20. That cell contributes $(30-20)^2/20=5$ before contributions from the other cells are added. Its positive residual means the category combination occurs more often than independence predicts.

Conditions, sparse tables, and coding

Each sampled unit should contribute to one table cell. Repeated observations from the same person violate the independent-count model. Survey clusters and sampling weights also require design-based procedures. Inspect expected counts, not observed counts, when assessing the usual reference distribution. Sparse tables can give a poor chi-square approximation. Combine categories only when the scientific definition supports the combination. Exact or Monte Carlo procedures can address small tables without changing category meaning.

Build expected counts directly from the margins and verify that they reproduce every row and column total. Preserve structural zeros, which represent impossible combinations, separately from sampled zeros. Report missing-category handling because listwise deletion changes both margins. For ordered categories, a general independence test discards order. A trend test may answer a more focused question. (Pearson, 1900)

Further reading

Pearson introduces the chi-square goodness-of-fit framework (Pearson, 1900). Agresti develops contingency-table analysis and sparse-table cautions (Agresti, 2013).

Source status

Sources and further reading

  1. Karl Pearson. 1900. “On the Criterion That a Given System of Deviations from the Probable in the Case of a Correlated System of Variables Is Such That It Can Be Reasonably Supposed to Have Arisen from Random Sampling.” Philosophical Magazine 50(302): 157--175. doi:10.1080/14786440009463897.
  2. Alan Agresti. 2013. Categorical Data Analysis. Wiley. Source.

Related reading

About this benchmark task

Status
In the benchmark
Identifier
chisq_indep
Family
Other
Software
Stata, R, Python
Source
Benchmark task set

Task statement

Run Pearson's chi-square test of independence for row by col. Do not use Yates correction. Report rows chi2, df, and p_value.

Required output

chi2, df, p_value

Open every recorded run for this task.