fisher卡方
计算公式
符号
| 符号 | 含义 |
|---|---|
| Oij | 第 i 行第 j 列的实际频数 |
| Eij | 期望频数 |
| N | 总样本量 |
| R、C | 行、列类别数 |
| t | 固定边际下的任意一张同边际表 |
| pF | Fisher 精确检验的双侧 p 值 |
核心公式
公式 (1.1):Pearson 卡方与期望频数
\[
\chi^2=\sum_{i=1}^{R}\sum_{j=1}^{C}\frac{(O_{ij}-E_{ij})^2}{E_{ij}},\qquad E_{ij}=\frac{O_{i\cdot}O_{\cdot j}}{N},\qquad \mathrm{df}=(R-1)(C-1)
\]
公式 (1.2):2×2 的 Yates 连续校正
\[
\chi^2_{\mathrm{Yates}}=\sum_{i=1}^{2}\sum_{j=1}^{2}\frac{\left(\max\left(0,\ |O_{ij}-E_{ij}|-0.5\right)\right)^2}{E_{ij}}
\]
每格先把观测值朝期望值方向移动 min(0.5, |Oij−Eij|),有效偏差即 max(0, |Oij−Eij|−0.5)。
公式 (1.3):Fisher 精确检验的双侧 p 值
\[
p_F=\sum_{\{t:\,P(t)\le P_{\mathrm{obs}}\}}P(t),\qquad P(t)=\frac{\prod_{i=1}^{R}O_{i\cdot}!\prod_{j=1}^{C}O_{\cdot j}!}{N!\prod_{i=1}^{R}\prod_{j=1}^{C}t_{ij}!}
\]
参考文献
- Fisher, R. A. (1935). The Design of Experiments. Oliver & Boyd.
- Cochran, W. G. (1954). Some methods for strengthening the common χ² tests. Biometrics, 10(4), 417–451.
- Agresti, A. (2002). Categorical Data Analysis (2nd ed.). Wiley.
算法参数
本方法没有需要额外选择的算法参数:输入一张频数表即可,全部计算按上式的定义直接完成。
验证报告
数据与 R 包
提示词
下面是一张 2×3 的汇总频数表,行 A、B,列 Low、Mid、High:[[38, 9, 5], [62, 4, 2]],请做概要卡方检验,并给出 Fisher 精确检验。
R 代码
options(digits = 15)
d <- read.csv("summary_chisquare.csv")
tab <- table(d$X, d$Y)
print(tab)
cat("N", sum(tab), "\n")
ct <- chisq.test(tab, correct = FALSE)
cat("pearson chisq", unname(ct$statistic), "df", unname(ct$parameter), "p", ct$p.value, "\n")
ft <- fisher.test(tab)
print(ft)
cat("fisher p", ft$p.value, "\n")
对比结果
| 指标 | SPSSAU | R | 差值 |
|---|---|---|---|
| 样本量 N | 120 | 120 | 0 |
| Pearson χ² | 6.9591765700 | 6.9591765700 | 3.63e−11 |
| 自由度 | 2 | 2 | 0 |
| Pearson p | 0.0308200975 | 0.0308200975 | 1.69e−11 |
| Fisher 精确 p | 0.0300683823 | 0.0300683823 | 4.34e−11 |
结论:Pearson χ²、自由度与 p、Fisher 精确 p 均与 R 一致,最大差 4.3e−11。
引用SPSSAU. fisher卡方:计算公式与参数[EB/OL]. SPSSAU算法手册, 2026-11-20. https://www.spssau.com/algorithm/general/summary_chisquare.html.