分层卡方
计算公式
符号
| 符号 | 含义 |
|---|---|
| s | 层(第 s 层) |
| as、bs、cs、ds | 第 s 层 2×2 表的四个格子 |
| ns | 第 s 层的样本量 |
| Es、Vs | 第 s 层 a 的期望与方差 |
| r1s、c1s | 第 s 层第 1 行、第 1 列的合计 |
| adj | 连续性校正项,取 0 或 0.5 |
| z1−α/2 | 标准正态分布的双侧临界值 |
| ψMH | Mantel-Haenszel 共同 OR |
| χ2CMH | CMH 合并卡方 |
| χ2BD | Breslow-Day 齐性卡方 |
| K | 参与合并的层数 |
核心公式
公式 (1.1):CMH 合并卡方
\[
\chi^2_{\mathrm{CMH}}=\frac{\left(\left|\sum_s a_s-\sum_s E_s\right|-\mathrm{adj}\right)^2}{\sum_s V_s},\qquad E_s=\frac{r_{1s}c_{1s}}{n_s},\qquad V_s=\frac{r_{1s}(n_s-r_{1s})c_{1s}(n_s-c_{1s})}{n_s^2(n_s-1)}
\]
公式 (1.2):Mantel-Haenszel 共同 OR 与置信区间
\[
\begin{aligned}\psi_{\mathrm{MH}}&=\frac{\sum_s a_s d_s/n_s}{\sum_s b_s c_s/n_s},\qquad \mathrm{CI}=\exp\left(\ln\psi_{\mathrm{MH}}\pm z_{1-\alpha/2}\sqrt{\mathrm{Var}(\ln\psi_{\mathrm{MH}})}\right)\\\mathrm{Var}(\ln\psi_{\mathrm{MH}})&=\frac{\sum_s (a_s+d_s)a_s d_s/n_s^2}{2R^2}+\frac{\sum_s\left[(a_s+d_s)b_s c_s+(b_s+c_s)a_s d_s\right]/n_s^2}{2RS}+\frac{\sum_s (b_s+c_s)b_s c_s/n_s^2}{2S^2}\\R&=\sum_s\frac{a_s d_s}{n_s},\qquad S=\sum_s\frac{b_s c_s}{n_s}\end{aligned}
\]
公式 (1.3):Breslow-Day 齐性检验
\[
\chi^2_{\mathrm{BD}}=\sum_s\frac{(a_s-E_s(\psi))^2}{\mathrm{Var}(a_s\mid\psi)},\qquad \mathrm{df}=K-1
\]
公式 (1.4):Breslow-Day 的期望与方差
\[
(1-\psi)E_s^2+\left[(r_{1s}+c_{1s})(\psi-1)+n_s\right]E_s-\psi r_{1s}c_{1s}=0,\qquad \mathrm{Var}(a_s\mid\psi)=\left(\frac{1}{E_s}+\frac{1}{r_{1s}-E_s}+\frac{1}{c_{1s}-E_s}+\frac{1}{n_s-r_{1s}-c_{1s}+E_s}\right)^{-1}
\]
参考文献
- Mantel, N., & Haenszel, W. (1959). Statistical aspects of the analysis of data from retrospective studies of disease. Journal of the National Cancer Institute, 22(4), 719–748.
- Breslow, N. E., & Day, N. E. (1980). Statistical Methods in Cancer Research: Vol. 1. The Analysis of Case-Control Studies. International Agency for Research on Cancer.
- Robins, J., Breslow, N., & Greenland, S. (1986). Estimators of the Mantel-Haenszel variance consistent in both sparse data and large-strata limiting models. Biometrics, 42(2), 311–323.
算法参数
| 参数 | 取值 | 默认 | 影响 |
|---|---|---|---|
| 连续性校正 | 开 / 关 | 开 | CMH 在 |Σa−ΣE| ≥ 0.5 时减 0.5;关闭后校正项为 0。 |
验证报告
数据与 R 包
提示词
按 S 分层,分析 X 与 Y 的关联,做分层卡方检验(CMH),连续性校正用默认设置。
R 代码
library(DescTools)
options(digits = 15)
d <- read.csv("chi_stratified.csv")
tab <- table(d$X, d$Y, d$S)
m <- mantelhaen.test(tab, correct = TRUE)
cat("CMH chisq", unname(m$statistic), "df", unname(m$parameter), "p", m$p.value, "\n")
cat("MH OR", unname(m$estimate), "\n")
cat("MH CI", m$conf.int[1], m$conf.int[2], "\n")
b <- BreslowDayTest(tab)
cat("BD chisq", unname(b$statistic), "df", unname(b$parameter), "p", b$p.value, "\n")
对比结果
| 指标 | SPSSAU | R | 差值 |
|---|---|---|---|
| CMH χ² | 11.5304231900 | 11.5304231900 | 8.9e−12 |
| CMH 自由度 | 1 | 1 | 0 |
| CMH p | 0.0006846643 | 0.0006846643 | 3.3e−11 |
| MH 共同 OR | 4.0153846154 | 4.0153846154 | 1.54e−11 |
| OR CI 下限 | 1.8622725313 | 1.8622725313 | 4.17e−11 |
| OR CI 上限 | 8.6578700694 | 8.6578700694 | 1.1e−11 |
| Breslow-Day χ² | 0.1163921534 | 0.1163921534 | 1.23e−12 |
| Breslow-Day 自由度 | 2 | 2 | 0 |
| Breslow-Day p | 0.9434649378 | 0.9434649378 | 8.36e−12 |
结论:CMH 合并 χ²、自由度、p、MH 共同 OR 及其 95% CI、Breslow-Day χ²、自由度与 p 均与 R 一致,最大差 4.2e−11。
引用SPSSAU. 分层卡方:计算公式与参数[EB/OL]. SPSSAU算法手册, 2026-11-20. https://www.spssau.com/algorithm/general/chi_stratified.html.