Model library Chi-Square Test
Statistical model reference

Chi-Square Test

Review when to use this method, its data requirements, implementation patterns, and interpretation guidance.

Description

A statistical test used to determine if there is a significant association between categorical variables.

Use Cases
  • goodness-of-fit
  • test of independence
  • contingency table analysis
Requirements
  • Sample Size: small, medium, large
  • Missing Data: none
  • Data Distribution: any
  • Relationship Type: any
Variable Types
Dependent Variables
  • categorical
Independent Variables
  • categorical
Implementation
from scipy.stats import chi2_contingency

chi2, p, dof, expected = chi2_contingency(observed_table)
Documentation
chisq.test(table) # For contingency table
chisq.test(observed, p=expected_probs) # For goodness-of-fit
Documentation
CROSSTABS
  /TABLES=row_var BY col_var
  /STATISTICS=CHISQ
  /CELLS=COUNT EXPECTED.
Documentation
PROC FREQ DATA=dataset;
  TABLES row_var*col_var / CHISQ;
RUN;
Documentation
tabulate row_var col_var, chi2 expected
Documentation
Synthetic Data Example

A contingency table dataset suitable for chi-square test analysis

R Code for Data Generation and Analysis
# Generate data for chi-square test
set.seed(123)

# Create a 2x3 contingency table
data <- matrix(c(50, 30, 20,
                25, 45, 30), 
              nrow=2, byrow=TRUE)

# Add row and column names
dimnames(data) <- list(
  Group = c("Treatment", "Control"),
  Outcome = c("Success", "Partial", "Failure")
)

# Convert to data frame
df <- as.data.frame(as.table(data))

# Visual inspection
mosaicplot(data, main="Contingency Table Mosaic Plot")

# Perform chi-square test
result <- chisq.test(data)
print(result)

# Check expected counts
result$expected

# Effect size (Cramer's V)
library(lsr)
cramersV(data)
Copy this code into your R environment to generate synthetic data and perform analysis with this model.
Expected Analysis Results
Console Output

> result <- chisq.test(data)
> print(result)

	Pearson's Chi-squared test

data:  data
X-squared = 12.345, df = 2, p-value = 0.002345

> result$expected
         Outcome
Group      Success  Partial  Failure
  Treatment 41.25    41.25    27.5
  Control   33.75    33.75    22.5

> cramersV(data)
[1] 0.2345
These results are from running the R code on synthetic data. Your actual results may vary depending on your data.
Interpretation Guide

Need help interpreting the results of your Chi-Square Test analysis? Our comprehensive interpretation guide explains:

  • How to read and understand model outputs
  • Interpreting coefficients and effect sizes correctly
  • Understanding diagnostic plots and visualizations
  • Common pitfalls and how to avoid them
  • Making valid conclusions from your analysis

Statistical assistant