独立性計算機のカイ二乗検定
独立性のカイ二乗検定は、 2 つのカテゴリ変数間に有意な関連があるかどうかを判断するために使用されます。
独立性のカイ二乗検定を実行するには、以下のセルに入力するだけで、最大 5 行 5 列の分割表が得られます。表が 5×5 より小さい場合は、他のセルを空白のままにしておきます。
グループ1 | グループ2 | グループ3 | グループ4 | グループ5 | |
---|---|---|---|---|---|
カテゴリー1 | |||||
カテゴリー2 | |||||
カテゴリー3 | |||||
カテゴリー4 | |||||
カテゴリー5 |
検定統計量 x 2 : 0.864035
p値: 0.649198
function calc() {
//get input data var o11 = document.getElementById('o11').value; var o12 = document.getElementById('o12').value; var o13 = document.getElementById('o13').value; var o14 = document.getElementById('o14').value; var o15 = document.getElementById('o15').value; var o21 = document.getElementById('o21').value; var o22 = document.getElementById('o22').value; var o23 = document.getElementById('o23').value; var o24 = document.getElementById('o24').value; var o25 = document.getElementById('o25').value; var o31 = document.getElementById('o31').value; var o32 = document.getElementById('o32').value; var o33 = document.getElementById('o33').value; var o34 = document.getElementById('o34').value; var o35 = document.getElementById('o35').value; var o41 = document.getElementById('o41').value; var o42 = document.getElementById('o42').value; var o43 = document.getElementById('o43').value; var o44 = document.getElementById('o44').value; var o45 = document.getElementById('o45').value; var o51 = document.getElementById('o51').value; var o52 = document.getElementById('o52').value; var o53 = document.getElementById('o53').value; var o54 = document.getElementById('o54').value; var o55 = document.getElementById('o55').value;
var cols = [o11, o12, o13, o14, o15]; var colCount = cols.filter(String).length; var rows = [o11, o21, o31, o41, o51]; var rowCount = rows.filter(String).length; var df = (colCount-1)*(rowCount-1);
var col1 = [o11, o21, o31, o41, o51]; var col2 = [o12, o22, o32, o42, o52]; var col3 = [o13, o23, o33, o43, o53]; var col4 = [o14, o24, o34, o44, o54]; var col5 = [o15, o25, o35, o45, o55];
var row1 = [o11, o12, o13, o14, o15]; var row2 = [o21, o22, o23, o24, o25]; var row3 = [o31, o32, o33, o34, o35]; var row4 = [o41, o42, o43, o44, o45]; var row5 = [o51, o52, o53, o54, o55];
var col1sum = math.sum(col1); var col2sum = math.sum(col2); var col3sum = math.sum(col3); var col4sum = math.sum(col4); var col5sum = math.sum(col5);
var row1sum = math.sum(row1); var row2sum = math.sum(row2); var row3sum = math.sum(row3); var row4sum = math.sum(row4); var row5sum = math.sum(row5);
var n = math.sum(row1sum, row2sum, row3sum, row4sum, row5sum);
//do calculations var diff11 = 0; if (o11) { o11 = +o11; var e11 = row1sum*col1sum/n; diff11 = Math.pow(o11-e11,2) / e11; } var diff12 = 0; if (o12) { o12 = +o12; var e12 = row1sum*col2sum/n; diff12 = Math.pow(o12-e12,2) / e12; } var diff13 = 0; if (o13) { o13 = +o13; var e13 = row1sum*col3sum/n; diff13 = Math.pow(o13-e13,2) / e13; } var diff14 = 0; if (o14) { o14 = +o14; var e14 = row1sum*col4sum/n; diff14 = Math.pow(o14-e14,2) / e14; } var diff15 = 0; if (o15) { o15 = +o15; var e15 = row1sum*col5sum/n; diff15 = Math.pow(o15-e15,2) / e15; } var diff21 = 0; if (o21) { o21 = +o21; var e21 = row2sum*col1sum/n; diff21 = Math.pow(o21-e21,2) / e21; } var diff22 = 0; if (o22) { o22 = +o22; var e22 = row2sum*col2sum/n; diff22 = Math.pow(o22-e22,2) / e22; } var diff23 = 0; if (o23) { o23 = +o23; var e23 = row2sum*col3sum/n; diff23 = Math.pow(o23-e23,2) / e23; } var diff24 = 0; if (o24) { o24 = +o24; var e24 = row2sum*col4sum/n; diff24 = Math.pow(o24-e24,2) / e24; } var diff25 = 0; if (o25) { o25 = +o25; var e25 = row2sum*col5sum/n; diff25 = Math.pow(o25-e25,2) / e25; } var diff31 = 0; if (o31) { o31 = +o31; var e31 = row3sum*col1sum/n; diff31 = Math.pow(o31-e31,2) / e31; } var diff32 = 0; if (o32) { o32 = +o32; var e32 = row3sum*col2sum/n; diff32 = Math.pow(o32-e32,2) / e32; } var diff33 = 0; if (o33) { o33 = +o33; var e33 = row3sum*col3sum/n; diff33 = Math.pow(o33-e33,2) / e33; } var diff34 = 0; if (o34) { o34 = +o34; var e34 = row3sum*col4sum/n; diff34 = Math.pow(o34-e34,2) / e34; } var diff35 = 0; if (o35) { o35 = +o35; var e35 = row3sum*col5sum/n; diff35 = Math.pow(o35-e35,2) / e35; } var diff41 = 0; if (o41) { o41 = +o41; var e41 = row4sum*col1sum/n; diff41 = Math.pow(o41-e41,2) / e41; } var diff42 = 0; if (o42) { o42 = +o42; var e42 = row4sum*col2sum/n; diff42 = Math.pow(o42-e42,2) / e42; } var diff43 = 0; if (o43) { o43 = +o43; var e43 = row4sum*col3sum/n; diff43 = Math.pow(o43-e43,2) / e43; } var diff44 = 0; if (o44) { o44 = +o44; var e44 = row4sum*col4sum/n; diff44 = Math.pow(o44-e44,2) / e44; } var diff45 = 0; if (o45) { o45 = +o45; var e45 = row4sum*col5sum/n; diff45 = Math.pow(o45-e45,2) / e45; } var diff51 = 0; if (o51) { o51 = +o51; var e51 = row5sum*col1sum/n; diff51 = Math.pow(o51-e51,2) / e51; } var diff52 = 0; if (o52) { o52 = +o52; var e52 = row5sum*col2sum/n; diff52 = Math.pow(o52-e52,2) / e52; } var diff53 = 0; if (o53) { o53 = +o53; var e53 = row5sum*col3sum/n; diff53 = Math.pow(o53-e53,2) / e53; } var diff54 = 0; if (o54) { o54 = +o54; var e54 = row5sum*col4sum/n; diff54 = Math.pow(o54-e54,2) / e54; } var diff55 = 0; if (o55) { o55 = +o55; var e55 = row5sum*col5sum/n; diff55 = Math.pow(o55-e55,2) / e55; } console.log(o31, o32, o33, e31, e32, e33, diff31, diff32, diff33); var errors = [diff11, diff12, diff13, diff14, diff15, diff21, diff22, diff23, diff24, diff25, diff31, diff32, diff33, diff34, diff35, diff41, diff42, diff43, diff44, diff45, diff51, diff52, diff53, diff54, diff55]; var X2 = math.sum(errors); var p = 1-jStat.chisquare.cdf(X2, df);
//output results document.getElementById('X2').innerHTML = X2.toFixed(6); document.getElementById('p').innerHTML = p.toFixed(6);
} //end calc function