算法手册/ 实验/医学研究/ 分层卡方

分层卡方

计算公式

符号

符号含义
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标准正态分布的双侧临界值
ψMHMantel-Haenszel 共同 OR
χ2CMHCMH 合并卡方
χ2BDBreslow-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} \]

参考文献

  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.
  2. 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.
  3. 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 包

数据文件 chi_stratified.csv
样本与变量 N = 120;S 分 3 层,X、Y 各 2 类
R 包 DescTools 0.99.60、stats 4.6.2(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")

对比结果

指标SPSSAUR差值
CMH χ²11.530423190011.53042319008.9e−12
CMH 自由度110
CMH p0.00068466430.00068466433.3e−11
MH 共同 OR4.01538461544.01538461541.54e−11
OR CI 下限1.86227253131.86227253134.17e−11
OR CI 上限8.65787006948.65787006941.1e−11
Breslow-Day χ²0.11639215340.11639215341.23e−12
Breslow-Day 自由度220
Breslow-Day p0.94346493780.94346493788.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.