Location : Home > Languages > Perl > Package Title : Statistics::ChisqIndep |
![]() |
Statistics::ChisqIndep - χ2検定を計算するためのモジュール
# Statistics::ChisqIndep の使用例 use strict; use Statistics::ChisqIndep; use POSIX; # 配列形式で配列参照を入力データとして渡す my @obs = ([15, 68, 83], [23,47,65]); my $chi = new Statistics::ChisqIndep; $chi->load_data(\@obs); # 分割表に従いデータ要約を出力 $chi->print_summary(); # 分割表のみを出力 $chi->print_contingency_table(); # 次の出力は print_summary を呼び出したときと同じ。 # 期待値・自由度・合計などの詳細情報はオブジェクトにアクセスすれば取得可能。 # load_data() の呼び出しが成功したかを確認 if($chi->{valid}) { print "Rows: ", $chi->{rows}, "\n"; print "Columns: ", $chi->{cols}, "\n"; print "Degree of Freedom: ", $chi->{df}, "\n"; print "Total Count: ", $chi->{total}, "\n"; print "Chi-square Statistic: ", $chi->{chisq_statistic}, "\n"; print "p-value: ", $chi->{p_value}, "\n"; print "Warning: some of the cell counts might be too low.\n" if ($chi->{warning}); # 分割表を出力 my $rows = $chi->{rows}; # 行 my $cols = $chi->{cols}; # 列 my $obs = $chi->{obs}; # 観測値 my $exp = $chi->{expected}; # 期待値 my $rtotals = $chi->{rtotals}; # 行和 my $ctotals = $chi->{ctotals}; # 列和 my $total = $chi->{total}; # 合計 for (my $j = 0; $j < $cols; $j++) { print "\t",$j + 1; } print "\trtotal\n"; for (my $i = 0; $i < $rows; $i ++) { print $i + 1, "\t"; for(my $j = 0 ; $j < $cols; $j ++) { # 以下の形式でアクセスできる観測値 print $obs->[$i]->[$j], "\t"; } # 以下の形式でアクセスできる行和 print $rtotals->[$i], "\n"; print "\t"; for(my $j = 0 ; $j < $cols; $j ++) { # 以下の形式でアクセスできる期待値 printf "(%.2f)\t", $exp->[$i]->[$j]; } print "\n"; } print "ctotal\t"; for (my $j = 0; $j < $cols; $j++) { #以下の形式でアクセスできる列和 print $ctotals->[$j], "\t"; } # 合計を出力 print $total, "\n"; }
これは2次の分割表(contingency tables)におけるピアソンのχ2検定(the Pearson's Chi-squared test)を計算するモジュールである。ユーザはテーブル形式で観測値を入力し、モジュールは独立仮定に従って各セルの期待値を計算する。モジュールはχ2統計値並びに対応する真に独立であった場合の観測値と期待値によるp値を計算する。
Yun-Fang Juan , Yahoo! Inc.
yunfang@yahoo-inc.com
yunfangjuan@yahoo.com
Statistics::Distributions
![]() |
Updated : 2006/06/17 |