Description
From Example 4.6
A 2×2 contingency table cross-classifying chest-pain status (pain / no pain) against gender (male / female) for 1073 patients. Used to illustrate the chi-squared test for independence and odds-ratio computation.
| pain | no pain | |
|---|---|---|
| male | 46 | 474 |
| female | 37 | 516 |
Total: 1073 patients. Marginal proportions: , .
Expected counts
From Example 4.6
Under (independence), the joint probability factors:
Expected count for the male-pain cell: .
General formula:
R code
chisq_output <- chisq.test(chest_tab)
chisq_output$expectedPython code
chisq_output.expected_freqAll expected counts exceed 5, so the chi-squared test is valid.
Chi-squared test
From Example 4.6
Formula (with continuity correction):
R code
x <- matrix(c(46, 37, 474, 516), nrow=2)
dimnames(x) <- list(c("male", "female"), c("pain", "no pain"))
chest_tab <- as.table(x)
chisq_output <- chisq.test(chest_tab)
chisq_outputPython code
from scipy import stats
import numpy as np
chest_array = np.array([[46, 474], [37, 516]])
chisq_output = stats.chi2_contingency(chest_array)
print(f"The p-value is {chisq_output.pvalue:.3f}.")
## The p-value is 0.228.
print(f"The test-statistic value is {chisq_output.statistic:.3f}.")
## The test-statistic value is 1.456.Conclusion: p-value = 0.228, do not reject at the 5% level. Not enough evidence to conclude gender and chest pain are associated.
Odds ratio
Sample odds ratio:
95% CI via log-OR + Normal approx:
where , then exponentiate the endpoints.
R code
library(DescTools)
OddsRatio(chest_tab, conf.level = .95)Python code
import statsmodels.api as sm
chest_tab2 = sm.stats.Table2x2(chest_array)
print(chest_tab2.summary())The CI for the OR includes 1 — consistent with the chi-squared test, no evidence of association.
See Odds Ratio & Relative Risk for further interpretation notes, and Chi-Square & Fisher for the independence test in general.
See also: L4 Exploring Categorical Data · Chi-Square & Fisher · Odds Ratio & Relative Risk