Octave でマクネマー検定
マクネマー検定は,対応のある 2 標本の比率の差の検定である。また,対応する値(カテゴリー)が $k$ 種類ある場合は,マクネマー検定の拡張である「対称性の適合度検定(goodness of fit test of symmetry)」が行われる(R でも採用されている)。
usage:
[pval, chisq, df] = mcnemar_test (x)
x は正方行列
2 x 2 行列を与えた場合,連続性の補正をした検定結果が示される。連続性の補正をしないというオプションはない。
戻り値を指定しない場合は p 値のみを表示する
次式で計算される $\chi^2_0$ が,自由度 $\displaystyle \frac{k\ (k-1)}{2}$ の $\chi^2$ 分布に従う。
$$\displaystyle
\chi^2_0 = \begin{array}{c}
{ } \
{\sum \sum} \
{\scriptsize i \lt j}
\end{array}
\frac{\left( n_{ij} - n_{ji} \right)^2} {n_{ij} + n_{ji}}
$$
pkg load statistics % statistics が必要
format long % 表示精度を高くする(任意)
2 x 2 行列を与えた場合
x = [13 6
9 7]
x =
13 6
9 7
mcnemar_test(x);
pval: 0.605577
[pval, chisq, df] = mcnemar_test(x);
printf("chisq = %.5g, df = %d, p value = %.5g\n", chisq, df, pval)
chisq = 0.26667, df = 1, p value = 0.60558
mcnemar()
は漸近検定なので,二項検定 binotest()
を用いれば正確な $p$ 値が得られる。
[h, pval,ci] = binotest(6, 6+9, 0.5)
h = 0
pval = 0.607238769531250
ci =
0.163364323859513 0.677130233793718
k x k 行列を与えた場合
x = [13 6 8
9 7 5
1 3 8]
x =
13 6 8
9 7 5
1 3 8
[pval, chisq, df] = mcnemar_test(x);
printf("chisq = %.5g, df = %d, p value = %.5g\n", chisq, df, pval)
chisq = 6.5444, df = 3, p value = 0.087926
以下のような計算をしている
chisq = (6 - 9)^2/(6 + 9) + (8 - 1)^2/(8 + 1) + (5 - 3)^2/(5 + 3)
chisq = 6.544444444444444
k = size(x)(1)
k = 3
df = k * (k - 1) / 2
df = 3
pval = 1 - chi2cdf(chisq, df)
pval = 8.792610801907608e-02